Skip to content

Commit 2a31d54

Browse files
committed
refactor(actions): remove auth, categories and faqs actions and split them into separate files
1 parent f5f0ac4 commit 2a31d54

File tree

14 files changed

+102
-74
lines changed

14 files changed

+102
-74
lines changed

src/actions/auth.ts renamed to src/actions/auth/get-admin-user-or-redirect.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,6 @@ import { redirect } from "next/navigation";
44
import { createClient } from "@/lib/supabase/server";
55
import { HOME_PATH } from "@/config/constants";
66

7-
export async function logout() {
8-
const supabase = await createClient();
9-
await supabase.auth.signOut();
10-
}
11-
12-
export async function getAuthenticatedUser() {
13-
const supabase = await createClient();
14-
const {
15-
data: { user },
16-
error,
17-
} = await supabase.auth.getUser();
18-
19-
if (error || !user) return null;
20-
return user;
21-
}
22-
237
export async function getAdminUserOrRedirect() {
248
const supabase = await createClient();
259
const {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use server";
2+
3+
import { createClient } from "@/lib/supabase/server";
4+
5+
export async function getAuthenticatedUser() {
6+
const supabase = await createClient();
7+
const {
8+
data: { user },
9+
error,
10+
} = await supabase.auth.getUser();
11+
12+
if (error || !user) return null;
13+
return user;
14+
}

src/actions/auth/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./get-admin-user-or-redirect";
2+
export * from "./get-authenticated-user";
3+
export * from "./logout";

src/actions/auth/logout.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use server";
2+
3+
import { createClient } from "@/lib/supabase/server";
4+
5+
export async function logout() {
6+
const supabase = await createClient();
7+
await supabase.auth.signOut();
8+
}

src/actions/categories.ts

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use server";
2+
3+
import { createClient } from "@/lib/supabase/server";
4+
import type { CategoryForm } from "@/types";
5+
6+
export const createCategoryByAdmin = async (input: CategoryForm) => {
7+
const supabase = await createClient();
8+
const { data, error } = await supabase
9+
.from("categories")
10+
.insert(input)
11+
.select()
12+
.single();
13+
14+
if (error) throw new Error("Error creating category");
15+
return data;
16+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use server";
2+
3+
import { createClient } from "@/lib/supabase/server";
4+
5+
export async function deleteCategoryByAdmin(categoryId: string) {
6+
const supabase = await createClient();
7+
const { error } = await supabase
8+
.from("categories")
9+
.delete()
10+
.eq("id", categoryId);
11+
12+
if (error) throw new Error("Failed to delete category");
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use server";
2+
3+
import { createClient } from "@/lib/supabase/server";
4+
5+
export const getCategories = async () => {
6+
const supabase = await createClient();
7+
const { data, error } = await supabase.from("categories").select("*");
8+
9+
if (error) throw error;
10+
return data;
11+
};

src/actions/categories/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from "./create-category-by-admin";
2+
export * from "./delete-category-by-admin";
3+
export * from "./get-categories";
4+
export * from "./update-category-by-admin";
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use server";
2+
3+
import { createClient } from "@/lib/supabase/server";
4+
import type { CategoryForm } from "@/types";
5+
6+
export const updateCategoryByAdmin = async (
7+
id: string,
8+
input: CategoryForm,
9+
) => {
10+
const supabase = await createClient();
11+
const { data, error } = await supabase
12+
.from("categories")
13+
.update(input)
14+
.eq("id", id)
15+
.select()
16+
.single();
17+
18+
if (error) throw new Error("Error updating category");
19+
return data;
20+
};

0 commit comments

Comments
 (0)