Skip to content

Commit

Permalink
feat(auto-save): Auto-save on custom / page type builder
Browse files Browse the repository at this point in the history
  • Loading branch information
xrutayisire committed Dec 27, 2023
1 parent 69c5d33 commit 1831c8e
Showing 65 changed files with 1,967 additions and 2,661 deletions.
Original file line number Diff line number Diff line change
@@ -15,10 +15,7 @@ import { InputBox } from "../components/InputBox";

type CreateSliceModalProps = {
onClose: () => void;
onSuccess: (
newSlice: SharedSlice,
libraryName: string,
) => Promise<void> | void;
onSuccess: (newSlice: SharedSlice, libraryName: string) => void;
localLibraries: readonly LibraryUI[];
remoteSlices: ReadonlyArray<SliceSM>;
};
@@ -49,7 +46,7 @@ export const CreateSliceModal: FC<CreateSliceModalProps> = ({
// Update Redux store
createSliceSuccess(serverState.libraries);

await onSuccess(newSlice, libraryName);
onSuccess(newSlice, libraryName);
},
});
};
Original file line number Diff line number Diff line change
@@ -6,13 +6,14 @@ import { InputBox } from "../components/InputBox";
import { useSelector } from "react-redux";
import { SliceMachineStoreType } from "@src/redux/type";
import { FormikErrors } from "formik";
import { selectAllCustomTypeLabels } from "@src/modules/availableCustomTypes";

import { selectAllCustomTypeLabels } from "@src/modules/availableCustomTypes";
import { CustomType } from "@prismicio/types-internal/lib/customtypes";
import { CustomTypeFormat } from "@slicemachine/manager";
import { CUSTOM_TYPES_MESSAGES } from "@src/features/customTypes/customTypesMessages";

import { renameCustomType } from "@src/features/customTypes/actions/renameCustomType";
import { useCustomTypeState } from "@src/features/customTypes/customTypesBuilder/CustomTypeProvider";

