Skip to content

Commit

Permalink
feat: rearrange code
Browse files Browse the repository at this point in the history
  • Loading branch information
HereEast committed Sep 12, 2024
1 parent 3c8f904 commit 4394752
Show file tree
Hide file tree
Showing 21 changed files with 203 additions and 238 deletions.
26 changes: 0 additions & 26 deletions client/src/api/createTask.ts

This file was deleted.

26 changes: 0 additions & 26 deletions client/src/api/createUser.ts

This file was deleted.

26 changes: 0 additions & 26 deletions client/src/api/deleteTask.ts

This file was deleted.

20 changes: 14 additions & 6 deletions client/src/api/getEntries.ts → client/src/api/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import axios from "axios";
import { BASE_URL } from "~/utils";
import { IEntry } from "~/~/models/Entry";

// Get month entries by task ID
export async function getMonthEntriesByTaskId(
userId: string,
taskId: mongoose.Types.ObjectId | undefined,
Expand All @@ -12,7 +13,10 @@ export async function getMonthEntriesByTaskId(
): Promise<IEntry[] | undefined> {
try {
const response = await axios.get(
`${BASE_URL}/entries/month/task/${userId}/${taskId}/${year}/${month}`,
`${BASE_URL}/entries/${userId}/${taskId}`,
{
params: { year, month },
},
);

if (response.status !== 200) {
Expand All @@ -27,17 +31,21 @@ export async function getMonthEntriesByTaskId(
}
}

// All daily entries
export async function getAllDailyEntries(
// All users entries by day
export async function getUserEntriesByDay(
userId: string,
year: number,
month: number,
day: number,
): Promise<IEntry[] | undefined> {
try {
const response = await axios.get(
`${BASE_URL}/entries/day/${userId}/${year}/${month}/${day}`,
);
const response = await axios.get(`${BASE_URL}/entries/day/${userId}`, {
params: {
year,
month,
day,
},
});

if (response.status !== 200) {
throw new Error(`${response.status} ${response.statusText}`);
Expand Down
22 changes: 0 additions & 22 deletions client/src/api/getTasks.ts

This file was deleted.

86 changes: 0 additions & 86 deletions client/src/api/getUsers.ts

This file was deleted.

6 changes: 0 additions & 6 deletions client/src/api/index.ts

This file was deleted.

72 changes: 72 additions & 0 deletions client/src/api/tasks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import axios from "axios";
import mongoose from "mongoose";

import { BASE_URL } from "~/utils";
// import { ITask } from "~/~/models/Task";

// Get user's tasks
export async function getTasksByUserId(userId: string) {
try {
const response = await axios.get(`${BASE_URL}/tasks/${userId}`);

if (response.status !== 200) {
throw new Error(`${response.status} ${response.statusText}`);
}

const data = response.data;

return data;
} catch (err) {
console.log(err); // Handle
}
}

// Create
export async function createTask(userId: string, title: string) {
try {
const response = await fetch("http://localhost:5050/api/tasks", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
userId,
title,
}),
});

if (!response.ok) {
throw new Error(`${response.status} ${response.statusText}`);
}

const data = await response.json();

return data;
} catch (err) {
console.log(err);
}
}

// Delete
export async function deleteTaskById(
userId: string,
taskId: mongoose.Types.ObjectId,
) {
try {
const response = await axios.delete(
`${BASE_URL}/tasks/${userId}/${taskId}`,
);

if (response.status !== 200) {
throw new Error(`${response.status} ${response.statusText}`);
}

const data = response.data;

console.log(data);

return data;
} catch (err) {
console.log(err); // Handle
}
}
52 changes: 52 additions & 0 deletions client/src/api/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import axios from "axios";

import { BASE_URL } from "~/utils";

// Get user by ID
export async function getUserById(userId: string) {
try {
const response = await axios.get(`${BASE_URL}/users`, {
params: userId,
});

if (response.status !== 200) {
throw new Error(`${response.status} ${response.statusText}`);
}

const data = response.data;
return data;
} catch (err) {
console.log(err);
}
}

// Create user
export async function createUser(
username: string,
email: string,
password: string,
) {
try {
const response = await fetch(`${BASE_URL}/users`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
username,
email,
password,
}),
});

if (!response.ok) {
throw new Error(`${response.status} ${response.statusText}`);
}

const data = await response.json();
console.log(data);
} catch (err) {
console.log(err);
}
}

2 changes: 1 addition & 1 deletion client/src/components/CreateTaskForm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FormEvent, useState } from "react";
import { createTask } from "~/api";
import { createTask } from "~/api/tasks";
import { Input } from "./ui/Input";
import { Button } from "./ui/Button";
import { useAppContext } from "~/hooks";
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/TaskListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { EntryBox } from "./EntryBox";

import { useAppContext, useEntries } from "~/hooks";
import { getDaysInMonth } from "~/utils";
import { deleteTaskById } from "~/api";
import { deleteTaskById } from "~/api/tasks";

interface TaskListItemProps {
taskId?: mongoose.Types.ObjectId;
Expand Down
Loading

0 comments on commit 4394752

Please sign in to comment.