From 895bf473d05cd6ee44f18541c33b0b5f7b9f152c Mon Sep 17 00:00:00 2001 From: Sakura Knoll Date: Mon, 27 Dec 2021 23:44:17 +0900 Subject: [PATCH] Refactor date string methods --- src/lib/string.ts | 14 +++++++++ src/pages/honkai3rd/versions/index.tsx | 43 ++++++++++++++++---------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/lib/string.ts b/src/lib/string.ts index 2f390aa3..2088c807 100644 --- a/src/lib/string.ts +++ b/src/lib/string.ts @@ -1,3 +1,5 @@ +import { format as formatDate, add as addDate } from 'date-fns' + export function isVersionString(version: string): boolean { return /[0-9]+\.[0-9]+(\.[0-9]+)?/.test(version) } @@ -20,3 +22,15 @@ export function compareVersion(aVersion: string, bVersion: string): number { return parseInt(aPatch, 10) - parseInt(bPatch, 10) } + +export function getDateString(date: Date) { + return formatDate(date, 'yyyy-MM-dd') +} + +export function addDateToDateString(dateString: string, duration: Duration) { + return getDateString( + addDate(new Date(dateString), { + weeks: 6, + }) + ) +} diff --git a/src/pages/honkai3rd/versions/index.tsx b/src/pages/honkai3rd/versions/index.tsx index 364c12df..761ae01e 100644 --- a/src/pages/honkai3rd/versions/index.tsx +++ b/src/pages/honkai3rd/versions/index.tsx @@ -1,6 +1,5 @@ /** @jsxImportSource theme-ui */ import { Box, Flex, Heading, Link, Text } from '@theme-ui/components' -import { format } from 'date-fns' import NextLink from 'next/link' import SquareImageBox from '../../../components/atoms/SquareImageBox' import Breadcrumb from '../../../components/organisms/Breadcrumb' @@ -20,6 +19,7 @@ import { VersionData, } from '../../../data/honkai3rd/versions' import { getWeaponById, WeaponData } from '../../../data/honkai3rd/weapons' +import { addDateToDateString, getDateString } from '../../../lib/string' interface VersionIndexPageProps { versionDataList: VersionData[] @@ -47,7 +47,7 @@ const VersionIndexPage = ({ { href: '/honkai3rd/versions', label: 'Versions' }, ]} /> - + v{currentVersionData.version} : {currentVersionData.name}
@@ -55,8 +55,10 @@ const VersionIndexPage = ({
- New Battlesuits - + + New Battlesuits + + {currentVersionNewBattlesuits.map((battlesuit) => { return ( - New Weapons - + + New Weapons + + {currentVersionNewWeapons.map((weapon) => { return ( @@ -101,7 +105,9 @@ const VersionIndexPage = ({ - Supply Events + + Supply Events + { @@ -125,9 +131,15 @@ const VersionIndexPage = ({ row: supplyEventData.track, } })} - today={format(new Date(), 'yyyy-MM-dd')} + today={getDateString(new Date())} startDate={currentVersionData.duration[0]} - endDate={currentVersionData.duration[1]} + endDate={ + currentVersionData.duration[1] != null + ? currentVersionData.duration[1] + : addDateToDateString(currentVersionData.duration[0], { + weeks: 6, + }) + } /> @@ -140,11 +152,13 @@ const VersionIndexPage = ({ - All Versions + + All Versions + {versionDataList.map((versionData) => { return ( - + {versionData.version} : {versionData.name} ( - {format(new Date(versionData.duration[0]), 'yyyy/MM/dd')}- + {versionData.duration[0].replace(/-/g, '/')}- {versionData.duration[1] != null - ? format( - new Date(versionData.duration[1]), - 'yyyy/MM/dd' - ) + ? versionData.duration[1].replace(/-/g, '/') : ''} )