From 932664a61fc1c9f09bc4b3ad7e5d930b430a84c2 Mon Sep 17 00:00:00 2001 From: Gustav Hansen Date: Fri, 3 Feb 2023 14:21:32 +0100 Subject: [PATCH] Settings: Allow stages to be added even if the workflow doesn't have any yet --- .../pages/ReviewWorkflows/ReviewWorkflows.js | 2 +- .../components/Stages/Stages.js | 1 + .../pages/ReviewWorkflows/reducer/index.js | 17 ++++++---- .../reducer/tests/index.test.js | 32 +++++++++++++++++++ 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/ReviewWorkflows.js b/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/ReviewWorkflows.js index d77b30be29..b28b0bbc0c 100644 --- a/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/ReviewWorkflows.js +++ b/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/ReviewWorkflows.js @@ -90,7 +90,7 @@ export function ReviewWorkflowsPage() { )} - {formik.values?.stages && } + diff --git a/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/components/Stages/Stages.js b/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/components/Stages/Stages.js index e3c51493b0..4b7f12ff75 100644 --- a/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/components/Stages/Stages.js +++ b/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/components/Stages/Stages.js @@ -70,6 +70,7 @@ Stages.propTypes = { stages: PropTypes.arrayOf( PropTypes.shape({ id: PropTypes.number, + __temp_key__: PropTypes.number, name: PropTypes.string.isRequired, }) ), diff --git a/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/reducer/index.js b/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/reducer/index.js index 5319a1e627..db6954adb2 100644 --- a/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/reducer/index.js +++ b/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/reducer/index.js @@ -56,13 +56,16 @@ export function reducer(state = initialState, action) { case ACTION_ADD_STAGE: { const { currentWorkflow } = state.clientState; - draft.clientState.currentWorkflow.data.stages = [ - ...currentWorkflow.data.stages, - { - ...payload, - __temp_key__: currentWorkflow.data.stages.length + 1, - }, - ]; + if (!currentWorkflow.data) { + draft.clientState.currentWorkflow.data = { + stages: [], + }; + } + + draft.clientState.currentWorkflow.data.stages.push({ + ...payload, + __temp_key__: (currentWorkflow.data?.stages?.length ?? 0) + 1, + }); break; } diff --git a/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/reducer/tests/index.test.js b/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/reducer/tests/index.test.js index e68815fb2b..1e61ce39f9 100644 --- a/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/reducer/tests/index.test.js +++ b/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/reducer/tests/index.test.js @@ -133,6 +133,38 @@ describe('Admin | Settings | Review Workflows | reducer', () => { ); }); + test('ACTION_ADD_STAGE when there are not stages yet', () => { + const action = { + type: ACTION_ADD_STAGE, + payload: { name: 'something' }, + }; + + state = { + status: expect.any(String), + serverState: expect.any(Object), + clientState: { + currentWorkflow: { data: null, isDirty: false }, + }, + }; + + expect(reducer(state, action)).toStrictEqual( + expect.objectContaining({ + clientState: expect.objectContaining({ + currentWorkflow: expect.objectContaining({ + data: expect.objectContaining({ + stages: expect.arrayContaining([ + { + __temp_key__: 1, + name: 'something', + }, + ]), + }), + }), + }), + }) + ); + }); + test('ACTION_UPDATE_STAGE', () => { const action = { type: ACTION_UPDATE_STAGE,