general destination and verify
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
"use server";
|
||||
|
||||
import { getServerSession } from "next-auth";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { authOptions } from "@/lib/auth";
|
||||
import { profileService } from "@/server/services/profile.service";
|
||||
import { updateProfileSchema } from "./schemas";
|
||||
|
||||
export async function updateProfileAction(formData: FormData) {
|
||||
const session = await getServerSession(authOptions);
|
||||
if (!session?.user) {
|
||||
return { error: "Kamu harus login terlebih dahulu" };
|
||||
}
|
||||
|
||||
const interests = formData
|
||||
.getAll("interests")
|
||||
.map((v) => (v as string).trim())
|
||||
.filter(Boolean);
|
||||
|
||||
const raw = {
|
||||
bio: formData.get("bio"),
|
||||
city: formData.get("city"),
|
||||
instagram: formData.get("instagram"),
|
||||
interests,
|
||||
};
|
||||
|
||||
const parsed = updateProfileSchema.safeParse(raw);
|
||||
if (!parsed.success) {
|
||||
return { error: parsed.error.issues[0].message };
|
||||
}
|
||||
|
||||
try {
|
||||
await profileService.updateProfile(session.user.id, parsed.data);
|
||||
revalidatePath("/profile");
|
||||
revalidatePath(`/u/${session.user.id}`);
|
||||
return { success: true };
|
||||
} catch (err) {
|
||||
return { error: (err as Error).message };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,280 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { updateProfileAction } from "@/features/profile/actions";
|
||||
import { LIMITS } from "@/lib/limits";
|
||||
|
||||
interface ProfileEditorProps {
|
||||
userId: string;
|
||||
initial: {
|
||||
bio: string | null;
|
||||
city: string | null;
|
||||
interests: string[];
|
||||
instagram: string | null;
|
||||
} | null;
|
||||
}
|
||||
|
||||
export function ProfileEditor({ userId, initial }: ProfileEditorProps) {
|
||||
const router = useRouter();
|
||||
const [open, setOpen] = useState(initial === null);
|
||||
const [error, setError] = useState("");
|
||||
const [success, setSuccess] = useState("");
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const [bio, setBio] = useState(initial?.bio ?? "");
|
||||
const [city, setCity] = useState(initial?.city ?? "");
|
||||
const [instagram, setInstagram] = useState(initial?.instagram ?? "");
|
||||
const [interests, setInterests] = useState<string[]>(initial?.interests ?? []);
|
||||
const [interestDraft, setInterestDraft] = useState("");
|
||||
|
||||
function addInterest() {
|
||||
const v = interestDraft.trim().toLowerCase();
|
||||
if (!v) return;
|
||||
if (interests.includes(v)) {
|
||||
setInterestDraft("");
|
||||
return;
|
||||
}
|
||||
if (interests.length >= LIMITS.MAX_PROFILE_INTERESTS_COUNT) {
|
||||
setError(`Maksimal ${LIMITS.MAX_PROFILE_INTERESTS_COUNT} minat`);
|
||||
return;
|
||||
}
|
||||
setInterests([...interests, v]);
|
||||
setInterestDraft("");
|
||||
setError("");
|
||||
}
|
||||
|
||||
function removeInterest(tag: string) {
|
||||
setInterests(interests.filter((t) => t !== tag));
|
||||
}
|
||||
|
||||
function handleInterestKeyDown(e: React.KeyboardEvent<HTMLInputElement>) {
|
||||
if (e.key === "Enter" || e.key === ",") {
|
||||
e.preventDefault();
|
||||
addInterest();
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
|
||||
e.preventDefault();
|
||||
setError("");
|
||||
setSuccess("");
|
||||
setLoading(true);
|
||||
|
||||
const formData = new FormData();
|
||||
if (bio.trim()) formData.set("bio", bio.trim());
|
||||
if (city.trim()) formData.set("city", city.trim());
|
||||
if (instagram.trim()) formData.set("instagram", instagram.trim());
|
||||
interests.forEach((t) => formData.append("interests", t));
|
||||
|
||||
const result = await updateProfileAction(formData);
|
||||
setLoading(false);
|
||||
|
||||
if (result.error) {
|
||||
setError(result.error);
|
||||
} else {
|
||||
setSuccess("Profil berhasil disimpan");
|
||||
router.refresh();
|
||||
}
|
||||
}
|
||||
|
||||
if (!open) {
|
||||
return (
|
||||
<section className="rounded-2xl border border-neutral-200 bg-white p-4 shadow-sm sm:p-5">
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<div className="min-w-0">
|
||||
<h2 className="text-sm font-bold text-neutral-800 sm:text-base">
|
||||
Profil sosial
|
||||
</h2>
|
||||
<p className="mt-0.5 truncate text-xs text-neutral-500">
|
||||
{initial?.city || initial?.bio
|
||||
? "Profil terisi — klik untuk edit"
|
||||
: "Lengkapi profil supaya orang lain mengenalmu"}
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex shrink-0 gap-2">
|
||||
<a
|
||||
href={`/u/${userId}`}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="rounded-lg border border-neutral-200 px-3 py-1.5 text-xs font-medium text-neutral-600 hover:bg-neutral-50"
|
||||
>
|
||||
Lihat publik ↗
|
||||
</a>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setOpen(true)}
|
||||
className="rounded-lg bg-primary-600 px-3 py-1.5 text-xs font-semibold text-white hover:bg-primary-700"
|
||||
>
|
||||
Edit profil
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<section className="rounded-2xl border border-neutral-200 bg-white p-5 shadow-sm sm:p-6">
|
||||
<div className="mb-4 flex items-center justify-between gap-3">
|
||||
<h2 className="text-base font-bold text-neutral-800 sm:text-lg">
|
||||
Edit profil sosial
|
||||
</h2>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setOpen(false)}
|
||||
className="text-xs font-medium text-neutral-500 hover:text-neutral-700"
|
||||
>
|
||||
Tutup
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="mb-4 rounded-xl bg-red-50 px-4 py-3 text-sm font-medium text-red-600">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
{success && (
|
||||
<div className="mb-4 rounded-xl bg-secondary-50 px-4 py-3 text-sm font-medium text-secondary-700">
|
||||
{success}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
<div>
|
||||
<label
|
||||
htmlFor="bio"
|
||||
className="mb-1.5 block text-sm font-semibold text-neutral-700"
|
||||
>
|
||||
Bio singkat
|
||||
</label>
|
||||
<textarea
|
||||
id="bio"
|
||||
value={bio}
|
||||
onChange={(e) => setBio(e.target.value)}
|
||||
rows={3}
|
||||
maxLength={LIMITS.MAX_PROFILE_BIO_LENGTH}
|
||||
placeholder="Cerita singkat tentang kamu — vibe, gaya jalan, hal yang kamu cari di trip bareng..."
|
||||
className="w-full rounded-xl border border-neutral-200 bg-neutral-50 px-4 py-2.5 text-sm text-neutral-800 placeholder:text-neutral-400 focus:bg-white"
|
||||
/>
|
||||
<p className="mt-1 text-right text-[11px] text-neutral-400">
|
||||
{bio.length}/{LIMITS.MAX_PROFILE_BIO_LENGTH}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-4 sm:grid-cols-2">
|
||||
<div>
|
||||
<label
|
||||
htmlFor="city"
|
||||
className="mb-1.5 block text-sm font-semibold text-neutral-700"
|
||||
>
|
||||
Kota
|
||||
</label>
|
||||
<input
|
||||
id="city"
|
||||
type="text"
|
||||
value={city}
|
||||
onChange={(e) => setCity(e.target.value)}
|
||||
maxLength={LIMITS.MAX_PROFILE_CITY_LENGTH}
|
||||
placeholder="Bandung"
|
||||
className="w-full rounded-xl border border-neutral-200 bg-neutral-50 px-4 py-2.5 text-sm text-neutral-800 placeholder:text-neutral-400 focus:bg-white"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label
|
||||
htmlFor="instagram"
|
||||
className="mb-1.5 block text-sm font-semibold text-neutral-700"
|
||||
>
|
||||
Instagram <span className="text-xs font-normal text-neutral-400">(opsional)</span>
|
||||
</label>
|
||||
<div className="flex items-center rounded-xl border border-neutral-200 bg-neutral-50 px-3 focus-within:bg-white">
|
||||
<span className="text-sm text-neutral-400">@</span>
|
||||
<input
|
||||
id="instagram"
|
||||
type="text"
|
||||
value={instagram}
|
||||
onChange={(e) =>
|
||||
setInstagram(e.target.value.replace(/^@/, ""))
|
||||
}
|
||||
maxLength={LIMITS.MAX_PROFILE_INSTAGRAM_LENGTH}
|
||||
placeholder="username"
|
||||
className="w-full bg-transparent py-2.5 pl-1 text-sm text-neutral-800 placeholder:text-neutral-400 outline-none"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label
|
||||
htmlFor="interest-input"
|
||||
className="mb-1.5 block text-sm font-semibold text-neutral-700"
|
||||
>
|
||||
Minat aktivitas{" "}
|
||||
<span className="text-xs font-normal text-neutral-400">
|
||||
({interests.length}/{LIMITS.MAX_PROFILE_INTERESTS_COUNT})
|
||||
</span>
|
||||
</label>
|
||||
<div className="mb-2 flex flex-wrap gap-1.5">
|
||||
{interests.map((tag) => (
|
||||
<span
|
||||
key={tag}
|
||||
className="inline-flex items-center gap-1 rounded-full bg-secondary-50 px-2.5 py-0.5 text-xs font-medium text-secondary-700"
|
||||
>
|
||||
#{tag}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => removeInterest(tag)}
|
||||
className="text-secondary-500 hover:text-red-600"
|
||||
aria-label={`Hapus ${tag}`}
|
||||
>
|
||||
×
|
||||
</button>
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<input
|
||||
id="interest-input"
|
||||
type="text"
|
||||
value={interestDraft}
|
||||
onChange={(e) => setInterestDraft(e.target.value)}
|
||||
onKeyDown={handleInterestKeyDown}
|
||||
maxLength={LIMITS.MAX_PROFILE_INTEREST_LENGTH}
|
||||
placeholder="hiking, fotografi, yoga... (Enter untuk tambah)"
|
||||
className="flex-1 rounded-xl border border-neutral-200 bg-neutral-50 px-4 py-2.5 text-sm text-neutral-800 placeholder:text-neutral-400 focus:bg-white"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={addInterest}
|
||||
disabled={
|
||||
interests.length >= LIMITS.MAX_PROFILE_INTERESTS_COUNT
|
||||
}
|
||||
className="rounded-xl border border-neutral-200 px-3 py-2.5 text-sm font-medium text-neutral-700 hover:bg-neutral-50 disabled:cursor-not-allowed disabled:opacity-50"
|
||||
>
|
||||
+ Tambah
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex gap-2 pt-2">
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
className="rounded-xl bg-primary-600 px-5 py-2.5 text-sm font-bold text-white shadow-md shadow-primary-600/20 transition-colors hover:bg-primary-700 disabled:cursor-not-allowed disabled:opacity-50"
|
||||
>
|
||||
{loading ? "Menyimpan..." : "Simpan profil"}
|
||||
</button>
|
||||
<a
|
||||
href={`/u/${userId}`}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="rounded-xl border border-neutral-200 px-4 py-2.5 text-sm font-medium text-neutral-700 hover:bg-neutral-50"
|
||||
>
|
||||
Lihat publik ↗
|
||||
</a>
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import { z } from "zod/v4";
|
||||
import { LIMITS } from "@/lib/limits";
|
||||
|
||||
const optionalTrimmed = (max: number, label: string) =>
|
||||
z.preprocess(
|
||||
(val) => {
|
||||
if (val == null) return undefined;
|
||||
const s = String(val).trim();
|
||||
return s === "" ? undefined : s;
|
||||
},
|
||||
z.string().max(max, `${label} maksimal ${max} karakter`).optional()
|
||||
);
|
||||
|
||||
const interestSchema = z
|
||||
.string()
|
||||
.trim()
|
||||
.min(2, "Minat minimal 2 karakter")
|
||||
.max(
|
||||
LIMITS.MAX_PROFILE_INTEREST_LENGTH,
|
||||
`Setiap minat maksimal ${LIMITS.MAX_PROFILE_INTEREST_LENGTH} karakter`
|
||||
)
|
||||
.regex(
|
||||
/^[a-zA-Z0-9 \-]+$/,
|
||||
"Minat hanya boleh huruf, angka, spasi, atau strip"
|
||||
);
|
||||
|
||||
export const updateProfileSchema = z.object({
|
||||
bio: optionalTrimmed(LIMITS.MAX_PROFILE_BIO_LENGTH, "Bio"),
|
||||
city: optionalTrimmed(LIMITS.MAX_PROFILE_CITY_LENGTH, "Kota"),
|
||||
instagram: z.preprocess(
|
||||
(val) => {
|
||||
if (val == null) return undefined;
|
||||
const s = String(val).trim().replace(/^@/, "");
|
||||
return s === "" ? undefined : s;
|
||||
},
|
||||
z
|
||||
.string()
|
||||
.max(
|
||||
LIMITS.MAX_PROFILE_INSTAGRAM_LENGTH,
|
||||
`Instagram maksimal ${LIMITS.MAX_PROFILE_INSTAGRAM_LENGTH} karakter`
|
||||
)
|
||||
.regex(
|
||||
/^[a-zA-Z0-9._]+$/,
|
||||
"Username Instagram hanya boleh huruf, angka, titik, atau underscore"
|
||||
)
|
||||
.optional()
|
||||
),
|
||||
interests: z
|
||||
.array(interestSchema)
|
||||
.max(
|
||||
LIMITS.MAX_PROFILE_INTERESTS_COUNT,
|
||||
`Maksimal ${LIMITS.MAX_PROFILE_INTERESTS_COUNT} minat`
|
||||
)
|
||||
.default([]),
|
||||
});
|
||||
|
||||
export type UpdateProfileInput = z.infer<typeof updateProfileSchema>;
|
||||
Reference in New Issue
Block a user