Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: switch to using milestoneId instead of index #8845

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,17 @@ const StyledMilestoneCardTitle = styled('span')(({ theme }) => ({
}));

interface IMilestoneCardProps {
index: number;
milestone: IReleasePlanMilestonePayload;
milestoneNameChanged: (index: number, name: string) => void;
milestoneNameChanged: (milestoneId: string, name: string) => void;
showAddStrategyDialog: (
index: number,
milestoneId: string,
strategy: IReleasePlanMilestoneStrategy,
) => void;
errors: { [key: string]: string };
clearErrors: () => void;
}

export const MilestoneCard = ({
index,
milestone,
milestoneNameChanged,
showAddStrategyDialog,
Expand All @@ -88,7 +86,7 @@ export const MilestoneCard = ({
milestoneId: string,
strategy: IReleasePlanMilestoneStrategy,
) => {
showAddStrategyDialog(index, strategy);
showAddStrategyDialog(milestone.id, strategy);
};

return (
Expand All @@ -101,7 +99,10 @@ export const MilestoneCard = ({
label=''
value={milestone.name}
onChange={(e) =>
milestoneNameChanged(index, e.target.value)
milestoneNameChanged(
milestone.id,
e.target.value,
)
}
error={Boolean(errors?.name)}
errorText={errors?.name}
Expand Down Expand Up @@ -151,7 +152,7 @@ export const MilestoneCard = ({
}}
>
<MilestoneStrategyMenuCards
milestoneId={milestone.id ?? index.toString()}
milestoneId={milestone.id}
openAddStrategy={onSelectStrategy}
/>
</Popover>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import { MilestoneCard } from './MilestoneCard';
import { styled } from '@mui/material';
import { Button } from '@mui/material';
import Add from '@mui/icons-material/Add';
import { v4 as uuidv4 } from 'uuid';

interface IMilestoneListProps {
milestones: IReleasePlanMilestonePayload[];
setMilestones: React.Dispatch<
React.SetStateAction<IReleasePlanMilestonePayload[]>
>;
openAddStrategyForm: (
index: number,
milestoneId: string,
strategy: IReleasePlanMilestoneStrategy,
) => void;
errors: { [key: string]: string };
Expand All @@ -32,20 +33,21 @@ export const MilestoneList = ({
errors,
clearErrors,
}: IMilestoneListProps) => {
const milestoneNameChanged = (index: number, name: string) => {
const milestoneNameChanged = (milestoneId: string, name: string) => {
setMilestones((prev) =>
prev.map((milestone, i) =>
i === index ? { ...milestone, name } : milestone,
prev.map((milestone) =>
milestone.id === milestoneId
? { ...milestone, name }
: milestone,
),
);
};

return (
<>
{milestones.map((milestone, index) => (
{milestones.map((milestone) => (
<MilestoneCard
key={`milestone_${index.toString()}`}
index={index}
key={milestone.id}
milestone={milestone}
milestoneNameChanged={milestoneNameChanged}
showAddStrategyDialog={openAddStrategyForm}
Expand All @@ -61,6 +63,7 @@ export const MilestoneList = ({
setMilestones((prev) => [
...prev,
{
id: uuidv4(),
name: `Milestone ${prev.length + 1}`,
sortOrder: prev.length,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export const TemplateForm: React.FC<ITemplateFormProps> = ({
children,
}) => {
const [addStrategyOpen, setAddStrategyOpen] = useState(false);
const [activeMilestoneIndex, setActiveMilestoneIndex] = useState<
number | undefined
const [activeMilestoneId, setActiveMilestoneId] = useState<
string | undefined
>();
const [strategy, setStrategy] = useState<IReleasePlanMilestoneStrategy>({
name: 'flexibleRollout',
Expand All @@ -70,14 +70,33 @@ export const TemplateForm: React.FC<ITemplateFormProps> = ({
id: '',
});
const openAddStrategyForm = (
index: number,
milestoneId: string,
strategy: IReleasePlanMilestoneStrategy,
) => {
setActiveMilestoneIndex(index);
setActiveMilestoneId(milestoneId);
setStrategy(strategy);
setAddStrategyOpen(true);
};

const addStrategy = (
milestoneId: string,
strategy: IReleasePlanMilestoneStrategy,
) => {
setMilestones((prev) =>
prev.map((milestone, i) =>
milestone.id === milestoneId
? {
...milestone,
strategies: [
...(milestone.strategies || []),
strategy,
],
}
: milestone,
),
);
};

return (
<FormTemplate
title={formTitle}
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/component/releases/hooks/useTemplateForm.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { IReleasePlanMilestonePayload } from 'interfaces/releasePlans';
import { useEffect, useState } from 'react';
import { v4 as uuidv4 } from 'uuid';

export const useTemplateForm = (
initialName = '',
initialDescription = '',
initialMilestones: IReleasePlanMilestonePayload[] = [
{ name: 'Milestone 1', sortOrder: 0 },
{ id: uuidv4(), name: 'Milestone 1', sortOrder: 0 },
],
) => {
const [name, setName] = useState(initialName);
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/interfaces/releasePlans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface IReleasePlanTemplatePayload {
}

export interface IReleasePlanMilestonePayload {
id?: string;
id: string;
name: string;
sortOrder: number;
strategies?: IReleasePlanStrategyPayload[];
Expand Down
Loading