From 9aef0d0f089f0d400b51d5fb75d97e17a9653bc9 Mon Sep 17 00:00:00 2001 From: Gustav Hansen Date: Fri, 7 Jul 2023 11:20:11 +0200 Subject: [PATCH] Chore: Update license limits in create, edit and list-views --- .../useReviewWorkflowLicenseLimits.test.js | 8 +++++ .../hooks/useReviewWorkflows.js | 4 ++- .../pages/CreateView/CreateView.js | 21 ++++++++++--- .../pages/EditView/EditView.js | 31 ++++++++++++++----- .../pages/ListView/ListView.js | 25 +++++++++++++-- 5 files changed, 74 insertions(+), 15 deletions(-) diff --git a/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/hooks/tests/useReviewWorkflowLicenseLimits.test.js b/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/hooks/tests/useReviewWorkflowLicenseLimits.test.js index 6fec4f9881..a533f65a3e 100644 --- a/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/hooks/tests/useReviewWorkflowLicenseLimits.test.js +++ b/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/hooks/tests/useReviewWorkflowLicenseLimits.test.js @@ -24,6 +24,10 @@ jest.mock('../../../../../../hooks', () => ({ }, ], }, + + meta: { + workflowCount: 2, + }, }, })), })); @@ -40,5 +44,9 @@ describe('useReviewWorkflowLicenseLimits', () => { workflows: 10, stagesPerWorkflow: 10, }); + + expect(result.current.meta).toStrictEqual({ + workflowCount: 2, + }); }); }); diff --git a/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/hooks/useReviewWorkflows.js b/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/hooks/useReviewWorkflows.js index eb2b64a010..b2cc3416b5 100644 --- a/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/hooks/useReviewWorkflows.js +++ b/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/hooks/useReviewWorkflows.js @@ -36,7 +36,9 @@ export function useReviewWorkflows(params = {}) { } return { - pagination: data?.pagination ?? {}, + // meta contains e.g. the total of all workflows. we can not use + // the pagination object here, because the list is not paginated. + meta: data?.meta ?? {}, workflows, isLoading, status, diff --git a/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/pages/CreateView/CreateView.js b/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/pages/CreateView/CreateView.js index 0a41179ba5..24a6a1ec89 100644 --- a/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/pages/CreateView/CreateView.js +++ b/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/pages/CreateView/CreateView.js @@ -37,7 +37,7 @@ export function ReviewWorkflowsCreateView() { } = useSelector((state) => state?.[REDUX_NAMESPACE] ?? initialState); const [showLimitModal, setShowLimitModal] = React.useState(false); const { limits, isLoading: isLicenseLoading } = useReviewWorkflowLicenseLimits(); - const { pagination, isLoading: isWorkflowLoading } = useReviewWorkflows(); + const { meta, isLoading: isWorkflowLoading } = useReviewWorkflows(); const { mutateAsync, isLoading } = useMutation( async ({ workflow }) => { @@ -94,21 +94,34 @@ export function ReviewWorkflowsCreateView() { dispatch(resetWorkflow()); }, [dispatch]); + /** + * If the current license has a limit: + * check if the total count of workflows or stages exceeds that limit and display + * the limits modal on page load. It can be closed by the user, but the + * API will throw an error in case they try to create a new workflow or update the + * stages. + * + * If the current license does not have a limit (e.g. offline license): + * do nothing (for now). In case they are trying to create the 201st workflow/ stage + * the API will throw an error. + * + */ + React.useEffect(() => { if (!isWorkflowLoading && !isLicenseLoading) { - if (pagination?.total >= limits?.workflows) { + if (meta?.workflowsTotal >= limits?.workflows) { setShowLimitModal('workflow'); } else if (currentWorkflow.stages.length >= limits.stagesPerWorkflow) { setShowLimitModal('stage'); } } }, [ - currentWorkflow.stages.length, isLicenseLoading, isWorkflowLoading, limits.stagesPerWorkflow, limits?.workflows, - pagination?.total, + meta?.workflowsTotal, + currentWorkflow.stages.length, ]); return ( diff --git a/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/pages/EditView/EditView.js b/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/pages/EditView/EditView.js index 7e015b8ca2..66f5c7ddde 100644 --- a/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/pages/EditView/EditView.js +++ b/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/pages/EditView/EditView.js @@ -38,7 +38,7 @@ export function ReviewWorkflowsEditView() { const toggleNotification = useNotification(); const { isLoading: isWorkflowLoading, - pagination, + meta, workflows: [workflow], status: workflowStatus, refetch, @@ -132,11 +132,27 @@ export function ReviewWorkflowsEditView() { dispatch(setWorkflow({ status: workflowStatus, data: workflow })); }, [workflowStatus, workflow, dispatch]); + /** + * If the current license has a limit: + * check if the total count of workflows or stages exceeds that limit and display + * the limits modal on page load. It can be closed by the user, but the + * API will throw an error in case they try to create a new workflow or update the + * stages. + * + * If the current license does not have a limit (e.g. offline license): + * do nothing (for now). In case they are trying to create the 201st workflow/ stage + * the API will throw an error. + * + */ + React.useEffect(() => { if (!isWorkflowLoading && !isLicenseLoading) { - if (pagination?.total >= limits?.workflows) { + if (limits?.workflows && meta?.workflowCount >= limits.workflows) { setShowLimitModal('workflow'); - } else if (currentWorkflow.stages.length >= limits?.stagesPerWorkflow) { + } else if ( + limits?.stagesPerWorkflow && + currentWorkflow.stages.length >= limits.stagesPerWorkflow + ) { setShowLimitModal('stage'); } } @@ -144,12 +160,13 @@ export function ReviewWorkflowsEditView() { currentWorkflow.stages.length, isLicenseLoading, isWorkflowLoading, - limits?.stagesPerWorkflow, - limits?.workflows, - pagination?.total, + limits.stagesPerWorkflow, + limits.workflows, + meta?.workflowCount, + meta.workflowsTotal, ]); - // TODO redirect back to list-view if workflow is not found? + // TODO: redirect back to list-view if workflow is not found? return ( <> diff --git a/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/pages/ListView/ListView.js b/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/pages/ListView/ListView.js index 4291e54876..4c2009841a 100644 --- a/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/pages/ListView/ListView.js +++ b/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/pages/ListView/ListView.js @@ -67,7 +67,7 @@ export function ReviewWorkflowsListView() { const { formatMessage } = useIntl(); const { push } = useHistory(); const { collectionTypes, singleTypes, isLoading: isLoadingModels } = useContentTypes(); - const { pagination, workflows, isLoading, refetch } = useReviewWorkflows(); + const { meta, workflows, isLoading, refetch } = useReviewWorkflows(); const [workflowToDelete, setWorkflowToDelete] = React.useState(null); const [showLimitModal, setShowLimitModal] = React.useState(false); const { del } = useFetchClient(); @@ -138,7 +138,17 @@ export function ReviewWorkflowsListView() { size="S" to="/settings/review-workflows/create" onClick={(event) => { - if (pagination?.total >= limits.workflows) { + /** + * If the current license has a workflow limit: + * check if the total count of workflows exceeds that limit. If so, + * prevent the navigation and show the limits overlay. + * + * If the current license does not have a limit (e.g. offline license): + * allow the user to navigate to the create-view. In case they exceed the + * current hard-limit of 200 they will see an error thrown by the API. + */ + + if (limits?.workflows && meta?.workflowCount >= limits.workflows) { event.preventDefault(); setShowLimitModal(true); } @@ -177,7 +187,16 @@ export function ReviewWorkflowsListView() { } onClick={() => { - if (pagination?.total >= limits?.workflows) { + /** + * If the current license has a workflow limit: + * check if the total count of workflows exceeds that limit + * + * If the current license does not have a limit (e.g. offline license): + * allow the user to navigate to the create-view. In case they exceed the + * current hard-limit of 200 they will see an error thrown by the API. + */ + + if (limits?.workflows && meta?.workflowCount >= limits.workflows) { setShowLimitModal(true); } else { push('/settings/review-workflows/create');