From 17d9d09509f9cc870ea11776fbb302fa853f999d Mon Sep 17 00:00:00 2001 From: Xavier Rutayisire Date: Wed, 24 Jan 2024 17:09:52 +0100 Subject: [PATCH] refactor(changes): Remove pushChanges saga and store (#1270) --- packages/manager/src/index.ts | 5 +- .../PrismicRepositoryManager.ts | 20 +- .../src/managers/prismicRepository/types.ts | 12 +- .../manager/src/managers/telemetry/types.ts | 4 +- .../CustomTypeTable/changesPage.tsx | 1 + .../HardDeleteDocumentsDrawer.tsx | 34 +-- .../ReferencesErrorDrawer.tsx | 92 ------ .../SoftDeleteDocumentsDrawer.tsx | 34 +-- .../DeleteDocumentsDrawer/index.tsx | 1 - .../components/SliceMachineDrawer/index.tsx | 10 +- .../lib/models/common/TransactionalPush.ts | 10 - packages/slice-machine/pages/changes.tsx | 95 +++++-- .../features/changes/actions/pushChanges.ts | 66 +++++ .../actions}/trackPushChangesSuccess.ts | 28 +- .../src/modules/loading/types.ts | 1 - .../slice-machine/src/modules/modal/types.ts | 3 - .../src/modules/pushChangesSaga/index.ts | 191 ------------- .../src/modules/useSliceMachineActions.ts | 21 +- .../src/modules/userContext/index.ts | 9 +- packages/slice-machine/src/redux/reducer.ts | 2 - packages/slice-machine/src/redux/saga.ts | 2 - packages/slice-machine/src/redux/type.ts | 2 - .../test/src/modules/pushChangesSaga.test.ts | 261 ------------------ playwright/tests/changes/changes.spec.ts | 10 +- 24 files changed, 214 insertions(+), 700 deletions(-) delete mode 100644 packages/slice-machine/components/DeleteDocumentsDrawer/ReferencesErrorDrawer.tsx delete mode 100644 packages/slice-machine/lib/models/common/TransactionalPush.ts create mode 100644 packages/slice-machine/src/features/changes/actions/pushChanges.ts rename packages/slice-machine/src/{modules/pushChangesSaga => features/changes/actions}/trackPushChangesSuccess.ts (80%) delete mode 100644 packages/slice-machine/src/modules/pushChangesSaga/index.ts delete mode 100644 packages/slice-machine/test/src/modules/pushChangesSaga.test.ts diff --git a/packages/manager/src/index.ts b/packages/manager/src/index.ts index 4851d0e0ab..dcdba4d486 100644 --- a/packages/manager/src/index.ts +++ b/packages/manager/src/index.ts @@ -13,7 +13,10 @@ export { createSliceMachineManager } from "./managers/createSliceMachineManager" export { createSliceMachineManagerMiddleware } from "./managers/createSliceMachineManagerMiddleware"; export type { CreateSliceMachineManagerMiddlewareArgs } from "./managers/createSliceMachineManagerMiddleware"; -export type { Environment } from "./managers/prismicRepository/types"; +export type { + Environment, + PushChangesLimit, +} from "./managers/prismicRepository/types"; export type { PrismicAuthManager, diff --git a/packages/manager/src/managers/prismicRepository/PrismicRepositoryManager.ts b/packages/manager/src/managers/prismicRepository/PrismicRepositoryManager.ts index 3815160517..badf117b1a 100644 --- a/packages/manager/src/managers/prismicRepository/PrismicRepositoryManager.ts +++ b/packages/manager/src/managers/prismicRepository/PrismicRepositoryManager.ts @@ -23,13 +23,13 @@ import { BulkBody, ChangeTypes, ClientError, - Limit, - LimitType, + PushChangesLimit, + PushChangesLimitType, PrismicRepository, PrismicRepositoryRole, PrismicRepositoryUserAgent, PrismicRepositoryUserAgents, - RawLimit, + PushChangesRawLimit, TransactionalMergeArgs, TransactionalMergeReturnType, FrameworkWroomTelemetryID, @@ -421,7 +421,7 @@ export class PrismicRepositoryManager extends BaseManager { return this._decodeLimitOrThrow( await response.json(), response.status, - LimitType.SOFT, + PushChangesLimitType.SOFT, ); case 204: return null; @@ -431,7 +431,7 @@ export class PrismicRepositoryManager extends BaseManager { return this._decodeLimitOrThrow( await response.json(), response.status, - LimitType.HARD, + PushChangesLimitType.HARD, ); case 400: const text = await response.text(); @@ -509,9 +509,9 @@ export class PrismicRepositoryManager extends BaseManager { private _decodeLimitOrThrow( potentialLimit: unknown, statusCode: number, - limitType: LimitType, - ): Limit | null { - return fold( + limitType: PushChangesLimitType, + ): PushChangesLimit | null { + return fold( () => { const error: ClientError = { status: statusCode, @@ -521,12 +521,12 @@ export class PrismicRepositoryManager extends BaseManager { }; throw error; }, - (rawLimit: RawLimit) => { + (rawLimit: PushChangesRawLimit) => { const limit = { ...rawLimit, type: limitType }; return limit; }, - )(RawLimit.decode(potentialLimit)); + )(PushChangesRawLimit.decode(potentialLimit)); } private async _fetch(args: { diff --git a/packages/manager/src/managers/prismicRepository/types.ts b/packages/manager/src/managers/prismicRepository/types.ts index c0c59fc752..5ffca8c79c 100644 --- a/packages/manager/src/managers/prismicRepository/types.ts +++ b/packages/manager/src/managers/prismicRepository/types.ts @@ -85,7 +85,7 @@ export interface BulkBody extends Record { changes: AllChangeTypes[]; } -export const RawLimit = t.type({ +export const PushChangesRawLimit = t.type({ details: t.type({ customTypes: t.array( t.type({ @@ -96,13 +96,13 @@ export const RawLimit = t.type({ ), }), }); -export type RawLimit = t.TypeOf; -export enum LimitType { +export type PushChangesRawLimit = t.TypeOf; +export enum PushChangesLimitType { SOFT = "SOFT", HARD = "HARD", } -export type Limit = RawLimit & { - type: LimitType; +export type PushChangesLimit = PushChangesRawLimit & { + type: PushChangesLimitType; }; export interface ClientError { @@ -130,7 +130,7 @@ export type TransactionalMergeArgs = { changes: (CustomTypeChange | SliceChange)[]; }; -export type TransactionalMergeReturnType = Limit | null; +export type TransactionalMergeReturnType = PushChangesLimit | null; /** * Framework id sent to Segment from wroom. Property used for the "framework" diff --git a/packages/manager/src/managers/telemetry/types.ts b/packages/manager/src/managers/telemetry/types.ts index 144f95ec77..1d3c4a31af 100644 --- a/packages/manager/src/managers/telemetry/types.ts +++ b/packages/manager/src/managers/telemetry/types.ts @@ -1,5 +1,5 @@ import { CustomTypeFormat } from "../customTypes/types"; -import type { LimitType } from "../prismicRepository/types"; +import type { PushChangesLimitType } from "../prismicRepository/types"; export const SegmentEventType = { command_init_start: "command:init:start", @@ -248,7 +248,7 @@ type ChangesPushedSegmentEvent = SegmentEvent< type ChangesLimitReachSegmentEvent = SegmentEvent< typeof SegmentEventType.changes_limitReach, - { limitType: LimitType } + { limitType: PushChangesLimitType } >; type EditorWidgetUsedSegmentEvent = SegmentEvent< diff --git a/packages/slice-machine/components/CustomTypeTable/changesPage.tsx b/packages/slice-machine/components/CustomTypeTable/changesPage.tsx index c0d17609a1..6af2fc36d7 100644 --- a/packages/slice-machine/components/CustomTypeTable/changesPage.tsx +++ b/packages/slice-machine/components/CustomTypeTable/changesPage.tsx @@ -90,6 +90,7 @@ export const CustomTypeTable: React.FC = ({ tabIndex={0} className="disabled" data-cy={`custom-type-${customType.remote.id}`} + key={customType.remote.id} > void; -}> = ({ pushChanges }) => { - const { isDeleteDocumentsDrawerOpen, remoteOnlyCustomTypes, modalData } = - useSelector((store: SliceMachineStoreType) => ({ - isDeleteDocumentsDrawerOpen: isModalOpen( - store, - ModalKeysEnum.HARD_DELETE_DOCUMENTS_DRAWER, - ), + modalData?: PushChangesLimit; + onClose: () => void; +}> = ({ pushChanges, modalData, onClose }) => { + const { remoteOnlyCustomTypes } = useSelector( + (store: SliceMachineStoreType) => ({ remoteOnlyCustomTypes: selectAllCustomTypes(store).filter(isRemoteOnly), - modalData: store.pushChanges, - })); - - const { closeModals, openToaster } = useSliceMachineActions(); - - if (!isDeleteDocumentsDrawerOpen) return null; + }), + ); - if (modalData?.type !== "HARD") { - openToaster("No change data", ToasterType.ERROR); - return null; - } + if (modalData?.type !== "HARD") return null; const associatedDocumentsCards = modalData.details.customTypes.map( (customTypeDetail) => { @@ -57,14 +45,14 @@ export const HardDeleteDocumentsDrawer: React.FunctionComponent<{ return ( { - closeModals(); pushChanges(false); }} sx={{ diff --git a/packages/slice-machine/components/DeleteDocumentsDrawer/ReferencesErrorDrawer.tsx b/packages/slice-machine/components/DeleteDocumentsDrawer/ReferencesErrorDrawer.tsx deleted file mode 100644 index b44924bc4d..0000000000 --- a/packages/slice-machine/components/DeleteDocumentsDrawer/ReferencesErrorDrawer.tsx +++ /dev/null @@ -1,92 +0,0 @@ -import React from "react"; -import { Text } from "theme-ui"; -import useSliceMachineActions from "@src/modules/useSliceMachineActions"; -import { Button } from "@components/Button"; -import { useSelector } from "react-redux"; -import { SliceMachineStoreType } from "@src/redux/type"; -import { ModalKeysEnum } from "@src/modules/modal/types"; -import { isModalOpen } from "@src/modules/modal"; -import { getModelId, hasLocal } from "@lib/models/common/ModelData"; -import { CustomTypesReferencesCard } from "./AssociatedDocumentsCard"; -import { ToasterType } from "@src/modules/toaster"; -import { selectAllCustomTypes } from "@src/modules/availableCustomTypes"; -import { SliceMachineDrawerUI } from "@components/SliceMachineDrawer"; - -export const ReferencesErrorDrawer: React.FunctionComponent<{ - pushChanges: (confirmDeleteDocuments: boolean) => void; -}> = ({ pushChanges }) => { - const { isOpen, modalData, localCustomTypes } = useSelector( - (store: SliceMachineStoreType) => ({ - isOpen: isModalOpen(store, ModalKeysEnum.REFERENCES_MISSING_DRAWER), - localCustomTypes: selectAllCustomTypes(store).filter(hasLocal), - modalData: store.pushChanges, - }), - ); - const { closeModals, openToaster } = useSliceMachineActions(); - - if (!isOpen) return null; - - if (modalData?.details === undefined) { - openToaster("No change data", ToasterType.ERROR); - return null; - } - - const hasMoreThanOne = modalData?.details.customTypes.length > 1; - - const associatedDocumentsCards = modalData.details.customTypes.map( - (customTypeDetail) => { - const customType = localCustomTypes.find( - (customType) => getModelId(customType) === customTypeDetail.id, - ); - if (customType === undefined) return null; - - return ( - - ); - }, - ); - - return ( - -