Skip to content

Commit

Permalink
feat: allow adding project to segment (#3290)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunogois committed Mar 10, 2023
1 parent 9b563f6 commit ff7185f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 2 deletions.
Expand Up @@ -30,6 +30,8 @@ export const CreateSegment = () => {
setName,
description,
setDescription,
project,
setProject,
constraints,
setConstraints,
getSegmentPayload,
Expand Down Expand Up @@ -91,6 +93,8 @@ export const CreateSegment = () => {
setName={setName}
description={description}
setDescription={setDescription}
project={project}
setProject={setProject}
constraints={constraints}
setConstraints={setConstraints}
errors={errors}
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/component/segments/EditSegment/EditSegment.tsx
Expand Up @@ -33,6 +33,8 @@ export const EditSegment = () => {
setName,
description,
setDescription,
project,
setProject,
constraints,
setConstraints,
getSegmentPayload,
Expand All @@ -41,6 +43,7 @@ export const EditSegment = () => {
} = useSegmentForm(
segment?.name,
segment?.description,
segment?.project,
segment?.constraints
);

Expand Down Expand Up @@ -94,6 +97,8 @@ export const EditSegment = () => {
setName={setName}
description={description}
setDescription={setDescription}
project={project}
setProject={setProject}
constraints={constraints}
setConstraints={setConstraints}
errors={errors}
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/component/segments/SegmentForm.tsx
Expand Up @@ -12,9 +12,11 @@ export type SegmentFormMode = 'create' | 'edit';
interface ISegmentProps {
name: string;
description: string;
project: string | null;
constraints: IConstraint[];
setName: React.Dispatch<React.SetStateAction<string>>;
setDescription: React.Dispatch<React.SetStateAction<string>>;
setProject: React.Dispatch<React.SetStateAction<string | null>>;
setConstraints: React.Dispatch<React.SetStateAction<IConstraint[]>>;
handleSubmit: (e: any) => void;
errors: { [key: string]: string };
Expand All @@ -32,9 +34,11 @@ export const SegmentForm: React.FC<ISegmentProps> = ({
children,
name,
description,
project,
constraints,
setName,
setDescription,
setProject,
setConstraints,
handleSubmit,
errors,
Expand All @@ -54,8 +58,10 @@ export const SegmentForm: React.FC<ISegmentProps> = ({
<SegmentFormStepOne
name={name}
description={description}
project={project}
setName={setName}
setDescription={setDescription}
setProject={setProject}
errors={errors}
clearErrors={clearErrors}
setCurrentStep={setCurrentStep}
Expand Down
43 changes: 41 additions & 2 deletions frontend/src/component/segments/SegmentFormStepOne.tsx
@@ -1,19 +1,24 @@
import { Button, styled } from '@mui/material';
import { Autocomplete, Button, styled, TextField } from '@mui/material';
import Input from 'component/common/Input/Input';
import React from 'react';
import React, { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { SegmentFormStep } from './SegmentForm';
import {
SEGMENT_NAME_ID,
SEGMENT_DESC_ID,
SEGMENT_NEXT_BTN_ID,
} from 'utils/testIds';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
import useProjects from 'hooks/api/getters/useProjects/useProjects';

interface ISegmentFormPartOneProps {
name: string;
description: string;
project: string | null;
setName: React.Dispatch<React.SetStateAction<string>>;
setDescription: React.Dispatch<React.SetStateAction<string>>;
setProject: React.Dispatch<React.SetStateAction<string | null>>;
errors: { [key: string]: string };
clearErrors: () => void;
setCurrentStep: React.Dispatch<React.SetStateAction<SegmentFormStep>>;
Expand Down Expand Up @@ -52,13 +57,25 @@ export const SegmentFormStepOne: React.FC<ISegmentFormPartOneProps> = ({
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 (
<StyledForm>
Expand Down Expand Up @@ -87,6 +104,28 @@ export const SegmentFormStepOne: React.FC<ISegmentFormPartOneProps> = ({
errorText={errors.description}
data-testid={SEGMENT_DESC_ID}
/>
<ConditionallyRender
condition={Boolean(uiConfig.flags.projectScopedSegments)}
show={
<>
<StyledInputDescription>
Is this segment tied to a specific project?
</StyledInputDescription>
<Autocomplete
size="small"
value={selectedProject}
onChange={(_, newValue) => {
setProject(newValue?.id ?? null);
}}
options={projects}
getOptionLabel={option => option.name}
renderInput={params => (
<TextField {...params} label="Project" />
)}
/>
</>
}
/>
</StyledContainer>
<StyledButtonContainer>
<Button
Expand Down
9 changes: 9 additions & 0 deletions frontend/src/component/segments/hooks/useSegmentForm.ts
Expand Up @@ -5,10 +5,12 @@ import { useSegmentValidation } from 'hooks/api/getters/useSegmentValidation/use
export const useSegmentForm = (
initialName = '',
initialDescription = '',
initialProject: string | null = null,
initialConstraints: IConstraint[] = []
) => {
const [name, setName] = useState(initialName);
const [description, setDescription] = useState(initialDescription);
const [project, setProject] = useState<string | null>(initialProject);
const [constraints, setConstraints] =
useState<IConstraint[]>(initialConstraints);
const [errors, setErrors] = useState({});
Expand All @@ -22,6 +24,10 @@ export const useSegmentForm = (
setDescription(initialDescription);
}, [initialDescription]);

useEffect(() => {
setProject(initialProject);
}, [initialProject]);

useEffect(() => {
setConstraints(initialConstraints);
// eslint-disable-next-line
Expand All @@ -38,6 +44,7 @@ export const useSegmentForm = (
return {
name,
description,
project,
constraints,
};
};
Expand All @@ -51,6 +58,8 @@ export const useSegmentForm = (
setName,
description,
setDescription,
project,
setProject,
constraints,
setConstraints,
getSegmentPayload,
Expand Down
1 change: 1 addition & 0 deletions frontend/src/interfaces/segment.ts
Expand Up @@ -4,6 +4,7 @@ export interface ISegment {
id: number;
name: string;
description: string;
project: string | null;
createdAt: string;
createdBy: string;
constraints: IConstraint[];
Expand Down

0 comments on commit ff7185f

Please sign in to comment.