2026-09-03 18:59:51 +01:00
|
|
|
<script setup lang="ts">
|
2026-09-03 19:10:31 +01:00
|
|
|
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
|
|
|
|
|
import { useRoute, useRouter } from 'vue-router'
|
2026-09-03 18:59:51 +01:00
|
|
|
import draggable from 'vuedraggable'
|
2026-09-04 11:28:59 +01:00
|
|
|
import CardRow from '../components/CardRow.vue'
|
2026-09-03 18:59:51 +01:00
|
|
|
import { ApiError, apiRequest } from '../lib/api'
|
2026-09-04 11:28:59 +01:00
|
|
|
import { useCardsStore } from '../stores/cards'
|
|
|
|
|
import { useProjectsStore } from '../stores/projects'
|
|
|
|
|
import type { Card, Project } from '../types'
|
2026-09-03 18:59:51 +01:00
|
|
|
|
|
|
|
|
const route = useRoute()
|
2026-09-03 19:10:31 +01:00
|
|
|
const router = useRouter()
|
2026-09-04 11:28:59 +01:00
|
|
|
const cards = useCardsStore()
|
|
|
|
|
const projects = useProjectsStore()
|
2026-09-03 18:59:51 +01:00
|
|
|
|
2026-09-04 11:28:59 +01:00
|
|
|
const projectId = Number(route.params.id)
|
|
|
|
|
const project = ref<Project | null>(null)
|
2026-09-03 18:59:51 +01:00
|
|
|
const loadError = ref<string | null>(null)
|
|
|
|
|
const actionError = ref<string | null>(null)
|
|
|
|
|
|
2026-09-03 19:10:31 +01:00
|
|
|
const titleDraft = ref('')
|
|
|
|
|
const descriptionDraft = ref('')
|
|
|
|
|
|
|
|
|
|
const menuOpen = ref(false)
|
|
|
|
|
const confirmingDelete = ref(false)
|
|
|
|
|
const deleting = ref(false)
|
|
|
|
|
const deleteError = ref<string | null>(null)
|
|
|
|
|
const cancelButton = ref<HTMLButtonElement>()
|
|
|
|
|
|
2026-09-03 18:59:51 +01:00
|
|
|
const newText = ref('')
|
|
|
|
|
const submitting = ref(false)
|
|
|
|
|
|
|
|
|
|
const summary = computed(() => {
|
2026-09-04 11:28:59 +01:00
|
|
|
const total = cards.cards.length
|
|
|
|
|
if (total === 0) return 'No cards yet.'
|
|
|
|
|
return `${cards.completedCount()} of ${total} done.`
|
2026-09-03 18:59:51 +01:00
|
|
|
})
|
|
|
|
|
|
2026-09-04 11:28:59 +01:00
|
|
|
watch(project, (value) => {
|
2026-09-03 19:10:31 +01:00
|
|
|
if (value) {
|
|
|
|
|
titleDraft.value = value.title
|
|
|
|
|
descriptionDraft.value = value.description
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
watch(confirmingDelete, async (open) => {
|
|
|
|
|
if (open) {
|
|
|
|
|
await nextTick()
|
|
|
|
|
cancelButton.value?.focus()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
void load()
|
|
|
|
|
window.addEventListener('keydown', onKeydown)
|
|
|
|
|
})
|
|
|
|
|
onBeforeUnmount(() => window.removeEventListener('keydown', onKeydown))
|
|
|
|
|
|
|
|
|
|
function onKeydown(event: KeyboardEvent) {
|
|
|
|
|
if (event.key !== 'Escape') return
|
|
|
|
|
if (confirmingDelete.value) confirmingDelete.value = false
|
|
|
|
|
else if (menuOpen.value) menuOpen.value = false
|
|
|
|
|
}
|
2026-09-03 18:59:51 +01:00
|
|
|
|
|
|
|
|
async function load() {
|
|
|
|
|
loadError.value = null
|
|
|
|
|
try {
|
2026-09-04 11:28:59 +01:00
|
|
|
const [{ project: fetched }] = await Promise.all([
|
|
|
|
|
apiRequest<{ project: Project }>(`/projects/${projectId}`, { auth: true }),
|
|
|
|
|
cards.load(projectId),
|
2026-09-03 18:59:51 +01:00
|
|
|
])
|
2026-09-04 11:28:59 +01:00
|
|
|
project.value = fetched
|
2026-09-03 18:59:51 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
if (e instanceof ApiError && e.status === 404) {
|
2026-09-04 11:28:59 +01:00
|
|
|
loadError.value = 'That project does not exist.'
|
2026-09-03 18:59:51 +01:00
|
|
|
} else {
|
2026-09-04 11:28:59 +01:00
|
|
|
loadError.value = e instanceof ApiError ? e.message : 'Could not load the project.'
|
2026-09-03 18:59:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-04 11:28:59 +01:00
|
|
|
async function patchProject(fields: { title?: string; description?: string }) {
|
2026-09-03 19:10:31 +01:00
|
|
|
actionError.value = null
|
|
|
|
|
try {
|
2026-09-04 11:28:59 +01:00
|
|
|
const { project: updated } = await apiRequest<{ project: Project }>(`/projects/${projectId}`, {
|
2026-09-03 19:10:31 +01:00
|
|
|
method: 'PATCH',
|
|
|
|
|
auth: true,
|
|
|
|
|
body: fields,
|
|
|
|
|
})
|
2026-09-04 11:28:59 +01:00
|
|
|
project.value = updated
|
2026-09-03 19:10:31 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
actionError.value = e instanceof ApiError ? e.message : 'Could not save the change.'
|
2026-09-04 11:28:59 +01:00
|
|
|
if (project.value) {
|
|
|
|
|
titleDraft.value = project.value.title
|
|
|
|
|
descriptionDraft.value = project.value.description
|
2026-09-03 19:10:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function saveTitle() {
|
2026-09-04 11:28:59 +01:00
|
|
|
if (!project.value) return
|
2026-09-03 19:10:31 +01:00
|
|
|
const next = titleDraft.value.trim()
|
|
|
|
|
if (next === '') {
|
2026-09-04 11:28:59 +01:00
|
|
|
titleDraft.value = project.value.title // title is required
|
2026-09-03 19:10:31 +01:00
|
|
|
return
|
|
|
|
|
}
|
2026-09-04 11:28:59 +01:00
|
|
|
if (next !== project.value.title) void patchProject({ title: next })
|
2026-09-03 19:10:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function saveDescription() {
|
2026-09-04 11:28:59 +01:00
|
|
|
if (!project.value) return
|
2026-09-03 19:10:31 +01:00
|
|
|
const next = descriptionDraft.value.trim()
|
2026-09-04 11:28:59 +01:00
|
|
|
if (next !== project.value.description) void patchProject({ description: next })
|
2026-09-03 19:10:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function askDelete() {
|
|
|
|
|
menuOpen.value = false
|
|
|
|
|
deleteError.value = null
|
|
|
|
|
confirmingDelete.value = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function confirmDelete() {
|
|
|
|
|
deleting.value = true
|
|
|
|
|
deleteError.value = null
|
|
|
|
|
try {
|
2026-09-04 11:28:59 +01:00
|
|
|
await apiRequest(`/projects/${projectId}`, { method: 'DELETE', auth: true })
|
|
|
|
|
projects.reset()
|
|
|
|
|
cards.reset()
|
2026-09-03 19:10:31 +01:00
|
|
|
await router.push('/')
|
|
|
|
|
} catch (e) {
|
2026-09-04 11:28:59 +01:00
|
|
|
deleteError.value = e instanceof ApiError ? e.message : 'Could not delete the project.'
|
2026-09-03 19:10:31 +01:00
|
|
|
deleting.value = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-03 18:59:51 +01:00
|
|
|
/** Run a store mutation, surfacing failures and resyncing from the server. */
|
|
|
|
|
async function run(op: Promise<unknown>) {
|
|
|
|
|
actionError.value = null
|
|
|
|
|
try {
|
|
|
|
|
await op
|
|
|
|
|
} catch (e) {
|
|
|
|
|
actionError.value = e instanceof ApiError ? e.message : 'Something went wrong.'
|
2026-09-04 11:28:59 +01:00
|
|
|
await cards.load(projectId)
|
2026-09-03 18:59:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onReorder(event: { oldIndex?: number; newIndex?: number }) {
|
|
|
|
|
if (event.oldIndex === event.newIndex) return
|
2026-09-04 11:28:59 +01:00
|
|
|
void run(cards.persistOrder())
|
2026-09-03 18:59:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function onCreate() {
|
|
|
|
|
submitting.value = true
|
|
|
|
|
actionError.value = null
|
|
|
|
|
try {
|
2026-09-04 11:28:59 +01:00
|
|
|
await cards.add(newText.value)
|
2026-09-03 18:59:51 +01:00
|
|
|
newText.value = ''
|
|
|
|
|
} catch (e) {
|
2026-09-04 11:28:59 +01:00
|
|
|
actionError.value = e instanceof ApiError ? e.message : 'Could not add the card.'
|
2026-09-03 18:59:51 +01:00
|
|
|
} finally {
|
|
|
|
|
submitting.value = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
|
|
|
|
<section class="card">
|
2026-09-04 11:28:59 +01:00
|
|
|
<p><RouterLink to="/">← All projects</RouterLink></p>
|
2026-09-03 18:59:51 +01:00
|
|
|
|
|
|
|
|
<p v-if="loadError" class="form-error">{{ loadError }}</p>
|
|
|
|
|
|
2026-09-04 11:28:59 +01:00
|
|
|
<template v-else-if="project">
|
|
|
|
|
<div class="project-head">
|
|
|
|
|
<h1 class="project-head__title">
|
2026-09-03 19:10:31 +01:00
|
|
|
<input
|
|
|
|
|
v-model="titleDraft"
|
|
|
|
|
type="text"
|
|
|
|
|
maxlength="255"
|
2026-09-04 11:28:59 +01:00
|
|
|
aria-label="Project title"
|
2026-09-03 19:10:31 +01:00
|
|
|
@blur="saveTitle"
|
|
|
|
|
@keyup.enter="($event.target as HTMLInputElement).blur()"
|
|
|
|
|
/>
|
|
|
|
|
</h1>
|
|
|
|
|
|
|
|
|
|
<div class="menu">
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
class="menu__toggle"
|
|
|
|
|
aria-haspopup="true"
|
|
|
|
|
:aria-expanded="menuOpen"
|
|
|
|
|
@click="menuOpen = !menuOpen"
|
|
|
|
|
>
|
|
|
|
|
Manage ▾
|
|
|
|
|
</button>
|
|
|
|
|
|
|
|
|
|
<template v-if="menuOpen">
|
|
|
|
|
<div class="menu__backdrop" @click="menuOpen = false" />
|
|
|
|
|
<ul class="menu__list" role="menu">
|
|
|
|
|
<li role="none">
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
role="menuitem"
|
|
|
|
|
class="menu__item menu__item--danger"
|
|
|
|
|
@click="askDelete"
|
|
|
|
|
>
|
2026-09-04 11:28:59 +01:00
|
|
|
Delete project
|
2026-09-03 19:10:31 +01:00
|
|
|
</button>
|
|
|
|
|
</li>
|
|
|
|
|
</ul>
|
|
|
|
|
</template>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<textarea
|
|
|
|
|
v-model="descriptionDraft"
|
2026-09-04 11:28:59 +01:00
|
|
|
class="project-head__desc"
|
2026-09-03 19:10:31 +01:00
|
|
|
rows="2"
|
|
|
|
|
maxlength="2000"
|
|
|
|
|
placeholder="Add a description"
|
2026-09-04 11:28:59 +01:00
|
|
|
aria-label="Project description"
|
2026-09-03 19:10:31 +01:00
|
|
|
@blur="saveDescription"
|
|
|
|
|
/>
|
2026-09-03 18:59:51 +01:00
|
|
|
|
|
|
|
|
<p class="muted">{{ summary }}</p>
|
|
|
|
|
<p v-if="actionError" class="form-error">{{ actionError }}</p>
|
|
|
|
|
|
2026-09-04 11:28:59 +01:00
|
|
|
<p v-if="cards.loading && !cards.loaded" class="muted">Loading…</p>
|
2026-09-03 18:59:51 +01:00
|
|
|
|
|
|
|
|
<draggable
|
2026-09-04 11:28:59 +01:00
|
|
|
v-else-if="cards.cards.length"
|
|
|
|
|
:list="cards.cards"
|
2026-09-03 18:59:51 +01:00
|
|
|
item-key="id"
|
|
|
|
|
tag="ul"
|
2026-09-04 11:28:59 +01:00
|
|
|
class="cards"
|
|
|
|
|
handle=".card-row__handle"
|
|
|
|
|
ghost-class="card-row--ghost"
|
2026-09-03 18:59:51 +01:00
|
|
|
:animation="150"
|
|
|
|
|
@end="onReorder"
|
|
|
|
|
>
|
2026-09-04 11:28:59 +01:00
|
|
|
<template #item="{ element }: { element: Card }">
|
|
|
|
|
<CardRow
|
|
|
|
|
:card="element"
|
|
|
|
|
@toggle="(v) => run(cards.setComplete(element, v))"
|
|
|
|
|
@save-text="(v) => run(cards.setText(element, v))"
|
|
|
|
|
@delete="run(cards.remove(element))"
|
2026-09-03 18:59:51 +01:00
|
|
|
/>
|
|
|
|
|
</template>
|
|
|
|
|
</draggable>
|
|
|
|
|
|
2026-09-04 11:28:59 +01:00
|
|
|
<form class="form form--new-card" @submit.prevent="onCreate">
|
2026-09-03 18:59:51 +01:00
|
|
|
<label>
|
2026-09-04 11:28:59 +01:00
|
|
|
<span>New card</span>
|
2026-09-03 18:59:51 +01:00
|
|
|
<input v-model="newText" type="text" maxlength="1000" required />
|
|
|
|
|
</label>
|
|
|
|
|
<button type="submit" :disabled="submitting">
|
2026-09-04 11:28:59 +01:00
|
|
|
{{ submitting ? 'Adding…' : 'Add card' }}
|
2026-09-03 18:59:51 +01:00
|
|
|
</button>
|
|
|
|
|
</form>
|
|
|
|
|
</template>
|
|
|
|
|
</section>
|
2026-09-03 19:10:31 +01:00
|
|
|
|
|
|
|
|
<div
|
|
|
|
|
v-if="confirmingDelete"
|
|
|
|
|
class="modal"
|
|
|
|
|
role="dialog"
|
|
|
|
|
aria-modal="true"
|
|
|
|
|
aria-labelledby="confirm-delete-title"
|
|
|
|
|
>
|
|
|
|
|
<div class="modal__backdrop" @click="confirmingDelete = false" />
|
|
|
|
|
<div class="modal__dialog">
|
2026-09-04 11:28:59 +01:00
|
|
|
<h2 id="confirm-delete-title">Delete this project?</h2>
|
2026-09-03 19:10:31 +01:00
|
|
|
<p class="muted">
|
2026-09-04 11:28:59 +01:00
|
|
|
“{{ project?.title }}” and its {{ cards.cards.length }}
|
|
|
|
|
card{{ cards.cards.length === 1 ? '' : 's' }} will be permanently deleted.
|
2026-09-03 19:10:31 +01:00
|
|
|
</p>
|
|
|
|
|
<p v-if="deleteError" class="form-error">{{ deleteError }}</p>
|
|
|
|
|
<div class="modal__actions">
|
|
|
|
|
<button
|
|
|
|
|
ref="cancelButton"
|
|
|
|
|
type="button"
|
|
|
|
|
class="btn-secondary"
|
|
|
|
|
:disabled="deleting"
|
|
|
|
|
@click="confirmingDelete = false"
|
|
|
|
|
>
|
|
|
|
|
Cancel
|
|
|
|
|
</button>
|
|
|
|
|
<button type="button" class="btn-danger" :disabled="deleting" @click="confirmDelete">
|
2026-09-04 11:28:59 +01:00
|
|
|
{{ deleting ? 'Deleting…' : 'Delete project' }}
|
2026-09-03 19:10:31 +01:00
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-09-03 18:59:51 +01:00
|
|
|
</template>
|