mirror of
https://github.com/ascribe/onion.git
synced 2024-12-22 17:33:14 +01:00
Refactor fetch
This commit is contained in:
parent
699c07fe3f
commit
4ee4530029
@ -20,7 +20,8 @@ fetch.defaults({
|
||||
http: {
|
||||
headers: {
|
||||
'Authorization': 'Basic ' + AppConstants.debugCredentialBase64,
|
||||
'Accept': 'application/json'
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
},
|
||||
fatalErrorHandler: (err) => {
|
||||
|
@ -3,8 +3,14 @@ import AppConstants from './application_constants';
|
||||
let apiUrls = {
|
||||
'ownership_shares_mail' : AppConstants.baseUrl + 'ownership/shares/mail/',
|
||||
'ownership_transfers' : AppConstants.baseUrl + 'ownership/transfers/',
|
||||
|
||||
'user': AppConstants.baseUrl + 'users/',
|
||||
|
||||
'pieces_list': AppConstants.baseUrl + 'pieces/',
|
||||
'edition': AppConstants.baseUrl + 'editions/${bitcoin_id}/'
|
||||
'piece': AppConstants.baseUrl + 'pieces/${piece_id}',
|
||||
|
||||
'edition': AppConstants.baseUrl + 'editions/${bitcoin_id}/',
|
||||
'editions_list': AppConstants.baseUrl + 'pieces/${piece_id}/editions/'
|
||||
};
|
||||
|
||||
export default apiUrls;
|
||||
|
@ -1,4 +1,4 @@
|
||||
import fetch from 'isomorphic-fetch';
|
||||
import fetch from '../utils/fetch';
|
||||
|
||||
import AppConstants from '../constants/application_constants';
|
||||
|
||||
@ -8,12 +8,7 @@ let EditionListFetcher = {
|
||||
* Fetches a list of editions from the API.
|
||||
*/
|
||||
fetch(pieceId) {
|
||||
|
||||
return fetch(AppConstants.baseUrl + 'pieces/' + pieceId + '/editions/', {
|
||||
headers: {
|
||||
'Authorization': 'Basic ' + AppConstants.debugCredentialBase64
|
||||
}
|
||||
}).then((res) => res.json());
|
||||
return fetch.get('editions_list', { 'piece_id': pieceId });
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
import fetch from 'isomorphic-fetch';
|
||||
import fetch from '../utils/fetch';
|
||||
|
||||
import AppConstants from '../constants/application_constants';
|
||||
import FetchApiUtils from '../utils/fetch_api_utils';
|
||||
|
||||
|
||||
let PieceFetcher = {
|
||||
@ -11,11 +10,7 @@ let PieceFetcher = {
|
||||
*
|
||||
*/
|
||||
fetchOne(pieceId) {
|
||||
return fetch(AppConstants.baseUrl + 'pieces/' + pieceId + '/', {
|
||||
headers: {
|
||||
'Authorization': 'Basic ' + AppConstants.debugCredentialBase64
|
||||
}
|
||||
}).then((res) => res.json());
|
||||
return fetch.get('piece');
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import fetch from 'isomorphic-fetch';
|
||||
import fetch from '../utils/fetch';
|
||||
|
||||
import AppConstants from '../constants/application_constants';
|
||||
import FetchApiUtils from '../utils/fetch_api_utils';
|
||||
@ -11,11 +11,7 @@ let UserFetcher = {
|
||||
*
|
||||
*/
|
||||
fetchOne() {
|
||||
return fetch(AppConstants.baseUrl + 'users/', {
|
||||
headers: {
|
||||
'Authorization': 'Basic ' + AppConstants.debugCredentialBase64
|
||||
}
|
||||
}).then((res) => res.json());
|
||||
return fetch.get('user');
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
import fetch from '../utils/fetch';
|
||||
import React from 'react';
|
||||
|
||||
import AppConstants from '../constants/application_constants'
|
||||
@ -13,18 +14,11 @@ export const FormMixin = {
|
||||
submit(e) {
|
||||
e.preventDefault();
|
||||
this.setState({submitted: true});
|
||||
fetch(this.url(), {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Authorization': 'Basic ' + AppConstants.debugCredentialBase64,
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(this.getFormData())
|
||||
})
|
||||
.then(
|
||||
(response) => this.handleResponse(response)
|
||||
);
|
||||
fetch
|
||||
.post(this.url(), { body: this.getFormData() })
|
||||
.then(response => { this.props.onRequestHide(); })
|
||||
.catch(this.handleError);
|
||||
|
||||
},
|
||||
handleResponse(response){
|
||||
if (response.status >= 200 && response.status < 300){
|
||||
@ -34,20 +28,19 @@ export const FormMixin = {
|
||||
this.handleError(response);
|
||||
}
|
||||
else {
|
||||
this.setState({submitted: false, status: response.status});
|
||||
}
|
||||
},
|
||||
handleError(response){
|
||||
response.json().then((response) => this.dispatchErrors(response.errors));
|
||||
|
||||
},
|
||||
dispatchErrors(errors){
|
||||
handleError(err){
|
||||
if (err.json) {
|
||||
for (var input in errors){
|
||||
if (this.refs && this.refs[input] && this.refs[input].state){
|
||||
if (this.refs && this.refs[input] && this.refs[input].state) {
|
||||
this.refs[input].setAlerts(errors[input]);
|
||||
}
|
||||
}
|
||||
this.setState({submitted: false});
|
||||
} else {
|
||||
this.setState({submitted: false, status: response.status});
|
||||
}
|
||||
},
|
||||
render(){
|
||||
let alert = null;
|
||||
|
@ -92,7 +92,11 @@ class Fetch {
|
||||
post(url, params) {
|
||||
let paramsCopy = this._merge(params);
|
||||
let newUrl = this.prepareUrl(url, params);
|
||||
let body = JSON.stringify(params);
|
||||
let body = null;
|
||||
|
||||
if (params['body']) {
|
||||
body = JSON.stringify(params['body'])
|
||||
}
|
||||
return this.request('post', url, { body });
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user