diff --git a/thoughts-frontend/app/actions/profile.ts b/thoughts-frontend/app/actions/profile.ts new file mode 100644 index 0000000..214fcdf --- /dev/null +++ b/thoughts-frontend/app/actions/profile.ts @@ -0,0 +1,23 @@ +"use server"; + +import { revalidateTag } from "next/cache"; +import { cookies } from "next/headers"; +import { updateProfile as apiUpdateProfile, UpdateProfileSchema } from "@/lib/api"; +import { z } from "zod"; + +async function getToken(): Promise { + const token = (await cookies()).get("auth_token")?.value; + if (!token) throw new Error("Not authenticated"); + return token; +} + +export async function updateProfile( + username: string, + data: z.infer +) { + const token = await getToken(); + const updated = await apiUpdateProfile(data, token); + revalidateTag(`profile:${username}`); + revalidateTag("me"); + return updated; +} diff --git a/thoughts-frontend/app/actions/social.ts b/thoughts-frontend/app/actions/social.ts new file mode 100644 index 0000000..adb0f7e --- /dev/null +++ b/thoughts-frontend/app/actions/social.ts @@ -0,0 +1,28 @@ +"use server"; + +import { revalidateTag } from "next/cache"; +import { cookies } from "next/headers"; +import { + followUser as apiFollowUser, + unfollowUser as apiUnfollowUser, +} from "@/lib/api"; + +async function getToken(): Promise { + const token = (await cookies()).get("auth_token")?.value; + if (!token) throw new Error("Not authenticated"); + return token; +} + +export async function followUser(username: string) { + const token = await getToken(); + await apiFollowUser(username, token); + revalidateTag(`profile:${username}`); + revalidateTag("feed"); +} + +export async function unfollowUser(username: string) { + const token = await getToken(); + await apiUnfollowUser(username, token); + revalidateTag(`profile:${username}`); + revalidateTag("feed"); +} diff --git a/thoughts-frontend/app/actions/thoughts.ts b/thoughts-frontend/app/actions/thoughts.ts new file mode 100644 index 0000000..6d9e149 --- /dev/null +++ b/thoughts-frontend/app/actions/thoughts.ts @@ -0,0 +1,30 @@ +"use server"; + +import { revalidateTag } from "next/cache"; +import { cookies } from "next/headers"; +import { + createThought as apiCreateThought, + deleteThought as apiDeleteThought, + CreateThoughtSchema, +} from "@/lib/api"; +import { z } from "zod"; + +async function getToken(): Promise { + const token = (await cookies()).get("auth_token")?.value; + if (!token) throw new Error("Not authenticated"); + return token; +} + +export async function createThought(data: z.infer) { + const token = await getToken(); + const thought = await apiCreateThought(data, token); + revalidateTag("feed"); + return thought; +} + +export async function deleteThought(thoughtId: string) { + const token = await getToken(); + await apiDeleteThought(thoughtId, token); + revalidateTag("feed"); + revalidateTag(`thought:${thoughtId}`); +}