1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/frontend/src/hooks/api/actions/useFeatureApi/useFeatureApi.ts
Fredrik Strand Oseberg 2c1353d0ea Feat/new constraints (#718)
* fix: add initial interface

* feat: create separate components

* feat: implement constraints for existing strategies

* feat: add searchfield

* fix: reset values on context change

* fix: key issue with duplicate values

* fix: increase auto hide duration of errors

* fix: types

* feat: resolve inputs

* fix: add date input

* fix: add filter

* fix: create strategy

* fix: remove unused deps

* feat: validation

* fix: type setError

* feat: handle currentTime based on client spec

* fix: date field

* feat: api validation

* fix: refactor

* fix: refactor

* feat: add compact

* fix: remove unused code

* feat: mobile optimisations

* fix: remove coalescing operator for constraint

* fix: clone deep

* fix: move parseDate

* fix: lift state up for value setting on dates

* fix: rename values

* fix: change type to interface

* fix: lazy initialise values

* fix: create operator type

* fix: update naming

* fix: naming

* fix: aria hidden

* fix: remove optional operator

* fix: rename new constraints

* fix: setup flag

* fix: refactor date check to date-fns

* fix: use date-fns for validation

* fix: detach validators from state

* refactor: move resolve input to it's own component

* fix: remove unused imports

* fix: change values container to overflow auto

* fix: update placeholder

* fix: update import

* fix: backwards compatability

* fix: hide paragraphs if not active

* fix: update path

* fix: update strategy text
2022-03-04 17:29:51 +01:00

262 lines
6.7 KiB
TypeScript

import { IFeatureTogglePayload } from '../../../../interfaces/featureToggle';
import { ITag } from '../../../../interfaces/tags';
import useAPI from '../useApi/useApi';
import { Operation } from 'fast-json-patch';
import { IConstraint } from 'interfaces/strategy';
const useFeatureApi = () => {
const { makeRequest, createRequest, errors, loading } = useAPI({
propagateErrors: true,
});
const validateFeatureToggleName = async (name: string) => {
const path = `api/admin/features/validate`;
const req = createRequest(path, {
method: 'POST',
body: JSON.stringify({ name }),
});
try {
const res = await makeRequest(req.caller, req.id);
return res;
} catch (e) {
throw e;
}
};
const validateConstraint = async (
projectId: string,
featureName: string,
constraint: IConstraint
) => {
const path = `api/admin/projects/${projectId}/features/${featureName}/constraint/validate`;
const req = createRequest(path, {
method: 'POST',
body: JSON.stringify(constraint),
});
try {
const res = await makeRequest(req.caller, req.id);
return res;
} catch (e) {
throw e;
}
};
const createFeatureToggle = async (
projectId: string,
featureToggle: IFeatureTogglePayload
) => {
const path = `api/admin/projects/${projectId}/features`;
const req = createRequest(path, {
method: 'POST',
body: JSON.stringify(featureToggle),
});
try {
const res = await makeRequest(req.caller, req.id);
return res;
} catch (e) {
throw e;
}
};
const toggleFeatureEnvironmentOn = async (
projectId: string,
featureId: string,
environmentId: string
) => {
const path = `api/admin/projects/${projectId}/features/${featureId}/environments/${environmentId}/on`;
const req = createRequest(
path,
{ method: 'POST' },
'toggleFeatureEnvironmentOn'
);
try {
const res = await makeRequest(req.caller, req.id);
return res;
} catch (e) {
throw e;
}
};
const toggleFeatureEnvironmentOff = async (
projectId: string,
featureId: string,
environmentId: string
) => {
const path = `api/admin/projects/${projectId}/features/${featureId}/environments/${environmentId}/off`;
const req = createRequest(
path,
{ method: 'POST' },
'toggleFeatureEnvironmentOff'
);
try {
const res = await makeRequest(req.caller, req.id);
return res;
} catch (e) {
throw e;
}
};
const changeFeatureProject = async (
projectId: string,
featureId: string,
newProjectId: string
) => {
const path = `api/admin/projects/${projectId}/features/${featureId}/changeProject`;
const req = createRequest(path, {
method: 'POST',
body: JSON.stringify({ newProjectId }),
});
try {
const res = await makeRequest(req.caller, req.id);
return res;
} catch (e) {
throw e;
}
};
const addTagToFeature = async (featureId: string, tag: ITag) => {
// TODO: Change this path to the new API when moved.
const path = `api/admin/features/${featureId}/tags`;
const req = createRequest(path, {
method: 'POST',
body: JSON.stringify({ ...tag }),
});
try {
const res = await makeRequest(req.caller, req.id);
return res;
} catch (e) {
throw e;
}
};
const deleteTagFromFeature = async (
featureId: string,
type: string,
value: string
) => {
// TODO: Change this path to the new API when moved.
const path = `api/admin/features/${featureId}/tags/${type}/${value}`;
const req = createRequest(path, {
method: 'DELETE',
});
try {
const res = await makeRequest(req.caller, req.id);
return res;
} catch (e) {
throw e;
}
};
const archiveFeatureToggle = async (
projectId: string,
featureId: string
) => {
const path = `api/admin/projects/${projectId}/features/${featureId}`;
const req = createRequest(path, {
method: 'DELETE',
});
try {
const res = await makeRequest(req.caller, req.id);
return res;
} catch (e) {
throw e;
}
};
const patchFeatureToggle = async (
projectId: string,
featureId: string,
patchPayload: any
) => {
const path = `api/admin/projects/${projectId}/features/${featureId}`;
const req = createRequest(path, {
method: 'PATCH',
body: JSON.stringify(patchPayload),
});
try {
const res = await makeRequest(req.caller, req.id);
return res;
} catch (e) {
throw e;
}
};
const patchFeatureVariants = async (
projectId: string,
featureId: string,
patchPayload: Operation[]
) => {
const path = `api/admin/projects/${projectId}/features/${featureId}/variants`;
const req = createRequest(path, {
method: 'PATCH',
body: JSON.stringify(patchPayload),
});
try {
const res = await makeRequest(req.caller, req.id);
return res;
} catch (e) {
throw e;
}
};
const cloneFeatureToggle = async (
projectId: string,
featureId: string,
payload: { name: string; replaceGroupId: boolean }
) => {
const path = `api/admin/projects/${projectId}/features/${featureId}/clone`;
const req = createRequest(path, {
method: 'POST',
body: JSON.stringify(payload),
});
try {
const res = await makeRequest(req.caller, req.id);
return res;
} catch (e) {
throw e;
}
};
return {
validateFeatureToggleName,
validateConstraint,
createFeatureToggle,
changeFeatureProject,
errors,
toggleFeatureEnvironmentOn,
toggleFeatureEnvironmentOff,
addTagToFeature,
deleteTagFromFeature,
archiveFeatureToggle,
patchFeatureToggle,
patchFeatureVariants,
cloneFeatureToggle,
loading,
};
};
export default useFeatureApi;