From f3ab91556bd6d573405abf463fb0b7c71bf9be27 Mon Sep 17 00:00:00 2001 From: Youssef Date: Tue, 1 Feb 2022 07:17:23 +0100 Subject: [PATCH] feat: create useAddons and useAddonsApi --- .../api/actions/useAddonsApi/useAddonsApi.ts | 66 +++++++++++++++++++ .../hooks/api/getters/useAddons/useAddons.ts | 36 ++++++++++ frontend/src/interfaces/addons.ts | 8 +++ 3 files changed, 110 insertions(+) create mode 100644 frontend/src/hooks/api/actions/useAddonsApi/useAddonsApi.ts create mode 100644 frontend/src/hooks/api/getters/useAddons/useAddons.ts create mode 100644 frontend/src/interfaces/addons.ts diff --git a/frontend/src/hooks/api/actions/useAddonsApi/useAddonsApi.ts b/frontend/src/hooks/api/actions/useAddonsApi/useAddonsApi.ts new file mode 100644 index 0000000000..2dd5b48536 --- /dev/null +++ b/frontend/src/hooks/api/actions/useAddonsApi/useAddonsApi.ts @@ -0,0 +1,66 @@ +import { SWRConfiguration } from 'swr'; +import { IAddons } from '../../../../interfaces/addons'; +import useAPI from '../useApi/useApi'; + +const useAddonsApi = (options: SWRConfiguration = {}) => { + const { makeRequest, createRequest, errors, loading } = useAPI({ + propagateErrors: true, + }); + + const URI = 'api/admin/addons'; + + const createAddon = async (addonConfig: IAddons) => { + const path = URI; + const req = createRequest(path, { + method: 'POST', + body: JSON.stringify(addonConfig), + }); + + try { + const res = await makeRequest(req.caller, req.id); + + return res; + } catch (e) { + throw e; + } + }; + + const removeAddon = async (id: number) => { + const path = `${URI}/${id}`; + const req = createRequest(path, { + method: 'DELETE', + }); + try { + const res = await makeRequest(req.caller, req.id); + + return res; + } catch (e) { + throw e; + } + }; + + const updateAddon = async (addonConfig: IAddons) => { + const path = `${URI}/${addonConfig.id}`; + const req = createRequest(path, { + method: 'PUT', + body: JSON.stringify(addonConfig), + }); + try { + const res = await makeRequest(req.caller, req.id); + + return res; + } catch (e) { + throw e; + } + }; + + return { + createAddon, + updateAddon, + removeAddon, + errors, + loading, + }; +}; + +export default useAddonsApi; diff --git a/frontend/src/hooks/api/getters/useAddons/useAddons.ts b/frontend/src/hooks/api/getters/useAddons/useAddons.ts new file mode 100644 index 0000000000..9479641740 --- /dev/null +++ b/frontend/src/hooks/api/getters/useAddons/useAddons.ts @@ -0,0 +1,36 @@ +import useSWR, { mutate, SWRConfiguration } from 'swr'; +import { useState, useEffect } from 'react'; +import { formatApiPath } from '../../../../utils/format-path'; +import handleErrorResponses from '../httpErrorResponseHandler'; + +const useAddons = (options: SWRConfiguration = {}) => { + const fetcher = async () => { + const path = formatApiPath(`api/admin/addons`); + const res = await fetch(path, { + method: 'GET', + }).then(handleErrorResponses('Addons')); + return res.json(); + }; + + const KEY = `api/admin/addons`; + + const { data, error } = useSWR(KEY, fetcher, options); + const [loading, setLoading] = useState(!error && !data); + + const refetch = () => { + mutate(KEY); + }; + + useEffect(() => { + setLoading(!error && !data); + }, [data, error]); + + return { + addons: data || [], + error, + loading, + refetch, + }; +}; + +export default useAddons; diff --git a/frontend/src/interfaces/addons.ts b/frontend/src/interfaces/addons.ts new file mode 100644 index 0000000000..4ae31b449d --- /dev/null +++ b/frontend/src/interfaces/addons.ts @@ -0,0 +1,8 @@ +export interface IAddons { + id: number; + provider: string; + description: string; + enabled: boolean; + events: string[]; + parameters: object; +}