diff --git a/app/web_ui/src/lib/stores.test.ts b/app/web_ui/src/lib/stores.test.ts new file mode 100644 index 0000000..fa3bbb4 --- /dev/null +++ b/app/web_ui/src/lib/stores.test.ts @@ -0,0 +1,67 @@ +import { get } from "svelte/store" +import { projects, current_project } from "./stores" +import { describe, it, expect, beforeEach } from "vitest" + +const testProject = { + name: "Test Project", + path: "/test/path", + description: "Test Description", + created_at: new Date(), + created_by: "Test User", +} + +describe("stores", () => { + beforeEach(() => { + // Reset the projects store before each test + projects.set(null) + }) + + describe("projects store", () => { + it("should initialize with null", () => { + expect(get(projects)).toBeNull() + }) + + it("should update when set", () => { + const testProjects = { + projects: [testProject], + current_project_path: "/test/path", + error: null, + } + projects.set(testProjects) + expect(get(projects)).toEqual(testProjects) + }) + }) + + describe("current_project function", () => { + it("should return null when projects store is null", () => { + expect(current_project()).toBeNull() + }) + + it("should return null when current_project_path is null", () => { + projects.set({ + projects: [testProject], + current_project_path: null, + error: null, + }) + expect(current_project()).toBeNull() + }) + + it("should return null when no project matches current_project_path", () => { + projects.set({ + projects: [testProject], + current_project_path: "/non-existent/path", + error: null, + }) + expect(current_project()).toBeNull() + }) + + it("should return the correct project when it exists", () => { + projects.set({ + projects: [testProject], + current_project_path: "/test/path", + error: null, + }) + expect(current_project()).toEqual(testProject) + }) + }) +}) diff --git a/app/web_ui/src/lib/stores.ts b/app/web_ui/src/lib/stores.ts index 81e1cc4..61dd347 100644 --- a/app/web_ui/src/lib/stores.ts +++ b/app/web_ui/src/lib/stores.ts @@ -1,3 +1,59 @@ -import { writable } from "svelte/store" +import { writable, get } from "svelte/store" +import { post_error_handler, createKilnError } from "./utils/error_handlers" -export const current_project = writable(null) +type ProjectInfo = { + name: string + description: string + path: string + created_at: Date + created_by: string +} + +type AllProjects = { + projects: ProjectInfo[] + current_project_path: string | null + error: string | null +} + +export const projects = writable(null) + +export function current_project(): ProjectInfo | null { + const all_projects = get(projects) + + if (!all_projects) { + return null + } + const current_project_path = all_projects.current_project_path + if (!current_project_path) { + return null + } + const project = all_projects.projects.find( + (project) => project.path === current_project_path, + ) + if (!project) { + return null + } + return project +} + +export async function load_projects() { + try { + const response = await fetch("http://localhost:8757/api/projects") + const data = await response.json() + post_error_handler(response, data) + + const all_projects: AllProjects = { + projects: data.projects, + current_project_path: data.current_project_path, + error: null, + } + projects.set(all_projects) + } catch (error: unknown) { + const all_projects: AllProjects = { + projects: [], + current_project_path: null, + error: "Issue loading projects. " + createKilnError(error).getMessage(), + } + projects.set(all_projects) + } +} diff --git a/app/web_ui/src/lib/utils/json_schema_editor/json_schema_form_element.svelte b/app/web_ui/src/lib/utils/json_schema_editor/json_schema_form_element.svelte index b6ac624..e8e9d3f 100644 --- a/app/web_ui/src/lib/utils/json_schema_editor/json_schema_form_element.svelte +++ b/app/web_ui/src/lib/utils/json_schema_editor/json_schema_form_element.svelte @@ -17,7 +17,6 @@ }) // Trigger reactivity schema_model = schema_model - console.log(schema_model) // Scroll new item into view. Async to allow rendering first setTimeout(() => { const property = document.getElementById( diff --git a/app/web_ui/src/routes/(app)/+layout.svelte b/app/web_ui/src/routes/(app)/+layout.svelte index 54764e1..1a28e63 100644 --- a/app/web_ui/src/routes/(app)/+layout.svelte +++ b/app/web_ui/src/routes/(app)/+layout.svelte @@ -1,6 +1,5 @@
@@ -55,7 +54,6 @@
  • Sidebar Item 1
  • Sidebar Item 2
  • -
  • {$current_project}
  • diff --git a/app/web_ui/src/routes/(fullscreen)/setup/(setup)/create_project/edit_project.svelte b/app/web_ui/src/routes/(fullscreen)/setup/(setup)/create_project/edit_project.svelte index 4c1bb18..a46848e 100644 --- a/app/web_ui/src/routes/(fullscreen)/setup/(setup)/create_project/edit_project.svelte +++ b/app/web_ui/src/routes/(fullscreen)/setup/(setup)/create_project/edit_project.svelte @@ -1,6 +1,6 @@ +{#if loading || load_error} +
    + {#if load_error} + +

    Error loading projects

    +

    {load_error}

    + +
    + {:else} + + {/if} +
    +{/if} + {#if $navigating}