1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
import { useState } from "react";
import { Form, Link, redirect, useActionData, useLoaderData } from "react-router";
import type { ActionFunctionArgs } from "react-router";
import { createBand, getIpAddress, listArtists } from "~/lib/db.server";
export function loader() {
return { artists: listArtists() };
}
export async function action({ request }: ActionFunctionArgs) {
const fd = await request.formData();
const name = (fd.get("name") as string).trim();
const slug = (fd.get("slug") as string).trim();
const area = (fd.get("area") as string).trim() || null;
const description = (fd.get("description") as string).trim() || null;
const status = (fd.get("status") as string) || "active";
const message = (fd.get("message") as string).trim();
const links: { label: string; url: string }[] = JSON.parse(
(fd.get("links") as string) || "[]"
);
const artists: { id: string; role: string | null }[] = JSON.parse(
(fd.get("artists") as string) || "[]"
);
const errors: Record<string, string> = {};
if (!name) errors.name = "必須です";
if (!slug) errors.slug = "必須です";
if (!message) errors.message = "必須です";
if (Object.keys(errors).length > 0) return { errors };
const id = crypto.randomUUID();
try {
createBand({ id, slug, name, area, description, status, links, artists, message, ip_address: getIpAddress(request) });
} catch (e) {
if (e instanceof Error && e.message.includes("UNIQUE constraint failed: bands.slug")) {
return { errors: { slug: "このslugは既に使用されています" } };
}
throw e;
}
return redirect(`/bands/of/${id}`);
}
function toSlug(s: string) {
return s.trim().toLowerCase().replace(/\s+/g, "-").replace(/[^\w-ヿ一-鿿--]/g, "").replace(/^-+|-+$/g, "");
}
export default function BandNew() {
const { artists } = useLoaderData<typeof loader>();
const actionData = useActionData<typeof action>();
const errors = actionData?.errors ?? {};
const [name, setName] = useState("");
const [slug, setSlug] = useState("");
const [slugManual, setSlugManual] = useState(false);
const [links, setLinks] = useState<{ label: string; url: string }[]>([]);
const [picked, setPicked] = useState<{ id: string; name: string; role: string }[]>([]);
const available = artists.filter((a) => !picked.some((p) => p.id === a.id));
return (
<main className="max-w-3xl mx-auto px-4 py-8">
<div className="flex items-center gap-3 mb-6">
<Link to="/" className="text-gray-400 hover:text-gray-200 transition-colors">←</Link>
<h1 className="text-xl font-semibold">New Band</h1>
</div>
<Form method="post" className="space-y-5">
<input type="hidden" name="links" value={JSON.stringify(links)} />
<input
type="hidden"
name="artists"
value={JSON.stringify(picked.map((p) => ({ id: p.id, role: p.role || null })))}
/>
<div>
<label className="block text-sm font-medium text-gray-300 mb-1">
バンド名 <span className="text-red-400">*</span>
</label>
<input
name="name"
value={name}
onChange={(e) => {
setName(e.target.value);
if (!slugManual) setSlug(toSlug(e.target.value));
}}
className="w-full bg-gray-800 border border-gray-700 rounded px-3 py-2 text-gray-100 focus:outline-none focus:border-blue-500"
/>
{errors.name && <p className="text-red-400 text-sm mt-1">{errors.name}</p>}
</div>
<div>
<label className="block text-sm font-medium text-gray-300 mb-1">
Slug <span className="text-red-400">*</span>
</label>
<input
name="slug"
value={slug}
onChange={(e) => { setSlugManual(true); setSlug(e.target.value); }}
className="w-full bg-gray-800 border border-gray-700 rounded px-3 py-2 text-gray-100 focus:outline-none focus:border-blue-500 font-mono text-sm"
/>
{errors.slug && <p className="text-red-400 text-sm mt-1">{errors.slug}</p>}
</div>
<div>
<label className="block text-sm font-medium text-gray-300 mb-1">活動拠点</label>
<input
name="area"
className="w-full bg-gray-800 border border-gray-700 rounded px-3 py-2 text-gray-100 focus:outline-none focus:border-blue-500"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-300 mb-1">ステータス</label>
<select
name="status"
defaultValue="active"
className="bg-gray-800 border border-gray-700 rounded px-3 py-2 text-gray-100 focus:outline-none focus:border-blue-500"
>
<option value="active">活動中</option>
<option value="hiatus">活動休止</option>
<option value="disbanded">解散</option>
</select>
</div>
<div>
<label className="block text-sm font-medium text-gray-300 mb-1">説明</label>
<textarea
name="description"
rows={3}
className="w-full bg-gray-800 border border-gray-700 rounded px-3 py-2 text-gray-100 focus:outline-none focus:border-blue-500 resize-none"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-300 mb-2">リンク</label>
<div className="space-y-2">
{links.map((link, i) => (
<div key={i} className="flex gap-2">
<input
value={link.label}
onChange={(e) => setLinks(links.map((l, idx) => idx === i ? { ...l, label: e.target.value } : l))}
placeholder="ラベル (例: X)"
className="w-28 bg-gray-800 border border-gray-700 rounded px-3 py-2 text-gray-100 focus:outline-none focus:border-blue-500 text-sm"
/>
<input
value={link.url}
onChange={(e) => setLinks(links.map((l, idx) => idx === i ? { ...l, url: e.target.value } : l))}
placeholder="https://..."
className="flex-1 bg-gray-800 border border-gray-700 rounded px-3 py-2 text-gray-100 focus:outline-none focus:border-blue-500 text-sm"
/>
<button
type="button"
onClick={() => setLinks(links.filter((_, idx) => idx !== i))}
className="text-gray-500 hover:text-red-400 px-2 transition-colors"
>
×
</button>
</div>
))}
</div>
<button
type="button"
onClick={() => setLinks([...links, { label: "", url: "" }])}
className="mt-2 text-blue-400 hover:text-blue-300 text-sm transition-colors"
>
+ リンクを追加
</button>
</div>
<div>
<label className="block text-sm font-medium text-gray-300 mb-2">メンバー</label>
<div className="space-y-2 mb-2">
{picked.map((p, i) => (
<div key={p.id} className="flex gap-2 items-center">
<span className="text-gray-200 text-sm w-32 truncate">{p.name}</span>
<input
value={p.role}
onChange={(e) => setPicked(picked.map((a, idx) => idx === i ? { ...a, role: e.target.value } : a))}
placeholder="パート (例: Guitar)"
className="flex-1 bg-gray-800 border border-gray-700 rounded px-3 py-2 text-gray-100 focus:outline-none focus:border-blue-500 text-sm"
/>
<button
type="button"
onClick={() => setPicked(picked.filter((_, idx) => idx !== i))}
className="text-gray-500 hover:text-red-400 px-2 transition-colors"
>
×
</button>
</div>
))}
</div>
{available.length > 0 ? (
<select
onChange={(e) => {
const a = artists.find((x) => x.id === e.target.value);
if (a) { setPicked([...picked, { id: a.id, name: a.name, role: "" }]); e.target.value = ""; }
}}
defaultValue=""
className="bg-gray-800 border border-gray-700 rounded px-3 py-2 text-gray-400 focus:outline-none focus:border-blue-500 text-sm"
>
<option value="">+ アーティストを追加...</option>
{available.map((a) => (
<option key={a.id} value={a.id}>{a.name}</option>
))}
</select>
) : artists.length === 0 ? (
<p className="text-gray-500 text-sm">
アーティストがいません。{" "}
<Link to="/artists/new" className="text-blue-400 hover:text-blue-300">先に作成</Link>
</p>
) : null}
</div>
<div>
<label className="block text-sm font-medium text-gray-300 mb-1">
更新メッセージ <span className="text-red-400">*</span>
</label>
<input
name="message"
placeholder="例: 初回登録"
className="w-full bg-gray-800 border border-gray-700 rounded px-3 py-2 text-gray-100 focus:outline-none focus:border-blue-500"
/>
{errors.message && <p className="text-red-400 text-sm mt-1">{errors.message}</p>}
</div>
<div className="flex gap-3 pt-2">
<button
type="submit"
className="bg-blue-600 hover:bg-blue-500 text-white px-4 py-2 rounded font-medium transition-colors"
>
作成
</button>
<Link
to="/"
className="bg-gray-800 hover:bg-gray-700 text-gray-300 px-4 py-2 rounded transition-colors"
>
キャンセル
</Link>
</div>
</Form>
</main>
);
}
|