mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	feat: create useAddons and useAddonsApi
This commit is contained in:
		
							parent
							
								
									2c570d6539
								
							
						
					
					
						commit
						f3ab91556b
					
				
							
								
								
									
										66
									
								
								frontend/src/hooks/api/actions/useAddonsApi/useAddonsApi.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								frontend/src/hooks/api/actions/useAddonsApi/useAddonsApi.ts
									
									
									
									
									
										Normal 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;
 | 
			
		||||
							
								
								
									
										36
									
								
								frontend/src/hooks/api/getters/useAddons/useAddons.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								frontend/src/hooks/api/getters/useAddons/useAddons.ts
									
									
									
									
									
										Normal 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;
 | 
			
		||||
							
								
								
									
										8
									
								
								frontend/src/interfaces/addons.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								frontend/src/interfaces/addons.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,8 @@
 | 
			
		||||
export interface IAddons {
 | 
			
		||||
    id: number;
 | 
			
		||||
    provider: string;
 | 
			
		||||
    description: string;
 | 
			
		||||
    enabled: boolean;
 | 
			
		||||
    events: string[];
 | 
			
		||||
    parameters: object;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user