1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-10 01:16:39 +02:00
unleash.unleash/frontend/src/hooks/api/getters/useUser/useUser.ts
Youssef Khedher c34d8439bd Feat/splash (#491)
* splash screen

* add styles for controllers

* feat: animated circles

* fix: remove unused code

* fix: folder structure

* create splash screens for envs

* add styles and ui changes

* fix: revert App.tsx

* add splash state to store

* add splash to app.tsx + add a loader

* fix: mobile view + desktop view

* fix: render splash condition + styling fix

* fix: change splash display to full screen

* Update src/hooks/api/actions/useSplashApi/useSplashApi.ts

Co-authored-by: Fredrik Strand Oseberg <fredrik.no@gmail.com>

* fix: change function type

Co-authored-by: Fredrik Strand Oseberg <fredrik.no@gmail.com>

* fix: disable incrementing counter when matching the components length.

* fix: add SWR configuration

* fix: spelling mistakes in splash screen

* fix: add keys and adjust styling

* fix: tests

* fix: tests

* fix: default command timeout

Co-authored-by: Fredrik Oseberg <fredrik.no@gmail.com>
2021-11-26 11:12:37 +01:00

49 lines
1.3 KiB
TypeScript

import useSWR, { mutate, SWRConfiguration } from 'swr';
import { useState, useEffect } from 'react';
import { formatApiPath } from '../../../../utils/format-path';
import { IPermission } from '../../../../interfaces/user';
import handleErrorResponses from '../httpErrorResponseHandler';
export const USER_CACHE_KEY = `api/admin/user`;
const useUser = (
options: SWRConfiguration = {
revalidateIfStale: false,
revalidateOnFocus: false,
revalidateOnReconnect: false,
}
) => {
const fetcher = () => {
const path = formatApiPath(`api/admin/user`);
return fetch(path, {
method: 'GET',
})
.then(handleErrorResponses('User info'))
.then(res => res.json());
};
const { data, error } = useSWR(USER_CACHE_KEY, fetcher, options);
const [loading, setLoading] = useState(!error && !data);
const refetch = () => {
mutate(USER_CACHE_KEY);
};
useEffect(() => {
setLoading(!error && !data);
}, [data, error]);
return {
user: data?.user || {},
permissions: (data?.permissions || []) as IPermission[],
feedback: data?.feedback || [],
splash: data?.splash || {},
authDetails: data || {},
error,
loading,
refetch,
};
};
export default useUser;