From c9f30c0974b2678bcab47e9a8c0d644b72f90cb3 Mon Sep 17 00:00:00 2001 From: henrikmv Date: Thu, 16 Jan 2025 15:19:42 +0100 Subject: [PATCH] fix: code clean up --- .../common/Changelog/Changelog.component.js | 4 +- .../ChangelogFilterBar/ChangelogFilterBar.js | 2 +- .../common/hooks/useChangelogData.js | 4 +- .../common/hooks/useListDataValues.js | 65 +++++++------------ 4 files changed, 29 insertions(+), 46 deletions(-) diff --git a/src/core_modules/capture-core/components/WidgetsChangelog/common/Changelog/Changelog.component.js b/src/core_modules/capture-core/components/WidgetsChangelog/common/Changelog/Changelog.component.js index 0ea9a70912..972be5c01e 100644 --- a/src/core_modules/capture-core/components/WidgetsChangelog/common/Changelog/Changelog.component.js +++ b/src/core_modules/capture-core/components/WidgetsChangelog/common/Changelog/Changelog.component.js @@ -39,8 +39,8 @@ export const ChangelogComponent = ({ {i18n.t('Changelog')} @@ -51,7 +51,7 @@ export const ChangelogComponent = ({ setFilterValue={setFilterValue} dataItemDefinitions={dataItemDefinitions} /> - + const [sortDirection, setSortDirection] = useState(SORT_DIRECTION.DEFAULT); const [attributeToFilterBy, setAttributeToFilterBy] = useState(null); - const [filterValue, setFilterValue] = useState('Show all'); + const [filterValue, setFilterValue] = useState('SHOW_ALL'); const [page, setPage] = useState(1); const [pageSize, setPageSize] = useState(DEFAULT_PAGE_SIZE); @@ -32,7 +32,7 @@ export const useChangelogData = ({ entityId, entityType, programId }: Props) => }; const filterParam = - filterValue !== 'Show all' && attributeToFilterBy + filterValue !== 'SHOW_ALL' && attributeToFilterBy ? `${attributeToFilterBy}:eq:${filterValue.id}` : undefined; diff --git a/src/core_modules/capture-core/components/WidgetsChangelog/common/hooks/useListDataValues.js b/src/core_modules/capture-core/components/WidgetsChangelog/common/hooks/useListDataValues.js index 7f2878313f..37af0a633f 100644 --- a/src/core_modules/capture-core/components/WidgetsChangelog/common/hooks/useListDataValues.js +++ b/src/core_modules/capture-core/components/WidgetsChangelog/common/hooks/useListDataValues.js @@ -4,17 +4,13 @@ import log from 'loglevel'; import { useTimeZoneConversion, useConfig, useDataEngine } from '@dhis2/app-runtime'; import { ReactQueryAppNamespace } from 'capture-core/utils/reactQueryHelpers'; import { useQuery } from 'react-query'; -import { buildUrl, pipe } from 'capture-core-utils'; -import { convertServerToClient } from '../../../../converters'; -import { convert as convertClientToList } from '../../../../converters/clientToList'; +import { errorCreator, buildUrl, pipe } from 'capture-core-utils'; import { dataElementTypes } from '../../../../metaData'; import { CHANGELOG_ENTITY_TYPES } from '../Changelog/Changelog.constants'; -import type { - Change, - ItemDefinitions, - SortDirection, -} from '../Changelog/Changelog.types'; -import { subValueGetterByElementType, RECORD_TYPE } from '../utils/getSubValueForChangelogData'; +import type { Change, ItemDefinitions, SortDirection } from '../Changelog/Changelog.types'; +import { convertServerToClient } from '../../../../converters'; +import { convert as convertClientToList } from '../../../../converters/clientToList'; +import { RECORD_TYPE, subValueGetterByElementType } from '../utils/getSubValueForChangelogData'; import { makeQuerySingleResource } from '../../../../utils/api'; type Props = { @@ -42,11 +38,11 @@ const fetchFormattedValues = async ({ }) => { if (!rawRecords) return []; - // Hjelpefunksjon for å hente riktig metadata const getItemDefinition = (change: Change) => { - const fieldId = change.dataElement ?? change.attribute ?? change.field; + const { dataElement, attribute, field } = change; + const fieldId = dataElement ?? attribute ?? field; if (!fieldId) { - log.error('Ingen felt-id i endringen:', change); + log.error('Could not find fieldId in change:', change); return null; } return dataItemDefinitions[fieldId]; @@ -60,19 +56,18 @@ const fetchFormattedValues = async ({ const metadataElement = getItemDefinition(change); if (!metadataElement) { - log.error('Fant ikke metadata for element:', changelog); + log.error( + errorCreator('Could not find metadata for element')({ ...changelog }), + ); return null; } const getSubValue = subValueGetterByElementType[RECORD_TYPE[entityType]]?.[metadataElement.type]; const getValue = async (value, isLatestValue) => { - // Standardkonvertering hvis ingen spesialhåndtering er nødvendig if (!getSubValue) { return convertServerToClient(value, metadataElement.type); } - - // Ellers la subValueGetter hente f.eks. OptionLabel, Relationship, osv. if (entityType === RECORD_TYPE.trackedEntity) { return getSubValue({ trackedEntity: { teiId: entityId, value }, @@ -97,10 +92,7 @@ const fetchFormattedValues = async ({ const [previousValueClient, currentValueClient] = await Promise.all([ change.previousValue ? getValue(change.previousValue, false) : null, - getValue( - change.currentValue, - entityData?.[change.attribute ?? change.dataElement]?.value === change.currentValue, - ), + getValue(change.currentValue, entityData?.[change.attribute ?? change.dataElement]?.value === change.currentValue), ]); const { firstName, surname, username } = createdBy; @@ -111,10 +103,7 @@ const fetchFormattedValues = async ({ return { reactKey: metadataElement.id ? `${createdAt}-${metadataElement.id}` : createdAt, - date: pipe(convertServerToClient, convertClientToList)( - fromServerDate(createdAt), - dataElementTypes.DATETIME, - ), + date: pipe(convertServerToClient, convertClientToList)(fromServerDate(createdAt), dataElementTypes.DATETIME), user: `${firstName} ${surname} (${username})`, username, dataItemId: metadataElement.id, @@ -125,8 +114,6 @@ const fetchFormattedValues = async ({ }; }), ); - - // Fjern tomme/null-linjer: return results.filter(Boolean); }; @@ -144,11 +131,8 @@ export const useListDataValues = ({ const dataEngine = useDataEngine(); const { baseUrl, apiVersion } = useConfig(); const { fromServerDate } = useTimeZoneConversion(); - - // Bygg absolutt API-path const absoluteApiPath = buildUrl(baseUrl, `api/${apiVersion}`); - // Klargjør en "Single resource"-spørring const querySingleResource = useMemo( () => makeQuerySingleResource(dataEngine.query.bind(dataEngine)), [dataEngine], @@ -165,18 +149,17 @@ export const useListDataValues = ({ const { data: processedRecords, isError, isLoading } = useQuery( queryKey, - () => - fetchFormattedValues({ - rawRecords, - dataItemDefinitions, - entityId, - entityData, - entityType, - programId, - absoluteApiPath, - querySingleResource, - fromServerDate, - }), + () => fetchFormattedValues({ + rawRecords, + dataItemDefinitions, + entityId, + entityData, + entityType, + programId, + absoluteApiPath, + querySingleResource, + fromServerDate, + }), { enabled: !!rawRecords && !!dataItemDefinitions && !!entityId && !!entityType, staleTime: Infinity,