feat: update profile and thought handling with token validation and router refresh
Some checks failed
test / unit (push) Has been cancelled
lint / lint (push) Has been cancelled

This commit is contained in:
2026-07-04 15:31:58 +02:00
parent c22bc68a8b
commit 052d8c67bb
3 changed files with 15 additions and 9 deletions

View File

@@ -5,8 +5,7 @@ import { useRouter } from "next/navigation";
import { useForm } from "react-hook-form"; import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod"; import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod"; import { z } from "zod";
import { Me, UpdateProfileSchema, uploadAvatar, uploadBanner } from "@/lib/api"; import { Me, UpdateProfileSchema, updateProfile, uploadAvatar, uploadBanner } from "@/lib/api";
import { updateProfile } from "@/app/actions/profile";
import { toast } from "sonner"; import { toast } from "sonner";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { Card, CardContent, CardFooter } from "@/components/ui/card"; import { Card, CardContent, CardFooter } from "@/components/ui/card";
@@ -83,9 +82,11 @@ export function EditProfileForm({ currentUser, token }: EditProfileFormProps) {
}); });
async function onSubmit(values: z.infer<typeof UpdateProfileSchema>) { async function onSubmit(values: z.infer<typeof UpdateProfileSchema>) {
if (!token) return;
toast.info("Updating your profile..."); toast.info("Updating your profile...");
try { try {
await updateProfile(currentUser.username, values); await updateProfile(values, token);
router.refresh();
toast.success("Profile updated successfully!"); toast.success("Profile updated successfully!");
} catch (err) { } catch (err) {
toast.error(`Failed to update profile. ${err}`); toast.error(`Failed to update profile. ${err}`);

View File

@@ -7,10 +7,10 @@ import {
CardHeader, CardHeader,
} from "@/components/ui/card"; } from "@/components/ui/card";
import { UserAvatar } from "./user-avatar"; import { UserAvatar } from "./user-avatar";
import { Me, Thought } from "@/lib/api"; import { Me, Thought, deleteThought } from "@/lib/api";
import { deleteThought } from "@/app/actions/thoughts";
import { format, formatDistanceToNow } from "date-fns"; import { format, formatDistanceToNow } from "date-fns";
import { useAuth } from "@/hooks/use-auth"; import { useAuth } from "@/hooks/use-auth";
import { useRouter } from "next/navigation";
import { useState } from "react"; import { useState } from "react";
import { toast } from "sonner"; import { toast } from "sonner";
import { import {
@@ -69,6 +69,7 @@ export function ThoughtCard({
const [isReplyOpen, setIsReplyOpen] = useState(false); const [isReplyOpen, setIsReplyOpen] = useState(false);
const [deletingState, setDeletingState] = useState<"idle" | "shaking" | "fading">("idle"); const [deletingState, setDeletingState] = useState<"idle" | "shaking" | "fading">("idle");
const { token } = useAuth(); const { token } = useAuth();
const router = useRouter();
const timeAgo = formatDistanceToNow(new Date(thought.createdAt), { const timeAgo = formatDistanceToNow(new Date(thought.createdAt), {
addSuffix: true, addSuffix: true,
}); });
@@ -93,7 +94,9 @@ export function ThoughtCard({
setDeletingState("fading"); setDeletingState("fading");
await new Promise((r) => setTimeout(r, 300)); await new Promise((r) => setTimeout(r, 300));
try { try {
await deleteThought(thought.id); if (!token) throw new Error("not authenticated");
await deleteThought(thought.id, token);
router.refresh();
toast.success("Thought deleted."); toast.success("Thought deleted.");
} catch (error) { } catch (error) {
console.error("Failed to delete thought:", error); console.error("Failed to delete thought:", error);

View File

@@ -20,13 +20,13 @@ import {
SelectTrigger, SelectTrigger,
SelectValue, SelectValue,
} from "@/components/ui/select"; } from "@/components/ui/select";
import { CreateThoughtSchema, type Me } from "@/lib/api"; import { CreateThoughtSchema, createThought, type Me } from "@/lib/api";
import { useAuth } from "@/hooks/use-auth"; import { useAuth } from "@/hooks/use-auth";
import { useRouter } from "next/navigation";
import { toast } from "sonner"; import { toast } from "sonner";
import { Globe, Lock, Users } from "lucide-react"; import { Globe, Lock, Users } from "lucide-react";
import { useState } from "react"; import { useState } from "react";
import { Confetti } from "./confetti"; import { Confetti } from "./confetti";
import { createThought } from "@/app/actions/thoughts";
const DEFAULT_MOODS = [ const DEFAULT_MOODS = [
"relaxed 😌", "relaxed 😌",
@@ -68,6 +68,7 @@ export function ThoughtForm({
currentUser, currentUser,
}: ThoughtFormProps) { }: ThoughtFormProps) {
const { token } = useAuth(); const { token } = useAuth();
const router = useRouter();
const [showConfetti, setShowConfetti] = useState(false); const [showConfetti, setShowConfetti] = useState(false);
const allMoods = [ const allMoods = [
@@ -97,7 +98,8 @@ export function ThoughtForm({
return; return;
} }
try { try {
await createThought(values); await createThought(values, token);
router.refresh();
toast.success(replyToId ? "Reply posted!" : "Thought posted!"); toast.success(replyToId ? "Reply posted!" : "Thought posted!");
setShowConfetti(true); setShowConfetti(true);
form.reset(); form.reset();