-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better store of project data, and new /api/projects endpoint
- Loading branch information
Showing
9 changed files
with
308 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string | null>(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<AllProjects | null>(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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.