@open-mercato/ui 0.6.6-develop.6205.1.109e4b6a84 → 0.6.6-develop.6228.1.cf483c68b5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/backend/CrudForm.js +12 -0
- package/dist/backend/CrudForm.js.map +2 -2
- package/dist/backend/notifications/useNotificationActions.js +48 -20
- package/dist/backend/notifications/useNotificationActions.js.map +2 -2
- package/dist/backend/utils/customFieldForms.js +32 -0
- package/dist/backend/utils/customFieldForms.js.map +2 -2
- package/dist/primitives/tag-input.js +6 -2
- package/dist/primitives/tag-input.js.map +2 -2
- package/package.json +3 -3
- package/src/backend/CrudForm.tsx +12 -0
- package/src/backend/__tests__/CrudForm.custom-fields.test.tsx +67 -0
- package/src/backend/__tests__/custom-field-filters.test.ts +12 -0
- package/src/backend/__tests__/custom-field-forms.test.ts +29 -0
- package/src/backend/notifications/__tests__/useNotificationActions.failedWrites.test.tsx +8 -2
- package/src/backend/notifications/__tests__/useNotificationActions.test.tsx +147 -0
- package/src/backend/notifications/useNotificationActions.ts +58 -20
- package/src/backend/utils/customFieldForms.ts +34 -0
- package/src/primitives/__tests__/tag-input.test.tsx +7 -0
- package/src/primitives/tag-input.tsx +11 -1
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
import { act, renderHook } from '@testing-library/react'
|
|
5
|
+
import type { NotificationDto } from '@open-mercato/shared/modules/notifications/types'
|
|
6
|
+
|
|
7
|
+
const apiCallMock = jest.fn()
|
|
8
|
+
const apiCallOrThrowMock = jest.fn()
|
|
9
|
+
const runMutationMock = jest.fn()
|
|
10
|
+
const retryLastMutation = jest.fn(async () => true)
|
|
11
|
+
|
|
12
|
+
jest.mock('../../utils/apiCall', () => ({
|
|
13
|
+
apiCall: (...args: unknown[]) => apiCallMock(...args),
|
|
14
|
+
apiCallOrThrow: (...args: unknown[]) => apiCallOrThrowMock(...args),
|
|
15
|
+
}))
|
|
16
|
+
|
|
17
|
+
jest.mock('../../injection/useGuardedMutation', () => ({
|
|
18
|
+
useGuardedMutation: () => ({
|
|
19
|
+
runMutation: (...args: unknown[]) => runMutationMock(...args),
|
|
20
|
+
retryLastMutation,
|
|
21
|
+
}),
|
|
22
|
+
}))
|
|
23
|
+
|
|
24
|
+
jest.mock('@open-mercato/shared/lib/i18n/context', () => ({
|
|
25
|
+
useT: () => (key: string, fallback?: string) => fallback ?? key,
|
|
26
|
+
}))
|
|
27
|
+
|
|
28
|
+
import { useNotificationActions } from '../useNotificationActions'
|
|
29
|
+
|
|
30
|
+
type RunMutationInput = {
|
|
31
|
+
context: { resourceKind: string; retryLastMutation: () => Promise<boolean> }
|
|
32
|
+
mutationPayload: Record<string, unknown>
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function makeNotification(id: string, status: 'unread' | 'read' = 'unread'): NotificationDto {
|
|
36
|
+
return {
|
|
37
|
+
id,
|
|
38
|
+
type: 'example',
|
|
39
|
+
title: `title-${id}`,
|
|
40
|
+
severity: 'info',
|
|
41
|
+
status,
|
|
42
|
+
actions: [],
|
|
43
|
+
createdAt: new Date().toISOString(),
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function renderActions(initial: NotificationDto[] = []) {
|
|
48
|
+
const setNotifications = jest.fn()
|
|
49
|
+
const setUnreadCount = jest.fn()
|
|
50
|
+
const { result } = renderHook(() =>
|
|
51
|
+
useNotificationActions(initial, setNotifications, setUnreadCount),
|
|
52
|
+
)
|
|
53
|
+
return { result }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function lastRunMutationInput(): RunMutationInput {
|
|
57
|
+
return runMutationMock.mock.calls[runMutationMock.mock.calls.length - 1][0] as RunMutationInput
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
describe('useNotificationActions guarded mutations', () => {
|
|
61
|
+
beforeEach(() => {
|
|
62
|
+
apiCallMock.mockReset()
|
|
63
|
+
apiCallMock.mockResolvedValue({ ok: true, result: {} })
|
|
64
|
+
apiCallOrThrowMock.mockReset()
|
|
65
|
+
apiCallOrThrowMock.mockResolvedValue({ ok: true, result: {} })
|
|
66
|
+
runMutationMock.mockReset()
|
|
67
|
+
runMutationMock.mockImplementation(
|
|
68
|
+
async ({ operation }: { operation: () => Promise<unknown> }) => operation(),
|
|
69
|
+
)
|
|
70
|
+
retryLastMutation.mockClear()
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it('routes markAsRead through the guarded mutation path', async () => {
|
|
74
|
+
const { result } = renderActions([makeNotification('n1')])
|
|
75
|
+
await act(async () => {
|
|
76
|
+
await result.current.markAsRead('n1')
|
|
77
|
+
})
|
|
78
|
+
expect(runMutationMock).toHaveBeenCalledTimes(1)
|
|
79
|
+
const input = lastRunMutationInput()
|
|
80
|
+
expect(input.context.resourceKind).toBe('notification')
|
|
81
|
+
expect(input.context.retryLastMutation).toBe(retryLastMutation)
|
|
82
|
+
expect(input.mutationPayload).toEqual({ id: 'n1' })
|
|
83
|
+
expect(apiCallOrThrowMock).toHaveBeenCalledWith('/api/notifications/n1/read', { method: 'PUT' })
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('routes executeAction through the guarded mutation path and returns the href', async () => {
|
|
87
|
+
apiCallMock.mockResolvedValue({ ok: true, result: { href: '/go' } })
|
|
88
|
+
const { result } = renderActions([makeNotification('n1')])
|
|
89
|
+
let returned: { href?: string } = {}
|
|
90
|
+
await act(async () => {
|
|
91
|
+
returned = await result.current.executeAction('n1', 'approve')
|
|
92
|
+
})
|
|
93
|
+
expect(runMutationMock).toHaveBeenCalledTimes(1)
|
|
94
|
+
const input = lastRunMutationInput()
|
|
95
|
+
expect(input.context.resourceKind).toBe('notification')
|
|
96
|
+
expect(input.mutationPayload).toEqual({ id: 'n1', actionId: 'approve' })
|
|
97
|
+
expect(apiCallMock).toHaveBeenCalledWith(
|
|
98
|
+
'/api/notifications/n1/action',
|
|
99
|
+
expect.objectContaining({ method: 'POST' }),
|
|
100
|
+
)
|
|
101
|
+
expect(returned.href).toBe('/go')
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('routes dismiss through the guarded mutation path', async () => {
|
|
105
|
+
const { result } = renderActions([makeNotification('n1')])
|
|
106
|
+
await act(async () => {
|
|
107
|
+
await result.current.dismiss('n1')
|
|
108
|
+
})
|
|
109
|
+
expect(runMutationMock).toHaveBeenCalledTimes(1)
|
|
110
|
+
const input = lastRunMutationInput()
|
|
111
|
+
expect(input.context.resourceKind).toBe('notification')
|
|
112
|
+
expect(input.mutationPayload).toEqual({ id: 'n1' })
|
|
113
|
+
expect(apiCallOrThrowMock).toHaveBeenCalledWith('/api/notifications/n1/dismiss', { method: 'PUT' })
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it('routes undoDismiss through the guarded mutation path', async () => {
|
|
117
|
+
const { result } = renderActions([makeNotification('n1')])
|
|
118
|
+
await act(async () => {
|
|
119
|
+
await result.current.dismiss('n1')
|
|
120
|
+
})
|
|
121
|
+
runMutationMock.mockClear()
|
|
122
|
+
apiCallMock.mockClear()
|
|
123
|
+
apiCallOrThrowMock.mockClear()
|
|
124
|
+
await act(async () => {
|
|
125
|
+
await result.current.undoDismiss()
|
|
126
|
+
})
|
|
127
|
+
expect(runMutationMock).toHaveBeenCalledTimes(1)
|
|
128
|
+
const input = lastRunMutationInput()
|
|
129
|
+
expect(input.context.resourceKind).toBe('notification')
|
|
130
|
+
expect(input.mutationPayload).toEqual({ id: 'n1', status: 'unread' })
|
|
131
|
+
expect(apiCallOrThrowMock).toHaveBeenCalledWith(
|
|
132
|
+
'/api/notifications/n1/restore',
|
|
133
|
+
expect.objectContaining({ method: 'PUT' }),
|
|
134
|
+
)
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
it('routes markAllRead through the guarded mutation path', async () => {
|
|
138
|
+
const { result } = renderActions([makeNotification('n1')])
|
|
139
|
+
await act(async () => {
|
|
140
|
+
await result.current.markAllRead()
|
|
141
|
+
})
|
|
142
|
+
expect(runMutationMock).toHaveBeenCalledTimes(1)
|
|
143
|
+
const input = lastRunMutationInput()
|
|
144
|
+
expect(input.context.resourceKind).toBe('notification')
|
|
145
|
+
expect(apiCallOrThrowMock).toHaveBeenCalledWith('/api/notifications/mark-all-read', { method: 'PUT' })
|
|
146
|
+
})
|
|
147
|
+
})
|
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
"use client"
|
|
2
2
|
import * as React from 'react'
|
|
3
3
|
import { apiCall, apiCallOrThrow } from '../utils/apiCall'
|
|
4
|
+
import { useGuardedMutation } from '../injection/useGuardedMutation'
|
|
5
|
+
import { useT } from '@open-mercato/shared/lib/i18n/context'
|
|
4
6
|
import type { NotificationDto } from '@open-mercato/shared/modules/notifications/types'
|
|
5
7
|
|
|
8
|
+
const NOTIFICATION_ACTIONS_CONTEXT_ID = 'notifications-actions'
|
|
9
|
+
|
|
10
|
+
type NotificationMutationContext = {
|
|
11
|
+
formId: string
|
|
12
|
+
resourceKind: string
|
|
13
|
+
retryLastMutation: () => Promise<boolean>
|
|
14
|
+
}
|
|
15
|
+
|
|
6
16
|
export type NotificationDismissUndoState = {
|
|
7
17
|
notification: NotificationDto
|
|
8
18
|
previousStatus: 'read' | 'unread'
|
|
@@ -31,29 +41,44 @@ export function useNotificationActions(
|
|
|
31
41
|
const [dismissUndo, setDismissUndo] = React.useState<NotificationDismissUndoState>(null)
|
|
32
42
|
const dismissUndoTimerRef = React.useRef<number | null>(null)
|
|
33
43
|
|
|
44
|
+
const t = useT()
|
|
45
|
+
const { runMutation, retryLastMutation } = useGuardedMutation<NotificationMutationContext>({
|
|
46
|
+
contextId: NOTIFICATION_ACTIONS_CONTEXT_ID,
|
|
47
|
+
blockedMessage: t('ui.forms.flash.saveBlocked', 'Save blocked by validation'),
|
|
48
|
+
})
|
|
49
|
+
|
|
34
50
|
const markAsRead = React.useCallback(async (id: string) => {
|
|
35
|
-
await
|
|
51
|
+
await runMutation({
|
|
52
|
+
operation: () => apiCallOrThrow(`/api/notifications/${id}/read`, { method: 'PUT' }),
|
|
53
|
+
context: { formId: NOTIFICATION_ACTIONS_CONTEXT_ID, resourceKind: 'notification', retryLastMutation },
|
|
54
|
+
mutationPayload: { id },
|
|
55
|
+
})
|
|
36
56
|
setNotifications((prev) =>
|
|
37
57
|
prev.map((n) =>
|
|
38
58
|
n.id === id ? { ...n, status: 'read', readAt: new Date().toISOString() } : n,
|
|
39
59
|
),
|
|
40
60
|
)
|
|
41
61
|
setUnreadCount((prev) => Math.max(0, prev - 1))
|
|
42
|
-
}, [setNotifications, setUnreadCount])
|
|
62
|
+
}, [runMutation, retryLastMutation, setNotifications, setUnreadCount])
|
|
43
63
|
|
|
44
64
|
React.useEffect(() => {
|
|
45
65
|
markAsReadRef.current = markAsRead
|
|
46
66
|
}, [markAsRead])
|
|
47
67
|
|
|
48
68
|
const executeAction = React.useCallback(async (id: string, actionId: string) => {
|
|
49
|
-
const result = await
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
69
|
+
const result = await runMutation({
|
|
70
|
+
operation: () =>
|
|
71
|
+
apiCall<{ ok: boolean; href?: string }>(
|
|
72
|
+
`/api/notifications/${id}/action`,
|
|
73
|
+
{
|
|
74
|
+
method: 'POST',
|
|
75
|
+
headers: { 'content-type': 'application/json' },
|
|
76
|
+
body: JSON.stringify({ actionId }),
|
|
77
|
+
},
|
|
78
|
+
),
|
|
79
|
+
context: { formId: NOTIFICATION_ACTIONS_CONTEXT_ID, resourceKind: 'notification', retryLastMutation },
|
|
80
|
+
mutationPayload: { id, actionId },
|
|
81
|
+
})
|
|
57
82
|
|
|
58
83
|
if (result.ok) {
|
|
59
84
|
setNotifications((prev) =>
|
|
@@ -65,11 +90,15 @@ export function useNotificationActions(
|
|
|
65
90
|
}
|
|
66
91
|
|
|
67
92
|
return { href: result.result?.href }
|
|
68
|
-
}, [setNotifications, setUnreadCount])
|
|
93
|
+
}, [runMutation, retryLastMutation, setNotifications, setUnreadCount])
|
|
69
94
|
|
|
70
95
|
const dismiss = React.useCallback(
|
|
71
96
|
async (id: string) => {
|
|
72
|
-
await
|
|
97
|
+
await runMutation({
|
|
98
|
+
operation: () => apiCallOrThrow(`/api/notifications/${id}/dismiss`, { method: 'PUT' }),
|
|
99
|
+
context: { formId: NOTIFICATION_ACTIONS_CONTEXT_ID, resourceKind: 'notification', retryLastMutation },
|
|
100
|
+
mutationPayload: { id },
|
|
101
|
+
})
|
|
73
102
|
const notification = notifications.find((n) => n.id === id)
|
|
74
103
|
setNotifications((prev) => prev.filter((n) => n.id !== id))
|
|
75
104
|
if (notification?.status === 'unread') {
|
|
@@ -86,7 +115,7 @@ export function useNotificationActions(
|
|
|
86
115
|
}, 6000)
|
|
87
116
|
}
|
|
88
117
|
},
|
|
89
|
-
[notifications, setNotifications, setUnreadCount],
|
|
118
|
+
[notifications, setNotifications, setUnreadCount, runMutation, retryLastMutation],
|
|
90
119
|
)
|
|
91
120
|
|
|
92
121
|
React.useEffect(() => {
|
|
@@ -95,10 +124,15 @@ export function useNotificationActions(
|
|
|
95
124
|
|
|
96
125
|
const undoDismiss = React.useCallback(async () => {
|
|
97
126
|
if (!dismissUndo) return
|
|
98
|
-
await
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
127
|
+
await runMutation({
|
|
128
|
+
operation: () =>
|
|
129
|
+
apiCallOrThrow(`/api/notifications/${dismissUndo.notification.id}/restore`, {
|
|
130
|
+
method: 'PUT',
|
|
131
|
+
headers: { 'content-type': 'application/json' },
|
|
132
|
+
body: JSON.stringify({ status: dismissUndo.previousStatus }),
|
|
133
|
+
}),
|
|
134
|
+
context: { formId: NOTIFICATION_ACTIONS_CONTEXT_ID, resourceKind: 'notification', retryLastMutation },
|
|
135
|
+
mutationPayload: { id: dismissUndo.notification.id, status: dismissUndo.previousStatus },
|
|
102
136
|
})
|
|
103
137
|
|
|
104
138
|
setNotifications((prev) => {
|
|
@@ -126,10 +160,14 @@ export function useNotificationActions(
|
|
|
126
160
|
window.clearTimeout(dismissUndoTimerRef.current)
|
|
127
161
|
}
|
|
128
162
|
setDismissUndo(null)
|
|
129
|
-
}, [dismissUndo, setNotifications, setUnreadCount])
|
|
163
|
+
}, [dismissUndo, setNotifications, setUnreadCount, runMutation, retryLastMutation])
|
|
130
164
|
|
|
131
165
|
const markAllRead = React.useCallback(async () => {
|
|
132
|
-
await
|
|
166
|
+
await runMutation({
|
|
167
|
+
operation: () => apiCallOrThrow('/api/notifications/mark-all-read', { method: 'PUT' }),
|
|
168
|
+
context: { formId: NOTIFICATION_ACTIONS_CONTEXT_ID, resourceKind: 'notification', retryLastMutation },
|
|
169
|
+
mutationPayload: {},
|
|
170
|
+
})
|
|
133
171
|
setNotifications((prev) =>
|
|
134
172
|
prev.map((n) =>
|
|
135
173
|
n.status === 'unread'
|
|
@@ -138,7 +176,7 @@ export function useNotificationActions(
|
|
|
138
176
|
),
|
|
139
177
|
)
|
|
140
178
|
setUnreadCount(0)
|
|
141
|
-
}, [setNotifications, setUnreadCount])
|
|
179
|
+
}, [runMutation, retryLastMutation, setNotifications, setUnreadCount])
|
|
142
180
|
|
|
143
181
|
React.useEffect(() => {
|
|
144
182
|
return () => {
|
|
@@ -120,6 +120,40 @@ export function buildFormFieldFromCustomFieldDef(
|
|
|
120
120
|
: {}),
|
|
121
121
|
...(def.multi && def.input === 'listbox' ? ({ listbox: true } as any) : {}),
|
|
122
122
|
}
|
|
123
|
+
case 'dictionary': {
|
|
124
|
+
if (!def.multi) {
|
|
125
|
+
const input = FieldRegistry.getInput(def.kind)
|
|
126
|
+
if (input) {
|
|
127
|
+
return {
|
|
128
|
+
id,
|
|
129
|
+
label,
|
|
130
|
+
type: 'custom',
|
|
131
|
+
...baseProps,
|
|
132
|
+
component: (props) => input({ ...props, def }),
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return { id, label, type: 'text', ...baseProps }
|
|
136
|
+
}
|
|
137
|
+
const optionsUrl = def.optionsUrl || (def.dictionaryId ? `/api/dictionaries/${def.dictionaryId}/entries` : undefined)
|
|
138
|
+
return {
|
|
139
|
+
id,
|
|
140
|
+
label,
|
|
141
|
+
type: 'select',
|
|
142
|
+
description: def.description,
|
|
143
|
+
required,
|
|
144
|
+
options: [],
|
|
145
|
+
multiple: true,
|
|
146
|
+
listbox: true,
|
|
147
|
+
...(optionsUrl
|
|
148
|
+
? {
|
|
149
|
+
loadOptions: async (query?: string) => {
|
|
150
|
+
const url = buildOptionsUrl(optionsUrl, query)
|
|
151
|
+
return loadRemoteOptions(url)
|
|
152
|
+
},
|
|
153
|
+
}
|
|
154
|
+
: {}),
|
|
155
|
+
}
|
|
156
|
+
}
|
|
123
157
|
default: {
|
|
124
158
|
if (def.kind === 'text' && def.multi) {
|
|
125
159
|
const base: any = { id, label, type: 'tags', ...baseProps }
|
|
@@ -72,6 +72,13 @@ describe('TagInput primitive', () => {
|
|
|
72
72
|
expect(screen.getByText('stay')).toBeInTheDocument()
|
|
73
73
|
})
|
|
74
74
|
|
|
75
|
+
it('localizes the remove button accessible name via removeTagLabel', () => {
|
|
76
|
+
render(<Harness initial={['Call']} removeTagLabel={(tag) => `Usuń ${tag}`} aria-label="tags" />)
|
|
77
|
+
expect(screen.queryByRole('button', { name: 'Remove Call' })).not.toBeInTheDocument()
|
|
78
|
+
fireEvent.click(screen.getByRole('button', { name: 'Usuń Call' }))
|
|
79
|
+
expect(screen.queryByText('Call')).not.toBeInTheDocument()
|
|
80
|
+
})
|
|
81
|
+
|
|
75
82
|
it('blocks further additions when maxTags is reached', () => {
|
|
76
83
|
render(<Harness initial={['a', 'b']} maxTags={2} aria-label="tags" />)
|
|
77
84
|
const input = getInput()
|
|
@@ -25,6 +25,15 @@ export type TagInputProps = {
|
|
|
25
25
|
* the Figma `Tag Input` Filled state with `Right Icon=true`.
|
|
26
26
|
*/
|
|
27
27
|
rightIcon?: InputProps['rightIcon']
|
|
28
|
+
/**
|
|
29
|
+
* Builds the accessible label for each tag's remove (×) button. Lets callers
|
|
30
|
+
* localize the action; defaults to the English `Remove {tag}`.
|
|
31
|
+
*/
|
|
32
|
+
removeTagLabel?: (tag: string) => string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function defaultRemoveTagLabel(tag: string): string {
|
|
36
|
+
return `Remove ${tag}`
|
|
28
37
|
}
|
|
29
38
|
|
|
30
39
|
function escapeRegExp(input: string): string {
|
|
@@ -58,6 +67,7 @@ export const TagInput = React.forwardRef<HTMLInputElement, TagInputProps>(
|
|
|
58
67
|
'aria-label': ariaLabel,
|
|
59
68
|
'aria-invalid': ariaInvalid,
|
|
60
69
|
rightIcon,
|
|
70
|
+
removeTagLabel = defaultRemoveTagLabel,
|
|
61
71
|
},
|
|
62
72
|
ref,
|
|
63
73
|
) => {
|
|
@@ -199,7 +209,7 @@ export const TagInput = React.forwardRef<HTMLInputElement, TagInputProps>(
|
|
|
199
209
|
shape="square"
|
|
200
210
|
disabled={disabled}
|
|
201
211
|
onRemove={() => removeTagAt(index)}
|
|
202
|
-
removeAriaLabel={
|
|
212
|
+
removeAriaLabel={removeTagLabel(tag)}
|
|
203
213
|
>
|
|
204
214
|
{tag}
|
|
205
215
|
</Tag>
|