Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Loe committed Sep 13, 2024
1 parent 61432f3 commit 5598eca
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 50 deletions.
29 changes: 13 additions & 16 deletions src/pageEditor/hooks/useAddNewModComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,16 @@ import {
inspectedTab,
} from "@/pageEditor/context/connection";
import { getExampleBrickPipeline } from "@/pageEditor/panes/insert/exampleStarterBrickConfigs";
import {
type StarterBrickType,
StarterBrickTypes,
} from "@/types/starterBrickTypes";
import { StarterBrickTypes } from "@/types/starterBrickTypes";
import { openSidePanel } from "@/utils/sidePanelUtils";
import { useInsertPane } from "@/pageEditor/panes/insert/InsertPane";
import { type ModMetadata } from "@/types/modComponentTypes";
import { adapter } from "@/pageEditor/starterBricks/adapter";
import { getUnsavedModMetadataForFormState } from "@/pageEditor/utils";
import { type ModComponentFormStateAdapter } from "@/pageEditor/starterBricks/modComponentFormStateAdapter";

export type AddNewModComponent = (starterBrickType: StarterBrickType) => void;
export type AddNewModComponent = (
adapter: ModComponentFormStateAdapter,
) => void;

function useAddNewModComponent(modMetadata?: ModMetadata): AddNewModComponent {
const dispatch = useDispatch();
Expand All @@ -58,12 +57,11 @@ function useAddNewModComponent(modMetadata?: ModMetadata): AddNewModComponent {
);

const getInitialModComponentFormState = useCallback(
async (
starterBrickType: StarterBrickType,
): Promise<ModComponentFormState> => {
const { selectNativeElement, fromNativeElement } =
adapter(starterBrickType);

async ({
starterBrickType,
selectNativeElement,
fromNativeElement,
}: ModComponentFormStateAdapter): Promise<ModComponentFormState> => {
let element = null;
if (selectNativeElement) {
setInsertingStarterBrickType(starterBrickType);
Expand Down Expand Up @@ -91,17 +89,16 @@ function useAddNewModComponent(modMetadata?: ModMetadata): AddNewModComponent {
);

return useCallback(
async (starterBrickType: StarterBrickType) => {
const { label, flag, asDraftModComponent } = adapter(starterBrickType);
async (adapter: ModComponentFormStateAdapter) => {
const { starterBrickType, label, flag, asDraftModComponent } = adapter;

if (flag && flagOff(flag)) {
dispatch(actions.betaError());
return;
}

try {
const initialFormState =
await getInitialModComponentFormState(starterBrickType);
const initialFormState = await getInitialModComponentFormState(adapter);

dispatch(actions.addModComponentFormState(initialFormState));
dispatch(actions.checkActiveModComponentAvailability());
Expand Down
27 changes: 3 additions & 24 deletions src/pageEditor/modListingPanel/ModListItem.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,7 @@ describe("ModListItem", () => {
render(
<Accordion defaultActiveKey={modMetadata.id}>
<ListGroup>
<ModListItem
modMetadata={modMetadata}
onSave={jest.fn()}
isSaving={false}
onReset={jest.fn()}
onDeactivate={jest.fn()}
onClone={jest.fn()}
>
<ModListItem modMetadata={modMetadata}>
<div>test children</div>
</ModListItem>
</ListGroup>
Expand All @@ -70,14 +63,7 @@ describe("ModListItem", () => {
render(
<Accordion>
<ListGroup>
<ModListItem
modMetadata={modMetadata}
onSave={jest.fn()}
isSaving={false}
onReset={jest.fn()}
onDeactivate={jest.fn()}
onClone={jest.fn()}
>
<ModListItem modMetadata={modMetadata}>
<div>test children</div>
</ModListItem>
</ListGroup>
Expand Down Expand Up @@ -111,14 +97,7 @@ describe("ModListItem", () => {
render(
<Accordion defaultActiveKey={modMetadata.id}>
<ListGroup>
<ModListItem
modMetadata={modMetadata}
onSave={jest.fn()}
isSaving={false}
onReset={jest.fn()}
onDeactivate={jest.fn()}
onClone={jest.fn()}
>
<ModListItem modMetadata={modMetadata}>
<div>test children</div>
</ModListItem>
</ListGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/pageEditor/modListingPanel/actionMenus/ModActionMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ const ModActionMenu: React.FC<{ modMetadata: ModMetadata }> = ({
const addStarterBrickSubMenu = useMemo(
() =>
modComponentFormStateAdapters.map((adapter) => ({
title: adapter.label,
title: adapter.flag ? `${adapter.label} (Beta)` : adapter.label,
action() {
addNewModComponent(adapter.starterBrickType);
addNewModComponent(adapter);
},
icon: <FontAwesomeIcon icon={adapter.icon} fixedWidth />,
})),
Expand Down
4 changes: 0 additions & 4 deletions src/pageEditor/modals/Modals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@

import AddBrickModal from "@/pageEditor/modals/addBrickModal/AddBrickModal";
import React from "react";
import AddToModModal from "@/pageEditor/modListingPanel/modals/AddToModModal";
import CreateModModal from "@/pageEditor/modListingPanel/modals/CreateModModal";
import MoveFromModModal from "@/pageEditor/modListingPanel/modals/MoveFromModModal";
import SaveAsNewModModal from "@/pageEditor/modListingPanel/modals/SaveAsNewModModal";
import SaveDataIntegrityErrorModal from "@/pageEditor/panes/save/SaveDataIntegrityErrorModal";

const Modals: React.FunctionComponent = () => (
<>
<AddToModModal />
<MoveFromModModal />
<SaveAsNewModModal />
<CreateModModal />
<AddBrickModal />
<SaveDataIntegrityErrorModal />
Expand Down
12 changes: 8 additions & 4 deletions src/pageEditor/store/editor/editorSlice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*/

import {
editorSlice,
actions,
editorSlice,
initialState,
persistEditorConfig,
} from "@/pageEditor/store/editor/editorSlice";
Expand Down Expand Up @@ -49,6 +49,7 @@ import { migrations } from "@/store/editorMigrations";
import { modMetadataFactory } from "@/testUtils/factories/modComponentFactories";
import { setActiveModId } from "./editorSliceHelpers";
import { castDraft } from "immer";
import { type ModComponentsRootState } from "@/store/modComponents/modComponentTypes";

function getTabState(
state: EditorState,
Expand Down Expand Up @@ -222,11 +223,14 @@ describe("Add/Remove Bricks", () => {
).toBeArrayOfSize(initialIntegrationDependencies.length);
});

test("Can clone a mod compoenent", async () => {
test("Can clone a mod component", async () => {
const dispatch = jest.fn();
const getState: () => EditorRootState = () => ({ editor });
const getState: () => EditorRootState & ModComponentsRootState = () => ({
editor,
options: { activatedModComponents: [] },
});

await actions.cloneActiveModComponent()(dispatch, getState, undefined);
await actions.duplicateActiveModComponent()(dispatch, getState, undefined);

// Dispatch call args (actions) should be:
// 1. thunk pending
Expand Down

0 comments on commit 5598eca

Please sign in to comment.