Skip to content

Commit

Permalink
feat: show available parent dependency options (#4837)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed Sep 27, 2023
1 parent 6d5eec2 commit 889377a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
Expand Up @@ -3,6 +3,7 @@ import { Box, styled, Typography } from '@mui/material';
import { Dialogue } from 'component/common/Dialogue/Dialogue';
import GeneralSelect from 'component/common/GeneralSelect/GeneralSelect';
import { useDependentFeaturesApi } from 'hooks/api/actions/useDependentFeaturesApi/useDependentFeaturesApi';
import { useParentOptions } from 'hooks/api/getters/useParentOptions/useParentOptions';

interface IAddDependencyDialogueProps {
featureId: string;
Expand All @@ -15,21 +16,33 @@ const StyledSelect = styled(GeneralSelect)(({ theme }) => ({
marginBottom: theme.spacing(1.5),
}));

const REMOVE_DEPENDENCY_OPTION = {
key: 'none (remove dependency)',
label: 'none (remove dependency)',
};

export const AddDependencyDialogue = ({
featureId,
showDependencyDialogue,
onClose,
}: IAddDependencyDialogueProps) => {
const [parent, setParent] = useState('');
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 (
<Dialogue
open={showDependencyDialogue}
title="Add parent feature dependency"
onClose={onClose}
onClick={async () => {
if (parent === '') {
if (parent === REMOVE_DEPENDENCY_OPTION.key) {
await removeDependencies(featureId);
} else {
await addDependency(featureId, { feature: parent });
Expand All @@ -47,11 +60,7 @@ export const AddDependencyDialogue = ({
<Typography>What feature do you want to depend on?</Typography>
<StyledSelect
fullWidth
options={[
{ key: 'colors', label: 'colors' },
{ key: 'parent', label: 'parent' },
{ key: 'empty', label: '' },
]}
options={options}
value={parent}
onChange={setParent}
/>
Expand Down
@@ -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;
};

0 comments on commit 889377a

Please sign in to comment.