Skip to content

Commit

Permalink
refactor(custom types): Remove createCustomTypeSaga
Browse files Browse the repository at this point in the history
  • Loading branch information
xrutayisire committed Jan 29, 2024
1 parent c804ad1 commit d8c6687
Show file tree
Hide file tree
Showing 22 changed files with 618 additions and 854 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@ import { SetStateAction, useState } from "react";
import { Box } from "theme-ui";
import { FormikErrors } from "formik";
import { useSelector } from "react-redux";
import { useRouter } from "next/router";

import ModalFormCard from "@components/ModalFormCard";
import { InputBox } from "../components/InputBox";
import { SelectRepeatable } from "../components/SelectRepeatable";
import useSliceMachineActions from "@src/modules/useSliceMachineActions";
import { SliceMachineStoreType } from "@src/redux/type";
import {
selectAllCustomTypeIds,
selectAllCustomTypeLabels,
} from "@src/modules/availableCustomTypes";
import { isModalOpen } from "@src/modules/modal";
import { ModalKeysEnum } from "@src/modules/modal/types";
import { isLoading } from "@src/modules/loading";
import { LoadingKeysEnum } from "@src/modules/loading/types";
import { telemetry } from "@src/apiClient";
import { slugify } from "@lib/utils/str";
import { API_ID_REGEX } from "@lib/consts";
import type { CustomTypeFormat } from "@slicemachine/manager";
import { CUSTOM_TYPES_MESSAGES } from "@src/features/customTypes/customTypesMessages";
import {
CustomTypeOrigin,
createCustomType,
} from "@src/features/customTypes/actions/createCustomType";
import { CUSTOM_TYPES_CONFIG } from "@src/features/customTypes/customTypesConfig";
import { getFormat } from "@src/domain/customType";

import { InputBox } from "../components/InputBox";
import { SelectRepeatable } from "../components/SelectRepeatable";

