From 8d0c64099479c87b7392fc8864f4f414b6ff4055 Mon Sep 17 00:00:00 2001 From: Maciej Lenartowicz Date: Fri, 6 Dec 2019 10:27:48 +0100 Subject: [PATCH] Set suffix for API URL --- package-lock.json | 2 +- package.json | 2 +- src/api/hooks.js | 12 +++++++++--- src/form/components/ForisForm.js | 2 +- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 04e4540..12c5734 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "foris", - "version": "1.4.0", + "version": "2.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 4b44a5c..d2ca0d5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "foris", - "version": "1.4.0", + "version": "2.0.0", "description": "Set of components and utils for Foris and its plugins.", "author": "CZ.NIC, z.s.p.o.", "repository": { diff --git a/src/api/hooks.js b/src/api/hooks.js index 0429d57..b5f00d0 100644 --- a/src/api/hooks.js +++ b/src/api/hooks.js @@ -17,13 +17,13 @@ import { const DATA_METHODS = ["POST", "PATCH", "PUT"]; function createAPIHook(method) { - return (url, contentType) => { + return (urlRoot, contentType) => { const [state, dispatch] = useReducer(APIReducer, { state: API_STATE.INIT, data: null, }); - const sendRequest = useCallback(async (data) => { + const sendRequest = useCallback(async ({ data, suffix } = {}) => { const headers = { ...HEADERS }; if (contentType) { headers["Content-Type"] = contentType; @@ -31,17 +31,23 @@ function createAPIHook(method) { dispatch({ type: API_ACTIONS.INIT }); try { + // Prepare request const request = API_METHODS[method]; const config = { timeout: TIMEOUT, headers, }; + const url = suffix ? `${urlRoot}/${suffix}` : urlRoot; + + // Make request let result; if (DATA_METHODS.includes(method)) { result = await request(url, data, config); } else { result = await request(url, config); } + + // Process request result dispatch({ type: API_ACTIONS.SUCCESS, payload: result.data, @@ -53,7 +59,7 @@ function createAPIHook(method) { payload: getErrorPayload(error), }); } - }, [url, contentType]); + }, [urlRoot, contentType]); return [state, sendRequest]; }; } diff --git a/src/form/components/ForisForm.js b/src/form/components/ForisForm.js index 86b552b..960ffd6 100644 --- a/src/form/components/ForisForm.js +++ b/src/form/components/ForisForm.js @@ -103,7 +103,7 @@ export function ForisForm({ resetFormData(); const copiedFormData = JSON.parse(JSON.stringify(formState.data)); const preparedData = prepDataToSubmit(copiedFormData); - post(preparedData); + post({ data: preparedData }); } function getSubmitButtonState() {