mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-14 00:19:16 +01:00
feat: Connect add dependency api (#4831)
This commit is contained in:
parent
e7b1e7979e
commit
45aca5b09e
@ -2,8 +2,10 @@ import React, { useState } from 'react';
|
|||||||
import { Box, styled, Typography } from '@mui/material';
|
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';
|
||||||
|
|
||||||
interface IAddDependencyDialogueProps {
|
interface IAddDependencyDialogueProps {
|
||||||
|
featureId: string;
|
||||||
showDependencyDialogue: boolean;
|
showDependencyDialogue: boolean;
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
}
|
}
|
||||||
@ -14,18 +16,27 @@ const StyledSelect = styled(GeneralSelect)(({ theme }) => ({
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
export const AddDependencyDialogue = ({
|
export const AddDependencyDialogue = ({
|
||||||
|
featureId,
|
||||||
showDependencyDialogue,
|
showDependencyDialogue,
|
||||||
onClose,
|
onClose,
|
||||||
}: IAddDependencyDialogueProps) => {
|
}: IAddDependencyDialogueProps) => {
|
||||||
const [dependency, setDependency] = useState('');
|
const [parent, setParent] = useState('');
|
||||||
|
const { addDependency, removeDependencies } = useDependentFeaturesApi();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialogue
|
<Dialogue
|
||||||
open={showDependencyDialogue}
|
open={showDependencyDialogue}
|
||||||
title="Add parent feature dependency"
|
title="Add parent feature dependency"
|
||||||
onClose={onClose}
|
onClose={onClose}
|
||||||
onClick={() => {}}
|
onClick={async () => {
|
||||||
primaryButtonText={'Add'}
|
if (parent === '') {
|
||||||
|
await removeDependencies(featureId);
|
||||||
|
} else {
|
||||||
|
await addDependency(featureId, { feature: parent });
|
||||||
|
}
|
||||||
|
onClose();
|
||||||
|
}}
|
||||||
|
primaryButtonText="Add"
|
||||||
secondaryButtonText="Cancel"
|
secondaryButtonText="Cancel"
|
||||||
>
|
>
|
||||||
<Box>
|
<Box>
|
||||||
@ -37,11 +48,12 @@ export const AddDependencyDialogue = ({
|
|||||||
<StyledSelect
|
<StyledSelect
|
||||||
fullWidth
|
fullWidth
|
||||||
options={[
|
options={[
|
||||||
{ key: 'a', label: 'featureA' },
|
{ key: 'colors', label: 'colors' },
|
||||||
|
{ key: 'parent', label: 'parent' },
|
||||||
{ key: 'empty', label: '' },
|
{ key: 'empty', label: '' },
|
||||||
]}
|
]}
|
||||||
value={dependency}
|
value={parent}
|
||||||
onChange={setDependency}
|
onChange={setParent}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
</Dialogue>
|
</Dialogue>
|
||||||
|
@ -94,6 +94,7 @@ export const FeatureOverviewSidePanelDetails = ({
|
|||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
<AddDependencyDialogue
|
<AddDependencyDialogue
|
||||||
|
featureId={feature.name}
|
||||||
onClose={() => setShowDependencyDialogue(false)}
|
onClose={() => setShowDependencyDialogue(false)}
|
||||||
showDependencyDialogue={
|
showDependencyDialogue={
|
||||||
dependentFeatures && showDependencyDialogue
|
dependentFeatures && showDependencyDialogue
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
import useAPI from '../useApi/useApi';
|
import useAPI from '../useApi/useApi';
|
||||||
|
import useToast from '../../../useToast';
|
||||||
|
import { formatUnknownError } from '../../../../utils/formatUnknownError';
|
||||||
|
import { useCallback } from 'react';
|
||||||
|
|
||||||
// TODO: generate from orval
|
// TODO: generate from orval
|
||||||
interface IParentFeaturePayload {
|
interface IParentFeaturePayload {
|
||||||
@ -8,6 +11,7 @@ export const useDependentFeaturesApi = () => {
|
|||||||
const { makeRequest, createRequest, errors, loading } = useAPI({
|
const { makeRequest, createRequest, errors, loading } = useAPI({
|
||||||
propagateErrors: true,
|
propagateErrors: true,
|
||||||
});
|
});
|
||||||
|
const { setToastData, setToastApiError } = useToast();
|
||||||
|
|
||||||
const addDependency = async (
|
const addDependency = async (
|
||||||
childFeature: string,
|
childFeature: string,
|
||||||
@ -22,13 +26,67 @@ export const useDependentFeaturesApi = () => {
|
|||||||
);
|
);
|
||||||
try {
|
try {
|
||||||
await makeRequest(req.caller, req.id);
|
await makeRequest(req.caller, req.id);
|
||||||
} catch (e) {
|
|
||||||
throw e;
|
setToastData({
|
||||||
|
title: 'Dependency added',
|
||||||
|
type: 'success',
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
setToastApiError(formatUnknownError(error));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const removeDependency = async (
|
||||||
|
childFeature: string,
|
||||||
|
parentFeature: string
|
||||||
|
) => {
|
||||||
|
const req = createRequest(
|
||||||
|
`/api/admin/projects/default/features/${childFeature}/dependencies/${parentFeature}`,
|
||||||
|
{
|
||||||
|
method: 'DELETE',
|
||||||
|
}
|
||||||
|
);
|
||||||
|
try {
|
||||||
|
await makeRequest(req.caller, req.id);
|
||||||
|
|
||||||
|
setToastData({
|
||||||
|
title: 'Dependency removed',
|
||||||
|
type: 'success',
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
setToastApiError(formatUnknownError(error));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const removeDependencies = async (childFeature: string) => {
|
||||||
|
const req = createRequest(
|
||||||
|
`/api/admin/projects/default/features/${childFeature}/dependencies`,
|
||||||
|
{
|
||||||
|
method: 'DELETE',
|
||||||
|
}
|
||||||
|
);
|
||||||
|
try {
|
||||||
|
await makeRequest(req.caller, req.id);
|
||||||
|
|
||||||
|
setToastData({
|
||||||
|
title: 'Dependencies removed',
|
||||||
|
type: 'success',
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
setToastApiError(formatUnknownError(error));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const callbackDeps = [
|
||||||
|
createRequest,
|
||||||
|
makeRequest,
|
||||||
|
setToastData,
|
||||||
|
formatUnknownError,
|
||||||
|
];
|
||||||
return {
|
return {
|
||||||
addDependency,
|
addDependency: useCallback(addDependency, callbackDeps),
|
||||||
|
removeDependency: useCallback(removeDependency, callbackDeps),
|
||||||
|
removeDependencies: useCallback(removeDependencies, callbackDeps),
|
||||||
errors,
|
errors,
|
||||||
loading,
|
loading,
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user