Skip to content

Commit 0565291

Browse files
committed
Major refactor
1 parent 265faab commit 0565291

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+945
-911
lines changed

api/categories/fetchCategories.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { CATEGORIES_API_ENDPOINT, Category } from "@/types";
2+
import { fetchService } from "@/services";
3+
4+
interface FetchCategoriesParams {
5+
q?: string | null;
6+
}
7+
8+
export const fetchCategories = async ({
9+
q,
10+
}: FetchCategoriesParams): Promise<Category[]> => {
11+
let url = `${CATEGORIES_API_ENDPOINT}`;
12+
13+
if (q) {
14+
url += `?q=${q}`;
15+
}
16+
17+
const response = await fetchService(url, { method: "GET" });
18+
19+
if (!response.success) {
20+
throw new Error(response?.error);
21+
}
22+
23+
return response.data;
24+
};

api/categories/useCreateCategory.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
import { useSnackbar } from "notistack";
12
import { fetchService } from "@/services";
23
import { ApiResponse, CATEGORIES_API_ENDPOINT, Category } from "@/types";
3-
import { useSnackbar } from "notistack";
44
import { useMutation, useQueryClient } from "@tanstack/react-query";
55

66
export const useCreateCategory = () => {
77
const { enqueueSnackbar } = useSnackbar();
88
const queryClient = useQueryClient();
99

1010
const { mutateAsync: createCategory, isPending } = useMutation({
11-
mutationFn: async (newCategory: Omit<Category, "id">) => {
11+
mutationFn: async (newCategory: Omit<Category, "id" | "user_id">) => {
1212
const response: ApiResponse<Category> = await fetchService(
1313
CATEGORIES_API_ENDPOINT,
1414
{

api/categories/useGetCategories.ts

+4-30
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,14 @@
1-
import { fetchService } from "@/services";
2-
import { ApiResponse, CATEGORIES_API_ENDPOINT, Category } from "@/types";
1+
import { CATEGORIES_API_ENDPOINT } from "@/types";
32
import { useQuery } from "@tanstack/react-query";
4-
import { useGetUser } from "../../hooks/useGetUser";
53
import { useSearchParams } from "next/navigation";
6-
7-
export const fetchCategories = async ({
8-
userId,
9-
q,
10-
}: {
11-
userId: string;
12-
q?: string | null;
13-
}): Promise<Category[]> => {
14-
let url = `${CATEGORIES_API_ENDPOINT}?userId=${userId}`;
15-
if (q) {
16-
url += `&q=${q}`;
17-
}
18-
19-
const response: ApiResponse<Category[]> = await fetchService(url, {
20-
method: "GET",
21-
});
22-
23-
if (!response.success) {
24-
throw new Error(response?.error);
25-
}
26-
27-
return response.data;
28-
};
4+
import { fetchCategories } from "./fetchCategories";
295

306
export const useGetCategories = () => {
31-
const { userId } = useGetUser();
327
const searchParams = useSearchParams();
338
const q = searchParams.get("q");
349

3510
return useQuery({
36-
queryKey: [CATEGORIES_API_ENDPOINT, userId, q],
37-
queryFn: () => fetchCategories({ userId, q }),
38-
enabled: !!userId,
11+
queryKey: [CATEGORIES_API_ENDPOINT],
12+
queryFn: () => fetchCategories({ q }),
3913
});
4014
};

api/categories/useUpdateCategory.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const useUpdateCategory = ({
1414
const queryClient = useQueryClient();
1515

1616
const { mutateAsync: updateCategory, isPending } = useMutation({
17-
mutationFn: async (category: Category) => {
17+
mutationFn: async (category: Omit<Category, "user_id">) => {
1818
const response: ApiResponse<Category> = await fetchService(
1919
`${CATEGORIES_API_ENDPOINT}/${category.id}`,
2020
{

api/codeItems/fetchCodeItems.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@ import { fetchService } from "@/services";
22
import { ApiResponse, CodeItem, CODEITEMS_API_ENDPOINT } from "@/types";
33

44
interface FetchCodeItemsParams {
5-
userId: string;
65
q?: string | null;
76
categoryId: any;
87
}
98

109
export const fetchCodeItems = async ({
11-
userId,
1210
q,
1311
categoryId,
1412
}: FetchCodeItemsParams): Promise<CodeItem[]> => {
15-
let url = `${CODEITEMS_API_ENDPOINT}?userId=${userId}&categoryId=${categoryId}`;
13+
let url = `${CODEITEMS_API_ENDPOINT}?categoryId=${categoryId}`;
1614

1715
if (q) {
1816
url += `&q=${q}`;

api/codeItems/fetchOneCodeItem.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ import { ApiResponse, CodeItem, CODEITEMS_API_ENDPOINT } from "@/types";
33

44
interface FetchOneCodeItemParams {
55
codeItemId: string;
6-
userId: string;
6+
headers?: any;
77
}
88

99
export const fetchOneCodeItem = async ({
1010
codeItemId,
11-
userId,
11+
headers,
1212
}: FetchOneCodeItemParams): Promise<CodeItem> => {
13-
let url = `${CODEITEMS_API_ENDPOINT}/${codeItemId}?userId=${userId}`;
13+
let url = `${CODEITEMS_API_ENDPOINT}/${codeItemId}`;
1414

1515
const response: ApiResponse<CodeItem> = await fetchService(url, {
1616
method: "GET",
17+
headers,
1718
});
1819

1920
if (!response.success) {

api/codeItems/useCreateCodeItem.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const useCreateCodeItem = () => {
88
const queryClient = useQueryClient();
99

1010
const { mutateAsync: createCodeItem, isPending } = useMutation({
11-
mutationFn: async (newCodeItem: Omit<CodeItem, "id">) => {
11+
mutationFn: async (newCodeItem: Omit<CodeItem, "id" | "user_id">) => {
1212
const response: ApiResponse<CodeItem> = await fetchService(
1313
CODEITEMS_API_ENDPOINT,
1414
{

api/codeItems/useDeleteCodeItem.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const useDeleteCodeItem = () => {
2020
throw new Error(response.error);
2121
}
2222

23-
await queryClient.invalidateQueries({
23+
queryClient.invalidateQueries({
2424
queryKey: [CODEITEMS_API_ENDPOINT],
2525
});
2626
},

api/codeItems/useGetCodeItems.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
import { useGetUser } from "@/hooks";
21
import { useQuery } from "@tanstack/react-query";
32
import { useSearchParams } from "next/navigation";
43
import { fetchCodeItems } from "./fetchCodeItems";
54
import { CODEITEMS_API_ENDPOINT } from "@/types";
65

76
export const useGetCodeItems = () => {
8-
const { userId } = useGetUser();
97
const searchParams = useSearchParams();
108
const q = searchParams.get("q");
119
const categoryId = searchParams.get("categoryId");
1210

1311
return useQuery({
14-
queryFn: () => fetchCodeItems({ userId, q, categoryId }),
15-
queryKey: [CODEITEMS_API_ENDPOINT, userId, q, categoryId],
16-
enabled: !!userId,
12+
queryFn: () => fetchCodeItems({ q, categoryId }),
13+
queryKey: [CODEITEMS_API_ENDPOINT, q, categoryId],
1714
});
1815
};

api/labels/fetchLabels.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { fetchService } from "@/services";
2+
import { ApiResponse, Label, LABELS_API_ENDPOINT, LabelType } from "@/types";
3+
4+
interface FetchLabelsParams {
5+
labelType: LabelType;
6+
}
7+
8+
export const fetchLabels = async ({
9+
labelType,
10+
}: FetchLabelsParams): Promise<Label[]> => {
11+
const urlWithQuery = `${LABELS_API_ENDPOINT}?type=${labelType}`;
12+
13+
const response: ApiResponse<Label[]> = await fetchService(urlWithQuery, {
14+
method: "GET",
15+
});
16+
17+
if (!response.success) {
18+
throw new Error(response?.error);
19+
}
20+
21+
return response.data;
22+
};

api/labels/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from "./useCreateLabel";
22
export * from "./useGetLabels";
33
export * from "./useUpdateLabel";
44
export * from "./useDeleteLabel";
5+
export * from "./fetchLabels";

api/labels/useCreateLabel.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ export const useCreateLabel = () => {
88
const queryClient = useQueryClient();
99

1010
const { mutateAsync: createLabel, isPending } = useMutation({
11-
mutationFn: async (
12-
newLabel: Pick<Label, "name" | "color" | "user_id" | "type">
13-
) => {
11+
mutationFn: async (newLabel: Pick<Label, "name" | "color" | "type">) => {
1412
const response: ApiResponse<Label> = await fetchService(
1513
LABELS_API_ENDPOINT,
1614
{

api/labels/useGetLabels.ts

+4-29
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,14 @@
1-
import { fetchService } from "@/services";
2-
import { ApiResponse, LABELS_API_ENDPOINT, Label, LabelType } from "@/types";
1+
import { LABELS_API_ENDPOINT, LabelType } from "@/types";
32
import { useQuery } from "@tanstack/react-query";
4-
import { useGetUser } from "../../hooks/useGetUser";
3+
import { fetchLabels } from "./fetchLabels";
54

65
interface UseGetLabelsParams {
76
labelType: LabelType;
87
}
98

10-
interface FetchLabelsParams extends UseGetLabelsParams {
11-
userId: string;
12-
}
13-
14-
const fetchLabels = async ({
15-
userId,
16-
labelType,
17-
}: FetchLabelsParams): Promise<Label[]> => {
18-
const urlWithQuery = `${LABELS_API_ENDPOINT}?userId=${userId}&type=${labelType}`;
19-
20-
const response: ApiResponse<Label[]> = await fetchService(urlWithQuery, {
21-
method: "GET",
22-
});
23-
24-
if (!response.success) {
25-
throw new Error(response?.error);
26-
}
27-
28-
return response.data;
29-
};
30-
319
export const useGetLabels = ({ labelType }: UseGetLabelsParams) => {
32-
const { userId } = useGetUser();
33-
3410
return useQuery({
35-
queryFn: () => fetchLabels({ userId, labelType }),
36-
queryKey: [LABELS_API_ENDPOINT, userId, labelType],
37-
enabled: !!userId,
11+
queryFn: () => fetchLabels({ labelType }),
12+
queryKey: [LABELS_API_ENDPOINT, labelType],
3813
});
3914
};

app/api/auth/users/route.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { connectDb } from "@/services";
1+
import { connectDb } from "@/lib";
22
import User from "@/models/user";
33
import { NextRequest, NextResponse } from "next/server";
44
import { ZodError, z } from "zod";

app/api/categories/[id]/route.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NextRequest, NextResponse } from "next/server";
2-
import { connectDb } from "@/services";
2+
import { connectDb } from "@/lib";
33
import Category from "@/models/category";
44
import { ZodError, z } from "zod";
55
import Label from "@/models/label";

app/api/categories/route.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { NextRequest, NextResponse } from "next/server";
2-
import { connectDb } from "@/services";
2+
import { connectDb } from "@/lib";
33
import Category from "@/models/category";
44
import { ZodError, z } from "zod";
55
import User from "@/models/user";
66
import mongoose from "mongoose";
7+
import { getServerSession } from "next-auth";
8+
import { authConfig } from "@/configs";
79

810
interface NewCategoryRequest {
911
name: string;
10-
user_id: string;
1112
}
1213

1314
interface MongoError {
@@ -19,12 +20,13 @@ export async function GET(req: NextRequest) {
1920
await connectDb();
2021

2122
const { searchParams } = new URL(req.url);
22-
const userId = searchParams.get("userId");
2323
const searchQuery = searchParams.get("q");
24+
const session = await getServerSession({ ...authConfig });
25+
const userId = session?.user?.id;
2426

2527
if (!userId) {
2628
return NextResponse.json(
27-
{ success: false, error: "User id is required" },
29+
{ success: false, error: "Not authenticated" },
2830
{
2931
status: 400,
3032
}
@@ -80,15 +82,16 @@ export async function POST(req: NextRequest) {
8082
await connectDb();
8183

8284
const body = (await req.json()) as NewCategoryRequest;
85+
const session = await getServerSession({ ...authConfig });
86+
const userId = session?.user?.id;
8387

8488
const parsedBody = z
8589
.object({
8690
name: z.string().min(1),
87-
user_id: z.string(),
8891
})
8992
.parse(body);
9093

91-
const user = await User.findById(body.user_id);
94+
const user = await User.findById(userId);
9295

9396
if (!user) {
9497
return NextResponse.json(
@@ -100,7 +103,7 @@ export async function POST(req: NextRequest) {
100103
);
101104
}
102105

103-
const category = await Category.create({ ...parsedBody });
106+
const category = await Category.create({ ...parsedBody, user_id: userId });
104107

105108
return NextResponse.json({
106109
success: true,

app/api/code-items/[id]/route.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { NextRequest, NextResponse } from "next/server";
2-
import { connectDb } from "@/services";
2+
import { connectDb } from "@/lib";
33
import { ZodError, z } from "zod";
44
import CodeItem from "@/models/code-item";
5+
import { getServerSession } from "next-auth";
6+
import { authConfig } from "@/configs";
57

68
interface UpdateCodeItemRequest {
79
name: string;
@@ -105,8 +107,8 @@ export async function GET(
105107
try {
106108
await connectDb();
107109

108-
const { searchParams } = new URL(req.url);
109-
const userId = searchParams.get("userId");
110+
const session = await getServerSession({ ...authConfig });
111+
const userId = session?.user?.id;
110112
const codeItemId = params.id;
111113

112114
const codeItem = await CodeItem.findById(codeItemId);

0 commit comments

Comments
 (0)