1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-11-10 01:19:53 +01:00

refactor: simplify

This commit is contained in:
FredrikOseberg 2025-10-21 14:30:04 +02:00
parent bc13e919a8
commit 0bdf2d3d1c
No known key found for this signature in database
GPG Key ID: 282FD8A6D8F9BCF0
4 changed files with 64 additions and 92 deletions

View File

@ -250,11 +250,7 @@ const CreateMilestoneProgression: FC<{
change: IChangeRequestCreateMilestoneProgression;
currentReleasePlan?: IReleasePlan;
actions?: ReactNode;
projectId: string;
environmentName: string;
featureName: string;
changeRequestState: ChangeRequestState;
onUpdate?: () => void;
onUpdateChangeRequestSubmit?: (
sourceMilestoneId: string,
payload: UpdateMilestoneProgressionSchema,
@ -264,11 +260,7 @@ const CreateMilestoneProgression: FC<{
change,
currentReleasePlan,
actions,
projectId,
environmentName,
featureName,
changeRequestState,
onUpdate,
onUpdateChangeRequestSubmit,
onDeleteChangeRequestSubmit,
}) => {
@ -358,6 +350,7 @@ const CreateMilestoneProgression: FC<{
milestone.id,
payload,
);
return { shouldReset: true };
}}
onDelete={() =>
onDeleteChangeRequestSubmit?.(
@ -404,11 +397,7 @@ const UpdateMilestoneProgression: FC<{
change: IChangeRequestUpdateMilestoneProgression;
currentReleasePlan?: IReleasePlan;
actions?: ReactNode;
projectId: string;
environmentName: string;
featureName: string;
changeRequestState: ChangeRequestState;
onUpdate?: () => void;
onUpdateChangeRequestSubmit?: (
sourceMilestoneId: string,
payload: UpdateMilestoneProgressionSchema,
@ -418,11 +407,7 @@ const UpdateMilestoneProgression: FC<{
change,
currentReleasePlan,
actions,
projectId,
environmentName,
featureName,
changeRequestState,
onUpdate,
onUpdateChangeRequestSubmit,
onDeleteChangeRequestSubmit,
}) => {
@ -502,6 +487,7 @@ const UpdateMilestoneProgression: FC<{
milestone.id,
payload,
);
return { shouldReset: true };
}}
onDelete={() =>
onDeleteChangeRequestSubmit?.(
@ -547,11 +533,7 @@ const UpdateMilestoneProgression: FC<{
const ConsolidatedProgressionChanges: FC<{
feature: IChangeRequestFeature;
currentReleasePlan?: IReleasePlan;
projectId: string;
environmentName: string;
featureName: string;
changeRequestState: ChangeRequestState;
onUpdate?: () => void;
onUpdateChangeRequestSubmit?: (
sourceMilestoneId: string,
payload: UpdateMilestoneProgressionSchema,
@ -560,11 +542,7 @@ const ConsolidatedProgressionChanges: FC<{
}> = ({
feature,
currentReleasePlan,
projectId,
environmentName,
featureName,
changeRequestState,
onUpdate,
onUpdateChangeRequestSubmit,
onDeleteChangeRequestSubmit,
}) => {
@ -766,6 +744,7 @@ const ConsolidatedProgressionChanges: FC<{
displayMilestone.id,
payload,
);
return { shouldReset: true };
}}
onDelete={() =>
onDeleteChangeRequestSubmit?.(
@ -844,13 +823,6 @@ export const ReleasePlanChange: FC<{
usePendingChangeRequests(projectId);
const { setToastData } = useToast();
const handleUpdate = async () => {
await refetch();
if (onRefetch) {
await onRefetch();
}
};
const handleUpdateChangeRequestSubmit = async (
sourceMilestoneId: string,
payload: UpdateMilestoneProgressionSchema,
@ -924,11 +896,7 @@ export const ReleasePlanChange: FC<{
<ConsolidatedProgressionChanges
feature={feature}
currentReleasePlan={currentReleasePlan}
projectId={projectId}
environmentName={environmentName}
featureName={featureName}
changeRequestState={changeRequestState}
onUpdate={handleUpdate}
onUpdateChangeRequestSubmit={handleUpdateChangeRequestSubmit}
onDeleteChangeRequestSubmit={handleDeleteChangeRequestSubmit}
/>
@ -967,11 +935,7 @@ export const ReleasePlanChange: FC<{
change={change}
currentReleasePlan={currentReleasePlan}
actions={actions}
projectId={projectId}
environmentName={environmentName}
featureName={featureName}
changeRequestState={changeRequestState}
onUpdate={handleUpdate}
onUpdateChangeRequestSubmit={
handleUpdateChangeRequestSubmit
}
@ -985,11 +949,7 @@ export const ReleasePlanChange: FC<{
change={change}
currentReleasePlan={currentReleasePlan}
actions={actions}
projectId={projectId}
environmentName={environmentName}
featureName={featureName}
changeRequestState={changeRequestState}
onUpdate={handleUpdate}
onUpdateChangeRequestSubmit={
handleUpdateChangeRequestSubmit
}

View File

@ -314,29 +314,19 @@ export const ReleasePlan = ({
});
};
const handleProgressionSave = async () => {
setProgressionFormOpenIndex(null);
await refetch();
};
const handleProgressionChangeRequestSubmit = (
payload: CreateMilestoneProgressionSchema,
const handleAddToChangeRequest = (
action:
| {
type: 'createMilestoneProgression';
payload: CreateMilestoneProgressionSchema;
}
| {
type: 'updateMilestoneProgression';
sourceMilestoneId: string;
payload: UpdateMilestoneProgressionSchema;
},
) => {
setChangeRequestAction({
type: 'createMilestoneProgression',
payload,
});
};
const handleUpdateProgressionChangeRequestSubmit = (
sourceMilestoneId: string,
payload: UpdateMilestoneProgressionSchema,
) => {
setChangeRequestAction({
type: 'updateMilestoneProgression',
sourceMilestoneId,
payload,
});
setChangeRequestAction(action);
};
const handleDeleteProgression = (milestone: IReleasePlanMilestone) => {
@ -433,13 +423,7 @@ export const ReleasePlan = ({
}
onStartMilestone={onStartMilestone}
onDeleteProgression={handleDeleteProgression}
onProgressionSave={handleProgressionSave}
onProgressionChangeRequestSubmit={
handleProgressionChangeRequestSubmit
}
onUpdateProgressionChangeRequestSubmit={
handleUpdateProgressionChangeRequestSubmit
}
onAddToChangeRequest={handleAddToChangeRequest}
getPendingProgressionChange={
getPendingProgressionChange
}

View File

@ -9,6 +9,7 @@ import {
getTimeValueAndUnitFromMinutes,
} from '../hooks/useMilestoneProgressionForm.js';
import type { UpdateMilestoneProgressionSchema } from 'openapi';
import { useEffect } from 'react';
const StyledDisplayContainer = styled('div')(({ theme }) => ({
display: 'flex',
@ -57,7 +58,9 @@ const StyledButtonGroup = styled('div')(({ theme }) => ({
interface IMilestoneTransitionDisplayProps {
intervalMinutes: number;
onSave: (payload: UpdateMilestoneProgressionSchema) => Promise<void>;
onSave: (
payload: UpdateMilestoneProgressionSchema,
) => Promise<{ shouldReset?: boolean }>;
onDelete: () => void;
milestoneName: string;
status?: MilestoneStatus;
@ -89,6 +92,13 @@ export const MilestoneTransitionDisplay = ({
const showDraftBadge = hasPendingUpdate || hasPendingDelete;
// Sync form when intervalMinutes prop changes (e.g., after refetch)
useEffect(() => {
const newInitial = getTimeValueAndUnitFromMinutes(intervalMinutes);
form.setTimeValue(newInitial.value);
form.setTimeUnit(newInitial.unit);
}, [intervalMinutes]);
const handleSave = async () => {
if (!hasChanged) return;
@ -98,9 +108,13 @@ export const MilestoneTransitionDisplay = ({
},
};
await onSave(payload);
// Reset the form after save
const result = await onSave(payload);
// If change request, reset to current saved value
// Otherwise, form will automatically sync when intervalMinutes prop updates after refetch
if (result?.shouldReset) {
handleReset();
}
};
const handleReset = () => {

View File

@ -84,13 +84,17 @@ interface IReleasePlanMilestoneItemProps {
// API callbacks
onStartMilestone?: (milestone: IReleasePlanMilestone) => void;
onDeleteProgression: (milestone: IReleasePlanMilestone) => void;
onProgressionSave: () => Promise<void>;
onProgressionChangeRequestSubmit: (
payload: CreateMilestoneProgressionSchema,
) => void;
onUpdateProgressionChangeRequestSubmit: (
sourceMilestoneId: string,
payload: UpdateMilestoneProgressionSchema,
onAddToChangeRequest: (
action:
| {
type: 'createMilestoneProgression';
payload: CreateMilestoneProgressionSchema;
}
| {
type: 'updateMilestoneProgression';
sourceMilestoneId: string;
payload: UpdateMilestoneProgressionSchema;
},
) => void;
// Context
@ -103,7 +107,7 @@ interface IReleasePlanMilestoneItemProps {
environment: string;
featureName: string;
onUpdate: () => void;
onUpdate: () => void | Promise<void>;
}
export const ReleasePlanMilestoneItem = ({
@ -119,9 +123,7 @@ export const ReleasePlanMilestoneItem = ({
onSetProgressionFormOpenIndex,
onStartMilestone,
onDeleteProgression,
onProgressionSave,
onProgressionChangeRequestSubmit,
onUpdateProgressionChangeRequestSubmit,
onAddToChangeRequest,
getPendingProgressionChange,
projectId,
environment,
@ -146,7 +148,10 @@ export const ReleasePlanMilestoneItem = ({
payload: CreateMilestoneProgressionSchema,
) => {
if (isChangeRequestConfigured(environment)) {
onProgressionChangeRequestSubmit(payload);
onAddToChangeRequest({
type: 'createMilestoneProgression',
payload,
});
handleCloseProgressionForm();
return;
}
@ -162,7 +167,8 @@ export const ReleasePlanMilestoneItem = ({
type: 'success',
text: 'Automation configured successfully',
});
await onProgressionSave();
handleCloseProgressionForm();
await onUpdate();
} catch (error: unknown) {
setToastApiError(formatUnknownError(error));
}
@ -171,10 +177,15 @@ export const ReleasePlanMilestoneItem = ({
// Unified handler for updating progression
const handleUpdateProgression = async (
payload: UpdateMilestoneProgressionSchema,
) => {
): Promise<{ shouldReset?: boolean }> => {
if (isChangeRequestConfigured(environment)) {
onUpdateProgressionChangeRequestSubmit(milestone.id, payload);
return;
onAddToChangeRequest({
type: 'updateMilestoneProgression',
sourceMilestoneId: milestone.id,
payload,
});
// Return shouldReset=true for change requests so form resets to current value
return { shouldReset: true };
}
try {
@ -190,8 +201,11 @@ export const ReleasePlanMilestoneItem = ({
text: 'Automation updated successfully',
});
await onUpdate();
// Return empty object for direct updates - form will sync via useEffect
return {};
} catch (error: unknown) {
setToastApiError(formatUnknownError(error));
return {};
}
};