cg / hackerproducts

63 files · 5,740 lines · 159.1 KB — Next.js 15, React 19, TypeScript, PostgreSQL, Object Storage, plain CSS.

Quick jump: page.tsx / page.tsx / globals.css / schema.sql / package.json

root / src / components / ProfileForm.tsx

src/components/ProfileForm.tsx · 149 lines · 4.3 KB← back to folder
1"use client";
2
3import { useState } from "react";
4import { useRouter } from "next/navigation";
5
6export function ProfileForm({
7 initial,
8}: {
9 initial: {
10 username: string;
11 bio: string;
12 website_url: string;
13 avatar_url: string | null;
14 };
15}) {
16 const router = useRouter();
17 const [username, setUsername] = useState(initial.username);
18 const [bio, setBio] = useState(initial.bio);
19 const [websiteUrl, setWebsiteUrl] = useState(initial.website_url);
20 const [avatarUrl, setAvatarUrl] = useState(initial.avatar_url ?? "");
21 const [uploading, setUploading] = useState(false);
22 const [saving, setSaving] = useState(false);
23 const [error, setError] = useState<string | null>(null);
24 const [saved, setSaved] = useState(false);
25
26 async function handleAvatarUpload(file: File) {
27 setUploading(true);
28 setError(null);
29 try {
30 const formData = new FormData();
31 formData.append("file", file);
32 formData.append("kind", "avatar");
33 const res = await fetch("/api/upload", { method: "POST", body: formData });
34 const data = (await res.json()) as { url?: string; error?: string };
35 if (!res.ok || !data.url) {
36 setError(data.error || "Upload failed.");
37 return;
38 }
39 setAvatarUrl(data.url);
40 } catch {
41 setError("Upload failed.");
42 } finally {
43 setUploading(false);
44 }
45 }
46
47 async function handleSubmit(e: React.FormEvent) {
48 e.preventDefault();
49 setSaving(true);
50 setError(null);
51 setSaved(false);
52 try {
53 const res = await fetch("/api/profile", {
54 method: "PATCH",
55 headers: { "Content-Type": "application/json" },
56 body: JSON.stringify({
57 username,
58 bio,
59 website_url: websiteUrl,
60 avatar_url: avatarUrl || null,
61 }),
62 });
63 const data = (await res.json()) as { username?: string; error?: string };
64 if (!res.ok) {
65 setError(data.error || "Could not update profile.");
66 return;
67 }
68 setSaved(true);
69 router.push(`/${data.username ?? username}`);
70 router.refresh();
71 } catch {
72 setError("Network error.");
73 } finally {
74 setSaving(false);
75 }
76 }
77
78 return (
79 <form className="plain-form" onSubmit={handleSubmit}>
80 {error && <div className="form-error">{error}</div>}
81 {saved && <p className="form-status">Profile saved.</p>}
82
83 <div className="form-field">
84 <label htmlFor="username">Username</label>
85 <input
86 id="username"
87 type="text"
88 required
89 value={username}
90 onChange={(e) => setUsername(e.target.value)}
91 />
92 <p className="form-hint">Your profile URL: /{username || "username"}</p>
93 </div>
94
95 <div className="form-field">
96 <label htmlFor="bio">Short biography</label>
97 <input
98 id="bio"
99 type="text"
100 maxLength={300}
101 value={bio}
102 onChange={(e) => setBio(e.target.value)}
103 placeholder="Building small tools on the internet."
104 />
105 </div>
106
107 <div className="form-field">
108 <label htmlFor="website_url">Personal website</label>
109 <input
110 id="website_url"
111 type="url"
112 value={websiteUrl}
113 onChange={(e) => setWebsiteUrl(e.target.value)}
114 placeholder="https://example.com"
115 />
116 </div>
117
118 <div className="form-field">
119 <label htmlFor="avatar_file">Profile picture</label>
120 <div className="upload-row">
121 <input
122 id="avatar_file"
123 type="file"
124 accept="image/png,image/jpeg,image/webp,image/gif"
125 disabled={uploading}
126 onChange={(e) => {
127 const file = e.target.files?.[0];
128 if (file) void handleAvatarUpload(file);
129 }}
130 />
131 {uploading && <span className="muted">Uploading...</span>}
132 {avatarUrl && (
133 <>
134 <img src={avatarUrl} alt="Avatar preview" width={32} height={32} className="maker-avatar-sm" />
135 <button type="button" className="link-button" onClick={() => setAvatarUrl("")}>
136 remove
137 </button>
138 </>
139 )}
140 </div>
141 </div>
142
143 <button type="submit" className="btn" disabled={saving || uploading}>
144 {saving ? "Saving..." : "Save profile"}
145 </button>
146 </form>
147 );
148}
149
cg / hackerproducts — hacker products