1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

feat: create useAddons and useAddonsApi

This commit is contained in:
Youssef 2022-02-01 07:17:23 +01:00
parent 2c570d6539
commit f3ab91556b
3 changed files with 110 additions and 0 deletions

View File

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

View File

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

View File

@ -0,0 +1,8 @@
export interface IAddons {
id: number;
provider: string;
description: string;
enabled: boolean;
events: string[];
parameters: object;
}