Skip to content

Commit

Permalink
Bedre sortering av sisteDato
Browse files Browse the repository at this point in the history
  • Loading branch information
tu55eladd committed Nov 1, 2024
1 parent a8487b2 commit 912a787
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/utils/Typer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface DialogData {
aktivitetId: StringOrNull;
overskrift: StringOrNull;
sisteTekst: StringOrNull;
sisteDato: StringOrNull;
sisteDato: string;
opprettetDato: StringOrNull;
historisk: boolean;
lest: boolean;
Expand Down
7 changes: 5 additions & 2 deletions src/view/dialogliste/DialogListe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import HistoriskeDialogerOversikt from './HistoriskDialogListe';
import { useRootLoaderData } from '../../routing/loaders';
import { Loader } from '@navikt/ds-react';
import { isAfter } from 'date-fns';
import { parseISO } from 'date-fns/parseISO';

interface Res {
naaverende: DialogData[];
Expand All @@ -22,7 +23,7 @@ export function DialogListe() {
const dialoger = useDialoger();
const { dialogId } = useParams();

const sorterteDialoger = dialoger.sort((a, b) => erNyere(a.sisteDato, b.sisteDato));
const sorterteDialoger = dialoger.toSorted((a, b) => erNyere(a.sisteDato, b.sisteDato));
const { naaverende, historiske } = sorterteDialoger.reduce(splitHistoriske, { naaverende: [], historiske: [] });

const loaderData = useRootLoaderData();
Expand All @@ -43,7 +44,9 @@ export function DialogListe() {
}

export function erNyere(sisteDatoA: string | null, sisteDatoB: string | null): number {
return isAfter(sisteDatoA || '', sisteDatoB || '') ? -1 : sisteDatoA === sisteDatoB ? 0 : 1;
const a = sisteDatoA ? parseISO(sisteDatoA) : new Date();
const b = sisteDatoB ? parseISO(sisteDatoB) : new Date();
return isAfter(a, b) ? -1 : sisteDatoA === sisteDatoB ? 0 : 1;
}

const DialogListeFallback = () => {
Expand Down
6 changes: 3 additions & 3 deletions src/view/dialogliste/sortDialoger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import { describe } from 'vitest';
import { erNyere } from './DialogListe';

describe('erNyere', () => {
it('skal håndtere når all tid er i zulu', () => {
it('skal sortere riktig når all tid er i zulu', () => {
const gammelTid = '2024-09-09T08:00:30.171+00:00';
const nyereTid = '2024-09-09T08:00:31.171+00:00';
const dialoger = [nyereTid, gammelTid];
expect(dialoger.sort((a, b) => erNyere(a, b))).toStrictEqual([nyereTid, gammelTid]);
});

it('skal funke når input har tidssoner', () => {
it('skal sortere riktig når input har tidssoner', () => {
const gammelTid = '2024-09-09T08:00:30.171+02:00';
const nyereTid = '2024-09-09T08:00:31.171+02:00';
const dialoger = [nyereTid, gammelTid];
expect(dialoger.sort((a, b) => erNyere(a, b))).toStrictEqual([nyereTid, gammelTid]);
});

it('skal funke når input har forskjellige tidssoner', () => {
it('skal sortere riktig når input har forskjellige tidssoner', () => {
const gammelTid = '2024-09-09T10:00:30.171+02:00';
const nyereTid = '2024-09-09T08:00:31.171+00:00';
const dialoger = [nyereTid, gammelTid];
Expand Down

0 comments on commit 912a787

Please sign in to comment.