interface RenameCustomTypeModalProps {
isChangesLocal: boolean;
@@ -29,15 +30,18 @@ export const RenameCustomTypeModal: React.FC<RenameCustomTypeModalProps> = ({
}) => {
const customTypeName = customType?.label ?? "";
const customTypeId = customType?.id ?? "";
const { renameAvailableCustomTypeSuccess, renameSelectedCustomType } =
useSliceMachineActions();
const { renameAvailableCustomTypeSuccess } = useSliceMachineActions();
const { setCustomType } = useCustomTypeState();

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

const handleOnSubmit = async (values: { customTypeName: string }) => {
setIsRenaming(true);
if (isChangesLocal) {
renameSelectedCustomType(values.customTypeName);
setCustomType({
...customType,
label: values.customTypeName,
});
} else {
await renameCustomType({
model: customType,
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ import { sliceTemplatesComingSoon } from "./sliceTemplatesComingSoon";
interface UpdateSliceModalProps {
formId: string;
close: () => void;
onSuccess: (slices: SharedSlice[]) => Promise<void>;
onSuccess: (slices: SharedSlice[]) => void;
availableSlicesTemplates: SliceTemplate[];
localLibraries: readonly LibraryUI[];
}
@@ -69,7 +69,7 @@ export const SlicesTemplatesModal: FC<UpdateSliceModalProps> = ({
) as Promise<SharedSlice>[],
);

await onSuccess(slices);
onSuccess(slices);
},
});
}}
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ import UpdateSliceZoneModalList from "./UpdateSliceZoneModalList";
interface UpdateSliceModalProps {
formId: string;
close: () => void;
onSubmit: (slices: SharedSlice[]) => Promise<void>;
onSubmit: (slices: SharedSlice[]) => void;
availableSlices: ReadonlyArray<ComponentUI>;
}

@@ -37,7 +37,7 @@ const UpdateSliceZoneModal: React.FC<UpdateSliceModalProps> = ({
availableSlices.find((s) => s.model.id === sliceKey)?.model,
)
.filter((slice) => slice !== undefined) as SharedSlice[];
void onSubmit(slices);
onSubmit(slices);
}}
initialValues={{
sliceKeys: [],
Original file line number Diff line number Diff line change
@@ -31,14 +31,14 @@ import {
} from "@src/modules/slices";
import type { SliceMachineStoreType } from "@src/redux/type";
import { useSlicesTemplates } from "@src/features/slicesTemplates/useSlicesTemplates";
import useSliceMachineActions from "@src/modules/useSliceMachineActions";
import { addSlicesToSliceZone } from "@src/features/slices/actions/addSlicesToSliceZone";
import { ToastMessageWithPath } from "@components/ToasterContainer";

import { DeleteSliceZoneModal } from "./DeleteSliceZoneModal";
import UpdateSliceZoneModal from "./UpdateSliceZoneModal";
import { SlicesTemplatesModal } from "./SlicesTemplatesModal";
import { SlicesList } from "./List";
import { useCustomTypeState } from "@src/features/customTypes/customTypesBuilder/CustomTypeProvider";

const mapAvailableAndSharedSlices = (
sliceZone: SlicesSM,
@@ -121,8 +121,8 @@ const SliceZone: React.FC<SliceZoneProps> = ({
slices: getFrontendSlices(store),
}),
);
const { initCustomTypeStore, saveCustomTypeSuccess } =
useSliceMachineActions();
const { setCustomType } = useCustomTypeState();

const localLibraries: readonly LibraryUI[] = libraries.filter(
(library) => library.isLocal,
);
@@ -197,14 +197,6 @@ const SliceZone: React.FC<SliceZoneProps> = ({
setIsSlicesTemplatesModalOpen(false);
};

const onAddSlicesToSliceZone = (newCustomType: CustomTypeSM) => {
// Reset selected custom type store to update slice zone and saving status
initCustomTypeStore(newCustomType, newCustomType);

// Update available custom type store with new custom type
saveCustomTypeSuccess(CustomTypes.fromSM(newCustomType));
};

return (
<>
{query.newPageType === undefined ? (
@@ -299,13 +291,13 @@ const SliceZone: React.FC<SliceZoneProps> = ({
<UpdateSliceZoneModal
formId={`tab-slicezone-form-${tabId}`}
availableSlices={availableSlicesToAdd}
onSubmit={async (slices: SharedSlice[]) => {
const newCustomType = await addSlicesToSliceZone({
onSubmit={(slices: SharedSlice[]) => {
const newCustomType = addSlicesToSliceZone({
customType,
tabId,
slices,
});
onAddSlicesToSliceZone(newCustomType);
setCustomType(CustomTypes.fromSM(newCustomType));
closeUpdateSliceZoneModal();
redirectToEditMode();
toast.success("Slice(s) added to slice zone");
@@ -318,13 +310,13 @@ const SliceZone: React.FC<SliceZoneProps> = ({
formId={`tab-slicezone-form-${tabId}`}
availableSlicesTemplates={availableSlicesTemplates}
localLibraries={localLibraries}
onSuccess={async (slices: SharedSlice[]) => {
const newCustomType = await addSlicesToSliceZone({
onSuccess={(slices: SharedSlice[]) => {
const newCustomType = addSlicesToSliceZone({
customType,
tabId,
slices,
});
onAddSlicesToSliceZone(newCustomType);
setCustomType(CustomTypes.fromSM(newCustomType));
closeSlicesTemplatesModal();
redirectToEditMode();
toast.success(
@@ -350,13 +342,13 @@ const SliceZone: React.FC<SliceZoneProps> = ({
)}
{localLibraries?.length !== 0 && isCreateSliceModalOpen && (
<CreateSliceModal
onSuccess={async (newSlice: SharedSlice) => {
const newCustomType = await addSlicesToSliceZone({
onSuccess={(newSlice: SharedSlice) => {
const newCustomType = addSlicesToSliceZone({
customType,
tabId,
slices: [newSlice],
});
onAddSlicesToSliceZone(newCustomType);
setCustomType(CustomTypes.fromSM(newCustomType));
closeCreateSliceModal();
redirectToEditMode();
toast.success(
Original file line number Diff line number Diff line change
@@ -10,11 +10,13 @@ const UpdateCustomTypeForm = ({
onSubmit,
close,
tabIds,
initialTabKey,
}: {
isOpen: boolean;
onSubmit: (values: { id: string }) => void;
close: () => void;
tabIds: ReadonlyArray<string>;
initialTabKey: string;
}) => {
return (
<ModalFormCard
@@ -29,7 +31,7 @@ const UpdateCustomTypeForm = ({
close();
}}
initialValues={{
id: "",
id: initialTabKey,
}}
validate={({ id }) => {
if (!id) {
Loading

0 comments on commit 1831c8e

Please sign in to comment.