Replace Luxon DateTime with number in redux state

Luxon DateTimes are not serializable for redux. Therefore redux throws errors.

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2022-05-09 20:08:20 +02:00
parent 0d325b5e3b
commit 0d30b599d8
6 changed files with 15 additions and 13 deletions

View file

@ -3,12 +3,13 @@
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import React from 'react' import React, { useMemo } from 'react'
import { Trans } from 'react-i18next' import { Trans } from 'react-i18next'
import { NoteInfoLine } from './note-info-line' import { NoteInfoLine } from './note-info-line'
import type { NoteInfoTimeLineProps } from './note-info-time-line' import type { NoteInfoTimeLineProps } from './note-info-time-line'
import { useApplicationState } from '../../../../hooks/common/use-application-state' import { useApplicationState } from '../../../../hooks/common/use-application-state'
import { UnitalicBoldTimeFromNow } from './utils/unitalic-bold-time-from-now' import { UnitalicBoldTimeFromNow } from './utils/unitalic-bold-time-from-now'
import { DateTime } from 'luxon'
/** /**
* Renders an info line about the creation of the current note. * Renders an info line about the creation of the current note.
@ -16,11 +17,12 @@ import { UnitalicBoldTimeFromNow } from './utils/unitalic-bold-time-from-now'
*/ */
export const NoteInfoLineCreated: React.FC<NoteInfoTimeLineProps> = ({ size }) => { export const NoteInfoLineCreated: React.FC<NoteInfoTimeLineProps> = ({ size }) => {
const noteCreateTime = useApplicationState((state) => state.noteDetails.createdAt) const noteCreateTime = useApplicationState((state) => state.noteDetails.createdAt)
const noteCreateDateTime = useMemo(() => DateTime.fromSeconds(noteCreateTime), [noteCreateTime])
return ( return (
<NoteInfoLine icon={'plus'} size={size}> <NoteInfoLine icon={'plus'} size={size}>
<Trans i18nKey={'editor.modal.documentInfo.created'}> <Trans i18nKey={'editor.modal.documentInfo.created'}>
<UnitalicBoldTimeFromNow time={noteCreateTime} /> <UnitalicBoldTimeFromNow time={noteCreateDateTime} />
</Trans> </Trans>
</NoteInfoLine> </NoteInfoLine>
) )

View file

@ -11,6 +11,7 @@ import type { NoteInfoTimeLineProps } from './note-info-time-line'
import { UnitalicBoldTimeFromNow } from './utils/unitalic-bold-time-from-now' import { UnitalicBoldTimeFromNow } from './utils/unitalic-bold-time-from-now'
import { UnitalicBoldTrans } from './utils/unitalic-bold-trans' import { UnitalicBoldTrans } from './utils/unitalic-bold-trans'
import { UserAvatarForUsername } from '../../../common/user-avatar/user-avatar-for-username' import { UserAvatarForUsername } from '../../../common/user-avatar/user-avatar-for-username'
import { DateTime } from 'luxon'
/** /**
* Renders an info line about the last update of the current note. * Renders an info line about the last update of the current note.
@ -19,6 +20,7 @@ import { UserAvatarForUsername } from '../../../common/user-avatar/user-avatar-f
export const NoteInfoLineUpdated: React.FC<NoteInfoTimeLineProps> = ({ size }) => { export const NoteInfoLineUpdated: React.FC<NoteInfoTimeLineProps> = ({ size }) => {
useTranslation() useTranslation()
const noteUpdateTime = useApplicationState((state) => state.noteDetails.updatedAt) const noteUpdateTime = useApplicationState((state) => state.noteDetails.updatedAt)
const noteUpdateDateTime = useMemo(() => DateTime.fromSeconds(noteUpdateTime), [noteUpdateTime])
const noteUpdateUser = useApplicationState((state) => state.noteDetails.updateUsername) const noteUpdateUser = useApplicationState((state) => state.noteDetails.updateUsername)
const userBlock = useMemo(() => { const userBlock = useMemo(() => {
@ -38,7 +40,7 @@ export const NoteInfoLineUpdated: React.FC<NoteInfoTimeLineProps> = ({ size }) =
<NoteInfoLine icon={'pencil'} size={size}> <NoteInfoLine icon={'pencil'} size={size}>
<Trans i18nKey={'editor.modal.documentInfo.edited'}> <Trans i18nKey={'editor.modal.documentInfo.edited'}>
{userBlock} {userBlock}
<UnitalicBoldTimeFromNow time={noteUpdateTime} /> <UnitalicBoldTimeFromNow time={noteUpdateDateTime} />
</Trans> </Trans>
</NoteInfoLine> </NoteInfoLine>
) )

View file

@ -4,7 +4,6 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { DateTime } from 'luxon'
import type { NoteDetails } from './types/note-details' import type { NoteDetails } from './types/note-details'
import { NoteTextDirection, NoteType } from './types/note-details' import { NoteTextDirection, NoteType } from './types/note-details'
import type { SlideOptions } from './types/slide-show-options' import type { SlideOptions } from './types/slide-show-options'
@ -18,7 +17,6 @@ export const initialSlideOptions: SlideOptions = {
} }
export const initialState: NoteDetails = { export const initialState: NoteDetails = {
updatedAt: DateTime.fromSeconds(0),
updateUsername: null, updateUsername: null,
version: 0, version: 0,
markdownContent: { markdownContent: {
@ -35,7 +33,8 @@ export const initialState: NoteDetails = {
slideOptions: initialSlideOptions slideOptions: initialSlideOptions
}, },
id: '', id: '',
createdAt: DateTime.fromSeconds(0), createdAt: 0,
updatedAt: 0,
aliases: [], aliases: [],
primaryAddress: '', primaryAddress: '',
permissions: { permissions: {

View file

@ -114,8 +114,8 @@ describe('build state from set note data from server', () => {
firstHeading: '', firstHeading: '',
rawFrontmatter: '', rawFrontmatter: '',
id: 'id', id: 'id',
createdAt: DateTime.fromISO('2012-05-25T09:08:34.123'), createdAt: DateTime.fromISO('2012-05-25T09:08:34.123').toSeconds(),
updatedAt: DateTime.fromISO('2020-05-25T09:08:34.123'), updatedAt: DateTime.fromISO('2020-05-25T09:08:34.123').toSeconds(),
updateUsername: 'updateusername', updateUsername: 'updateusername',
viewCount: 987, viewCount: 987,
aliases: [ aliases: [

View file

@ -46,7 +46,7 @@ const convertNoteDtoToNoteDetails = (note: Note): NoteDetails => {
lineStartIndexes: calculateLineStartIndexes(newLines) lineStartIndexes: calculateLineStartIndexes(newLines)
}, },
rawFrontmatter: '', rawFrontmatter: '',
createdAt: DateTime.fromISO(note.metadata.createdAt), createdAt: DateTime.fromISO(note.metadata.createdAt).toSeconds(),
updatedAt: DateTime.fromISO(note.metadata.updatedAt) updatedAt: DateTime.fromISO(note.metadata.updatedAt).toSeconds()
} }
} }

View file

@ -4,7 +4,6 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import type { DateTime } from 'luxon'
import type { SlideOptions } from './slide-show-options' import type { SlideOptions } from './slide-show-options'
import type { ISO6391 } from './iso6391' import type { ISO6391 } from './iso6391'
import type { CursorSelection } from '../../editor/types' import type { CursorSelection } from '../../editor/types'
@ -16,8 +15,8 @@ type UnnecessaryNoteAttributes = 'updatedAt' | 'createdAt' | 'tags' | 'descripti
* Redux state containing the currently loaded note with its content and metadata. * Redux state containing the currently loaded note with its content and metadata.
*/ */
export interface NoteDetails extends Omit<NoteMetadata, UnnecessaryNoteAttributes> { export interface NoteDetails extends Omit<NoteMetadata, UnnecessaryNoteAttributes> {
updatedAt: DateTime updatedAt: number
createdAt: DateTime createdAt: number
markdownContent: { markdownContent: {
plain: string plain: string
lines: string[] lines: string[]