1
0
mirror of https://gitlab.nic.cz/turris/reforis/foris-js.git synced 2024-07-02 20:30:27 +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",
"version": "1.4.0",
"version": "2.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@ -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": {

View File

@ -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];
};
}

View File

@ -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() {