Skip to content

Commit

Permalink
Merge pull request #13 from ma-ray/add-not-found-page
Browse files Browse the repository at this point in the history
Add not found page
  • Loading branch information
ma-ray authored Jan 4, 2024
2 parents 8831e42 + 4f62e19 commit fd04d65
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import { createBrowserRouter, RouterProvider } from 'react-router-dom'
import HomePage from './pages/HomePage.tsx'
import DiaryPage from './pages/DiaryPage.tsx'
import { SettingsProvider } from './context/SettingsContext.tsx'
import NotFoundPage from './pages/NotFoundPage.tsx'

const router = createBrowserRouter([
{
path: '*',
element: <NotFoundPage />,
},
{
path: '/',
element: <HomePage />,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Calendar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Calendar as CalendarBase, CalendarDate } from 'calendar-base'
import moment from 'moment'
import { Link } from 'react-router-dom'
import { months } from '../util/time'
import { months } from '../util/date'

type DayProps = {
date: CalendarDate
Expand Down
12 changes: 10 additions & 2 deletions src/pages/DiaryPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '@mdxeditor/editor/style.css'
import { Link, useParams } from 'react-router-dom'
import { Link, useNavigate, useParams } from 'react-router-dom'
import {
MDXEditor,
headingsPlugin,
Expand All @@ -20,8 +20,9 @@ import {
dateStatus,
getNextDayLink,
getPreviousDayLink,
isDateValid,
months,
} from '../util/time'
} from '../util/date'
import { debounce } from 'lodash'
import { useEffect, useRef, useState } from 'react'
import moment from 'moment'
Expand All @@ -34,6 +35,7 @@ type ParamsType = {

const DiaryPage = () => {
const { year, month, day } = useParams<ParamsType>()
const navigate = useNavigate()
const [canEdit, setCanEdit] = useState(false)
const [loading, setLoading] = useState(true)
const [fileContent, setFileContent] = useState('')
Expand All @@ -51,6 +53,12 @@ const DiaryPage = () => {
])
)

useEffect(() => {
if (!isDateValid(day, month, year)) {
navigate('/')
}
}, [day, month, year, navigate])

useEffect(() => {
if (year && month && day) {
const isToday = moment([year, month, day]).isSame(moment().startOf('day'))
Expand Down
21 changes: 21 additions & 0 deletions src/pages/NotFoundPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { AlertOctagon } from 'lucide-react'
import { Link } from 'react-router-dom'
import { Button } from '@/components/ui/button'

const NotFoundPage = () => {
return (
<div className="h-screen">
<div className="flex flex-col items-center justify-center h-full gap-4">
<AlertOctagon className="h-20 w-20" />
<h3 className="scroll-m-20 text-2xl font-semibold tracking-tight">
whoops... whatever you are looking for is not here
</h3>
<Link to="/">
<Button>go back home</Button>
</Link>
</div>
</div>
)
}

export default NotFoundPage
10 changes: 10 additions & 0 deletions src/util/time.ts → src/util/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,13 @@ export const getPreviousDayLink = (
'month'
)}/${currentDate.get('date')}`
}

export const isDateValid = (day?: string, month?: string, year?: string) =>
!isNaN(Number(year)) &&
!isNaN(Number(month)) &&
!isNaN(Number(day)) &&
moment([
parseInt(year ?? ''),
parseInt(month ?? ''),
parseInt(day ?? ''),
]).isValid()

0 comments on commit fd04d65

Please sign in to comment.