1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-23 01:39:36 +01:00

add encoding and camel to snake case conversion to FetchApiUtils

This commit is contained in:
Tim Daubenschütz 2015-05-19 14:09:10 +02:00
parent adb4c72253
commit 9c58fa321c
4 changed files with 16 additions and 10 deletions

View File

@ -10,15 +10,11 @@ class ArtworkListActions {
fetchArtworkList() { fetchArtworkList() {
ArtworkFetcher.fetch() ArtworkFetcher.fetch()
/*.then((res) => {
return res.json();
})*/
.then((res) => { .then((res) => {
this.actions.updateArtworkList(res.pieces); this.actions.updateArtworkList(res.pieces);
}) })
.catch((err) => { .catch((err) => {
console.log(err); console.log(err);
console.error('OMG cannot retrieve the artworks');
}); });
} }
}; };

View File

@ -10,8 +10,11 @@ class AscribeApp extends React.Component {
render() { render() {
return ( return (
<div> <div>
<h1>ascribe all the things!</h1> <header>ascribe</header>
<Link to="artworks">artworks</Link> <navigation>
<Link to="artworks">artworks</Link>
</navigation>
<RouteHandler /> <RouteHandler />
</div> </div>
); );

View File

@ -7,14 +7,14 @@ var ArtworkListFetcher = {
fetch(page=1, pageSize=10) { fetch(page=1, pageSize=10) {
let params = FetchApiUtils.argsToQueryParams({ let params = FetchApiUtils.argsToQueryParams({
page, page,
'page_size': pageSize // this is kind of a bummer... pageSize
}); });
return fetch(AppConstants.baseUrl + 'pieces/' + params, { return fetch(AppConstants.baseUrl + 'pieces/' + params, {
headers: { headers: {
'Authorization': 'Basic ZGltaUBtYWlsaW5hdG9yLmNvbTowMDAwMDAwMDAw' 'Authorization': 'Basic ZGltaUBtYWlsaW5hdG9yLmNvbTowMDAwMDAwMDAw'
} }
}).then((res) => { return res.json(); }); }).then((res) => res.json());
} }
}; };

View File

@ -9,6 +9,11 @@ let FetchApiUtils = {
* } * }
* *
* and converts it to a query-parameter, which you can append to your URL. * and converts it to a query-parameter, which you can append to your URL.
* The return looks like this:
*
* ?page=1&page_size=10
*
* CamelCase gets converted to snake_case!
* *
* @param {[type]} * @param {[type]}
* @return {[type]} * @return {[type]}
@ -25,7 +30,9 @@ let FetchApiUtils = {
s += '&'; s += '&';
} }
return s + key + '=' + obj[key]; let snakeCaseKey = key.replace(/[A-Z]/, (match) => '_' + match.toLowerCase());
return s + snakeCaseKey + '=' + encodeURIComponent(obj[key]);
}) })
.join(''); .join('');
} }