mirror of
https://github.com/ascribe/onion.git
synced 2025-01-03 10:25:08 +01:00
refactor all util functions for nicer usability
This commit is contained in:
parent
366fcbf4f3
commit
347517a0b3
@ -5,7 +5,7 @@ import TableItem from '../ascribe_table/table_item';
|
|||||||
|
|
||||||
import TableColumnContentModel from '../../models/table_column_content_model';
|
import TableColumnContentModel from '../../models/table_column_content_model';
|
||||||
|
|
||||||
import getLangText from '../../utils/lang_utils';
|
import { getLangText } from '../../utils/lang_utils';
|
||||||
|
|
||||||
let AccordionListItemTable = React.createClass({
|
let AccordionListItemTable = React.createClass({
|
||||||
getInitialState() {
|
getInitialState() {
|
||||||
|
@ -12,7 +12,7 @@ import TableItemText from '../ascribe_table/table_item_text';
|
|||||||
import TableItemCheckbox from '../ascribe_table/table_item_checkbox';
|
import TableItemCheckbox from '../ascribe_table/table_item_checkbox';
|
||||||
import TableItemAclFiltered from '../ascribe_table/table_item_acl_filtered';
|
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({
|
let AccordionListItemTableEditions = React.createClass({
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import fetch from '../utils/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 EditionFetcher = {
|
let EditionFetcher = {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import AppConstants from '../constants/application_constants';
|
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';
|
import fetch from '../utils/fetch';
|
||||||
|
|
||||||
|
|
||||||
@ -9,7 +9,7 @@ let PieceListFetcher = {
|
|||||||
* Can be called with all supplied queryparams the API.
|
* Can be called with all supplied queryparams the API.
|
||||||
*/
|
*/
|
||||||
fetch(page, pageSize, search, orderBy, orderAsc) {
|
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 });
|
return fetch.get('pieces_list', { page, pageSize, search, ordering });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import fetch from '../utils/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 UserFetcher = {
|
let UserFetcher = {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { default as _fetch } from 'isomorphic-fetch';
|
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 {};
|
class UrlMapError extends Error {};
|
||||||
@ -66,7 +66,7 @@ class Fetch {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (attachParamsToQuery && params && Object.keys(params).length > 0) {
|
if (attachParamsToQuery && params && Object.keys(params).length > 0) {
|
||||||
newUrl += FetchApiUtils.argsToQueryParams(params);
|
newUrl += argsToQueryParams(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
return newUrl;
|
return newUrl;
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
import { sanitize } from './general_utils';
|
import { sanitize } from './general_utils';
|
||||||
|
|
||||||
|
|
||||||
// TODO: Create Unittests that test all functions
|
// TODO: Create Unittests that test all functions
|
||||||
let FetchApiUtils = {
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Takes a key-value object of this form:
|
* Takes a key-value object of this form:
|
||||||
@ -20,7 +18,7 @@ let FetchApiUtils = {
|
|||||||
* CamelCase gets converted to snake_case!
|
* CamelCase gets converted to snake_case!
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
argsToQueryParams(obj) {
|
export function argsToQueryParams(obj) {
|
||||||
|
|
||||||
obj = sanitize(obj);
|
obj = sanitize(obj);
|
||||||
|
|
||||||
@ -40,13 +38,13 @@ let FetchApiUtils = {
|
|||||||
return s + snakeCaseKey + '=' + encodeURIComponent(obj[key]);
|
return s + snakeCaseKey + '=' + encodeURIComponent(obj[key]);
|
||||||
})
|
})
|
||||||
.join('');
|
.join('');
|
||||||
},
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Takes a string and a boolean and generates a string query parameter for
|
* Takes a string and a boolean and generates a string query parameter for
|
||||||
* an API call.
|
* an API call.
|
||||||
*/
|
*/
|
||||||
generateOrderingQueryParams(orderBy, orderAsc) {
|
export function generateOrderingQueryParams(orderBy, orderAsc) {
|
||||||
let interpolation = '';
|
let interpolation = '';
|
||||||
|
|
||||||
if(!orderAsc) {
|
if(!orderAsc) {
|
||||||
@ -54,14 +52,11 @@ let FetchApiUtils = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return interpolation + orderBy;
|
return interpolation + orderBy;
|
||||||
},
|
};
|
||||||
|
|
||||||
status(response) {
|
export function status(response) {
|
||||||
if (response.status >= 200 && response.status < 300) {
|
if (response.status >= 200 && response.status < 300) {
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
throw new Error(response.json())
|
throw new Error(response.json())
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default FetchApiUtils;
|
|
||||||
|
@ -2,7 +2,7 @@ import languages from '../constants/languages';
|
|||||||
|
|
||||||
import { formatText } from './general_utils';
|
import { formatText } from './general_utils';
|
||||||
|
|
||||||
let getLangText = function(s, ...args) {
|
export function getLangText(s, ...args) {
|
||||||
let lang = navigator.language || navigator.userLanguage;
|
let lang = navigator.language || navigator.userLanguage;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -21,5 +21,3 @@ let getLangText = function(s, ...args) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default getLangText;
|
|
Loading…
Reference in New Issue
Block a user