interface FormValues {
id: string;
Expand All @@ -30,46 +33,64 @@ interface FormValues {

type CreateCustomTypeModalProps = {
format: CustomTypeFormat;
origin?: "onboarding" | "table";
isCreating: boolean;
isOpen: boolean;
origin?: CustomTypeOrigin;
onCreateChange: (isCreating: boolean) => void;
onOpenChange: (isOpen: boolean) => void;
};

export const CreateCustomTypeModal: React.FC<CreateCustomTypeModalProps> = ({
format,
isCreating,
isOpen,
origin = "table",
onCreateChange,
onOpenChange,
}) => {
const { createCustomType, closeModals } = useSliceMachineActions();

const {
customTypeIds,
isCreateCustomTypeModalOpen,
isCreatingCustomType,
customTypeLabels,
} = useSelector((store: SliceMachineStoreType) => ({
customTypeIds: selectAllCustomTypeIds(store),
customTypeLabels: selectAllCustomTypeLabels(store),
isCreateCustomTypeModalOpen: isModalOpen(
store,
ModalKeysEnum.CREATE_CUSTOM_TYPE,
),
isCreatingCustomType: isLoading(store, LoadingKeysEnum.CREATE_CUSTOM_TYPE),
}));
const { createCustomTypeSuccess } = useSliceMachineActions();

const { customTypeIds, customTypeLabels } = useSelector(
(store: SliceMachineStoreType) => ({
customTypeIds: selectAllCustomTypeIds(store),
customTypeLabels: selectAllCustomTypeLabels(store),
}),
);
const customTypesMessages = CUSTOM_TYPES_MESSAGES[format];
const [isIdFieldPristine, setIsIdFieldPristine] = useState(true);
const router = useRouter();

const createCustomTypeAndTrack = ({ id, label, repeatable }: FormValues) => {
const name = label || id;
const onSubmit = async ({ id, label, repeatable }: FormValues) => {
onCreateChange(true);

void telemetry.track({
event: "custom-type:created",
id,
name,
await createCustomType({
format,
type: repeatable ? "repeatable" : "single",
id,
label,
origin,
repeatable,
onSuccess: async (newCustomType) => {
createCustomTypeSuccess(newCustomType);

const format = getFormat(newCustomType);
const customTypesConfig = CUSTOM_TYPES_CONFIG[format];

setIsIdFieldPristine(true);

await router.push({
pathname: customTypesConfig.getBuilderPagePathname(id),
query:
newCustomType.format === "page"
? {
newPageType: true,
}
: undefined,
});
},
});
createCustomType(id, name, repeatable, format);
closeModals();
setIsIdFieldPristine(true);

onCreateChange(false);
onOpenChange(false);
};

const handleLabelChange = (
Expand Down Expand Up @@ -106,16 +127,18 @@ export const CreateCustomTypeModal: React.FC<CreateCustomTypeModalProps> = ({
return (
<ModalFormCard
dataCy="create-ct-modal"
isOpen={isCreateCustomTypeModalOpen}
isOpen={isOpen}
widthInPx="530px"
formId="create-custom-type"
buttonLabel={"Create"}
close={() => {
closeModals();
onOpenChange(false);
setIsIdFieldPristine(true);
}}
onSubmit={createCustomTypeAndTrack}
isLoading={isCreatingCustomType}
onSubmit={(values) => {
void onSubmit(values);
}}
isLoading={isCreating}
initialValues={{
repeatable: true,
id: "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const RenameCustomTypeModal: React.FC<RenameCustomTypeModalProps> = ({
}) => {
const customTypeName = customType?.label ?? "";
const customTypeId = customType?.id ?? "";
const { renameAvailableCustomTypeSuccess } = useSliceMachineActions();
const { renameCustomTypeSuccess } = useSliceMachineActions();

const [isRenaming, setIsRenaming] = useState(false);

Expand All @@ -46,7 +46,7 @@ export const RenameCustomTypeModal: React.FC<RenameCustomTypeModalProps> = ({
await renameCustomType({
model: customType,
newLabel: values.customTypeName,
onSuccess: renameAvailableCustomTypeSuccess,
onSuccess: renameCustomTypeSuccess,
});
}
setIsRenaming(false);
Expand Down
46 changes: 14 additions & 32 deletions packages/slice-machine/components/Navigation/ChangesListItem.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
import { type FC, useState } from "react";
import { type FC } from "react";
import Link from "next/link";
import { useRouter } from "next/router";
import { SliceMachineStoreType } from "@src/redux/type";
import { useSelector } from "react-redux";

import { SideNavLink, SideNavListItem } from "@src/components/SideNav";
import { RadarIcon } from "@src/icons/RadarIcon";
import { isLoading } from "@src/modules/loading";
import { LoadingKeysEnum } from "@src/modules/loading/types";
import {
HoverCard,
HoverCardCloseButton,
HoverCardDescription,
HoverCardMedia,
HoverCardTitle,
} from "@src/components/HoverCard";
import {
userHasSeenChangesToolTip,
userHasSeenSimulatorToolTip,
} from "@src/modules/userContext";
import useSliceMachineActions from "@src/modules/useSliceMachineActions";

import { ChangesRightElement } from "./ChangesRightElement";

export const ChangesListItem: FC = () => {
Expand Down Expand Up @@ -63,30 +56,19 @@ export const ChangesListItem: FC = () => {
);
};

// TODO(DT-1925): Reactivate this feature
const useOpenChangesHoverCard = () => {
const {
hasSeenChangesToolTip,
hasSeenSimulatorToolTip,
isSavingCustomType,
isSavingSlice,
} = useSelector((store: SliceMachineStoreType) => ({
hasSeenChangesToolTip: userHasSeenChangesToolTip(store),
hasSeenSimulatorToolTip: userHasSeenSimulatorToolTip(store),
isSavingCustomType: isLoading(store, LoadingKeysEnum.SAVE_CUSTOM_TYPE),
isSavingSlice: isLoading(store, LoadingKeysEnum.SAVE_SLICE),
}));

const isSaving = isSavingCustomType || isSavingSlice;
const [prevIsSaving, setPrevIsSaving] = useState(isSaving);
// const { hasSeenChangesToolTip, hasSeenSimulatorToolTip } = useSelector(
// (store: SliceMachineStoreType) => ({
// hasSeenChangesToolTip: userHasSeenChangesToolTip(store),
// hasSeenSimulatorToolTip: userHasSeenSimulatorToolTip(store),
// }),
// );

if (!prevIsSaving && isSaving) {
setPrevIsSaving(isSaving);
}
// return (
// !hasSeenChangesToolTip &&
// hasSeenSimulatorToolTip
// );

return (
!hasSeenChangesToolTip &&
hasSeenSimulatorToolTip &&
!isSaving &&
prevIsSaving
);
return false;
};
Loading

0 comments on commit d8c6687

Please sign in to comment.