Skip to content

Commit

Permalink
Move addProjectDirectory and remove newProject thunk
Browse files Browse the repository at this point in the history
  • Loading branch information
charlielee committed Oct 18, 2024
1 parent fe77ee7 commit 92ec569
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import { useNavigate } from "react-router-dom";
import { PageRoute } from "../../../../common/PageRoute";
import { DEFAULT_PROJECT_NAME } from "../../../../common/utils";
import useWorkingDirectory from "../../../hooks/useWorkingDirectory";
import { updateProject } from "../../../redux/slices/projectSlice";
import {
addProject,
addTake,
setProjectDirectoryId,
updateProject,
} from "../../../redux/slices/projectSlice";
import { RootState } from "../../../redux/store";
import { newProject } from "../../../redux/thunks/projectThunk";
import {
formatProjectName,
makeProject,
makeProjectDirectoryName,
makeTake,
} from "../../../services/project/projectBuilder";
import Button from "../../common/Button/Button";
import Content from "../../common/Content/Content";
Expand All @@ -30,12 +35,13 @@ import "./ProjectSettingsModal.css";
import classNames from "classnames";
import { JSXElementWithTestIds } from "../../../types";
import { RecentDirectoriesContext } from "../../../context/RecentDirectoriesContext/RecentDirectoriesContext";
import { Project } from "../../../../common/project/Project";

const ProjectSettingsModal = (): JSXElementWithTestIds => {
const dispatch: ThunkDispatch<RootState, void, Action> = useDispatch();
const navigate = useNavigate();

const { changeWorkingDirectory } = useContext(RecentDirectoriesContext);
const { changeWorkingDirectory, addProjectDirectory } = useContext(RecentDirectoriesContext);

const currentProject = useSelector((state: RootState) => state.project.project);
const [project, setProject] = useState(currentProject ?? makeProject({ name: "" }));
Expand All @@ -52,12 +58,24 @@ const ProjectSettingsModal = (): JSXElementWithTestIds => {
if (currentProject) {
dispatch(updateProject(formattedProject));
} else {
await dispatch(newProject(formattedProject));
await newProject(formattedProject);
}

navigate(PageRoute.ANIMATOR);
};

const newProject = async (formattedProject: Project) => {
const projectDirectoryEntry = await addProjectDirectory!(formattedProject);
dispatch(setProjectDirectoryId(projectDirectoryEntry.id));
dispatch(addProject(formattedProject));
const take = makeTake({
shotNumber: 1,
takeNumber: 1,
frameRate: 15,
});
dispatch(addTake(take));
};

return (
<Modal onClose={currentProject ? PageRoute.ANIMATOR : PageRoute.STARTUP_MODAL}>
<ModalBody>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { createContext } from "react";
import { RecentDirectoryEntry } from "../../services/database/RecentDirectoryEntry";
import { Project } from "../../../common/project/Project";

interface RecentDirectoriesContextProps {
changeWorkingDirectory: () => Promise<void>;
addProjectDirectory: (project: Project) => Promise<RecentDirectoryEntry>;
}

export const RecentDirectoriesContext = createContext<Partial<RecentDirectoriesContextProps>>({});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import { RecentDirectoriesContext } from "./RecentDirectoriesContext";
// import useProjectDirectory from "../../hooks/useProjectDirectory";
// import useWorkingDirectory from "../../hooks/useWorkingDirectory";
import { FileManagerContext } from "../FileManagerContext/FileManagerContext";
import { putOrAddWorkingDirectory } from "../../services/database/RecentDirectoryEntry";
import {
addProjectDirectoryEntry,
putOrAddWorkingDirectoryEntry,
RecentDirectoryEntry,
} from "../../services/database/RecentDirectoryEntry";
import useWorkingDirectory from "../../hooks/useWorkingDirectory";
import { Project } from "../../../common/project/Project";
import { makeProjectDirectoryName } from "../../services/project/projectBuilder";

interface RecentDirectoriesContextProviderProps {
children: ReactNode;
Expand All @@ -12,8 +19,7 @@ interface RecentDirectoriesContextProviderProps {
export const RecentDirectoriesContextProvider = ({
children,
}: RecentDirectoriesContextProviderProps) => {
// const workingDirectory = useWorkingDirectory();
// const projectDirectory = useProjectDirectory();
const workingDirectory = useWorkingDirectory();

const { fileManager } = useContext(FileManagerContext);
if (fileManager === undefined) {
Expand All @@ -25,12 +31,26 @@ export const RecentDirectoriesContextProvider = ({
await fileManager.current.openDirectoryDialog("changeWorkingDirectory");

if (workingDirectoryHandle !== undefined) {
await putOrAddWorkingDirectory(workingDirectoryHandle);
await putOrAddWorkingDirectoryEntry(workingDirectoryHandle);
}
};

const addProjectDirectory = async (project: Project): Promise<RecentDirectoryEntry> => {
if (workingDirectory === undefined) {
throw "workingDirectory was not found";
}

const projectDirectoryName = makeProjectDirectoryName(project);
const handle = await fileManager.current.createDirectory(
projectDirectoryName,
workingDirectory.handle
);

return addProjectDirectoryEntry(project.name, handle);
};

return (
<RecentDirectoriesContext.Provider value={{ changeWorkingDirectory }}>
<RecentDirectoriesContext.Provider value={{ changeWorkingDirectory, addProjectDirectory }}>
{children}
</RecentDirectoriesContext.Provider>
);
Expand Down
37 changes: 0 additions & 37 deletions src/renderer/redux/thunks/projectThunk.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/renderer/services/database/RecentDirectoryEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ export interface RecentDirectoryEntry {
handle: FileSystemDirectoryHandle;
}

export const getWorkingDirectory = async () =>
export const getWorkingDirectoryEntry = async () =>
db.recentDirectories.get({
type: RecentDirectoryType.WORKING_DIRECTORY,
});

export const putOrAddWorkingDirectory = async (handle: FileSystemDirectoryHandle) => {
const workingDirectory = await getWorkingDirectory();
export const putOrAddWorkingDirectoryEntry = async (handle: FileSystemDirectoryHandle) => {
const workingDirectory = await getWorkingDirectoryEntry();
const newEntry: RecentDirectoryEntry = {
id: workingDirectory?.id ?? uuidv4(),
type: RecentDirectoryType.WORKING_DIRECTORY,
Expand All @@ -33,7 +33,7 @@ export const putOrAddWorkingDirectory = async (handle: FileSystemDirectoryHandle
return newEntry;
};

export const addProjectDirectory = async (
export const addProjectDirectoryEntry = async (
friendlyName: string,
handle: FileSystemDirectoryHandle
) => {
Expand Down

0 comments on commit 92ec569

Please sign in to comment.