Skip to content

Commit

Permalink
Simplify reducer update
Browse files Browse the repository at this point in the history
  • Loading branch information
WardBrian committed Oct 30, 2024
1 parent 6e1a975 commit f67c9a1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
19 changes: 15 additions & 4 deletions gui/src/app/Project/ProjectReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type ProjectReducerAction =
type: "retitle";
title: string;
}
| { type: "setDataSource"; dataSource: DataSource | undefined }
| { type: "generateData"; content: string; dataSource: DataSource }
| {
type: "editFile";
content: string;
Expand Down Expand Up @@ -71,9 +71,23 @@ const ProjectReducer = (s: ProjectDataModel, a: ProjectReducerAction) => {
}
case "commitFile": {
const newState = { ...s };
if (a.filename === ProjectKnownFiles.DATAFILE) {
newState.meta = { ...s.meta, dataSource: undefined };
}
newState[a.filename] = s.ephemera[a.filename];
return newState;
}
case "generateData": {
return {
...s,
[ProjectKnownFiles.DATAFILE]: a.content,
ephemera: {
...s.ephemera,
[ProjectKnownFiles.DATAFILE]: a.content,
},
meta: { ...s.meta, dataSource: a.dataSource },
};
}
case "setSamplingOpts": {
return { ...s, samplingOpts: { ...s.samplingOpts, ...a.opts } };
}
Expand All @@ -83,9 +97,6 @@ const ProjectReducer = (s: ProjectDataModel, a: ProjectReducerAction) => {
case "clear": {
return initialDataModel;
}
case "setDataSource": {
return { ...s, meta: { ...s.meta, dataSource: a.dataSource } };
}
default:
return unreachable(a);
}
Expand Down
9 changes: 2 additions & 7 deletions gui/src/app/Scripting/DataGeneration/useDataGenState.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback, useContext, useRef, useState } from "react";
import { DataSource, ProjectKnownFiles } from "@SpCore/ProjectDataModel";
import { DataSource } from "@SpCore/ProjectDataModel";
import { writeConsoleOutToDiv } from "@SpScripting/OutputDivUtils";
import { InterpreterStatus } from "@SpScripting/InterpreterTypes";
import { ProjectContext } from "@SpCore/ProjectContextProvider";
Expand All @@ -21,13 +21,8 @@ const useDataGenState = (source: "python" | "r") => {
if (dataJson !== lastData.current) {
lastData.current = dataJson;
update({
type: "editFile",
type: "generateData",
content: dataJson,
filename: ProjectKnownFiles.DATAFILE,
});
update({ type: "commitFile", filename: ProjectKnownFiles.DATAFILE });
update({
type: "setDataSource",
dataSource:
source === "python"
? DataSource.GENERATED_BY_PYTHON
Expand Down
4 changes: 0 additions & 4 deletions gui/src/app/pages/HomePage/HomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,6 @@ const DataEditor: FunctionComponent<{}> = () => {
type: "commitFile",
filename: ProjectKnownFiles.DATAFILE,
});
update({
type: "setDataSource",
dataSource: undefined,
});
}, [update]);

return (
Expand Down
23 changes: 23 additions & 0 deletions gui/test/app/Project/ProjectReducer.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
DataSource,
getStringKnownFileKeys,
initialDataModel,
ProjectDataModel,
Expand Down Expand Up @@ -130,6 +131,7 @@ describe("Project reducer", () => {
const initialState = {
...permanentFiles,
ephemera: { ...ephemeralFiles },
meta: { dataSource: DataSource.GENERATED_BY_PYTHON },
} as any as ProjectDataModel;
const commitAction: ProjectReducerAction = {
type: "commitFile",
Expand All @@ -143,6 +145,7 @@ describe("Project reducer", () => {
expect(result[ProjectKnownFiles.DATAFILE]).toEqual(
result.ephemera[ProjectKnownFiles.DATAFILE],
);
expect(result.meta.dataSource).toBeUndefined();
});
test("Save action does not save non-chosen files", () => {
const result = ProjectReducer(initialState, commitAction);
Expand Down Expand Up @@ -220,4 +223,24 @@ describe("Project reducer", () => {
expect(result).toBe(initialDataModel);
});
});

describe("Generate data", () => {
test("Updates data file and ephemera with new data", () => {
const initialState = {
...permanentFiles,
ephemera: { ...ephemeralFiles },
meta: { dataSource: DataSource.GENERATED_BY_R },
} as any as ProjectDataModel;
const newData = "generated data";
const generateAction: ProjectReducerAction = {
type: "generateData",
content: newData,
dataSource: DataSource.GENERATED_BY_PYTHON,
};
const result = ProjectReducer(initialState, generateAction);
expect(result[ProjectKnownFiles.DATAFILE]).toEqual(newData);
expect(result.ephemera[ProjectKnownFiles.DATAFILE]).toEqual(newData);
expect(result.meta.dataSource).toEqual(DataSource.GENERATED_BY_PYTHON);
});
});
});

0 comments on commit f67c9a1

Please sign in to comment.