1
0
mirror of https://gitlab.nic.cz/turris/reforis/foris-js.git synced 2024-12-25 00:11:36 +01:00

Resolve "Handle PATCH request"

This commit is contained in:
Maciej Lenartowicz 2019-09-25 11:56:08 +00:00
parent edc5c92421
commit d658f0b112
12 changed files with 265 additions and 220 deletions

View File

@ -31,10 +31,10 @@ build-js:
publish-beta: publish-beta:
npm publish --tag beta npm publish --tag beta
lint-js: lint:
npm run lint npm run lint
test-js: test:
npm test npm test
create-messages: create-messages:

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "foris", "name": "foris",
"version": "0.1.0-beta.2", "version": "0.1.0-beta.3",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "foris", "name": "foris",
"version": "0.1.0-beta.2", "version": "0.1.0-beta.3",
"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": {

41
src/api/delete.js Normal file
View File

@ -0,0 +1,41 @@
/*
* Copyright (C) 2019 CZ.NIC z.s.p.o. (http://www.nic.cz/)
*
* This is free software, licensed under the GNU General Public License v3.
* See /LICENSE for more information.
*/
import { useReducer, useCallback } from "react";
import axios from "axios";
import {
API_ACTIONS, TIMEOUT, HEADERS, APIReducer,
} from "./utils";
export function useAPIDelete(url) {
const [state, dispatch] = useReducer(APIReducer, {
isSending: false,
isError: false,
isSuccess: false,
data: null,
});
const requestDelete = useCallback(async () => {
dispatch({ type: API_ACTIONS.INIT });
try {
await axios.delete(url, {
timeout: TIMEOUT,
headers: HEADERS,
});
dispatch({ type: API_ACTIONS.SUCCESS });
} catch (error) {
dispatch({
type: API_ACTIONS.FAILURE,
payload: error.response.data,
status: error.response.status,
});
}
}, [url]);
return [state, requestDelete];
}

65
src/api/get.js Normal file
View File

@ -0,0 +1,65 @@
/*
* Copyright (C) 2019 CZ.NIC z.s.p.o. (http://www.nic.cz/)
*
* This is free software, licensed under the GNU General Public License v3.
* See /LICENSE for more information.
*/
import { useReducer, useCallback } from "react";
import axios from "axios";
import { ForisURLs } from "forisUrls";
import { API_ACTIONS, TIMEOUT } from "./utils";
const APIGetReducer = (state, action) => {
switch (action.type) {
case API_ACTIONS.INIT:
return {
...state,
isLoading: true,
isError: false,
};
case API_ACTIONS.SUCCESS:
return {
...state,
isLoading: false,
isError: false,
data: action.payload,
};
case API_ACTIONS.FAILURE:
if (action.status === 403) window.location.assign(ForisURLs.login);
return {
...state,
isLoading: false,
isError: true,
data: action.payload,
};
default:
throw new Error();
}
};
export function useAPIGet(url) {
const [state, dispatch] = useReducer(APIGetReducer, {
isLoading: false,
isError: false,
data: null,
});
const get = useCallback(async () => {
dispatch({ type: API_ACTIONS.INIT });
try {
const result = await axios.get(url, {
timeout: TIMEOUT,
});
dispatch({ type: API_ACTIONS.SUCCESS, payload: result.data });
} catch (error) {
dispatch({
type: API_ACTIONS.FAILURE,
payload: error.response.data,
status: error.response.status,
});
}
}, [url]);
return [state, get];
}

View File

@ -1,213 +0,0 @@
/*
* Copyright (C) 2019 CZ.NIC z.s.p.o. (http://www.nic.cz/)
*
* This is free software, licensed under the GNU General Public License v3.
* See /LICENSE for more information.
*/
import axios from "axios";
import { useCallback, useReducer } from "react";
import { ForisURLs } from "forisUrls";
const HEADERS = {
Accept: "application/json",
"Content-Type": "application/json",
"X-CSRFToken": getCookie("_csrf_token"),
};
function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== "") {
const cookies = document.cookie.split(";");
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (`${name}=`)) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
export const TIMEOUT = 5000;
const API_ACTIONS = {
INIT: 1,
SUCCESS: 2,
FAILURE: 3,
};
const APIGetReducer = (state, action) => {
switch (action.type) {
case API_ACTIONS.INIT:
return {
...state,
isLoading: true,
isError: false,
};
case API_ACTIONS.SUCCESS:
return {
...state,
isLoading: false,
isError: false,
data: action.payload,
};
case API_ACTIONS.FAILURE:
if (action.status === 403) window.location.assign(ForisURLs.login);
return {
...state,
isLoading: false,
isError: true,
data: action.payload,
};
default:
throw new Error();
}
};
export function useAPIGet(url) {
const [state, dispatch] = useReducer(APIGetReducer, {
isLoading: false,
isError: false,
data: null,
});
const get = useCallback(async () => {
dispatch({ type: API_ACTIONS.INIT });
try {
const result = await axios.get(url, {
timeout: TIMEOUT,
});
dispatch({ type: API_ACTIONS.SUCCESS, payload: result.data });
} catch (error) {
dispatch({
type: API_ACTIONS.FAILURE,
payload: error.response.data,
status: error.response.status,
});
}
}, [url]);
return [state, get];
}
const APIPostReducer = (state, action) => {
switch (action.type) {
case API_ACTIONS.INIT:
return {
...state,
isSending: true,
isError: false,
isSuccess: false,
};
case API_ACTIONS.SUCCESS:
return {
...state,
isSending: false,
isError: false,
isSuccess: true,
data: action.payload,
};
case API_ACTIONS.FAILURE:
if (action.status === 403) window.location.assign(ForisURLs.login);
return {
...state,
isSending: false,
isError: true,
isSuccess: false,
data: action.payload,
};
default:
throw new Error();
}
};
export function useAPIPost(url) {
const [state, dispatch] = useReducer(APIPostReducer, {
isSending: false,
isError: false,
isSuccess: false,
data: null,
});
const post = async (data) => {
dispatch({ type: API_ACTIONS.INIT });
try {
const result = await axios.post(url, data, {
timeout: TIMEOUT,
headers: HEADERS,
});
dispatch({ type: API_ACTIONS.SUCCESS, payload: result.data });
} catch (error) {
dispatch({
type: API_ACTIONS.FAILURE,
payload: error.response.data,
status: error.response.status,
});
}
};
return [state, post];
}
const APIDeleteReducer = (state, action) => {
switch (action.type) {
case API_ACTIONS.INIT:
return {
...state,
isSending: true,
isError: false,
isSuccess: false,
};
case API_ACTIONS.SUCCESS:
return {
...state,
isSending: false,
isError: false,
isSuccess: true,
};
case API_ACTIONS.FAILURE:
if (action.status === 403) {
window.location.assign(ForisURLs.login);
}
return {
...state,
isSending: false,
isError: true,
isSuccess: false,
data: action.payload,
};
default:
throw new Error();
}
};
export function useAPIDelete(url) {
const [state, dispatch] = useReducer(APIDeleteReducer, {
isSending: false,
isError: false,
isSuccess: false,
data: null,
});
const requestDelete = useCallback(async () => {
dispatch({ type: API_ACTIONS.INIT });
try {
await axios.delete(url, {
timeout: TIMEOUT,
headers: HEADERS,
});
dispatch({ type: API_ACTIONS.SUCCESS });
} catch (error) {
dispatch({
type: API_ACTIONS.FAILURE,
payload: error.response.data,
status: error.response.status,
});
}
}, [url]);
return [state, requestDelete];
}

