1
0
mirror of https://gitlab.nic.cz/turris/reforis/foris-js.git synced 2024-07-05 20:53:12 +00:00

Merge branch '15-api-url-suffix' into 'dev'

Set suffix for API URL

Closes #15

See merge request turris/reforis/foris-js!54
This commit is contained in:
Maciej Lenartowicz 2019-12-06 10:27:48 +01:00
commit 5bb298270b
4 changed files with 12 additions and 6 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "foris", "name": "foris",
"version": "1.4.0", "version": "2.0.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "foris", "name": "foris",
"version": "1.4.0", "version": "2.0.0",
"description": "Set of components and utils for Foris and its plugins.", "description": "Set of components and utils for Foris and its plugins.",
"author": "CZ.NIC, z.s.p.o.", "author": "CZ.NIC, z.s.p.o.",
"repository": { "repository": {

View File

@ -17,13 +17,13 @@ import {
const DATA_METHODS = ["POST", "PATCH", "PUT"]; const DATA_METHODS = ["POST", "PATCH", "PUT"];
function createAPIHook(method) { function createAPIHook(method) {
return (url, contentType) => { return (urlRoot, contentType) => {
const [state, dispatch] = useReducer(APIReducer, { const [state, dispatch] = useReducer(APIReducer, {
state: API_STATE.INIT, state: API_STATE.INIT,
data: null, data: null,
}); });
const sendRequest = useCallback(async (data) => { const sendRequest = useCallback(async ({ data, suffix } = {}) => {
const headers = { ...HEADERS }; const headers = { ...HEADERS };
if (contentType) { if (contentType) {
headers["Content-Type"] = contentType; headers["Content-Type"] = contentType;
@ -31,17 +31,23 @@ function createAPIHook(method) {
dispatch({ type: API_ACTIONS.INIT }); dispatch({ type: API_ACTIONS.INIT });
try { try {
// Prepare request
const request = API_METHODS[method]; const request = API_METHODS[method];
const config = { const config = {
timeout: TIMEOUT, timeout: TIMEOUT,
headers, headers,
}; };
const url = suffix ? `${urlRoot}/${suffix}` : urlRoot;
// Make request
let result; let result;
if (DATA_METHODS.includes(method)) { if (DATA_METHODS.includes(method)) {
result = await request(url, data, config); result = await request(url, data, config);
} else { } else {
result = await request(url, config); result = await request(url, config);
} }
// Process request result
dispatch({ dispatch({
type: API_ACTIONS.SUCCESS, type: API_ACTIONS.SUCCESS,
payload: result.data, payload: result.data,
@ -53,7 +59,7 @@ function createAPIHook(method) {
payload: getErrorPayload(error), payload: getErrorPayload(error),
}); });
} }
}, [url, contentType]); }, [urlRoot, contentType]);
return [state, sendRequest]; return [state, sendRequest];
}; };
} }

View File

@ -103,7 +103,7 @@ export function ForisForm({
resetFormData(); resetFormData();
const copiedFormData = JSON.parse(JSON.stringify(formState.data)); const copiedFormData = JSON.parse(JSON.stringify(formState.data));
const preparedData = prepDataToSubmit(copiedFormData); const preparedData = prepDataToSubmit(copiedFormData);
post(preparedData); post({ data: preparedData });
} }
function getSubmitButtonState() { function getSubmitButtonState() {