Skip to content

Commit

Permalink
feat(date) move logic from hook
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindgrutle committed Sep 14, 2023
1 parent 3c0a3ab commit bdaabb5
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 723 deletions.
693 changes: 0 additions & 693 deletions next-tavla/firestore-debug.log

This file was deleted.

26 changes: 20 additions & 6 deletions next-tavla/pages/api/ping/[id].ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { initializeAdminApp, setLastActive } from 'Admin/utils/firebase'
import {
getBoard,
initializeAdminApp,
setLastActive,
} from 'Admin/utils/firebase'
import { NextApiRequest, NextApiResponse } from 'next'
import { TBoardID } from 'types/settings'

Expand All @@ -9,10 +13,20 @@ export default async function handler(
response: NextApiResponse,
) {
const { id } = request.query
try {
await setLastActive(id as TBoardID)
return response.status(200).json({ message: 'Successfully updated!' })
} catch (error) {
return response.status(400).json({ error: 'Could not update!' })
const board = (await getBoard(id as TBoardID)) || undefined
if (!board) {
return response.status(404).json({ error: 'Board not found!' })
}
const active = board?.meta?.lastActive ?? 0
const lastActiveDate = new Date(active).getTime()
if (Date.now() - lastActiveDate > 1000 * 60 * 60 * 24) {
try {
await setLastActive(id as TBoardID)
return response
.status(200)
.json({ message: 'Successfully updated!' })
} catch (error) {
return response.status(400).json({ error: 'Could not update!' })
}
}
}
15 changes: 2 additions & 13 deletions next-tavla/src/Admin/scenarios/BoardList/components/Row/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useEffect, useState } from 'react'
import { Cell } from 'Admin/scenarios/BoardList/components/Cell'
import { Tooltip } from '@entur/tooltip'
import { Info } from 'Admin/scenarios/Info'
import { formatDate } from 'utils/time'

function Row({ board }: { board: TBoard }) {
const { addToast } = useToast()
Expand All @@ -22,18 +23,6 @@ function Row({ board }: { board: TBoard }) {
await router.push('/edit/' + board.id)
}

function toLocaleString(date: Date) {
return date
.toLocaleDateString('no-NB', {
day: '2-digit',
month: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
})
.replace(',', '')
}

return (
<div className={classes.tableRow}>
<Cell>{board?.meta?.title ?? 'Tavla'}</Cell>
Expand Down Expand Up @@ -61,7 +50,7 @@ function Row({ board }: { board: TBoard }) {
</Cell>
<Cell>
{board?.meta?.dateModified &&
toLocaleString(new Date(board.meta.dateModified))}
formatDate(new Date(board.meta.dateModified))}
</Cell>
</div>
)
Expand Down
5 changes: 4 additions & 1 deletion next-tavla/src/Admin/utils/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ export async function setBoard(board: TBoard, uid: TUserID) {
return await firestore()
.collection('boards')
.doc(boardId)
.set(sanitizedBoard)
.set({
...sanitizedBoard,
meta: { ...sanitizedBoard.meta, dateModified: Date.now() },
})
}

export async function setLastActive(bid: TBoardID) {
Expand Down
13 changes: 3 additions & 10 deletions next-tavla/src/Shared/hooks/useUpdateLastActive.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import { useCallback, useEffect } from 'react'
import { TBoardID } from 'types/settings'
import { getBoard } from 'utils/firebase'

function useUpdateLastActive(documentId: TBoardID) {
const updateLastActive = useCallback(async () => {
const board = (await getBoard(documentId)) || undefined
if (!board) return
const lastActive = board?.meta?.lastActive ?? 0
const lastActiveDate = new Date(lastActive).getTime()
if (Date.now() - lastActiveDate > 1000 * 60 * 60 * 24) {
await fetch(`/api/ping/${documentId}`, {
method: 'POST',
})
}
await fetch(`/api/ping/${documentId}`, {
method: 'POST',
})
}, [documentId])

useEffect(() => {
Expand Down
12 changes: 12 additions & 0 deletions next-tavla/src/Shared/utils/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ export function formatTimeStamp(timestamp: number) {
minute: '2-digit',
}).format(timestamp)
}

export function formatDate(date: Date) {
return date
.toLocaleDateString('no-NB', {
day: '2-digit',
month: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
})
.replace(',', '')
}

0 comments on commit bdaabb5

Please sign in to comment.