mirror of
https://gitlab.nic.cz/turris/reforis/foris-js.git
synced 2024-11-13 17:25:34 +01:00
Set suffix for API URL
This commit is contained in:
parent
7b739f55a0
commit
8d0c640994
2
package-lock.json
generated
2
package-lock.json
generated
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "foris",
|
||||
"version": "1.4.0",
|
||||
"version": "2.0.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
|
|
@ -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": {
|
||||
|
|
|
@ -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];
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue
Block a user