feat(documents) : add document type constants and updateDocument method
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -11,6 +11,7 @@ export interface Document {
|
||||
size: number
|
||||
fileUrl: string
|
||||
downloadUrl: string
|
||||
type?: string
|
||||
/** @deprecated Legacy Base64 data URI — use fileUrl instead */
|
||||
path?: string
|
||||
createdAt?: string
|
||||
@@ -32,6 +33,7 @@ export interface UploadContext {
|
||||
composantId?: string
|
||||
productId?: string
|
||||
pieceId?: string
|
||||
type?: string
|
||||
}
|
||||
|
||||
export interface DocumentResult {
|
||||
@@ -63,7 +65,7 @@ const extractTotal = (payload: unknown, fallbackLength: number): number => {
|
||||
}
|
||||
|
||||
export function useDocuments() {
|
||||
const { get, postFormData, delete: del } = useApi()
|
||||
const { get, patch, postFormData, delete: del } = useApi()
|
||||
const { showError, showSuccess } = useToast()
|
||||
|
||||
const loadFromEndpoint = async (
|
||||
@@ -218,6 +220,7 @@ export function useDocuments() {
|
||||
const formData = new FormData()
|
||||
formData.append('file', file)
|
||||
formData.append('name', file.name)
|
||||
if (context.type) formData.append('type', context.type)
|
||||
|
||||
if (context.siteId) formData.append('siteId', context.siteId)
|
||||
if (context.machineId) formData.append('machineId', context.machineId)
|
||||
@@ -280,6 +283,33 @@ export function useDocuments() {
|
||||
}
|
||||
}
|
||||
|
||||
const updateDocument = async (
|
||||
id: string,
|
||||
data: { name?: string; type?: string },
|
||||
): Promise<DocumentResult> => {
|
||||
loading.value = true
|
||||
try {
|
||||
const result = await patch(`/documents/${id}`, data)
|
||||
if (result.success && result.data) {
|
||||
const updated = result.data as Document
|
||||
const index = documents.value.findIndex((doc) => doc.id === id)
|
||||
if (index !== -1) {
|
||||
documents.value[index] = { ...documents.value[index], ...updated }
|
||||
}
|
||||
showSuccess('Document mis à jour')
|
||||
return { success: true, data: updated }
|
||||
}
|
||||
if (result.error) showError(result.error)
|
||||
return result as DocumentResult
|
||||
} catch (error) {
|
||||
const err = error as Error
|
||||
showError('Impossible de mettre à jour le document')
|
||||
return { success: false, error: err.message }
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
documents,
|
||||
total,
|
||||
@@ -292,6 +322,7 @@ export function useDocuments() {
|
||||
loadDocumentsByPiece,
|
||||
loadDocumentsByProduct,
|
||||
uploadDocuments,
|
||||
updateDocument,
|
||||
deleteDocument,
|
||||
}
|
||||
}
|
||||
|
||||
15
app/shared/documentTypes.ts
Normal file
15
app/shared/documentTypes.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
export const DOCUMENT_TYPES = [
|
||||
{ value: 'documentation', label: 'Documentation' },
|
||||
{ value: 'devis', label: 'Devis' },
|
||||
{ value: 'facture', label: 'Facture' },
|
||||
{ value: 'plan', label: 'Plan' },
|
||||
{ value: 'photo', label: 'Photo' },
|
||||
{ value: 'autre', label: 'Autre' },
|
||||
] as const
|
||||
|
||||
export type DocumentTypeValue = (typeof DOCUMENT_TYPES)[number]['value']
|
||||
|
||||
export const getDocumentTypeLabel = (value: string): string => {
|
||||
const found = DOCUMENT_TYPES.find((t) => t.value === value)
|
||||
return found?.label ?? value
|
||||
}
|
||||
Reference in New Issue
Block a user