Skip to content

Commit

Permalink
fix: Entryオブジェクトからタイムゾーンを落としてただの文字列にする/entryRepositoryでタイムゾーンを補ってDate…
Browse files Browse the repository at this point in the history
…オブジェクトにする

createManyのみテスト落ちてるので後で修正
  • Loading branch information
kyonenya committed Jul 7, 2023
1 parent 3f8928f commit 91c7076
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
14 changes: 5 additions & 9 deletions app/[uuid]/ArticleHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
TrashIcon,
StarIcon,
} from '@heroicons/react/24/solid';
import dayjs from 'dayjs';
import { useRouter } from 'next/navigation';
import { experimental_useFormStatus as useFormStatus } from 'react-dom';
import { useForm, useWatch } from 'react-hook-form';
import { Entry } from '../../domain/Entry';
import dayjs from '../../infra/dayjs';
import { Button } from '../_components/Button';
import { IconButton } from '../_components/IconButton';
import { IconCheckbox } from '../_components/IconCheckbox';
Expand Down Expand Up @@ -74,15 +74,11 @@ export const ArticleHeader = ({
aria-label="Update Entry"
disabled={pending || !updateAction}
formAction={() =>
handleSubmit((formData: Form) =>
handleSubmit((formData: Form) => {
updateAction?.({
entry: {
...entry,
...formData,
createdAt: dayjs(formData.createdAt).tz().format(),
},
})
)()
entry: { ...entry, ...formData },
});
})()
}
>
{pending ? <Spinner /> : <CheckIcon />}
Expand Down
8 changes: 4 additions & 4 deletions domain/Entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type Entry = {
starred: boolean;
uuid: string;
tags: string[]; // [] if empty
createdAt: string; // ISO8601 without fraction seconds
createdAt: string;
modifiedAt: string;
};

Expand All @@ -23,8 +23,8 @@ export const newEntry = (props: {
starred: props.starred ?? false,
uuid: props.uuid ?? crypto.randomUUID().replace(/-/g, '').toUpperCase(),
tags: props.tags ?? [],
createdAt: dayjs(props.createdAt).format(), // current time if empty
modifiedAt: dayjs(props.modifiedAt).format(),
createdAt: dayjs(props.createdAt).format('YYYY-MM-DDTHH:mm:ss'),
modifiedAt: dayjs(props.modifiedAt).format('YYYY-MM-DDTHH:mm:ss'),
};
};

Expand All @@ -33,7 +33,7 @@ export const extractTagHistory = (posts: Entry[]): string[] => [
]; // uniq

// 基準になる日時
const baseDate = dayjs('2023-06-23T02:25:00+09:00');
const baseDate = dayjs('2023-06-23T02:25:00');

export const sampleEntries: Entry[] = [
newEntry({
Expand Down
21 changes: 11 additions & 10 deletions infra/entryRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Prisma } from '@prisma/client';
import { Entry as PrismaEntry, Tag, PrismaPromise } from '@prisma/client';
import { Entry, newEntry, extractTagHistory } from '../domain/Entry';
import { entriesTagToABs } from '../domain/Tag';
import dayjs from './dayjs';
import { prisma } from './prisma';

const toEntry = (row: PrismaEntry & { tags: Tag[] }): Entry =>
Expand All @@ -10,8 +11,8 @@ const toEntry = (row: PrismaEntry & { tags: Tag[] }): Entry =>
starred: row.starred,
uuid: row.uuid,
tags: row.tags?.map((tag) => tag.name),
createdAt: row.created_at,
modifiedAt: row.modified_at,
createdAt: dayjs.utc(row.created_at).tz(),
modifiedAt: dayjs.utc(row.modified_at).tz(),
});

/**
Expand All @@ -32,8 +33,8 @@ export const readMany = async (props: {
...(props.since || props.until
? {
created_at: {
...(props.since ? { gte: props.since } : {}),
...(props.until ? { lt: props.until } : {}),
...(props.since ? { gte: dayjs.tz(props.since).toDate() } : {}),
...(props.until ? { lt: dayjs.tz(props.until).toDate() } : {}),
},
}
: {}),
Expand Down Expand Up @@ -69,8 +70,8 @@ export const createOne = async (props: { entry: Entry }): Promise<Entry> => {
const row = await prisma.entry.create({
data: {
...rest,
created_at: createdAt,
modified_at: modifiedAt,
created_at: dayjs.tz(createdAt).toDate(),
modified_at: dayjs.tz(modifiedAt).toDate(),
tags: {
connectOrCreate: tags.map((tag) => ({
where: { name: tag },
Expand All @@ -95,8 +96,8 @@ export const createMany = async (props: {
data: props.entries.map(
({ createdAt, modifiedAt, tags: _tags, ...rest }) => ({
...rest,
created_at: createdAt,
modified_at: modifiedAt,
created_at: dayjs.utc(createdAt).toDate(),
modified_at: dayjs.utc(modifiedAt).toDate(),
})
),
});
Expand Down Expand Up @@ -134,8 +135,8 @@ export const updateOne = async (props: { entry: Entry }): Promise<Entry> => {
where: { uuid: props.entry.uuid.toUpperCase() },
data: {
...rest,
created_at: createdAt,
modified_at: modifiedAt,
created_at: dayjs.tz(createdAt).toDate(),
modified_at: dayjs.tz(modifiedAt).toDate(),
tags: {
connectOrCreate: tags.map((tag) => ({
where: { name: tag },
Expand Down

0 comments on commit 91c7076

Please sign in to comment.