2022-07-27 12:00:15 +02:00
|
|
|
import {
|
|
|
|
IFeatureStrategyPayload,
|
|
|
|
IFeatureStrategy,
|
|
|
|
IFeatureStrategySortOrder,
|
|
|
|
} from 'interfaces/strategy';
|
2021-09-27 13:35:32 +02:00
|
|
|
import useAPI from '../useApi/useApi';
|
|
|
|
|
|
|
|
const useFeatureStrategyApi = () => {
|
|
|
|
const { makeRequest, createRequest, errors, loading } = useAPI({
|
|
|
|
propagateErrors: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const addStrategyToFeature = async (
|
|
|
|
projectId: string,
|
|
|
|
featureId: string,
|
|
|
|
environmentId: string,
|
2022-05-04 15:16:34 +02:00
|
|
|
payload: IFeatureStrategyPayload
|
2022-03-29 09:30:57 +02:00
|
|
|
): Promise<IFeatureStrategy> => {
|
2021-09-27 13:35:32 +02:00
|
|
|
const path = `api/admin/projects/${projectId}/features/${featureId}/environments/${environmentId}/strategies`;
|
|
|
|
const req = createRequest(
|
|
|
|
path,
|
|
|
|
{ method: 'POST', body: JSON.stringify(payload) },
|
|
|
|
'addStrategyToFeature'
|
|
|
|
);
|
2022-03-29 09:30:57 +02:00
|
|
|
return (await makeRequest(req.caller, req.id)).json();
|
2021-09-27 13:35:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const deleteStrategyFromFeature = async (
|
|
|
|
projectId: string,
|
|
|
|
featureId: string,
|
|
|
|
environmentId: string,
|
|
|
|
strategyId: string
|
2022-03-29 09:30:57 +02:00
|
|
|
): Promise<void> => {
|
2021-09-27 13:35:32 +02:00
|
|
|
const path = `api/admin/projects/${projectId}/features/${featureId}/environments/${environmentId}/strategies/${strategyId}`;
|
|
|
|
const req = createRequest(
|
|
|
|
path,
|
|
|
|
{ method: 'DELETE' },
|
|
|
|
'deleteStrategyFromFeature'
|
|
|
|
);
|
2022-03-29 09:30:57 +02:00
|
|
|
await makeRequest(req.caller, req.id);
|
2021-09-27 13:35:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const updateStrategyOnFeature = async (
|
|
|
|
projectId: string,
|
|
|
|
featureId: string,
|
|
|
|
environmentId: string,
|
|
|
|
strategyId: string,
|
2022-05-04 15:16:34 +02:00
|
|
|
payload: IFeatureStrategyPayload
|
2022-03-29 09:30:57 +02:00
|
|
|
): Promise<void> => {
|
2021-09-27 13:35:32 +02:00
|
|
|
const path = `api/admin/projects/${projectId}/features/${featureId}/environments/${environmentId}/strategies/${strategyId}`;
|
|
|
|
const req = createRequest(
|
|
|
|
path,
|
|
|
|
{ method: 'PUT', body: JSON.stringify(payload) },
|
|
|
|
'updateStrategyOnFeature'
|
|
|
|
);
|
2022-03-29 09:30:57 +02:00
|
|
|
await makeRequest(req.caller, req.id);
|
2021-09-27 13:35:32 +02:00
|
|
|
};
|
|
|
|
|
2022-07-27 12:00:15 +02:00
|
|
|
const setStrategiesSortOrder = async (
|
|
|
|
projectId: string,
|
|
|
|
featureId: string,
|
|
|
|
environmentId: string,
|
|
|
|
payload: IFeatureStrategySortOrder[]
|
|
|
|
): Promise<void> => {
|
|
|
|
const path = `api/admin/projects/${projectId}/features/${featureId}/environments/${environmentId}/strategies/set-sort-order`;
|
|
|
|
const req = createRequest(
|
|
|
|
path,
|
|
|
|
{ method: 'POST', body: JSON.stringify(payload) },
|
|
|
|
'setStrategiesSortOrderOnFeature'
|
|
|
|
);
|
|
|
|
await makeRequest(req.caller, req.id);
|
|
|
|
};
|
|
|
|
|
2021-09-27 13:35:32 +02:00
|
|
|
return {
|
|
|
|
addStrategyToFeature,
|
|
|
|
updateStrategyOnFeature,
|
|
|
|
deleteStrategyFromFeature,
|
2022-07-27 12:00:15 +02:00
|
|
|
setStrategiesSortOrder,
|
2021-09-27 13:35:32 +02:00
|
|
|
loading,
|
|
|
|
errors,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useFeatureStrategyApi;
|