Refactor fetch

This commit is contained in:
vrde 2015-06-01 17:43:38 +02:00
parent 699c07fe3f
commit 4ee4530029
7 changed files with 38 additions and 48 deletions

View File

@ -20,7 +20,8 @@ fetch.defaults({
http: {
headers: {
'Authorization': 'Basic ' + AppConstants.debugCredentialBase64,
'Accept': 'application/json'
'Accept': 'application/json',
'Content-Type': 'application/json'
}
},
fatalErrorHandler: (err) => {

View File

@ -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;

View File

@ -1,4 +1,4 @@
import fetch from 'isomorphic-fetch';
import fetch from '../utils/fetch';
import AppConstants from '../constants/application_constants';
@ -8,13 +8,8 @@ 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 });
}
};
export default EditionListFetcher;
export default EditionListFetcher;

View File

@ -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');
}
};

View File

@ -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');
}
};

View File

@ -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,21 +28,20 @@ export const FormMixin = {
this.handleError(response);
}
else {
}
},
handleError(err){
if (err.json) {
for (var input in errors){
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});
}
},
handleError(response){
response.json().then((response) => this.dispatchErrors(response.errors));
},
dispatchErrors(errors){
for (var input in errors){
if (this.refs && this.refs[input] && this.refs[input].state){
this.refs[input].setAlerts(errors[input]);
}
}
this.setState({submitted: false});
},
render(){
let alert = null;
if (this.state.status >= 500){

View File

@ -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 });
}