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
|
import { createSignal, onMount, Show, For } from "solid-js";
import { useNavigate } from "@solidjs/router";
import { api } from "../../api";
import { auth } from "../../store/auth";
import { extractError } from "../../utils/api";
import type { District, SiteRequest } from "../../types/district";
import { districtImage, districtIconClass } from "../../utils/districts";
export default function SubmitSite() {
const navigate = useNavigate();
const [districts, setDistricts] = createSignal<District[]>([]);
const [selectedDistrict, setSelectedDistrict] = createSignal("");
const [title, setTitle] = createSignal("");
const [url, setUrl] = createSignal("");
const [description, setDescription] = createSignal("");
const [tagInput, setTagInput] = createSignal("");
const [tags, setTags] = createSignal<string[]>([]);
const [error, setError] = createSignal("");
const [submitting, setSubmitting] = createSignal(false);
onMount(async () => {
const response = await api<District[]>("/districts");
if (response.ok) {
setDistricts(response.data);
}
});
function addTag() {
const tag = tagInput().trim().toLowerCase();
if (tag && tags().length < 5 && !tags().includes(tag)) {
setTags([...tags(), tag]);
setTagInput("");
}
}
function removeTag(tag: string) {
setTags(tags().filter((existingTag) => existingTag !== tag));
}
function handleTagKeyDown(event: KeyboardEvent) {
if (event.key === "Enter") {
event.preventDefault();
addTag();
}
}
async function handleSubmit(event: Event) {
event.preventDefault();
setError("");
setSubmitting(true);
const response = await api<SiteRequest>("/districts/sites", {
method: "POST",
token: auth.token(),
body: {
district: selectedDistrict(),
title: title(),
url: url(),
description: description(),
tags: tags(),
},
});
if (response.ok) {
navigate("/districts");
} else {
setError(extractError(response.data));
}
setSubmitting(false);
}
return (
<section>
<h1 class="page-title">Submit a Site</h1>
<p class="district-intro">
Submit a website to be listed in a district. Your submission will be reviewed by staff before it appears.
</p>
<Show when={error()}>
<div class="form-error">{error()}</div>
</Show>
<form class="submit-site-form" onSubmit={handleSubmit}>
<div class="form-field">
<label>District</label>
<div class="district-select-grid">
<For each={districts()}>
{(district) => (
<button
type="button"
class={`district-select-option ${selectedDistrict() === district.slug ? "selected" : ""}`}
style={{
"border-color": selectedDistrict() === district.slug ? district.foreground : district.background,
"background-color": district.background,
}}
onClick={() => setSelectedDistrict(district.slug)}
>
<img src={districtImage(district.slug)} alt={district.name} class={`district-select-icon ${districtIconClass(district.slug)}`} />
<span style={{ color: district.foreground }}>{district.name}</span>
</button>
)}
</For>
</div>
</div>
<div class="form-field">
<label>Site Title</label>
<input
type="text"
value={title()}
onInput={(e) => setTitle(e.currentTarget.value)}
placeholder="My Cool Website"
maxLength={200}
/>
</div>
<div class="form-field">
<label>URL</label>
<input
type="url"
value={url()}
onInput={(e) => setUrl(e.currentTarget.value)}
placeholder="https://example.nekoweb.org"
/>
</div>
<div class="form-field">
<label>Description</label>
<textarea
value={description()}
onInput={(e) => setDescription(e.currentTarget.value)}
placeholder="A short description of the site..."
maxLength={1000}
rows={3}
/>
</div>
<div class="form-field">
<label>Tags (up to 5)</label>
<div class="tag-input-row">
<input
type="text"
value={tagInput()}
onInput={(e) => setTagInput(e.currentTarget.value)}
onKeyDown={handleTagKeyDown}
placeholder="Add a tag..."
maxLength={50}
disabled={tags().length >= 5}
/>
<button type="button" class="form-button" onClick={addTag} disabled={tags().length >= 5}>Add</button>
</div>
<Show when={tags().length > 0}>
<div class="tag-list">
<For each={tags()}>
{(tag) => (
<span class="site-tag removable" onClick={() => removeTag(tag)}>
{tag} ×
</span>
)}
</For>
</div>
</Show>
</div>
<button type="submit" class="form-button" disabled={submitting() || !selectedDistrict()}>
{submitting() ? "Submitting..." : "Submit Site"}
</button>
</form>
</section>
);
}
|