Merge pull request #288 from getmaxun/labreset-fix

fix: preserve previous labels steps before adding list step
This commit is contained in:
Karishma
2024-12-23 18:57:52 +05:30
committed by GitHub

View File

@@ -62,26 +62,35 @@ export const BrowserStepsProvider: React.FC<{ children: React.ReactNode }> = ({
const addListStep = (listSelector: string, newFields: { [key: string]: TextStep }, listId: number, pagination?: { type: string; selector: string }, limit?: number) => { const addListStep = (listSelector: string, newFields: { [key: string]: TextStep }, listId: number, pagination?: { type: string; selector: string }, limit?: number) => {
setBrowserSteps(prevSteps => { setBrowserSteps(prevSteps => {
const existingListStepIndex = prevSteps.findIndex(step => step.type === 'list' && step.id === listId); const existingListStepIndex = prevSteps.findIndex(step => step.type === 'list' && step.id === listId);
if (existingListStepIndex !== -1) { if (existingListStepIndex !== -1) {
const updatedSteps = [...prevSteps]; const updatedSteps = [...prevSteps];
const existingListStep = updatedSteps[existingListStepIndex] as ListStep; const existingListStep = updatedSteps[existingListStepIndex] as ListStep;
const filteredNewFields = Object.entries(newFields).reduce((acc, [key, value]) => { // Preserve existing labels for fields
const mergedFields = Object.entries(newFields).reduce((acc, [key, field]) => {
if (!discardedFields.has(`${listId}-${key}`)) { if (!discardedFields.has(`${listId}-${key}`)) {
acc[key] = value; // If field exists, preserve its label
if (existingListStep.fields[key]) {
acc[key] = {
...field,
label: existingListStep.fields[key].label
};
} else {
acc[key] = field;
}
} }
return acc; return acc;
}, {} as { [key: string]: TextStep }); }, {} as { [key: string]: TextStep });
updatedSteps[existingListStepIndex] = { updatedSteps[existingListStepIndex] = {
...existingListStep, ...existingListStep,
fields: { ...existingListStep.fields, ...filteredNewFields }, fields: mergedFields,
pagination: pagination, pagination: pagination || existingListStep.pagination,
limit: limit, limit: limit
}; };
return updatedSteps; return updatedSteps;
} else { } else {
// Create a new ListStep
return [ return [
...prevSteps, ...prevSteps,
{ id: listId, type: 'list', listSelector, fields: newFields, pagination, limit } { id: listId, type: 'list', listSelector, fields: newFields, pagination, limit }