|
| 1 | +import { useCallback } from 'react' |
| 2 | +import type { BatchPersistOptions, PersistenceResult, NotificationDetail } from '@contember/bindx' |
| 3 | +import { usePersist, type PersistApi } from './usePersist.js' |
| 4 | +import { useNotificationStore } from './BackendAdapterContext.js' |
| 5 | + |
| 6 | +/** |
| 7 | + * Extended persist API that automatically shows notifications on success/failure. |
| 8 | + */ |
| 9 | +export interface PersistWithFeedbackApi extends PersistApi { |
| 10 | + /** Persist all dirty entities with automatic toast feedback */ |
| 11 | + persistAllWithFeedback(options?: BatchPersistOptions): Promise<PersistenceResult> |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Wraps `usePersist()` with automatic notification feedback. |
| 16 | + * |
| 17 | + * - On success: shows a success notification |
| 18 | + * - On failure with server errors: shows an error notification with details |
| 19 | + * - On failure with client validation errors: shows a warning notification |
| 20 | + * |
| 21 | + * The raw `usePersist()` methods are still available for manual control. |
| 22 | + * |
| 23 | + * @example |
| 24 | + * ```tsx |
| 25 | + * function SaveButton() { |
| 26 | + * const { persistAllWithFeedback, isPersisting, isDirty } = usePersistWithFeedback() |
| 27 | + * return ( |
| 28 | + * <button onClick={() => persistAllWithFeedback()} disabled={isPersisting || !isDirty}> |
| 29 | + * Save |
| 30 | + * </button> |
| 31 | + * ) |
| 32 | + * } |
| 33 | + * ``` |
| 34 | + */ |
| 35 | +export function usePersistWithFeedback(): PersistWithFeedbackApi { |
| 36 | + const persistApi = usePersist() |
| 37 | + const notificationStore = useNotificationStore() |
| 38 | + |
| 39 | + const persistAllWithFeedback = useCallback( |
| 40 | + async (options?: BatchPersistOptions): Promise<PersistenceResult> => { |
| 41 | + const result = await persistApi.persistAll(options) |
| 42 | + |
| 43 | + if (result.success) { |
| 44 | + notificationStore.add({ |
| 45 | + type: 'success', |
| 46 | + message: 'Changes saved successfully', |
| 47 | + source: 'persist', |
| 48 | + dismissAfter: 6_000, |
| 49 | + }) |
| 50 | + } else { |
| 51 | + const details: NotificationDetail[] = [] |
| 52 | + let hasClientErrors = false |
| 53 | + |
| 54 | + for (const entityResult of result.results) { |
| 55 | + if (entityResult.success) continue |
| 56 | + |
| 57 | + if (entityResult.error) { |
| 58 | + // Server error |
| 59 | + details.push({ |
| 60 | + entityType: entityResult.entityType, |
| 61 | + message: entityResult.error.message, |
| 62 | + }) |
| 63 | + |
| 64 | + // Add field-level details from mutation result |
| 65 | + if (entityResult.error.mutationResult) { |
| 66 | + for (const err of entityResult.error.mutationResult.errors) { |
| 67 | + details.push({ message: err.message }) |
| 68 | + } |
| 69 | + for (const err of entityResult.error.mutationResult.validation.errors) { |
| 70 | + details.push({ message: err.message.text }) |
| 71 | + } |
| 72 | + } |
| 73 | + } else { |
| 74 | + // No server error means blocked by client validation |
| 75 | + hasClientErrors = true |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (hasClientErrors && details.length === 0) { |
| 80 | + notificationStore.add({ |
| 81 | + type: 'warning', |
| 82 | + message: 'Please fix validation errors before saving', |
| 83 | + source: 'persist', |
| 84 | + dismissAfter: 15_000, |
| 85 | + }) |
| 86 | + } else { |
| 87 | + notificationStore.add({ |
| 88 | + type: 'error', |
| 89 | + message: 'Failed to save changes', |
| 90 | + details: details.length > 0 ? details : undefined, |
| 91 | + source: 'persist', |
| 92 | + dismissAfter: 60_000, |
| 93 | + }) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return result |
| 98 | + }, |
| 99 | + [persistApi, notificationStore], |
| 100 | + ) |
| 101 | + |
| 102 | + return { |
| 103 | + ...persistApi, |
| 104 | + persistAllWithFeedback, |
| 105 | + } |
| 106 | +} |
0 commit comments