2024-06-18 17:23:04 +00:00
|
|
|
<script>
|
2024-06-20 08:07:55 +00:00
|
|
|
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 08:44:52 +00:00
|
|
|
import { functions } from '$lib/stores';
|
2024-06-20 08:07:55 +00:00
|
|
|
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 08:07:55 +00:00
|
|
|
let func = null;
|
2024-06-18 17:23:04 +00:00
|
|
|
|
|
|
|
|
const saveHandler = async (data) => {
|
|
|
|
|
console.log(data);
|
2024-06-20 08:07:55 +00:00
|
|
|
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) {
|
2024-06-20 08:07:55 +00:00
|
|
|
toast.success('Function updated successfully');
|
2024-06-20 08:44:52 +00:00
|
|
|
functions.set(await getFunctions(localStorage.token));
|
2024-06-18 17:23:04 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onMount(async () => {
|
|
|
|
|
console.log('mounted');
|
|
|
|
|
const id = $page.url.searchParams.get('id');
|
|
|
|
|
|
|
|
|
|
if (id) {
|
2024-06-20 08:07:55 +00:00
|
|
|
func = await getFunctionById(localStorage.token, id).catch((error) => {
|
2024-06-18 17:23:04 +00:00
|
|
|
toast.error(error);
|
2024-06-20 08:07:55 +00:00
|
|
|
goto('/workspace/functions');
|
2024-06-18 17:23:04 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
2024-06-20 08:07:55 +00:00
|
|
|
console.log(func);
|
2024-06-18 17:23:04 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
</script>
|
|
|
|
|
|
2024-06-20 08:07:55 +00:00
|
|
|
{#if func}
|
|
|
|
|
<FunctionEditor
|
2024-06-18 17:23:04 +00:00
|
|
|
edit={true}
|
2024-06-20 08:07:55 +00:00
|
|
|
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}
|