hezarfen_landing
Clone
https://git.tahinli.com/archive/hezarfen_landing.git
0 watching0 forksTypeScript581.6 KiB94b1804Burak Boduroğlu2 months ago15 commits
| 1 | import { useSubmission } from "@solidjs/router"; |
| 2 | import { For, Show } from "solid-js"; |
| 3 | import { PixelArrowRight, PixelCheck } from "./icons/pixel"; |
| 4 | import { submitContact } from "~/lib/contact"; |
| 5 | import { mailto, site } from "~/config"; |
| 6 | import { t } from "~/i18n"; |
| 7 | |
| 8 | /** |
| 9 | * The demo request form. |
| 10 | * |
| 11 | * It is a real `<form>` posting to a server action, so it works before the |
| 12 | * client bundle arrives and keeps working if it never does. The submission |
| 13 | * signal only adds what progressive enhancement cannot: a pending label and |
| 14 | * the field-level error the server decided on. |
| 15 | */ |
| 16 | export default function ContactForm() { |
| 17 | const submission = useSubmission(submitContact); |
| 18 | |
| 19 | const f = t.contact.fields; |
| 20 | const errorCode = () => { |
| 21 | const result = submission.result; |
| 22 | return result && !result.ok ? result.code : null; |
| 23 | }; |
| 24 | const errorFor = (field: string) => { |
| 25 | const code = errorCode(); |
| 26 | if (!code) return null; |
| 27 | const result = submission.result; |
| 28 | if (result && !result.ok && result.field !== field) return null; |
| 29 | return t.contact.errors[code as keyof typeof t.contact.errors] ?? t.contact.errors.unknown; |
| 30 | }; |
| 31 | |
| 32 | return ( |
| 33 | <div class="hz-form-card"> |
| 34 | <Show |
| 35 | when={!submission.result?.ok} |
| 36 | fallback={ |
| 37 | <div class="hz-form-done"> |
| 38 | <span class="hz-card-icon"> |
| 39 | <PixelCheck aria-hidden="true" /> |
| 40 | </span> |
| 41 | <h2 class="hz-h3">{t.contact.successTitle}</h2> |
| 42 | <p class="hz-small">{t.contact.successBody}</p> |
| 43 | <a class="hz-btn hz-btn-ghost" href={mailto(t.cta.subject)}> |
| 44 | {site.email} |
| 45 | </a> |
| 46 | </div> |
| 47 | } |
| 48 | > |
| 49 | <form action={submitContact} method="post" class="hz-form"> |
| 50 | <h2 class="hz-h3">{t.contact.formTitle}</h2> |
| 51 | |
| 52 | <div class="hz-field-row"> |
| 53 | <label class="hz-field"> |
| 54 | <span> |
| 55 | {f.name} <i>{f.required}</i> |
| 56 | </span> |
| 57 | <input name="name" required autocomplete="name" /> |
| 58 | </label> |
| 59 | <label class="hz-field"> |
| 60 | <span> |
| 61 | {f.institution} <i>{f.required}</i> |
| 62 | </span> |
| 63 | <input name="institution" required autocomplete="organization" /> |
| 64 | </label> |
| 65 | </div> |
| 66 | |
| 67 | <div class="hz-field-row"> |
| 68 | <label class="hz-field"> |
| 69 | <span>{f.role}</span> |
| 70 | <input name="role" placeholder={f.rolePlaceholder} autocomplete="organization-title" /> |
| 71 | </label> |
| 72 | <label class="hz-field"> |
| 73 | <span> |
| 74 | {f.email} <i>{f.required}</i> |
| 75 | </span> |
| 76 | <input name="email" type="email" required autocomplete="email" /> |
| 77 | </label> |
| 78 | </div> |
| 79 | |
| 80 | <div class="hz-field-row"> |
| 81 | <label class="hz-field"> |
| 82 | <span> |
| 83 | {f.phone} <i>{f.phoneHint}</i> |
| 84 | </span> |
| 85 | <input name="phone" type="tel" autocomplete="tel" /> |
| 86 | </label> |
| 87 | <label class="hz-field"> |
| 88 | <span>{f.students}</span> |
| 89 | <select name="students"> |
| 90 | <For each={f.studentsOptions}>{option => <option value={option}>{option}</option>}</For> |
| 91 | </select> |
| 92 | </label> |
| 93 | </div> |
| 94 | |
| 95 | <fieldset class="hz-field"> |
| 96 | <legend>{f.interest}</legend> |
| 97 | <div class="hz-choices"> |
| 98 | <For each={f.interestOptions}> |
| 99 | {option => ( |
| 100 | <label class="hz-choice"> |
| 101 | <input type="checkbox" name="interest" value={option} /> |
| 102 | <span>{option}</span> |
| 103 | </label> |
| 104 | )} |
| 105 | </For> |
| 106 | </div> |
| 107 | </fieldset> |
| 108 | |
| 109 | <label class="hz-field"> |
| 110 | <span>{f.message}</span> |
| 111 | <textarea name="message" rows={4} placeholder={f.messagePlaceholder} /> |
| 112 | </label> |
| 113 | |
| 114 | <label class="hz-consent"> |
| 115 | <input type="checkbox" name="consent" value="1" required /> |
| 116 | <span>{f.consent}</span> |
| 117 | </label> |
| 118 | |
| 119 | <Show when={errorCode()}> |
| 120 | <p class="hz-form-error" role="alert"> |
| 121 | {errorFor("name") ?? |
| 122 | errorFor("institution") ?? |
| 123 | errorFor("email") ?? |
| 124 | errorFor("consent") ?? |
| 125 | t.contact.errors.unknown} |
| 126 | </p> |
| 127 | </Show> |
| 128 | |
| 129 | <button type="submit" class="hz-btn hz-btn-primary hz-btn-lg" disabled={submission.pending}> |
| 130 | {submission.pending ? t.contact.submitting : t.contact.submit} |
| 131 | <Show when={!submission.pending}> |
| 132 | <PixelArrowRight aria-hidden="true" /> |
| 133 | </Show> |
| 134 | </button> |
| 135 | |
| 136 | <p class="hz-form-note">{t.contact.kvkk}</p> |
| 137 | </form> |
| 138 | </Show> |
| 139 | </div> |
| 140 | ); |
| 141 | } |