From ff7185fe5b0ebaa6e4445a7461cbc7970f56510b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nuno=20G=C3=B3is?= Date: Fri, 10 Mar 2023 08:16:54 +0000 Subject: [PATCH] feat: allow adding project to segment (#3290) https://linear.app/unleash/issue/2-741/add-project-dropdown-into-creation-of-segments Allows users to bind a segment to a project when creating or editing a segment. image --- .../segments/CreateSegment/CreateSegment.tsx | 4 ++ .../segments/EditSegment/EditSegment.tsx | 5 +++ .../src/component/segments/SegmentForm.tsx | 6 +++ .../component/segments/SegmentFormStepOne.tsx | 43 ++++++++++++++++++- .../segments/hooks/useSegmentForm.ts | 9 ++++ frontend/src/interfaces/segment.ts | 1 + 6 files changed, 66 insertions(+), 2 deletions(-) diff --git a/frontend/src/component/segments/CreateSegment/CreateSegment.tsx b/frontend/src/component/segments/CreateSegment/CreateSegment.tsx index 81273f32f2..a65241b6d2 100644 --- a/frontend/src/component/segments/CreateSegment/CreateSegment.tsx +++ b/frontend/src/component/segments/CreateSegment/CreateSegment.tsx @@ -30,6 +30,8 @@ export const CreateSegment = () => { setName, description, setDescription, + project, + setProject, constraints, setConstraints, getSegmentPayload, @@ -91,6 +93,8 @@ export const CreateSegment = () => { setName={setName} description={description} setDescription={setDescription} + project={project} + setProject={setProject} constraints={constraints} setConstraints={setConstraints} errors={errors} diff --git a/frontend/src/component/segments/EditSegment/EditSegment.tsx b/frontend/src/component/segments/EditSegment/EditSegment.tsx index 7fc3f2613e..dbf440470b 100644 --- a/frontend/src/component/segments/EditSegment/EditSegment.tsx +++ b/frontend/src/component/segments/EditSegment/EditSegment.tsx @@ -33,6 +33,8 @@ export const EditSegment = () => { setName, description, setDescription, + project, + setProject, constraints, setConstraints, getSegmentPayload, @@ -41,6 +43,7 @@ export const EditSegment = () => { } = useSegmentForm( segment?.name, segment?.description, + segment?.project, segment?.constraints ); @@ -94,6 +97,8 @@ export const EditSegment = () => { setName={setName} description={description} setDescription={setDescription} + project={project} + setProject={setProject} constraints={constraints} setConstraints={setConstraints} errors={errors} diff --git a/frontend/src/component/segments/SegmentForm.tsx b/frontend/src/component/segments/SegmentForm.tsx index dd9f2ed4c2..40bed15338 100644 --- a/frontend/src/component/segments/SegmentForm.tsx +++ b/frontend/src/component/segments/SegmentForm.tsx @@ -12,9 +12,11 @@ export type SegmentFormMode = 'create' | 'edit'; interface ISegmentProps { name: string; description: string; + project: string | null; constraints: IConstraint[]; setName: React.Dispatch>; setDescription: React.Dispatch>; + setProject: React.Dispatch>; setConstraints: React.Dispatch>; handleSubmit: (e: any) => void; errors: { [key: string]: string }; @@ -32,9 +34,11 @@ export const SegmentForm: React.FC = ({ children, name, description, + project, constraints, setName, setDescription, + setProject, setConstraints, handleSubmit, errors, @@ -54,8 +58,10 @@ export const SegmentForm: React.FC = ({ >; setDescription: React.Dispatch>; + setProject: React.Dispatch>; errors: { [key: string]: string }; clearErrors: () => void; setCurrentStep: React.Dispatch>; @@ -52,13 +57,25 @@ export const SegmentFormStepOne: React.FC = ({ children, name, description, + project, setName, setDescription, + setProject, errors, clearErrors, setCurrentStep, }) => { + const { uiConfig } = useUiConfig(); const navigate = useNavigate(); + const { projects } = useProjects(); + + const [selectedProject, setSelectedProject] = React.useState( + projects.find(({ id }) => id === project) ?? null + ); + + useEffect(() => { + setSelectedProject(projects.find(({ id }) => id === project) ?? null); + }, [project, projects]); return ( @@ -87,6 +104,28 @@ export const SegmentFormStepOne: React.FC = ({ errorText={errors.description} data-testid={SEGMENT_DESC_ID} /> + + + Is this segment tied to a specific project? + + { + setProject(newValue?.id ?? null); + }} + options={projects} + getOptionLabel={option => option.name} + renderInput={params => ( + + )} + /> + + } + />