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