Jobley / app / Sources / JobleyUI / FormViews.swift
| 1 | import SwiftUI |
| 2 | import JobleyKit |
| 3 | |
| 4 | /// Search and the three filters, as on jobley.ai/jobs/. |
| 5 | public struct BoardFilters: View { |
| 6 | @ObservedObject var board: BoardModel |
| 7 | public init(board: BoardModel) { self.board = board } |
| 8 | |
| 9 | public var body: some View { |
| 10 | HStack(spacing: 8) { |
| 11 | picker("Any workplace", $board.workplace, Meta.workplaceOrder, board.labels.workplace) |
| 12 | picker("Any job type", $board.type, Meta.employmentTypeOrder, board.labels.type) |
| 13 | picker("Any category", $board.category, Meta.categoryOrder, board.labels.category) |
| 14 | } |
| 15 | .onChange(of: board.workplace) { Task { await board.reload() } } |
| 16 | .onChange(of: board.type) { Task { await board.reload() } } |
| 17 | .onChange(of: board.category) { Task { await board.reload() } } |
| 18 | } |
| 19 | |
| 20 | func picker(_ any: String, _ value: Binding<String>, _ order: [String], _ label: @escaping (String) -> String) -> some View { |
| 21 | Menu { |
| 22 | Picker(any, selection: value) { |
| 23 | Text(any).tag("") |
| 24 | ForEach(order, id: \.self) { Text(label($0)).tag($0) } |
| 25 | } |
| 26 | .pickerStyle(.inline) |
| 27 | } label: { |
| 28 | HStack(spacing: 4) { |
| 29 | Text(value.wrappedValue.isEmpty ? any : label(value.wrappedValue)).lineLimit(1) |
| 30 | Image(systemName: "chevron.down").font(.caption2) |
| 31 | } |
| 32 | .font(.subheadline) |
| 33 | .foregroundStyle(value.wrappedValue.isEmpty ? Theme.ink2 : Theme.accent) |
| 34 | .padding(.horizontal, 10).padding(.vertical, 6) |
| 35 | .overlay(Capsule().strokeBorder(value.wrappedValue.isEmpty ? Theme.line : Theme.accent)) |
| 36 | } |
| 37 | .menuStyle(.button) |
| 38 | .buttonStyle(.plain) |
| 39 | .fixedSize() |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /// "Put me forward": for one job or for a kind of job. A person reads it; no software sorts it. |
| 44 | public struct PutMeForwardForm: View { |
| 45 | let api: APIClient |
| 46 | let job: Job? |
| 47 | var onDone: () -> Void |
| 48 | @State private var draft: CandidateDraft |
| 49 | @State private var sending = false |
| 50 | @State private var error: APIError? |
| 51 | @State private var received: Received? |
| 52 | @StateObject private var gate = TurnstileGate() |
| 53 | |
| 54 | public init(api: APIClient, job: Job?, onDone: @escaping () -> Void) { |
| 55 | self.api = api; self.job = job; self.onDone = onDone |
| 56 | _draft = State(initialValue: CandidateDraft(jobID: job?.id)) |
| 57 | } |
| 58 | |
| 59 | public var body: some View { |
| 60 | if let received { |
| 61 | Confirmation(title: received.already == true ? "You're already on our list." : "Thank you. A person will read it.", |
| 62 | detail: job.map { "If you're a good fit for “\($0.title)”, we'll introduce you to the people hiring by email." } |
| 63 | ?? "When a good job comes in that fits, we'll introduce you by email.", |
| 64 | done: onDone) |
| 65 | } else { |
| 66 | Form { |
| 67 | if let job { |
| 68 | Section { JobRow(job: job, labels: Labels()) } header: { Text("For this job") } |
| 69 | } |
| 70 | Section { |
| 71 | field("Your name", $draft.name, "name") |
| 72 | field("Email", $draft.email, "email") |
| 73 | field("Headline, e.g. “Support engineer, six years”", $draft.headline, "headline") |
| 74 | field("Where you are (optional)", $draft.location, "location") |
| 75 | } header: { Text("About you") } footer: { Text("Private. Only the people hiring see it, and only when we introduce you.") } |
| 76 | if job == nil { |
| 77 | Section("What you're looking for") { |
| 78 | TextField("The kind of job, place and hours", text: $draft.lookingFor, axis: .vertical).lineLimit(2...5) |
| 79 | errorText("looking_for") |
| 80 | } |
| 81 | } |
| 82 | Section { |
| 83 | TextField("A few lines about your work (optional)", text: $draft.about, axis: .vertical).lineLimit(3...8) |
| 84 | TextField("Links to your work, one per line (optional)", text: $draft.links, axis: .vertical).lineLimit(1...5) |
| 85 | .autocorrectionDisabled() |
| 86 | field("Link to your resume (optional)", $draft.resumeURL, "resume_url") |
| 87 | } header: { Text("Your work") } |
| 88 | Section { |
| 89 | Toggle("I agree that Jobley may share these details with the people hiring when a person introduces me.", isOn: $draft.consent) |
| 90 | errorText("consent") |
| 91 | if TurnstileGate.isNeeded(for: api.base) { |
| 92 | TurnstileView(page: api.turnstilePage, onResult: gate.receive).id(gate.generation) |
| 93 | Text(gate.statusText).font(.caption).foregroundStyle(Theme.muted) |
| 94 | } |
| 95 | if let error { Text(error.message).foregroundStyle(Theme.danger).font(.callout) } |
| 96 | Button { |
| 97 | Task { await send() } |
| 98 | } label: { |
| 99 | Text(sending ? "Sending…" : "Put me forward").frame(maxWidth: .infinity) |
| 100 | } |
| 101 | .buttonStyle(PrimaryButtonStyle()) |
| 102 | .disabled(sending || !draft.consent || (TurnstileGate.isNeeded(for: api.base) && !gate.isReady)) |
| 103 | .accessibilityIdentifier("putMeForwardSend") |
| 104 | } footer: { Text("A person reads every one. Nothing ranks, scores or filters you.") } |
| 105 | } |
| 106 | .formStyle(.grouped) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func field(_ title: String, _ text: Binding<String>, _ key: String) -> some View { |
| 111 | VStack(alignment: .leading, spacing: 2) { |
| 112 | TextField(title, text: text) |
| 113 | .autocorrectionDisabled(key == "email" || key == "resume_url") |
| 114 | #if os(iOS) |
| 115 | .textInputAutocapitalization(key == "email" || key == "resume_url" ? .never : .words) |
| 116 | .keyboardType(key == "email" ? .emailAddress : key == "resume_url" ? .URL : .default) |
| 117 | #endif |
| 118 | errorText(key) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | @ViewBuilder func errorText(_ key: String) -> some View { |
| 123 | if let message = error?.fields[key] { Text(message).font(.caption).foregroundStyle(Theme.danger) } |
| 124 | } |
| 125 | |
| 126 | func send() async { |
| 127 | sending = true; error = nil |
| 128 | defer { sending = false } |
| 129 | do { |
| 130 | received = try await api.putForward(draft, turnstileToken: gate.token) |
| 131 | } catch let failure as APIError { |
| 132 | error = failure |
| 133 | gate.reset() |
| 134 | } catch {} |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /// "Post a job": one form, no account. It waits for a person to read it. |
| 139 | public struct PostJobForm: View { |
| 140 | let api: APIClient |
| 141 | var onDone: () -> Void |
| 142 | @State private var draft = JobDraft() |
| 143 | @State private var sending = false |
| 144 | @State private var error: APIError? |
| 145 | @State private var posted: PostedJob? |
| 146 | @StateObject private var gate = TurnstileGate() |
| 147 | let labels = Labels() |
| 148 | |
| 149 | public init(api: APIClient, onDone: @escaping () -> Void) { self.api = api; self.onDone = onDone } |
| 150 | |
| 151 | public var body: some View { |
| 152 | if let posted { |
| 153 | VStack(alignment: .leading, spacing: 16) { |
| 154 | Confirmation(title: "Thank you. Your post is waiting for a person.", |
| 155 | detail: "\(HouseRules.guarantee) It's in the review queue as \(posted.id). If it's a good job it will appear on the board. If not, we'll write to you with the reason.", |
| 156 | done: onDone) |
| 157 | VStack(alignment: .leading, spacing: 6) { |
| 158 | Text("Keep this. It's the only way to take the job down yourself when it's filled. We can't show it again.") |
| 159 | .font(.callout).foregroundStyle(Theme.ink2) |
| 160 | Text(posted.manageToken).font(Theme.mono(12)).textSelection(.enabled) |
| 161 | .padding(10).background(Theme.surface2, in: RoundedRectangle(cornerRadius: 8)) |
| 162 | Button("Copy the id and token") { Clipboard.copy("\(posted.id)\n\(posted.manageToken)") } |
| 163 | } |
| 164 | .padding(.horizontal, 24) |
| 165 | } |
| 166 | } else { |
| 167 | Form { |
| 168 | Section("The job") { |
| 169 | field("Job title", $draft.title, "title") |
| 170 | field("Company", $draft.company, "company") |
| 171 | field("Company website (optional)", $draft.companyURL, "company_url") |
| 172 | choice("Workplace", $draft.workplace, Meta.workplaceOrder, labels.workplace, "workplace") |
| 173 | choice("Job type", $draft.employmentType, Meta.employmentTypeOrder, labels.type, "employment_type") |
| 174 | choice("Category", $draft.category, Meta.categoryOrder, labels.category, "category") |
| 175 | field("Location", $draft.location, "location") |
| 176 | } |
| 177 | Section { |
| 178 | field("Lowest, such as 100", $draft.payMin, "pay_min") |
| 179 | field("Highest, such as 140", $draft.payMax, "pay_max") |
| 180 | Picker("Per", selection: $draft.payPeriod) { ForEach(Meta.payPeriodOrder, id: \.self) { Text($0).tag($0) } } |
| 181 | field("Currency", $draft.payCurrency, "pay_currency") |
| 182 | } header: { Text("Pay") } footer: { Text("We publish jobs that pay \(HouseRules.payFloorShort) (or the same in local currency), by the hour or salaried, full or part time, with or without benefits. Give a lowest and a highest figure.") } |
| 183 | Section("Description") { |
| 184 | TextField("What the person would do, with whom, where and when", text: $draft.description, axis: .vertical).lineLimit(5...14) |
| 185 | errorText("description") |
| 186 | field("Your own job page (optional)", $draft.applyURL, "apply_url") |
| 187 | } |
| 188 | Section { |
| 189 | field("Your name", $draft.contactName, "contact_name") |
| 190 | field("Your email", $draft.contactEmail, "contact_email") |
| 191 | } header: { Text("You") } footer: { Text("Private. We use it to reach you about this post and to introduce candidates.") } |
| 192 | Section { |
| 193 | if TurnstileGate.isNeeded(for: api.base) { |
| 194 | TurnstileView(page: api.turnstilePage, onResult: gate.receive).id(gate.generation) |
| 195 | Text(gate.statusText).font(.caption).foregroundStyle(Theme.muted) |
| 196 | } |
| 197 | if let error { Text(error.message).foregroundStyle(Theme.danger).font(.callout) } |
| 198 | Button { Task { await send() } } label: { |
| 199 | Text(sending ? "Sending…" : "Send for review").frame(maxWidth: .infinity) |
| 200 | } |
| 201 | .buttonStyle(PrimaryButtonStyle()) |
| 202 | .disabled(sending || (TurnstileGate.isNeeded(for: api.base) && !gate.isReady)) |
| 203 | } footer: { Text("Free. No account. \(HouseRules.guarantee)") } |
| 204 | } |
| 205 | .formStyle(.grouped) |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | func field(_ title: String, _ text: Binding<String>, _ key: String) -> some View { |
| 210 | VStack(alignment: .leading, spacing: 2) { |
| 211 | TextField(title, text: text) |
| 212 | .autocorrectionDisabled(key.contains("url") || key.contains("email")) |
| 213 | #if os(iOS) |
| 214 | .textInputAutocapitalization(key.contains("url") || key.contains("email") ? .never : .sentences) |
| 215 | .keyboardType(key.hasPrefix("pay_m") ? .numberPad : key.contains("email") ? .emailAddress : key.contains("url") ? .URL : .default) |
| 216 | #endif |
| 217 | errorText(key) |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | func choice(_ title: String, _ value: Binding<String>, _ order: [String], _ label: @escaping (String) -> String, _ key: String) -> some View { |
| 222 | VStack(alignment: .leading, spacing: 2) { |
| 223 | Picker(title, selection: value) { |
| 224 | Text("Choose…").tag("") |
| 225 | ForEach(order, id: \.self) { Text(label($0)).tag($0) } |
| 226 | } |
| 227 | errorText(key) |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | @ViewBuilder func errorText(_ key: String) -> some View { |
| 232 | if let message = error?.fields[key] { Text(message).font(.caption).foregroundStyle(Theme.danger) } |
| 233 | } |
| 234 | |
| 235 | func send() async { |
| 236 | sending = true; error = nil |
| 237 | defer { sending = false } |
| 238 | if HouseRules.belowPayFloor(min: Int(draft.payMin.filter(\.isNumber)), period: draft.payPeriod, currency: draft.payCurrency) { |
| 239 | error = APIError(code: "invalid", message: "Please fix the marked fields.", fields: ["pay_min": HouseRules.payFloorMessage]) |
| 240 | return |
| 241 | } |
| 242 | do { posted = try await api.post(draft, turnstileToken: gate.token) } |
| 243 | catch let failure as APIError { error = failure; gate.reset() } |
| 244 | catch {} |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | struct Confirmation: View { |
| 249 | let title: String, detail: String |
| 250 | let done: () -> Void |
| 251 | var body: some View { |
| 252 | VStack(alignment: .leading, spacing: 12) { |
| 253 | Image(systemName: "checkmark.seal.fill").font(.system(size: 40)).foregroundStyle(Theme.seal) |
| 254 | Text(title).font(Theme.display(26)).foregroundStyle(Theme.ink) |
| 255 | Text(detail).foregroundStyle(Theme.ink2) |
| 256 | Button("Done", action: done).buttonStyle(PrimaryButtonStyle()) |
| 257 | } |
| 258 | .padding(24) |
| 259 | .frame(maxWidth: .infinity, alignment: .leading) |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | public enum Clipboard { |
| 264 | public static func copy(_ text: String) { |
| 265 | #if os(macOS) |
| 266 | NSPasteboard.general.clearContents() |
| 267 | NSPasteboard.general.setString(text, forType: .string) |
| 268 | #else |
| 269 | UIPasteboard.general.string = text |
| 270 | #endif |
| 271 | } |
| 272 | } |