Skip to content

Commit 674674e

Browse files
committed
Include resource name in default messages
1 parent e5ab087 commit 674674e

File tree

5 files changed

+41
-31
lines changed

5 files changed

+41
-31
lines changed

packages/ra-language-english/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,15 @@ const englishMessages: TranslationMessages = {
102102
bulk_update_content:
103103
'Are you sure you want to update this %{name}? |||| Are you sure you want to update these %{smart_count} items?',
104104
bulk_update_content_record_representation:
105-
'Are you sure you want to update %{name}? |||| Are you sure you want to update these %{smart_count} items?',
105+
'Are you sure you want to update %{resource} %{name}? |||| Are you sure you want to update these %{smart_count} items?',
106106
bulk_update_title:
107107
'Update %{name} |||| Update %{smart_count} %{name}',
108+
bulk_update_title_record_representation:
109+
'Update %{resource} %{name} |||| Update %{smart_count} %{name}',
108110
clear_array_input: 'Are you sure you want to clear the whole list?',
109111
delete_content: 'Are you sure you want to delete this item?',
110112
delete_title: 'Delete %{name} #%{id}',
111-
delete_title_record_representation: 'Delete %{name}',
113+
delete_title_record_representation: 'Delete %{resource} %{name}',
112114
details: 'Details',
113115
error: "A client error occurred and your request couldn't be completed.",
114116
invalid_form: 'The form is not valid. Please check for errors',

packages/ra-ui-materialui/src/button/DeleteWithConfirmButton.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ describe('<DeleteWithConfirmButton />', () => {
349349
) as HTMLElement
350350
).getByText('Delete')
351351
);
352-
await screen.findByText('Delete War and Peace');
352+
await screen.findByText('Delete book War and Peace');
353353
});
354354

355355
it('should use the default translation in the confirmation title when no record representation is available', async () => {
@@ -361,6 +361,6 @@ describe('<DeleteWithConfirmButton />', () => {
361361
) as HTMLElement
362362
).getByText('Delete')
363363
);
364-
await screen.findByText('Delete #1');
364+
await screen.findByText('Delete author #1');
365365
});
366366
});

packages/ra-ui-materialui/src/button/DeleteWithConfirmButton.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,20 @@ export const DeleteWithConfirmButton = <RecordType extends RaRecord = any>(
6161
const recordRepresentation = getRecordRepresentation(record);
6262
let confirmNameParam = recordRepresentation;
6363
let confirmTitle = 'ra.message.delete_title_record_representation';
64+
const resourceName = translate(`resources.${resource}.forcedCaseName`, {
65+
smart_count: 1,
66+
_: humanize(
67+
translate(`resources.${resource}.name`, {
68+
smart_count: 1,
69+
_: resource ? singularize(resource) : undefined,
70+
}),
71+
true
72+
),
73+
});
6474

6575
if (isValidElement(recordRepresentation)) {
6676
confirmTitle = 'ra.message.delete_title';
67-
confirmNameParam = translate(`resources.${resource}.forcedCaseName`, {
68-
smart_count: 1,
69-
_: humanize(
70-
translate(`resources.${resource}.name`, {
71-
smart_count: 1,
72-
_: resource ? singularize(resource) : undefined,
73-
}),
74-
true
75-
),
76-
});
77+
confirmNameParam = resourceName;
7778
}
7879

7980
return (
@@ -96,6 +97,7 @@ export const DeleteWithConfirmButton = <RecordType extends RaRecord = any>(
9697
confirmColor={confirmColor}
9798
translateOptions={{
9899
name: confirmNameParam,
100+
resource: resourceName,
99101
id: record?.id,
100102
...translateOptions,
101103
}}

packages/ra-ui-materialui/src/button/UpdateWithConfirmButton.spec.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ describe('<UpdateWithConfirmButton />', () => {
262262
fireEvent.click(await screen.findByText('Reset views'));
263263
await screen.findByRole('dialog');
264264
await screen.findByText(
265-
'Are you sure you want to update Lorem Ipsum?',
265+
'Are you sure you want to update post Lorem Ipsum?',
266266
undefined,
267267
{ timeout: 4000 }
268268
);
@@ -273,7 +273,9 @@ describe('<UpdateWithConfirmButton />', () => {
273273
// wait until next tick, as the settled side effect is called after the success side effect
274274
await waitFor(() => new Promise(resolve => setTimeout(resolve, 300)));
275275
expect(
276-
screen.queryByText('Are you sure you want to update Lorem Ipsum?')
276+
screen.queryByText(
277+
'Are you sure you want to update post Lorem Ipsum?'
278+
)
277279
).toBeNull();
278280
});
279281

@@ -286,9 +288,9 @@ describe('<UpdateWithConfirmButton />', () => {
286288
) as HTMLElement
287289
).getByText('Update')
288290
);
289-
await screen.findByText('Update War and Peace');
291+
await screen.findByText('Update book War and Peace');
290292
await screen.findByText(
291-
'Are you sure you want to update War and Peace?'
293+
'Are you sure you want to update book War and Peace?'
292294
);
293295
});
294296

@@ -301,6 +303,6 @@ describe('<UpdateWithConfirmButton />', () => {
301303
) as HTMLElement
302304
).getByText('Update')
303305
);
304-
await screen.findByText('Update #1');
306+
await screen.findByText('Update author #1');
305307
});
306308
});

packages/ra-ui-materialui/src/button/UpdateWithConfirmButton.tsx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const UpdateWithConfirmButton = (
3030
const record = useRecordContext(props);
3131

3232
const {
33-
confirmTitle = 'ra.message.bulk_update_title',
33+
confirmTitle: confirmTitleProp,
3434
confirmContent: confirmContentProp,
3535
data,
3636
icon = defaultIcon,
@@ -114,18 +114,21 @@ export const UpdateWithConfirmButton = (
114114
const recordRepresentation = getRecordRepresentation(record);
115115
let confirmNameParam = recordRepresentation;
116116
let confirmContent = 'ra.message.bulk_update_content_record_representation';
117+
let confirmTitle = 'ra.message.bulk_update_title_record_representation';
118+
const resourceName = translate(`resources.${resource}.forcedCaseName`, {
119+
smart_count: 1,
120+
_: humanize(
121+
translate(`resources.${resource}.name`, {
122+
smart_count: 1,
123+
_: resource ? inflect(resource, 1) : undefined,
124+
}),
125+
true
126+
),
127+
});
117128

118129
if (React.isValidElement(recordRepresentation)) {
119-
confirmNameParam = translate(`resources.${resource}.forcedCaseName`, {
120-
smart_count: 1,
121-
_: humanize(
122-
translate(`resources.${resource}.name`, {
123-
smart_count: 1,
124-
_: resource ? inflect(resource, 1) : undefined,
125-
}),
126-
true
127-
),
128-
});
130+
confirmNameParam = resourceName;
131+
confirmTitle = 'ra.message.bulk_update_title';
129132
confirmContent = 'ra.message.bulk_update_content';
130133
}
131134
return (
@@ -140,11 +143,12 @@ export const UpdateWithConfirmButton = (
140143
<Confirm
141144
isOpen={isOpen}
142145
loading={isPending}
143-
title={confirmTitle}
146+
title={confirmTitleProp ?? confirmTitle}
144147
content={confirmContentProp ?? confirmContent}
145148
translateOptions={{
146149
smart_count: 1,
147150
name: confirmNameParam,
151+
resource: resourceName,
148152
}}
149153
onConfirm={handleUpdate}
150154
onClose={handleDialogClose}

0 commit comments

Comments
 (0)