1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-06 01:15:28 +02:00
unleash.unleash/frontend/src/hooks/api/getters/useStrategies/useStrategies.ts
Youssef Khedher c2842c81e6 Refactor/strategies (#668)
* feat: add useStrategiesApi hook

* refactor: remove redux from strategies component

* refactor: CreateStrategy Component

* fix: remove ts errors

* refactor: change strategy-detail to functional component

* refactor: get strategy name from params

* refactor: use features hook and refactor toggle list link

* refactor: StrategiesList

* refactor: fix delete strategy function

* fix: ts errors

* refactor: CreateStrategy to StrategyForm

* feat: add toast for StrategyForm

* refactor: add StrategyView and delete old component

* refactor: StrategyDetails and clean unused files

* fix: cleanup unused code

* fix: add await

* fix: remove unused stores

Co-authored-by: Fredrik Oseberg <fredrik.no@gmail.com>
2022-02-11 00:08:55 +01:00

69 lines
1.9 KiB
TypeScript

import useSWR, { mutate, SWRConfiguration } from 'swr';
import { useEffect, useState } from 'react';
import { formatApiPath } from '../../../../utils/format-path';
import { IStrategy } from '../../../../interfaces/strategy';
import handleErrorResponses from '../httpErrorResponseHandler';
export const STRATEGIES_CACHE_KEY = 'api/admin/strategies';
const flexibleRolloutStrategy: IStrategy = {
deprecated: false,
name: 'flexibleRollout',
displayName: 'Gradual rollout',
editable: false,
description:
'Roll out to a percentage of your userbase, and ensure that the experience is the same for the user on each visit.',
parameters: [
{
name: 'rollout',
type: 'percentage',
description: '',
required: false,
},
{
name: 'stickiness',
type: 'string',
description: 'Used to defined stickiness',
required: true,
},
{ name: 'groupId', type: 'string', description: '', required: true },
],
};
const useStrategies = (options: SWRConfiguration = {}) => {
const fetcher = () => {
const path = formatApiPath(`api/admin/strategies`);
return fetch(path, {
method: 'GET',
credentials: 'include',
})
.then(handleErrorResponses('Strategies'))
.then(res => res.json());
};
const { data, error } = useSWR<{ strategies: IStrategy[] }>(
STRATEGIES_CACHE_KEY,
fetcher,
options
);
const [loading, setLoading] = useState(!error && !data);
const refetchStrategies = () => {
mutate(STRATEGIES_CACHE_KEY);
};
useEffect(() => {
setLoading(!error && !data);
}, [data, error]);
return {
strategies: data?.strategies || [flexibleRolloutStrategy],
error,
loading,
refetchStrategies,
};
};
export default useStrategies;