"use server"; import { revalidatePath, revalidateTag } from "next/cache"; import { ACCOUNTS_TAG, USERS_TAG, fetchApi, getAccountsPage, getUsersPage, type AccountsPageOpts, type Page, type UsersPageOpts, } from "@/lib/api"; import type { Acc, AccUpdate, User, UserUpdate } from "@/lib/types"; export type ActionResult = { ok: true } | { ok: false; error: string }; // Each mutation evicts the matching tag so the next GET bypasses the // 30s data cache and re-reads from MySQL. export async function updateAccount(data: AccUpdate): Promise { try { await fetchApi("/update-acc-data", { method: "POST", body: data }); revalidateTag(ACCOUNTS_TAG); revalidatePath("/"); return { ok: true }; } catch (err) { return { ok: false, error: err instanceof Error ? err.message : String(err) }; } } export async function updateUser(data: UserUpdate): Promise { try { await fetchApi("/update-user-data", { method: "POST", body: data }); revalidateTag(USERS_TAG); revalidatePath("/users"); return { ok: true }; } catch (err) { return { ok: false, error: err instanceof Error ? err.message : String(err) }; } } export async function createAccount(data: AccUpdate): Promise { try { await fetchApi("/create-acc-data", { method: "POST", body: data }); revalidateTag(ACCOUNTS_TAG); revalidatePath("/"); return { ok: true }; } catch (err) { return { ok: false, error: err instanceof Error ? err.message : String(err) }; } } export async function createUser(data: UserUpdate): Promise { try { await fetchApi("/create-user-data", { method: "POST", body: data }); revalidateTag(USERS_TAG); revalidatePath("/users"); return { ok: true }; } catch (err) { return { ok: false, error: err instanceof Error ? err.message : String(err) }; } } export async function deleteAccount(username: string): Promise { try { await fetchApi("/delete-acc-data", { method: "POST", body: { username } }); revalidateTag(ACCOUNTS_TAG); revalidatePath("/"); return { ok: true }; } catch (err) { return { ok: false, error: err instanceof Error ? err.message : String(err) }; } } export async function deleteUser(f_username: string): Promise { try { await fetchApi("/delete-user-data", { method: "POST", body: { f_username } }); revalidateTag(USERS_TAG); revalidatePath("/users"); return { ok: true }; } catch (err) { return { ok: false, error: err instanceof Error ? err.message : String(err) }; } } // ---- Pagination + force-refresh ---- export async function loadMoreAccounts(opts: AccountsPageOpts): Promise> { return getAccountsPage(opts); } export async function loadMoreUsers(opts: UsersPageOpts): Promise> { return getUsersPage(opts); } // Force-refresh evicts the cached tag before refetching the first page, // so manual Refresh always returns DB-fresh data even if the cache is // still warm. export async function refreshAccounts(opts: AccountsPageOpts = {}): Promise> { revalidateTag(ACCOUNTS_TAG); return getAccountsPage({ ...opts, offset: 0 }); } export async function refreshUsers(opts: UsersPageOpts = {}): Promise> { revalidateTag(USERS_TAG); return getUsersPage({ ...opts, offset: 0 }); }