open-webui/src/routes/(app)/workspace/functions/edit/+page.svelte

69 lines
1.5 KiB
Svelte
Raw Normal View History

2024-06-18 17:23:04 +00:00
<script>
import { toast } from 'svelte-sonner';
import { onMount } from 'svelte';
2024-06-18 17:23:04 +00:00
import { goto } from '$app/navigation';
import { page } from '$app/stores';
2024-06-20 11:51:51 +00:00
import { functions, models } from '$lib/stores';
import { updateFunctionById, getFunctions, getFunctionById } from '$lib/apis/functions';
import FunctionEditor from '$lib/components/workspace/Functions/FunctionEditor.svelte';
2024-06-18 17:23:04 +00:00
import Spinner from '$lib/components/common/Spinner.svelte';
2024-06-20 11:51:51 +00:00
import { getModels } from '$lib/apis';
2024-06-18 17:23:04 +00:00
let func = null;
2024-06-18 17:23:04 +00:00
const saveHandler = async (data) => {
console.log(data);
const res = await updateFunctionById(localStorage.token, func.id, {
2024-06-18 17:23:04 +00:00
id: data.id,
name: data.name,
meta: data.meta,
content: data.content
}).catch((error) => {
toast.error(error);
return null;
});
if (res) {
toast.success('Function updated successfully');
2024-06-20 08:44:52 +00:00
functions.set(await getFunctions(localStorage.token));
2024-06-20 11:51:51 +00:00
models.set(await getModels(localStorage.token));
2024-06-18 17:23:04 +00:00
}
};
onMount(async () => {
console.log('mounted');
const id = $page.url.searchParams.get('id');
if (id) {
func = await getFunctionById(localStorage.token, id).catch((error) => {
2024-06-18 17:23:04 +00:00
toast.error(error);
goto('/workspace/functions');
2024-06-18 17:23:04 +00:00
return null;
});
console.log(func);
2024-06-18 17:23:04 +00:00
}
});
</script>
{#if func}
<FunctionEditor
2024-06-18 17:23:04 +00:00
edit={true}
id={func.id}
name={func.name}
meta={func.meta}
content={func.content}
2024-06-18 17:23:04 +00:00
on:save={(e) => {
saveHandler(e.detail);
}}
/>
{:else}
<div class="flex items-center justify-center h-full">
<div class=" pb-16">
<Spinner />
</div>
</div>
{/if}