diff --git a/gui/src/app/Project/ProjectReducer.ts b/gui/src/app/Project/ProjectReducer.ts index 032d8cd..e791c27 100644 --- a/gui/src/app/Project/ProjectReducer.ts +++ b/gui/src/app/Project/ProjectReducer.ts @@ -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; @@ -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 } }; } @@ -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); } diff --git a/gui/src/app/Scripting/DataGeneration/useDataGenState.ts b/gui/src/app/Scripting/DataGeneration/useDataGenState.ts index 25c74cc..830918b 100644 --- a/gui/src/app/Scripting/DataGeneration/useDataGenState.ts +++ b/gui/src/app/Scripting/DataGeneration/useDataGenState.ts @@ -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"; @@ -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 diff --git a/gui/src/app/pages/HomePage/HomePage.tsx b/gui/src/app/pages/HomePage/HomePage.tsx index b1202c9..c407296 100644 --- a/gui/src/app/pages/HomePage/HomePage.tsx +++ b/gui/src/app/pages/HomePage/HomePage.tsx @@ -165,10 +165,6 @@ const DataEditor: FunctionComponent<{}> = () => { type: "commitFile", filename: ProjectKnownFiles.DATAFILE, }); - update({ - type: "setDataSource", - dataSource: undefined, - }); }, [update]); return ( diff --git a/gui/test/app/Project/ProjectReducer.test.ts b/gui/test/app/Project/ProjectReducer.test.ts index 04468cc..b868146 100644 --- a/gui/test/app/Project/ProjectReducer.test.ts +++ b/gui/test/app/Project/ProjectReducer.test.ts @@ -1,4 +1,5 @@ import { + DataSource, getStringKnownFileKeys, initialDataModel, ProjectDataModel, @@ -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", @@ -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); @@ -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); + }); + }); });