Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datoformatering i varseltabell #1284

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/AvtaleSide/Varsellogg/VarselTabell.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import HendelseIkon from '@/komponenter/HendelseIkon';
import { Varsel } from '@/types/varsel';
import { tidSidenTidspunkt, formaterDato } from '@/utils/datoUtils';
import { formaterDato, tidSidenTidspunktEllerDato } from '@/utils/datoUtils';
import { storForbokstav } from '@/utils/stringUtils';
import { Table } from '@navikt/ds-react';
import { Checkbox, CheckboxGroup } from '@navikt/ds-react';
Expand Down Expand Up @@ -75,7 +75,7 @@ const VarselTabell: FunctionComponent<Props> = (props) => {
<Table.Row key={varsel.id} role="row">
<Table.DataCell role="cell" aria-labelledby="tidspunkt">
<UtgråetTekst title={formaterDato(varsel.tidspunkt)} grå={varsel.skjules}>
{tidSidenTidspunkt(varsel.tidspunkt)} siden
{tidSidenTidspunktEllerDato(varsel.tidspunkt)}
</UtgråetTekst>
</Table.DataCell>
<Table.DataCell role="cell">
Expand Down
33 changes: 31 additions & 2 deletions src/utils/datoUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { differenceInDays, format, isBefore, formatDistanceToNowStrict, Duration, intervalToDuration } from 'date-fns';
import {
differenceInDays,
format,
isBefore,
formatDistanceToNowStrict,
Duration,
intervalToDuration,
differenceInHours,
isAfter,
} from 'date-fns';
import { nb } from 'date-fns/locale';

const units: Array<{ unit: keyof Duration; single: string; plural: string }> = [
Expand Down Expand Up @@ -26,6 +35,8 @@ export const formaterVarighet = (dato1: Date | string, dato2: Date | string): st
*/
export const NORSK_DATO_OG_TID_FORMAT_FULL = 'PPPp';

export const NORSK_DATO_OG_TID_FORMAT = 'dd.MM.yyyy HH:mm';

/**
* Eksempel:
* `format(new Date('2025-01-10', NORSK_DATO_FORMAT, {locale: nb}))` => '10.01.2025'
Expand Down Expand Up @@ -87,6 +98,24 @@ export const formaterDatoHvisDefinert = (
* `tidSidenTidspunkt(addHours(new Date(), -5))` => '5 timer'
* `tidSidenTidspunkt(addYears(new Date(), -5))` => '5 år'
*/
export const tidSidenTidspunkt = (tidspunkt: string) => {
export const tidSidenTidspunkt = (tidspunkt: string | Date) => {
return formatDistanceToNowStrict(tidspunkt, { locale: nb });
};

/**
* Eksempler (gitt at nåværende tidspunkt er '2025-01-27 15:00:00'):
* `tidSidenTidspunktEllerDato('2025-01-27')` => '27.01.2025 01:00'
* `tidSidenTidspunktEllerDato('2025-01-27 12:00:00')` => '3 timer siden'
* `tidSidenTidspunktEllerDato('2025-01-28')` => 'om 10 timer'
*/
export const tidSidenTidspunktEllerDato = (tidspunkt: string | Date): string => {
const now = new Date();
const hoursSince = differenceInHours(tidspunkt, now);
const inFuture = isAfter(tidspunkt, now);
if (Math.abs(hoursSince) > 12) {
return formaterDato(tidspunkt, NORSK_DATO_OG_TID_FORMAT);
} else if (inFuture) {
return 'om ' + tidSidenTidspunkt(tidspunkt);
}
return tidSidenTidspunkt(tidspunkt) + ' siden';
};
Loading