feat: edit an existing custom type

This commit is contained in:
Florian Sylvain committed 2026-10-20 04:42:50 +02:00
1 parent 083cc9e23e
commit 8e344b2500
57 files changed
+5648 -425

No files matched your search

+30 -2
View File
@@ -11,6 +11,9 @@ import { ApiError, NetworkError, useApi } from '@/composables/useApi'
import { useContentTypesStore } from '@/stores/contentTypes'
import { useSessionStore } from '@/stores/session'
import { DESKTOP_WIDTH, setViewportWidth } from '@/components/__tests__/viewport'
import { eventsFields, recipesFields } from '@/lib/__tests__/schemaFixtures'
export { apiField, eventsFields, recipesFields } from '@/lib/__tests__/schemaFixtures'
export type Responder = () => unknown
@@ -20,7 +23,7 @@ export const customTypes = [
key: 'events',
name: 'Events',
icon: 'files',
fields: new Array(7),
fields: eventsFields,
updated_at: '2026-10-15T09:30:00Z',
},
{
@@ -28,7 +31,7 @@ export const customTypes = [
key: 'recipes',
name: 'Recipes',
icon: null,
fields: new Array(4),
fields: recipesFields,
updated_at: '2026-10-02T09:30:00Z',
},
{
@@ -117,9 +120,17 @@ export interface PostedRequest {
body: Record<string, unknown>
}
export const entryCounts: Record<string, number> = { events: 37, recipes: 0 }
export function entryPage(total: number) {
return { items: [], total, page: 1, size: 1 }
}
export interface SchemaMount {
role?: string
create?: (body: PostedRequest['body']) => unknown
update?: (body: PostedRequest['body'], key: string) => unknown
entries?: (key: string) => unknown
width?: number
content?: Responder
builtin?: Responder
@@ -130,10 +141,17 @@ export interface SchemaMount {
let wrapper: VueWrapper | undefined
export const calls: string[] = []
export const posts: PostedRequest[] = []
export const puts: PostedRequest[] = []
export function updatedType(key: string, body: Record<string, unknown>) {
const base = customTypes.find((type) => type.key === key)!
return { ...base, ...body, key, updated_at: '2026-10-19T11:02:00Z' }
}
export async function mountAt(path: string, options: SchemaMount = {}, shellOnly = false) {
calls.length = 0
posts.length = 0
puts.length = 0
localStorage.clear()
setViewportWidth(options.width ?? DESKTOP_WIDTH)
const pinia = createPinia()
@@ -155,6 +173,12 @@ export async function mountAt(path: string, options: SchemaMount = {}, shellOnly
}
session.status = 'ready'
const request = vi.fn(async (url: string, init?: RequestInit) => {
if (init?.method === 'PUT') {
const body = JSON.parse(String(init.body)) as PostedRequest['body']
const key = url.split('/').pop()!
puts.push({ url, body })
return (options.update ?? ((sent, target) => updatedType(target, sent)))(body, key)
}
if (init?.method === 'POST') {
const body = JSON.parse(String(init.body)) as PostedRequest['body']
posts.push({ url, body })
@@ -164,6 +188,10 @@ export async function mountAt(path: string, options: SchemaMount = {}, shellOnly
if (url === '/v1/content-type') {
return (options.content ?? (() => [...customTypes]))()
}
const counted = url.match(/^\/v1\/content-type\/([^/]+)\/entry\?page=1&size=5$/)
if (counted) {
return (options.entries ?? ((key) => entryPage(entryCounts[key] ?? 0)))(counted[1]!)
}
if (url === '/v1/builtin-type') {
return (options.builtin ?? (() => builtinTypes))()
}