40
src/api/patch.js Normal file
View File

@ -0,0 +1,40 @@
/*
* Copyright (C) 2019 CZ.NIC z.s.p.o. (http://www.nic.cz/)
*
* This is free software, licensed under the GNU General Public License v3.
* See /LICENSE for more information.
*/
import { useReducer } from "react";
import axios from "axios";
import {
API_ACTIONS, TIMEOUT, HEADERS, APIReducer,
} from "./utils";
export function useAPIPatch(url) {
const [state, dispatch] = useReducer(APIReducer, {
isSending: false,
isError: false,
isSuccess: false,
data: null,
});
const patch = async (data) => {
dispatch({ type: API_ACTIONS.INIT });
try {
const result = await axios.patch(url, data, {
timeout: TIMEOUT,
headers: HEADERS,
});
dispatch({ type: API_ACTIONS.SUCCESS, payload: result.data });
} catch (error) {
dispatch({
type: API_ACTIONS.FAILURE,
payload: error.response.data,
status: error.response.status,
});
}
};
return [state, patch];
}

40
src/api/post.js Normal file
View File

@ -0,0 +1,40 @@
/*
* Copyright (C) 2019 CZ.NIC z.s.p.o. (http://www.nic.cz/)
*
* This is free software, licensed under the GNU General Public License v3.
* See /LICENSE for more information.
*/
import { useReducer } from "react";
import axios from "axios";
import {
API_ACTIONS, TIMEOUT, HEADERS, APIReducer,
} from "./utils";
export function useAPIPost(url) {
const [state, dispatch] = useReducer(APIReducer, {
isSending: false,
isError: false,
isSuccess: false,
data: null,
});
const post = async (data) => {
dispatch({ type: API_ACTIONS.INIT });
try {
const result = await axios.post(url, data, {
timeout: TIMEOUT,
headers: HEADERS,
});
dispatch({ type: API_ACTIONS.SUCCESS, payload: result.data });
} catch (error) {
dispatch({
type: API_ACTIONS.FAILURE,
payload: error.response.data,
status: error.response.status,
});
}
};
return [state, post];
}

