Skip to content

Commit

Permalink
UIPQB-143 Translate language codes to languages in the ResultsViewer
Browse files Browse the repository at this point in the history
  • Loading branch information
vashjs committed Oct 23, 2024
1 parent 33cb102 commit cb3f957
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
13 changes: 13 additions & 0 deletions src/QueryBuilder/ResultViewer/ResultViewer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ describe('ResultViewer', () => {
const limit = 200;
const changePage = jest.fn();

it('should format language name shortcuts to full name', async () => {
const { debug } = render(renderResultViewer({ visibleColumns: ['instance.languages'] }));

await waitFor(() => {
expect(screen.queryByText('ui-plugin-query-builder.result.inProgress')).not.toBeInTheDocument();

expect(screen.queryByText('Languages')).toBeVisible();
expect(screen.queryByText('English | French')).toBeVisible();

debug(undefined, Infinity);
});
});

it('should be rendered with pagination', async () => {
jest.spyOn(pagination, 'usePagination').mockImplementation(() => ({
limit,
Expand Down
22 changes: 15 additions & 7 deletions src/QueryBuilder/ResultViewer/helpers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { FormattedDate } from 'react-intl';
import { formattedLanguageName } from '@folio/stripes/components';
import { DATA_TYPES } from '../../constants/dataTypes';
import { DynamicTable } from './DynamicTable/DynamicTable';

export const getTableMetadata = (entityType, forcedVisibleValues) => {
export const getTableMetadata = (entityType, forcedVisibleValues, intl) => {
const defaultColumns = (entityType?.columns?.map((cell) => ({
label: cell.labelAlias,
value: cell.name,
Expand Down Expand Up @@ -34,22 +35,29 @@ export const getTableMetadata = (entityType, forcedVisibleValues) => {
const { value, dataType, properties } = column;

formatted[value] = (item) => {
const val = item[value];

if (properties?.length) {
return <DynamicTable properties={properties} values={item[value]} />;
return <DynamicTable properties={properties} values={val} />;
} else if (dataType === DATA_TYPES.DateType) {
return item[value] ? <FormattedDate value={item[value]} /> : '';
return val ? <FormattedDate value={val} /> : '';
} else if (dataType === DATA_TYPES.ArrayType) {
return item[value]?.join(' | ');
// Special case for instance languages, to format them as translated strings
if (value === 'instance.languages') {
return val?.map(lang => formattedLanguageName(lang, intl)).join(' | ');
}

return val?.join(' | ');
} else if (dataType === DATA_TYPES.NumberType || dataType === DATA_TYPES.IntegerType) {
if (item[value] === undefined) {
if (val === undefined) {
return '';
}

return item[value];
return val;
} else {
// If value is empty we will return empty string
// instead of undefined
return item[value] || '';
return val || '';
}
};

Expand Down
4 changes: 3 additions & 1 deletion src/hooks/useAsyncDataSource.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useQuery, useQueryClient } from 'react-query';
import { useIntl } from 'react-intl';

import { useNamespace } from '@folio/stripes/core';

Expand Down Expand Up @@ -33,6 +34,7 @@ export const useAsyncDataSource = ({
contentQueryKeys,
forcedVisibleValues,
}) => {
const intl = useIntl();
const queryClient = useQueryClient();
const [namespaceKey] = useNamespace();
const [entityKey] = useNamespace({ key: QUERY_KEYS.QUERY_PLUGIN_PREVIEW_ENTITY_TYPE });
Expand Down Expand Up @@ -79,7 +81,7 @@ export const useAsyncDataSource = ({
defaultVisibleColumns,
formatter,
columnWidths,
} = getTableMetadata(entityType, forcedVisibleValues);
} = getTableMetadata(entityType, forcedVisibleValues, intl);

return {
contentData,
Expand Down
1 change: 1 addition & 0 deletions test/jest/data/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const content = {
'item_material_type': 'book',
'content_id': '993eb55b-c237-44e7-bba5-227ce3ab5d80',
'sort_seq': 0,
'instance.languages': ['eng', 'fre'],
},
{
'user_id': '55c0f118-7a44-462a-a18b-861b91659543',
Expand Down
2 changes: 1 addition & 1 deletion test/jest/data/entityType.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export const entityType = {
'visibleByDefault': false,
},
{
'name': 'languages',
'name': 'instance.languages',
'queryable': true,
'dataType': {
'dataType': 'arrayType',
Expand Down

0 comments on commit cb3f957

Please sign in to comment.