Skip to content

Commit

Permalink
fix: code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikmv committed Jan 16, 2025
1 parent 8001694 commit c9f30c0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export const ChangelogComponent = ({
<Modal
large
hide={!isOpen}
dataTest={'changelog-modal'}
onClose={close}
dataTest="changelog-modal"
>
<ModalTitle>{i18n.t('Changelog')}</ModalTitle>
<ModalContent>
Expand All @@ -51,7 +51,7 @@ export const ChangelogComponent = ({
setFilterValue={setFilterValue}
dataItemDefinitions={dataItemDefinitions}
/>
<DataTable fixed dataTest="changelog-data-table" layout="fixed">
<DataTable fixed dataTest={'changelog-data-table'} layout={'fixed'}>
<ChangelogTableHeader
columnToSortBy={columnToSortBy}
setColumnToSortBy={setColumnToSortBy}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const ChangelogFilterBarPlain = ({
<DropdownFilter
label={i18n.t('Data item')}
items={dataItems}
filterColumn="dataItem"
filterColumn={'dataItem'}
openMenuName={openMenu}
onToggleMenu={toggleMenu}
onItemSelected={handleItemSelected}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const useChangelogData = ({ entityId, entityType, programId }: Props) =>
const [sortDirection, setSortDirection] = useState<SortDirection>(SORT_DIRECTION.DEFAULT);

const [attributeToFilterBy, setAttributeToFilterBy] = useState<string | null>(null);
const [filterValue, setFilterValue] = useState<Object>('Show all');
const [filterValue, setFilterValue] = useState<Object>('SHOW_ALL');

const [page, setPage] = useState<number>(1);
const [pageSize, setPageSize] = useState<number>(DEFAULT_PAGE_SIZE);
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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];
Expand All @@ -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 },
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -125,8 +114,6 @@ const fetchFormattedValues = async ({
};
}),
);

// Fjern tomme/null-linjer:
return results.filter(Boolean);
};

Expand All @@ -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],
Expand All @@ -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,
Expand Down

0 comments on commit c9f30c0

Please sign in to comment.