Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/context/reducer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import ActionTemplate from "../templates/ActionTemplate.js";
import Context from "../context";
import { publish } from "@nucleoidai/react-event";
import service from "../service.js";
import { storage } from "@nucleoidjs/webstorage";
import { v4 as uuid } from "uuid";

function contextReducer(context, { type, payload }) {
const { id } = storage.get("ide", "selected", "context");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are some conditions causing this failed in Cypress

const { project } = storage.get("ide", "context", id);
context = Context.copy(context);

const { specification, pages } = context;
Expand Down Expand Up @@ -455,6 +459,7 @@ function contextReducer(context, { type, payload }) {
} else {
specification.types.push(updatedTypes);
}

break;
}
case "ADD_TYPE": {
Expand All @@ -471,6 +476,7 @@ function contextReducer(context, { type, payload }) {
},
};
specification.types.push(newType);

break;
}
case "DELETE_TYPE": {
Expand All @@ -482,6 +488,7 @@ function contextReducer(context, { type, payload }) {
if (typeIndex !== -1) {
specification.types.splice(typeIndex, 1);
}

break;
}

Expand All @@ -495,6 +502,7 @@ function contextReducer(context, { type, payload }) {
specification.types[typeIndex].name = newTypeName;
specification.types[typeIndex].schema.name = newTypeName;
}

break;
}
case "SAVE_API_PARAMS": {
Expand All @@ -507,6 +515,7 @@ function contextReducer(context, { type, payload }) {
if (apiIndex !== -1) {
specification.api[apiIndex].params = params;
}

break;
}
case "UPDATE_API_PATH_METHOD": {
Expand Down Expand Up @@ -535,6 +544,7 @@ function contextReducer(context, { type, payload }) {
default:
}

service.saveContext({ project, specification });
console.debug("contextReducer", type, context);
return context;
}
Expand Down
19 changes: 17 additions & 2 deletions src/service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Settings from "./settings";
import http from "./http";
import { publish } from "@nucleoidai/react-event";
import { storage } from "@nucleoidjs/webstorage";

const query = async (body) => {
return fetch(Settings.url.terminal(), {
Expand Down Expand Up @@ -78,8 +80,21 @@ const getContext = (contextId) => {
return http.get(`services/${contextId}/specification`);
};

const saveContext = (contextId, context) => {
return http.put(`services/${contextId}/specification`, context);
const saveContext = (context) => {
const { project, specification } = context;
const { id, type } = project;
switch (type) {
case "LOCAL":
storage.set("ide", "context", id, { project, specification });
break;
case "CLOUD":
return http.put(`services/${id}/specification`, specification);
case "TERMINAL":
publish("RUNTIME_CONNECTION", {
status: true,
metrics: { total: 100, free: 50 },
});
}
};

const getGraph = () => {
Expand Down
28 changes: 28 additions & 0 deletions src/test/context.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
import Context from "../context";
import { contextReducer } from "../context/reducer";
import { storage } from "@nucleoidjs/webstorage";

jest.mock("@nucleoidjs/webstorage", () => {
const memory = new Map();
return {
storage: {
get: jest.fn((...args) => memory.get(args.join("."))),
set: jest.fn((...args) => {
const value = args.pop();
memory.set(args.join("."), value);
}),
clear: jest.fn(() => memory.clear()),
},
};
});

jest.mock("@nucleoidai/react-event", () => ({
publish: jest.fn(),
}));

beforeEach(() => {
storage.set("ide", "selected", "context", {
id: "21d2530b-4657-4ac0-b8cd-1a9f82786e32",
});
storage.set(
"ide",
"context",
"21d2530b-4657-4ac0-b8cd-1a9f82786e32",
Context.withBlank()
);
});

test("Resolve context with property", () => {
const state = contextReducer(Context.init(), {
type: "SET_SELECTED_API",
Expand Down
18 changes: 3 additions & 15 deletions src/widgets/Editor/Editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,8 @@ const Editor = React.forwardRef((props, ref) => {
};
}

const debouncedSave = debounce((id, specification, project) => {
if (mode === "cloud") {
service.saveContext(id, specification);
} else if (mode === "local") {
storage.set("ide", "context", id, {
specification,
project,
});
} else if (mode === "terminal") {
publish("RUNTIME_CONNECTION", {
status: true,
metrics: { total: 100, free: 50 },
});
}
const debouncedSave = debounce((id, context) => {
service.saveContext(context);

publish("CONTEXT_SAVED", { contextId: id, to: mode });
}, 300);
Expand All @@ -190,7 +178,7 @@ const Editor = React.forwardRef((props, ref) => {
return item;
});

debouncedSave(id, context.specification, context.project);
debouncedSave(id, context);
}
if (query) {
context.pages.query.text = e;
Expand Down
25 changes: 3 additions & 22 deletions src/widgets/VFSEditor/VFSEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { contextToMap } from "../../utils/Parser";
import { publish } from "@nucleoidai/react-event";
import rules from "./rules";
import service from "../../service";
import { storage } from "@nucleoidjs/webstorage";
import { useContext } from "../../context/context";

import { Box, Grid } from "@mui/material";
Expand Down Expand Up @@ -104,27 +103,9 @@ const VFSEditor = React.forwardRef((props, ref) => {
key = context.get("pages.functions.selected") + ".ts";
}

const {
project: { id },
} = context;

if (mode === "cloud") {
const nucContext = { ...context.specification };
delete nucContext.project;

service.saveContext(id, nucContext);
} else if (mode === "local") {
storage.set("ide", "context", id, {
specification: context.specification,
project: context.project,
});
} else if (mode === "terminal") {
publish("RUNTIME_CONNECTION", {
status: true,
metrics: { total: 100, free: 50 },
});
}
publish("CONTEXT_SAVED", { contextId: id, to: mode });
service.saveContext(context);

publish("CONTEXT_SAVED", { contextId: context.project.id, to: mode });
publish("CONTEXT_CHANGED", {
// TODO Optimize preparing files
files: contextToMap(context.specification).filter(
Expand Down