Skip to content

Fix save issue #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions cypress/e2e/functions.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ describe("Functions", () => {

cy.typeEditor(changedEditorValue);

cy.waitEvent("CONTEXT_SAVED");

cy.checkLocalContext(this.projectId, "function", changedEditorValue);
});
});
Expand Down
10 changes: 5 additions & 5 deletions cypress/e2e/projects-path.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("Projects Path", () => {

cy.waitEvent("CONTAINER_LOADED");

cy.storageGet("ide.selected.context").then((project) => {
cy.storageGet("ide.selected.project").then((project) => {
expect(project).to.exist;
expect(project).to.have.property("id", cloudProjectId);
expect(project).to.have.property("type", "CLOUD");
Expand Down Expand Up @@ -45,7 +45,7 @@ describe("Projects Path", () => {
const localProjectId = "3450f289-0fc5-45e9-9a4a-606c0a63cdfe";
const selectedProject = { id: localProjectId, type: "LOCAL" };

cy.storageSet("ide.selected.context", selectedProject);
cy.storageSet("ide.selected.project", selectedProject);

cy.visit("/");

Expand All @@ -62,11 +62,11 @@ describe("Projects Path", () => {
const pathParts = pathname.split("/");
const projectId = pathParts[pathParts.length - 2];

cy.storageGet(`ide.context.${projectId}`).as("project");
cy.storageGet(`ide.specification.${projectId}`).as("project");

cy.get("@project").should("exist");

cy.storageGet(`ide.selected.context`).as("selectedProject");
cy.storageGet(`ide.selected.project`).as("selectedProject");

cy.get("@selectedProject")
.should((selectedProject) => {
Expand All @@ -91,7 +91,7 @@ describe("Projects Path", () => {
const pathParts = pathname.split("/");
const projectId = pathParts[pathParts.length - 2];

cy.storageGet(`ide.selected.context`).then((selectedProject) => {
cy.storageGet(`ide.selected.project`).then((selectedProject) => {
expect(selectedProject.id).to.equal(projectId);
expect(selectedProject.type).to.equal("LOCAL");
});
Expand Down
28 changes: 17 additions & 11 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,19 @@ Cypress.Commands.add("setup", (container, fixtureType, type) => {
cy.fixture("PROJECTS/LOCAL/project").then((context) => {
const { project, types, functions, logic, api, declarations } =
context;
cy.storageSet(`ide.context.3450f289-0fc5-45e9-9a4a-606c0a63cdfe`, {
project: project,
specification: {
cy.storageSet(
`ide.specification.3450f289-0fc5-45e9-9a4a-606c0a63cdfe`,
{
api,
logic,
functions,
types,
declarations,
},
}
);
cy.storageSet(`ide.project.3450f289-0fc5-45e9-9a4a-606c0a63cdfe`, {
...project,
id: "3450f289-0fc5-45e9-9a4a-606c0a63cdfe",
});
});
}
Expand All @@ -97,14 +101,15 @@ Cypress.Commands.add("typeEditor", (changedEditorValue) => {
cy.get("section").should("be.visible");
cy.get(".monaco-editor").should("be.visible");

cy.get('textarea[role="textbox"]').type("{selectall}{del}");
cy.get(
"[class^='monaco-editor no-user-select showUnused showDeprecated vs-dark']"
).click();

cy.get('textarea[role="textbox"]').type("{selectAll}");
cy.get('textarea[role="textbox"]').type(changedEditorValue, {
force: true,
parseSpecialCharSequences: false,
});

cy.wait(1000);
});

Cypress.Commands.add("sendMessage", (message, fixture) => {
Expand Down Expand Up @@ -327,14 +332,15 @@ Cypress.Commands.add(
"checkLocalContext",
(projectId, specification, changedEditorValue) => {
let checkedSpecification;
cy.storageGet(`ide.context.${projectId}`).then((project) => {
cy.storageGet(`ide.specification.${projectId}`).then((spec) => {
if (specification === "api") {
checkedSpecification = project.specification.api[0]["action"];
checkedSpecification = spec.api[0]["action"];
} else if (specification === "declaration") {
checkedSpecification = project.specification.declarations[0].definition;
checkedSpecification = spec.declarations[0].definition;
}
if (specification === "function") {
checkedSpecification = project.specification.functions[0].definition;
console.log(spec.functions[0].definition);
checkedSpecification = spec.functions[0].definition;
}

cy.normalizeString(checkedSpecification).then((normalizedDefinition) => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/LogoutButton/LogoutButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ function LogoutButton({ onLogout }) {

const handleLogout = () => {
storage.remove("oauth.token");
const latestContext = storage.get("ide", "selected", "context");
const latestContext = storage.get("ide", "selected", "project");
publish("USER", { login: false, id: null });
if (latestContext.type === "CLOUD") {
storage.remove("ide", "selected", "context");
storage.remove("ide", "selected", "project");
navigate("/new");
}
onLogout && onLogout();
Expand Down
24 changes: 9 additions & 15 deletions src/containers/IDE/IDE.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,8 @@ function IDE() {
}, [mobileSize]);

function getContextFromStorage(projectId) {
const localContext = storage.get("ide", "context", projectId);

if (!localContext) {
navigate("/error/api");
return null;
}

const { specification, project } = localContext;
const project = storage.get("ide", "project", projectId);
const specification = storage.get("ide", "specification", projectId);

if (!specification && !project) {
navigate("/error/api");
Expand Down Expand Up @@ -123,7 +117,7 @@ function IDE() {
description: project.description,
};

storage.set("ide", "selected", "context", {
storage.set("ide", "selected", "project", {
id: project.id,
type: "CLOUD",
});
Expand All @@ -138,10 +132,8 @@ function IDE() {
const context = Context.withSample();
context.get = (prop) => Context.resolve(context, prop);
const { specification, project } = context;
storage.set("ide", "context", project.id, {
specification: specification,
project: project,
});
storage.set("ide", "project", project.id, project);
storage.set("ide", "specification", project.id, specification);

navigate(`/${project.id}/api?mode=local`);

Expand All @@ -156,6 +148,7 @@ function IDE() {
}

const initContext = (context) => {
console.log(context);
if (
!Settings.description() ||
Settings.description() !== context.project.description
Expand All @@ -179,9 +172,9 @@ function IDE() {

if (
context.project.type === "LOCAL" &&
storage.get("ide", "context", context.project.id)
storage.get("ide", "project", context.project.id)
) {
storage.set("ide", "selected", "context", {
storage.set("ide", "selected", "project", {
id: context.project.id,
type: "LOCAL",
});
Expand All @@ -197,6 +190,7 @@ function IDE() {
};

const initVfs = (context) => {
console.log(context);
const files = contextToMap(context.specification);
vfs.init(files);
};
Expand Down
9 changes: 9 additions & 0 deletions src/context/reducer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
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 selectedProject = storage.get("ide", "selected", "project");
context = Context.copy(context);

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

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

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

break;
}

Expand All @@ -495,6 +501,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 +514,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 +543,7 @@ function contextReducer(context, { type, payload }) {
default:
}

service.saveSpecification(selectedProject, specification);
console.debug("contextReducer", type, context);
return context;
}
Expand Down
21 changes: 18 additions & 3 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 saveSpecification = (selected, specification) => {
const { id, type } = selected;

switch (type) {
case "LOCAL":
storage.set("ide", "specification", id, 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 Expand Up @@ -114,7 +129,7 @@ const service = {
deleteProject,
getProjectServices,
getContext,
saveContext,
saveSpecification,
createSandbox,
getGraph,
getConfig,
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", "project", {
id: "21d2530b-4657-4ac0-b8cd-1a9f82786e32",
});
storage.set(
"ide",
"project",
"21d2530b-4657-4ac0-b8cd-1a9f82786e32",
Context.withBlank()
);
});

test("Resolve context with property", () => {
const state = contextReducer(Context.init(), {
type: "SET_SELECTED_API",
Expand Down
2 changes: 1 addition & 1 deletion src/utils/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const addSlashMark = (path) => {
};

const getRecentProject = () => {
const recentProject = storage.get("ide", "selected", "context");
const recentProject = storage.get("ide", "selected", "project");

if (recentProject) {
return recentProject;
Expand Down
6 changes: 3 additions & 3 deletions src/utils/test/Path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ test("returns false when the new path is not used", () => {
test("returns the selected project in storage", () => {
const projectId = "f1f04060-1ea4-46fc-bbf9-fb69c1faca8b";

storage.set("ide", "selected", "context", projectId);
storage.set("ide", "selected", "project", projectId);

expect(storage.set).toHaveBeenCalledWith(
"ide",
"selected",
"context",
"project",
projectId
);

Expand All @@ -80,7 +80,7 @@ test("returns the selected project in storage", () => {
});

test("returns null when the selected project is not found", () => {
storage.set("ide", "selected", "context", null);
storage.set("ide", "selected", "project", null);
const recentProject = Path.getRecentProject();
expect(recentProject).toEqual(null);
});
Expand Down
Loading
Loading