Skip to content

Commit

Permalink
Store the current project, so it can be used anywhere in the app. Inc…
Browse files Browse the repository at this point in the history
…luding saving on creating.
  • Loading branch information
scosman committed Oct 7, 2024
1 parent 790ac01 commit 825e6bc
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 11 deletions.
3 changes: 3 additions & 0 deletions app/web_ui/src/lib/stores.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { writable } from "svelte/store"

export const current_project = writable<string | null>(null)
2 changes: 2 additions & 0 deletions app/web_ui/src/routes/(app)/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script>
import "../../app.css"
import { current_project } from "$lib/stores"
</script>

<div class="drawer lg:drawer-open">
Expand Down Expand Up @@ -54,6 +55,7 @@
</li>
<li><a href="/?1">Sidebar Item 1</a></li>
<li><a href="/?2">Sidebar Item 2</a></li>
<li>{$current_project}</li>
</ul>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import { goto } from "$app/navigation"
import { current_project } from "$lib/stores"
import FormContainer from "$lib/utils/form_container.svelte"
import FormElement from "$lib/utils/form_element.svelte"
Expand Down Expand Up @@ -33,10 +34,11 @@
data["message"] || "Unknown error (status: " + response.status + ")",
)
}
current_project.set(data["project_path"])
custom_error_message = null
if (redirect_on_created) {
goto(redirect_on_created)
}
custom_error_message = null
created = true
} catch (error) {
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import FormElement from "$lib/utils/form_element.svelte"
import FormList from "$lib/utils/form_list.svelte"
import FormContainer from "$lib/utils/form_container.svelte"
import { current_project } from "$lib/stores"
// Prevents flash of complete UI if we're going to redirect
export let redirect_on_created: string | null = null
Expand All @@ -11,14 +12,27 @@
export let task_description: string = ""
export let task_instructions: string = ""
export let task_requirements: TaskRequirement[] = []
let custom_error_message: string | null = null
let submitting = false
// Warn before unload if there's any user input
$: warn_before_unload =
[task_name, task_description, task_instructions].some((value) => !!value) ||
task_requirements.some((req) => !!req.name || !!req.instructions)
function create_task() {
console.log("TODO_P0 redirect_on_created", redirect_on_created)
try {
if (!$current_project) {
custom_error_message =
"You must create a project before creating a task"
return
}
console.log("TODO_P0 redirect_on_created", redirect_on_created)
} catch (error) {
custom_error_message = "Unknown error creating task: " + error
} finally {
submitting = false
}
}
</script>

Expand All @@ -27,6 +41,8 @@
submit_label="Create Task"
on:submit={create_task}
bind:warn_before_unload
bind:custom_error_message
bind:submitting
>
<div class="text-xl font-bold">Part 1: Overview</div>
<FormElement
Expand Down
11 changes: 5 additions & 6 deletions app/web_ui/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
import { slide } from "svelte/transition"
import { onMount } from "svelte"
import { goto } from "$app/navigation"
import { page } from "$app/stores"
import { current_project } from "$lib/stores"
const check_needs_setup = async () => {
if ($page.url.pathname.startsWith("/setup")) {
// We're already on a setup page, no need to redirect
return
}
try {
let res = await fetch("http://localhost:8757/api/settings")
let data = await res.json()
let projects = data["projects"]
let current_project_path = data["current_project"]
if (!projects || projects.length === 0) {
goto("/setup")
} else {
// Set the current_project to the current project, or first project
current_project.set(current_project_path || projects[0])
}
} catch (e) {
console.error("check_needs_setup error", e)
Expand Down
5 changes: 4 additions & 1 deletion libs/studio/kiln_studio/project_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,8 @@ async def create_project(project: dict):

return JSONResponse(
status_code=200,
content={"message": "Project created successfully"},
content={
"message": "Project created successfully",
"project_path": project_file,
},
)
13 changes: 11 additions & 2 deletions libs/studio/kiln_studio/test_project_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from libs.core.kiln_ai.utils.config import Config
from libs.studio.kiln_studio.project_management import (
connect_project_management,
default_project_path,
)


Expand Down Expand Up @@ -37,7 +38,12 @@ def test_create_project_success(client):
},
)

assert response.json() == {"message": "Project created successfully"}
assert response.json() == {
"message": "Project created successfully",
"project_path": os.path.join(
default_project_path(), "Test Project", "project.json"
),
}
assert response.status_code == 200


Expand Down Expand Up @@ -93,7 +99,10 @@ def test_create_and_load_project(client):
)

assert response.status_code == 200
assert response.json() == {"message": "Project created successfully"}
assert response.json() == {
"message": "Project created successfully",
"project_path": os.path.join(temp_dir, "Test Project", "project.json"),
}

# Verify the project file was created
project_path = os.path.join(temp_dir, "Test Project")
Expand Down

0 comments on commit 825e6bc

Please sign in to comment.