1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-19 01:17:18 +02:00
unleash.unleash/frontend/src/hooks/api/getters/useProjects/useProjects.ts
Fredrik Strand Oseberg 85a7c55fdf Feat/group by projects (#308)
This PR adds support for projects as a first class citizen, and toggling features on in different environments.
2021-07-07 11:04:36 +02:00

37 lines
887 B
TypeScript

import useSWR, { mutate } from 'swr';
import { useState, useEffect } from 'react';
import { formatApiPath } from '../../../../utils/format-path';
import { IProjectCard } from '../../../../interfaces/project';
const useProjects = () => {
const fetcher = () => {
const path = formatApiPath(`api/admin/projects`);
return fetch(path, {
method: 'GET',
}).then(res => res.json());
};
const KEY = `api/admin/projects`;
const { data, error } = useSWR<{ projects: IProjectCard[] }>(KEY, fetcher);
const [loading, setLoading] = useState(!error && !data);
const refetch = () => {
mutate(KEY);
};
useEffect(() => {
setLoading(!error && !data);
}, [data, error]);
return {
projects: data?.projects || [],
error,
loading,
refetch,
};
};
export default useProjects;