1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-09 00:18:00 +01:00

feat: show available parent dependency options (#4837)

This commit is contained in:
Mateusz Kwasniewski 2023-09-27 08:10:15 +02:00 committed by GitHub
parent 6d5eec2810
commit 889377a246
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 6 deletions

View File

@ -3,6 +3,7 @@ import { Box, styled, Typography } from '@mui/material';
import { Dialogue } from 'component/common/Dialogue/Dialogue'; import { Dialogue } from 'component/common/Dialogue/Dialogue';
import GeneralSelect from 'component/common/GeneralSelect/GeneralSelect'; import GeneralSelect from 'component/common/GeneralSelect/GeneralSelect';
import { useDependentFeaturesApi } from 'hooks/api/actions/useDependentFeaturesApi/useDependentFeaturesApi'; import { useDependentFeaturesApi } from 'hooks/api/actions/useDependentFeaturesApi/useDependentFeaturesApi';
import { useParentOptions } from 'hooks/api/getters/useParentOptions/useParentOptions';
interface IAddDependencyDialogueProps { interface IAddDependencyDialogueProps {
featureId: string; featureId: string;
@ -15,6 +16,11 @@ const StyledSelect = styled(GeneralSelect)(({ theme }) => ({
marginBottom: theme.spacing(1.5), marginBottom: theme.spacing(1.5),
})); }));
const REMOVE_DEPENDENCY_OPTION = {
key: 'none (remove dependency)',
label: 'none (remove dependency)',
};
export const AddDependencyDialogue = ({ export const AddDependencyDialogue = ({
featureId, featureId,
showDependencyDialogue, showDependencyDialogue,
@ -22,6 +28,13 @@ export const AddDependencyDialogue = ({
}: IAddDependencyDialogueProps) => { }: IAddDependencyDialogueProps) => {
const [parent, setParent] = useState(''); const [parent, setParent] = useState('');
const { addDependency, removeDependencies } = useDependentFeaturesApi(); const { addDependency, removeDependencies } = useDependentFeaturesApi();
const { parentOptions } = useParentOptions(featureId);
const options = parentOptions
? [
REMOVE_DEPENDENCY_OPTION,
...parentOptions.map(parent => ({ key: parent, label: parent })),
]
: [REMOVE_DEPENDENCY_OPTION];
return ( return (
<Dialogue <Dialogue
@ -29,7 +42,7 @@ export const AddDependencyDialogue = ({
title="Add parent feature dependency" title="Add parent feature dependency"
onClose={onClose} onClose={onClose}
onClick={async () => { onClick={async () => {
if (parent === '') { if (parent === REMOVE_DEPENDENCY_OPTION.key) {
await removeDependencies(featureId); await removeDependencies(featureId);
} else { } else {
await addDependency(featureId, { feature: parent }); await addDependency(featureId, { feature: parent });
@ -47,11 +60,7 @@ export const AddDependencyDialogue = ({
<Typography>What feature do you want to depend on?</Typography> <Typography>What feature do you want to depend on?</Typography>
<StyledSelect <StyledSelect
fullWidth fullWidth
options={[ options={options}
{ key: 'colors', label: 'colors' },
{ key: 'parent', label: 'parent' },
{ key: 'empty', label: '' },
]}
value={parent} value={parent}
onChange={setParent} onChange={setParent}
/> />

View File

@ -0,0 +1,25 @@
import useSWR, { SWRConfiguration } from 'swr';
import { formatApiPath } from 'utils/formatPath';
import handleErrorResponses from '../httpErrorResponseHandler';
export const useParentOptions = (
childFeatureId: string,
options: SWRConfiguration = {}
) => {
const path = formatApiPath(
`/api/admin/projects/default/features/${childFeatureId}/parents`
);
const { data, error, mutate } = useSWR(path, fetcher, options);
return {
parentOptions: data,
error,
loading: !error && !data,
};
};
const fetcher = async (path: string): Promise<string[]> => {
const res = await fetch(path).then(handleErrorResponses('Parent Options'));
const data = await res.json();
return data;
};