You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.4 KiB
60 lines
1.4 KiB
<script lang="ts" context="module">
|
|
const contextKey = {ctx: "projectCtx"};
|
|
|
|
interface ProjectContextData {
|
|
project: Readable<Project>,
|
|
reloadProject(): Promise<void>,
|
|
};
|
|
|
|
const fallback: ProjectContextData = {
|
|
project: readable({requirements: []} as Project),
|
|
reloadProject: () => Promise.resolve()
|
|
};
|
|
|
|
export function getProjectContext() {
|
|
return getContext(contextKey) as ProjectContextData || fallback
|
|
}
|
|
</script>
|
|
|
|
<script lang="ts">
|
|
import type Project from "$lib/models/project";
|
|
import { readable, writable, type Readable } from "svelte/store";
|
|
import { getContext, setContext } from "svelte";
|
|
import { sl3 } from "$lib/clients/sl3";
|
|
import { getScopeContext } from "./ScopeContext.svelte";
|
|
|
|
export let project: Project;
|
|
|
|
const {scope} = getScopeContext();
|
|
|
|
let projectWritable = writable<Project>(project);
|
|
let lastSet = project;
|
|
let loading = false;
|
|
|
|
setContext<ProjectContextData>(contextKey, {
|
|
project: {subscribe: projectWritable.subscribe},
|
|
reloadProject,
|
|
});
|
|
|
|
async function reloadProject() {
|
|
if (loading) {
|
|
return
|
|
}
|
|
|
|
try {
|
|
const newProject = await sl3(fetch).findProject($scope.id, project.id)
|
|
projectWritable.set(newProject);
|
|
} catch(e) {
|
|
console.warn("Failed to reload project:", e);
|
|
}
|
|
|
|
loading = false;
|
|
}
|
|
|
|
$: if (project !== lastSet) {
|
|
lastSet = project;
|
|
projectWritable.set(project);
|
|
}
|
|
</script>
|
|
|
|
<slot></slot>
|