Skip to content

Commit

Permalink
chore: simplify calling displayDefaultNotificationFlow (#809)
Browse files Browse the repository at this point in the history
  • Loading branch information
rangoo94 authored Aug 3, 2023
1 parent 3eb4b36 commit ab67371
Show file tree
Hide file tree
Showing 27 changed files with 57 additions and 80 deletions.
8 changes: 3 additions & 5 deletions src/components/molecules/Definition/Definition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,9 @@ const Definition: React.FC<PropsWithChildren<DefinitionProps>> = props => {
return update({name, value})
.then(displayDefaultNotificationFlow)
.then(res => {
if (res && 'data' in res) {
notificationCall('passed', `${capitalize(label)} was successfully updated.`);
onUpdate?.(res.data);
refetch();
}
notificationCall('passed', `${capitalize(label)} was successfully updated.`);
onUpdate?.(res.data);
refetch();
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const DeleteEntityModal: React.FC<{

const onDelete = () => {
deleteEntity(idToDelete || name)
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(() => {
notificationCall('passed', `${capitalize(entityLabel)} was successfully deleted.`);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const EntityDetailsContent: React.FC = () => {
},
},
})
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(() => {
if (entity === 'tests') {
telemetry.event('runTest', {type});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const FailureHandling: React.FC = () => {
},
},
})
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(() => notificationCall('passed', `Test was successfully updated.`));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const Labels: React.FC = () => {
labels: decomposeLabels(localLabels),
},
})
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(() => {
notificationCall('passed', `${capitalize(namingMap[entity])} was successfully updated.`);
setWasTouched(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const NameNDescription: React.FC = () => {
},
},
})
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(() => notificationCall('passed', `${capitalize(namingMap[entity])} was successfully updated.`));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const Timeout: React.FC = () => {
},
},
})
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(() => notificationCall('passed', 'Test Timeout was successfully updated.'));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const Schedule: React.FC = () => {
schedule: cronString,
},
})
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(() => {
setWasTouched(false);
notificationCall('passed', `${capitalize(namingMap[entity])} schedule was successfully updated.`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const SettingsTest: React.FC = () => {
...data,
},
})
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(() => {
notificationCall('passed', `Test settings was successfully updated.`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const Arguments: React.FC = () => {
id: details.name,
data: successRecord,
})
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(() => {
notificationCall('passed', 'Variables were successfully updated.');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const Variables: React.FC = () => {
id: details.name,
data: successRecord,
})
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(() => {
notificationCall('passed', `Variables were successfully updated.`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const Arguments: React.FC = () => {
args: values.arguments,
},
})
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(() => {
notificationCall('passed', 'Arguments were successfully updated.');
dispatch(updateCurrentExecutorData({args: values.arguments}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const Command: React.FC = () => {
command: values.command.split(' '),
},
})
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(() => {
notificationCall('passed', 'Command was successfully updated.');
dispatch(updateCurrentExecutorData({command: values.command!.split(' ')}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const ContainerImagePanel: React.FC = () => {
...values,
},
})
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(() => {
notificationCall('passed', 'Container image was successfully updated.');
dispatch(updateCurrentExecutorData({image: values.image}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const PrivateRegistry: React.FC = () => {
imagePullSecrets: newImagePullSecrets,
},
})
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(() => {
dispatch(updateCurrentExecutorData({imagePullSecrets: newImagePullSecrets}));
notificationCall('passed', 'Private registry was successfully updated.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,8 @@ const AddExecutorsModal: React.FC = () => {
delete body.type;

createExecutor(body)
.then(res => displayDefaultNotificationFlow(res))
.then(res => {
if (res && 'data' in res) {
navigate(`/executors/${res.data.metadata.name}`);
}
})
.then(displayDefaultNotificationFlow)
.then(res => navigate(`/executors/${res.data.metadata.name}`))
.catch(err => {
setError(err);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ const Authentication: React.FC = () => {
};

return updateSource(body)
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(res => {
if (res && 'data' in res) {
notificationCall('passed', 'Source was successfully updated.');
dispatch(setCurrentSource({...body, ...res.data.spec}));
}
notificationCall('passed', 'Source was successfully updated.');
dispatch(setCurrentSource({...body, ...res.data.spec}));
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const NameNUrl: React.FC = () => {
};

return updateSource(body)
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(() => {
notificationCall('passed', 'Source was successfully updated.');
dispatch(setCurrentSource(body));
Expand Down
8 changes: 2 additions & 6 deletions src/components/pages/Sources/SourcesList/AddSourceModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,8 @@ const AddSourceModal: React.FC = () => {
};

createSource(body)
.then(res => displayDefaultNotificationFlow(res))
.then(res => {
if (res && 'data' in res) {
navigate(`/sources/${res.data.metadata.name}`);
}
})
.then(displayDefaultNotificationFlow)
.then(res => navigate(`/sources/${res.data.metadata.name}`))
.catch(err => {
setError(err);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,14 @@ const TestSuiteCreationModalContent: React.FC = () => {
...values,
labels: decomposeLabels(localLabels),
})
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(res => {
if (res && 'data' in res) {
telemetry.event('createTestSuite');
telemetry.event('createTestSuite');

dispatch(setSettingsTabConfig({entity: 'test-suites', tab: 'Tests'}));
dispatch(setSettingsTabConfig({entity: 'test-suites', tab: 'Tests'}));

navigate(`/test-suites/executions/${res.data.metadata.name}`);
closeModal();
}
navigate(`/test-suites/executions/${res.data.metadata.name}`);
closeModal();
})
.catch(err => {
setError(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ const TestCreationModalContent: React.FC = () => {
}, [testType]);

const onSuccess = (res: RTKResponse<MetadataResponse<Test>>) => {
return displayDefaultNotificationFlow(res).then(() => {
if ('data' in res) {
telemetry.event('createTest', {type: res.data.spec?.type});
return Promise.resolve(res)
.then(displayDefaultNotificationFlow)
.then(({data}) => {
telemetry.event('createTest', {type: data.spec?.type});

navigate(`/tests/executions/${res.data.metadata.name}`);
navigate(`/tests/executions/${data.metadata.name}`);
closeModal();
}
});
});
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const Name: React.FC = () => {
};

return updateTrigger(body)
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(() => {
notificationCall('passed', 'Trigger was successfully updated.');
setCurrentTrigger(body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,11 @@ const Condition: React.FC = () => {
};

return updateTrigger(body)
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(res => {
if (res && 'data' in res) {
notificationCall('passed', 'Trigger was successfully updated.');
setCurrentTrigger(res.data);
form.setFieldsValue(getActionFormValues(res.data));
}
notificationCall('passed', 'Trigger was successfully updated.');
setCurrentTrigger(res.data);
form.setFieldsValue(getActionFormValues(res.data));
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ const Condition: React.FC = () => {
};

return updateTrigger(body)
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(res => {
if (res && 'data' in res) {
notificationCall('passed', 'Trigger was successfully updated.');
setCurrentTrigger(res.data);
form.setFieldsValue(getConditionFormValues(res.data));
}
notificationCall('passed', 'Trigger was successfully updated.');
setCurrentTrigger(res.data);
form.setFieldsValue(getConditionFormValues(res.data));
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,14 @@ const ResourceCondition: React.FC = () => {
};

return updateTrigger(body)
.then(res => displayDefaultNotificationFlow(res))
.then(displayDefaultNotificationFlow)
.then(res => {
if (res && 'data' in res) {
notificationCall('passed', 'Trigger was successfully updated.');
setCurrentTrigger(res.data);
form.setFieldsValue({
timeout: res.data.conditionSpec?.timeout,
conditions: res.data.conditionSpec?.conditions || [],
});
}
notificationCall('passed', 'Trigger was successfully updated.');
setCurrentTrigger(res.data);
form.setFieldsValue({
timeout: res.data.conditionSpec?.timeout,
conditions: res.data.conditionSpec?.conditions || [],
});
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,7 @@ const AddTriggerModal: React.FC = () => {
};
createTrigger(body)
.then(displayDefaultNotificationFlow)
.then(res => {
if (res && 'data' in res) {
navigate(`/triggers/${res.data.name}`);
}
})
.then(res => navigate(`/triggers/${res.data.name}`))
.catch(err => {
setError(err);

Expand Down
7 changes: 4 additions & 3 deletions src/utils/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ export const getErrorFromResponse = (res?: RTKResponse<unknown>): unknown => {
}
};

export async function displayDefaultNotificationFlow<T = unknown>(res?: RTKResponse<T>) {
export function displayDefaultNotificationFlow<T extends RTKResponse<any>>(
res?: T
): T extends {error: any} ? never : T {
const error = getErrorFromResponse(res);

if (error) {
throw error;
}
return res;
return res as any;
}

export function displayDefaultErrorNotification(err: DefaultRequestError) {
Expand Down

0 comments on commit ab67371

Please sign in to comment.