Skip to content

Commit 820052d

Browse files
bra-i-amdcoa
authored andcommitted
feat: add internationalization support for sorting and search placeholders
1 parent e0fe064 commit 820052d

3 files changed

Lines changed: 48 additions & 21 deletions

File tree

src/authz-module/libraries-manager/components/SortDropdown.tsx

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
useEffect,
44
FC,
55
} from 'react';
6+
import { useIntl } from '@edx/frontend-platform/i18n';
67
import {
78
DataTableContext,
89
Dropdown,
@@ -28,38 +29,33 @@ const SORT_BY_OPTIONS: SortByOptions = {
2829
oldest: { id: 'createdAt', desc: false },
2930
};
3031

31-
const SORT_LABELS: Record<string, string> = {
32-
'name-a-z': 'Name A-Z',
33-
'name-z-a': 'Name Z-A',
34-
newest: 'Newest',
35-
oldest: 'Oldest',
36-
};
37-
3832
const SortDropdown: FC = () => {
33+
const intl = useIntl();
3934
const { toggleSortBy, state } = useContext<DataTableContext>(DataTableContext);
4035
const [sortOrder, setSortOrder] = useState<string | undefined>(undefined);
4136

42-
// Get current sort state from DataTable context
37+
const SORT_LABELS: Record<string, string> = useMemo(() => ({
38+
'name-a-z': intl.formatMessage({ id: 'authz.libraries.team.table.sort.name-a-z', defaultMessage: 'Name A-Z' }),
39+
'name-z-a': intl.formatMessage({ id: 'authz.libraries.team.table.sort.name-z-a', defaultMessage: 'Name Z-A' }),
40+
newest: intl.formatMessage({ id: 'authz.libraries.team.table.sort.newest', defaultMessage: 'Newest' }),
41+
oldest: intl.formatMessage({ id: 'authz.libraries.team.table.sort.oldest', defaultMessage: 'Oldest' }),
42+
// eslint-disable-next-line react-hooks/exhaustive-deps
43+
}), []);
44+
4345
const currentSort = useMemo(() => {
4446
if (!state?.sortBy?.length) { return undefined; }
4547

4648
const activeSortBy = state.sortBy[0];
4749
return Object.entries(SORT_BY_OPTIONS).find(
4850
([, option]) => option.id === activeSortBy.id && option.desc === activeSortBy.desc,
49-
)?.[0];
51+
)?.[0]; // return the key
5052
}, [state?.sortBy]);
5153

52-
// Update local state when external sort changes
5354
useEffect(() => {
5455
setSortOrder(currentSort);
5556
}, [currentSort]);
5657

5758
const handleChangeSortBy = useCallback((newSortOrder: string) => {
58-
if (!SORT_BY_OPTIONS[newSortOrder]) {
59-
console.warn(`Invalid sort option: ${newSortOrder}`);
60-
return;
61-
}
62-
6359
setSortOrder(newSortOrder);
6460
const { id, desc } = SORT_BY_OPTIONS[newSortOrder];
6561
toggleSortBy(id, desc);
@@ -71,17 +67,15 @@ const SortDropdown: FC = () => {
7167
...option,
7268
label: SORT_LABELS[key],
7369
})),
70+
// eslint-disable-next-line react-hooks/exhaustive-deps
7471
[],
7572
);
7673

7774
const currentSortLabel = sortOrder ? SORT_LABELS[sortOrder] : 'Sort';
7875

7976
return (
8077
<Dropdown onSelect={handleChangeSortBy}>
81-
<Dropdown.Toggle
82-
variant="outline-primary"
83-
aria-label={`Sort options. Currently sorted by: ${currentSortLabel}`}
84-
>
78+
<Dropdown.Toggle variant="outline-primary">
8579
<Stack direction="horizontal" gap={2}>
8680
<Icon color="primary" src={SwapVert} />
8781
{currentSortLabel}
@@ -94,7 +88,6 @@ const SortDropdown: FC = () => {
9488
key={key}
9589
active={sortOrder === key}
9690
eventKey={key}
97-
aria-label={`Sort by ${label}`}
9891
>
9992
{label}
10093
</Dropdown.Item>

src/authz-module/libraries-manager/components/TableControlBar.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useContext } from 'react';
2+
import { useIntl } from '@edx/frontend-platform/i18n';
23
import {
34
DataTable, DataTableContext,
45
CheckboxFilter,
@@ -10,8 +11,10 @@ import {
1011
import MultipleChoiceFilter from './MultipleChoiceFilter';
1112
import SortDropdown from './SortDropdown';
1213
import SearchFilter from './SearchFilter';
14+
import messages from './messages';
1315

1416
const TableControlBar = () => {
17+
const intl = useIntl();
1518
const {
1619
columns,
1720
setAllFilters,
@@ -24,6 +27,11 @@ const TableControlBar = () => {
2427
.filter((column) => column.Filter === TextFilter)
2528
.map((column) => column.Header);
2629

30+
const getSearchPlaceholder = () => intl.formatMessage(messages['authz.libraries.team.table.search'], {
31+
firstField: columnTextFilterHeaders[0] || 'field',
32+
secondField: columnTextFilterHeaders[1] || 'field',
33+
});
34+
2735
return (
2836
<Stack className="pgn__data-table-status-bar mb-3 flex-wrap" gap={2} direction="horizontal">
2937

@@ -35,9 +43,10 @@ const TableControlBar = () => {
3543
if (column.Filter === TextFilter) {
3644
return (
3745
<SearchFilter
46+
key={column.id || column.accessor}
3847
filterValue={column.filterValue}
3948
setFilter={column.setFilter}
40-
placeholder={`Search by ${columnTextFilterHeaders.map((header) => header).join(' or ')}`}
49+
placeholder={getSearchPlaceholder()}
4150
/>
4251
);
4352
}

src/authz-module/libraries-manager/components/messages.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,31 @@ const messages = defineMessages({
3131
defaultMessage: 'Edit',
3232
description: 'Edit action',
3333
},
34+
'authz.libraries.team.table.search': {
35+
id: 'authz.libraries.team.table.search',
36+
defaultMessage: 'Search by {firstField} or {secondField}',
37+
description: 'Search placeholder for two specific fields',
38+
},
39+
'authz.libraries.team.table.sort.name-a-z': {
40+
id: 'authz.libraries.team.table.sort.name-a-z',
41+
defaultMessage: 'Name A-Z',
42+
description: 'Sort by name A-Z',
43+
},
44+
'authz.libraries.team.table.sort.name-z-a': {
45+
id: 'authz.libraries.team.table.sort.name-z-a',
46+
defaultMessage: 'Name Z-A',
47+
description: 'Sort by name Z-A',
48+
},
49+
'authz.libraries.team.table.sort.newest': {
50+
id: 'authz.libraries.team.table.sort.newest',
51+
defaultMessage: 'Newest',
52+
description: 'Sort by newest',
53+
},
54+
'authz.libraries.team.table.sort.oldest': {
55+
id: 'authz.libraries.team.table.sort.oldest',
56+
defaultMessage: 'Oldest',
57+
description: 'Sort by oldest',
58+
},
3459
});
3560

3661
export default messages;

0 commit comments

Comments
 (0)