Skip to content

Commit

Permalink
Merge pull request #69 from jphacks/fix/date-conversion
Browse files Browse the repository at this point in the history
date型への変換
  • Loading branch information
Inlet-back authored Oct 27, 2024
2 parents d4dad5a + ab7510c commit dc839e0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
11 changes: 10 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,15 @@ 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
16 changes: 14 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,24 @@ 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
13 changes: 11 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,12 @@ 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 +43,10 @@ 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 dc839e0

Please sign in to comment.