mirror of
				https://gitlab.nic.cz/turris/reforis/foris-js.git
				synced 2025-11-03 23:00:31 +01:00 
			
		
		
		
	Resolve "Handle PATCH request"
This commit is contained in:
		
							
								
								
									
										4
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								Makefile
									
									
									
									
									
								
							@@ -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
									
									
									
								
							
							
						
						
									
										2
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							@@ -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": {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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
									
								
							
							
						
						
									
										41
									
								
								src/api/delete.js
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										65
									
								
								src/api/get.js
									
									
									
									
									
										Normal 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];
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										213
									
								
								src/api/hooks.js
									
									
									
									
									
								
							
							
						
						
									
										213
									
								
								src/api/hooks.js
									
									
									
									
									
								
							@@ -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
									
								
							
							
						
						
									
										40
									
								
								src/api/patch.js
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										40
									
								
								src/api/post.js
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										69
									
								
								src/api/utils.js
									
									
									
									
									
										Normal 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();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -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";
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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";
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user