feat: update profile and thought handling with token validation and router refresh
This commit is contained in:
@@ -5,8 +5,7 @@ import { useRouter } from "next/navigation";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { z } from "zod";
|
||||
import { Me, UpdateProfileSchema, uploadAvatar, uploadBanner } from "@/lib/api";
|
||||
import { updateProfile } from "@/app/actions/profile";
|
||||
import { Me, UpdateProfileSchema, updateProfile, uploadAvatar, uploadBanner } from "@/lib/api";
|
||||
import { toast } from "sonner";
|
||||
import { Button } from "@/components/ui/button";
|
||||
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>) {
|
||||
if (!token) return;
|
||||
toast.info("Updating your profile...");
|
||||
try {
|
||||
await updateProfile(currentUser.username, values);
|
||||
await updateProfile(values, token);
|
||||
router.refresh();
|
||||
toast.success("Profile updated successfully!");
|
||||
} catch (err) {
|
||||
toast.error(`Failed to update profile. ${err}`);
|
||||
|
||||
@@ -7,10 +7,10 @@ import {
|
||||
CardHeader,
|
||||
} from "@/components/ui/card";
|
||||
import { UserAvatar } from "./user-avatar";
|
||||
import { Me, Thought } from "@/lib/api";
|
||||
import { deleteThought } from "@/app/actions/thoughts";
|
||||
import { Me, Thought, deleteThought } from "@/lib/api";
|
||||
import { format, formatDistanceToNow } from "date-fns";
|
||||
import { useAuth } from "@/hooks/use-auth";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import {
|
||||
@@ -69,6 +69,7 @@ export function ThoughtCard({
|
||||
const [isReplyOpen, setIsReplyOpen] = useState(false);
|
||||
const [deletingState, setDeletingState] = useState<"idle" | "shaking" | "fading">("idle");
|
||||
const { token } = useAuth();
|
||||
const router = useRouter();
|
||||
const timeAgo = formatDistanceToNow(new Date(thought.createdAt), {
|
||||
addSuffix: true,
|
||||
});
|
||||
@@ -93,7 +94,9 @@ export function ThoughtCard({
|
||||
setDeletingState("fading");
|
||||
await new Promise((r) => setTimeout(r, 300));
|
||||
try {
|
||||
await deleteThought(thought.id);
|
||||
if (!token) throw new Error("not authenticated");
|
||||
await deleteThought(thought.id, token);
|
||||
router.refresh();
|
||||
toast.success("Thought deleted.");
|
||||
} catch (error) {
|
||||
console.error("Failed to delete thought:", error);
|
||||
|
||||
@@ -20,13 +20,13 @@ import {
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} 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 { useRouter } from "next/navigation";
|
||||
import { toast } from "sonner";
|
||||
import { Globe, Lock, Users } from "lucide-react";
|
||||
import { useState } from "react";
|
||||
import { Confetti } from "./confetti";
|
||||
import { createThought } from "@/app/actions/thoughts";
|
||||
|
||||
const DEFAULT_MOODS = [
|
||||
"relaxed 😌",
|
||||
@@ -68,6 +68,7 @@ export function ThoughtForm({
|
||||
currentUser,
|
||||
}: ThoughtFormProps) {
|
||||
const { token } = useAuth();
|
||||
const router = useRouter();
|
||||
const [showConfetti, setShowConfetti] = useState(false);
|
||||
|
||||
const allMoods = [
|
||||
@@ -97,7 +98,8 @@ export function ThoughtForm({
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await createThought(values);
|
||||
await createThought(values, token);
|
||||
router.refresh();
|
||||
toast.success(replyToId ? "Reply posted!" : "Thought posted!");
|
||||
setShowConfetti(true);
|
||||
form.reset();
|
||||
|
||||
Reference in New Issue
Block a user