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

View File

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

View File

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

View File

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