69
src/api/utils.js Normal file
View File

@ -0,0 +1,69 @@
/*
* Copyright (C) 2019 CZ.NIC z.s.p.o. (http://www.nic.cz/)
*
* This is free software, licensed under the GNU General Public License v3.
* See /LICENSE for more information.
*/
import { ForisURLs } from "forisUrls";
function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== "") {
const cookies = document.cookie.split(";");
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (`${name}=`)) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
export const HEADERS = {
Accept: "application/json",
"Content-Type": "application/json",
"X-CSRFToken": getCookie("_csrf_token"),
};
export const TIMEOUT = 5000;
export const API_ACTIONS = {
INIT: 1,
SUCCESS: 2,
FAILURE: 3,
};
export function APIReducer(state, action) {
switch (action.type) {
case API_ACTIONS.INIT:
return {
...state,
isSending: true,
isError: false,
isSuccess: false,
};
case API_ACTIONS.SUCCESS:
return {
...state,
isSending: false,
isError: false,
isSuccess: true,
data: action.payload,
};
case API_ACTIONS.FAILURE:
if (action.status === 403) window.location.assign(ForisURLs.login);
return {
...state,
isSending: false,
isError: true,
isSuccess: false,
data: action.payload,
};
default:
throw new Error();
}
}

View File

@ -9,7 +9,7 @@ import React, { useEffect, useState } from "react";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import { Spinner } from "bootstrap/Spinner"; import { Spinner } from "bootstrap/Spinner";
import { useAPIPost } from "api/hooks"; import { useAPIPost } from "api/post";
import { Prompt } from "react-router"; import { Prompt } from "react-router";
import { useForisModule, useForm } from "../hooks"; import { useForisModule, useForm } from "../hooks";

View File

@ -8,7 +8,7 @@
import { useCallback, useEffect, useReducer } from "react"; import { useCallback, useEffect, useReducer } from "react";
import update from "immutability-helper"; import update from "immutability-helper";
import { useAPIGet } from "api/hooks"; import { useAPIGet } from "api/get";
import { useWSForisModule } from "webSockets/hooks"; import { useWSForisModule } from "webSockets/hooks";

View File

@ -1,5 +1,8 @@
// API // API
export { useAPIGet, useAPIPost, useAPIDelete } from "./api/hooks"; export { useAPIGet } from "api/get";
export { useAPIPost } from "api/post";
export { useAPIDelete } from "api/delete";
export { useAPIPatch } from "api/patch";
// Bootstrap // Bootstrap
export { Alert } from "bootstrap/Alert"; export { Alert } from "bootstrap/Alert";