diff --git a/src/lib/api.ts b/src/lib/api.ts index e9e9d6a79..7e4477472 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -138,6 +138,19 @@ export default ( }, body: JSON.stringify(params) }).then(parseResponseJSON), + update: ( + token: string, + repositoryId: string, + params: { name: string; description?: string } + ): Promise => + fetch(getUrl(`projects/${repositoryId}.json`), { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + 'X-Auth-Token': token + }, + body: JSON.stringify(params) + }).then(parseResponseJSON), list: (token: string): Promise => fetch(getUrl('projects.json'), { method: 'GET', diff --git a/src/routes/projects/[projectId]/settings/+page.svelte b/src/routes/projects/[projectId]/settings/+page.svelte index 9074acba7..e9a95bedb 100644 --- a/src/routes/projects/[projectId]/settings/+page.svelte +++ b/src/routes/projects/[projectId]/settings/+page.svelte @@ -2,20 +2,22 @@ import { derived } from 'svelte/store'; import { Login } from '$lib/components'; import type { PageData } from './$types'; + import { log, toasts } from '$lib'; + import MdAutorenew from 'svelte-icons/md/MdAutorenew.svelte'; export let data: PageData; const { project, user, api } = data; - function repo_id(url: string) { + const repo_id = (url: string) => { const hurl = new URL(url); const path = hurl.pathname.split('/'); return path[path.length - 1]; - } + }; - function hostname(url: string) { + const hostname = (url: string) => { const hurl = new URL(url); return hurl.hostname; - } + }; const isSyncing = derived(project, (project) => project?.api?.sync); @@ -26,16 +28,54 @@ const target = event.target as HTMLInputElement; const sync = target.checked; - if (!$project.api) { - const apiProject = await api.projects.create($user.access_token, { - name: $project.title, - uid: $project.id - }); - await project.update({ api: { ...apiProject, sync } }); - } else { - await project.update({ api: { ...$project.api, sync } }); + try { + if (!$project.api) { + const apiProject = await api.projects.create($user.access_token, { + name: $project.title, + uid: $project.id + }); + await project.update({ api: { ...apiProject, sync } }); + } else { + await project.update({ api: { ...$project.api, sync } }); + } + } catch (error) { + target.checked = $project.api?.sync || false; + log.error(`Failed to update project sync status: ${error}`); + toasts.error('Failed to update project sync status'); } }; + + $: saving = false; + const onSubmit = async (e: SubmitEvent) => { + if (!$project) return; + if (!$user) return; + saving = true; + + const target = e.target as HTMLFormElement; + const formData = new FormData(target); + const name = formData.get('name') as string | undefined; + const description = formData.get('description') as string | undefined; + + console.log({ name, description }); + try { + if (name) { + const updated = await api.projects.update($user.access_token, $project?.api.repository_id, { + name, + description + }); + await project.update({ + title: name, + api: { ...updated, sync: $project?.api.sync || false } + }); + } + toasts.success('Project updated'); + } catch (e) { + log.error(e); + toasts.error('Failed to update project'); + } + + saving = false; + };
@@ -109,30 +149,59 @@
{/if} -
-
Path
-
- {$project?.path} -
-
-
-
Project Name
- - -
-
-
Project Description
- -