Skip to content

Commit

Permalink
date型への変換
Browse files Browse the repository at this point in the history
  • Loading branch information
yuto-trd committed Oct 27, 2024
1 parent 5271955 commit 7f5360e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
8 changes: 7 additions & 1 deletion task_yell/src/lib/events.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Timestamp } from "firebase/firestore";
import {
createData,
deleteData,
Expand Down Expand Up @@ -41,7 +42,12 @@ export async function createEvent(
}

export async function readEvents(userId: string): Promise<Event[]> {
return readData<Event>(`users/${userId}/${COLLECTION_NAME}`);
return (await readData<Event>(`users/${userId}/${COLLECTION_NAME}`))
.map((event) => ({
...event,
start: event.start ? (event.start as unknown as Timestamp).toDate() : null,
end: event.end ? (event.end as unknown as Timestamp).toDate() : null,
}));
}

export async function readSingleEvent(
Expand Down
12 changes: 10 additions & 2 deletions task_yell/src/lib/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
updateData,
deleteData,
} from "../firebase/firestore";
import { Timestamp } from "firebase/firestore";
const COLLECTION_NAME = "notifications";

export async function createNotification(
Expand All @@ -15,13 +16,20 @@ export async function createNotification(
}

export async function readNotification(): Promise<Notification[]> {
return readData<Notification>(COLLECTION_NAME);
return (await readData<Notification>(COLLECTION_NAME))
.map((notification) => ({
...notification,
datetime: (notification.datetime as unknown as Timestamp).toDate(),
}));
}

export async function readSingleNotification(
id: string,
): Promise<Notification | null> {
return readSingleData<Notification>(COLLECTION_NAME, id);
return readSingleData<Notification>(COLLECTION_NAME, id).then((notification) => notification && ({
...notification,
datetime: (notification.datetime as unknown as Timestamp).toDate(),
}));
}

export async function updateNotification(
Expand Down
9 changes: 7 additions & 2 deletions task_yell/src/lib/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
updateData,
} from "@/firebase/firestore";
import { Task } from "@/lib/types";
import { Timestamp } from "firebase/firestore";
const COLLECTION_NAME = "tasks";

/**
Expand All @@ -24,7 +25,9 @@ export async function createTask(userId: string, task: Task): Promise<void> {
* @returns 全タスク
*/
export async function readTasks(userId: string): Promise<Task[]> {
return readData<Task>(`users/${userId}/${COLLECTION_NAME}`);
return readData<Task>(`users/${userId}/${COLLECTION_NAME}`).then((tasks) =>
tasks.map((task) => ({ ...task, due: (task.due as unknown as Timestamp).toDate() })),
);
}

/**
Expand All @@ -37,7 +40,9 @@ export async function readSingleTask(
userId: string,
id: string,
): Promise<Task | null> {
return readSingleData<Task>(`users/${userId}/${COLLECTION_NAME}`, id);
return readSingleData<Task>(`users/${userId}/${COLLECTION_NAME}`, id).then((task) =>
task && ({ ...task, due: (task.due as unknown as Timestamp).toDate() }),
);
}

/**
Expand Down

0 comments on commit 7f5360e

Please sign in to comment.