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

refactor all util functions for nicer usability

This commit is contained in:
Tim Daubenschütz 2015-06-02 13:48:01 +02:00
parent 366fcbf4f3
commit 347517a0b3
8 changed files with 43 additions and 52 deletions

View File

@ -5,7 +5,7 @@ import TableItem from '../ascribe_table/table_item';
import TableColumnContentModel from '../../models/table_column_content_model';
import getLangText from '../../utils/lang_utils';
import { getLangText } from '../../utils/lang_utils';
let AccordionListItemTable = React.createClass({
getInitialState() {

View File

@ -12,7 +12,7 @@ import TableItemText from '../ascribe_table/table_item_text';
import TableItemCheckbox from '../ascribe_table/table_item_checkbox';
import TableItemAclFiltered from '../ascribe_table/table_item_acl_filtered';
import getLangText from '../../utils/lang_utils';
import { getLangText } from '../../utils/lang_utils';
let AccordionListItemTableEditions = React.createClass({

View File

@ -1,7 +1,6 @@
import fetch from '../utils/fetch';
import AppConstants from '../constants/application_constants';
import FetchApiUtils from '../utils/fetch_api_utils';
let EditionFetcher = {

View File

@ -1,5 +1,5 @@
import AppConstants from '../constants/application_constants';
import FetchApiUtils from '../utils/fetch_api_utils';
import { generateOrderingQueryParams } from '../utils/fetch_api_utils';
import fetch from '../utils/fetch';
@ -9,7 +9,7 @@ let PieceListFetcher = {
* Can be called with all supplied queryparams the API.
*/
fetch(page, pageSize, search, orderBy, orderAsc) {
let ordering = FetchApiUtils.generateOrderingQueryParams(orderBy, orderAsc);
let ordering = generateOrderingQueryParams(orderBy, orderAsc);
return fetch.get('pieces_list', { page, pageSize, search, ordering });
}
};

View File

@ -1,7 +1,6 @@
import fetch from '../utils/fetch';
import AppConstants from '../constants/application_constants';
import FetchApiUtils from '../utils/fetch_api_utils';
let UserFetcher = {

View File

@ -1,5 +1,5 @@
import { default as _fetch } from 'isomorphic-fetch';
import FetchApiUtils from '../utils/fetch_api_utils';
import { argsToQueryParams } from '../utils/fetch_api_utils';
class UrlMapError extends Error {};
@ -66,7 +66,7 @@ class Fetch {
});
if (attachParamsToQuery && params && Object.keys(params).length > 0) {
newUrl += FetchApiUtils.argsToQueryParams(params);
newUrl += argsToQueryParams(params);
}
return newUrl;

View File

@ -1,8 +1,6 @@
import { sanitize } from './general_utils';
// TODO: Create Unittests that test all functions
let FetchApiUtils = {
/**
* Takes a key-value object of this form:
@ -20,48 +18,45 @@ let FetchApiUtils = {
* CamelCase gets converted to snake_case!
*
*/
argsToQueryParams(obj) {
export function argsToQueryParams(obj) {
obj = sanitize(obj);
obj = sanitize(obj);
return Object
.keys(obj)
.map((key, i) => {
let s = '';
return Object
.keys(obj)
.map((key, i) => {
let s = '';
if(i === 0) {
s += '?';
} else {
s += '&';
}
if(i === 0) {
s += '?';
} else {
s += '&';
}
let snakeCaseKey = key.replace(/[A-Z]/, (match) => '_' + match.toLowerCase());
let snakeCaseKey = key.replace(/[A-Z]/, (match) => '_' + match.toLowerCase());
return s + snakeCaseKey + '=' + encodeURIComponent(obj[key]);
})
.join('');
},
/**
* Takes a string and a boolean and generates a string query parameter for
* an API call.
*/
generateOrderingQueryParams(orderBy, orderAsc) {
let interpolation = '';
if(!orderAsc) {
interpolation += '-';
}
return interpolation + orderBy;
},
status(response) {
if (response.status >= 200 && response.status < 300) {
return response
}
throw new Error(response.json())
}
return s + snakeCaseKey + '=' + encodeURIComponent(obj[key]);
})
.join('');
};
export default FetchApiUtils;
/**
* Takes a string and a boolean and generates a string query parameter for
* an API call.
*/
export function generateOrderingQueryParams(orderBy, orderAsc) {
let interpolation = '';
if(!orderAsc) {
interpolation += '-';
}
return interpolation + orderBy;
};
export function status(response) {
if (response.status >= 200 && response.status < 300) {
return response
}
throw new Error(response.json())
};

View File

@ -2,7 +2,7 @@ import languages from '../constants/languages';
import { formatText } from './general_utils';
let getLangText = function(s, ...args) {
export function getLangText(s, ...args) {
let lang = navigator.language || navigator.userLanguage;
try {
@ -20,6 +20,4 @@ let getLangText = function(s, ...args) {
}
}
};
export default getLangText;
};