|
| 1 | +import { useFormSubmission, useSupportForm } from "@/hooks"; |
| 2 | +import { FONT_FAMILY_HEADING, THEME_COLORS } from "@/style"; |
| 3 | +import { SUPPORT_PLANS, SupportPlan } from "@/types/support"; |
| 4 | +import React, { FC, FormEvent } from "react"; |
| 5 | +import styled from "styled-components"; |
| 6 | +import { Button } from "./button"; |
| 7 | + |
| 8 | +interface SupportFormProps { |
| 9 | + readonly className?: string; |
| 10 | + readonly initialPlan?: SupportPlan; |
| 11 | +} |
| 12 | + |
| 13 | +export const SupportForm: FC<SupportFormProps> = ({ |
| 14 | + className, |
| 15 | + initialPlan = "Startup", |
| 16 | +}) => { |
| 17 | + const { formData, errors, updateField, validateForm } = useSupportForm({ |
| 18 | + initialPlan, |
| 19 | + }); |
| 20 | + |
| 21 | + const { isSubmitting, submitForm } = useFormSubmission(); |
| 22 | + |
| 23 | + const handleSubmit = async (e: FormEvent) => { |
| 24 | + e.preventDefault(); |
| 25 | + |
| 26 | + if (!validateForm()) { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + await submitForm(formData); |
| 31 | + }; |
| 32 | + |
| 33 | + return ( |
| 34 | + <FormContainer className={className}> |
| 35 | + <FormTitle>Contact Support</FormTitle> |
| 36 | + <FormDescription> |
| 37 | + Fill out the form below and we'll get back to you as soon as possible. |
| 38 | + </FormDescription> |
| 39 | + |
| 40 | + <Form onSubmit={handleSubmit}> |
| 41 | + <FormGroup> |
| 42 | + <Label htmlFor="name">Name *</Label> |
| 43 | + <Input |
| 44 | + id="name" |
| 45 | + type="text" |
| 46 | + value={formData.name} |
| 47 | + onChange={(e) => updateField("name", e.target.value)} |
| 48 | + $hasError={!!errors.name} |
| 49 | + disabled={isSubmitting} |
| 50 | + /> |
| 51 | + {errors.name && <ErrorText>{errors.name}</ErrorText>} |
| 52 | + </FormGroup> |
| 53 | + |
| 54 | + <FormGroup> |
| 55 | + <Label htmlFor="email">Email *</Label> |
| 56 | + <Input |
| 57 | + id="email" |
| 58 | + type="email" |
| 59 | + value={formData.email} |
| 60 | + onChange={(e) => updateField("email", e.target.value)} |
| 61 | + $hasError={!!errors.email} |
| 62 | + disabled={isSubmitting} |
| 63 | + /> |
| 64 | + {errors.email && <ErrorText>{errors.email}</ErrorText>} |
| 65 | + </FormGroup> |
| 66 | + |
| 67 | + <FormGroup> |
| 68 | + <Label htmlFor="company">Company *</Label> |
| 69 | + <Input |
| 70 | + id="company" |
| 71 | + type="text" |
| 72 | + value={formData.company} |
| 73 | + onChange={(e) => updateField("company", e.target.value)} |
| 74 | + $hasError={!!errors.company} |
| 75 | + disabled={isSubmitting} |
| 76 | + /> |
| 77 | + {errors.company && <ErrorText>{errors.company}</ErrorText>} |
| 78 | + </FormGroup> |
| 79 | + |
| 80 | + <FormGroup> |
| 81 | + <Label htmlFor="supportPlan">Support Plan</Label> |
| 82 | + <Select |
| 83 | + id="supportPlan" |
| 84 | + value={formData.supportPlan} |
| 85 | + onChange={(e) => updateField("supportPlan", e.target.value)} |
| 86 | + disabled={isSubmitting} |
| 87 | + > |
| 88 | + {SUPPORT_PLANS.map((plan) => ( |
| 89 | + <option key={plan} value={plan}> |
| 90 | + {plan} |
| 91 | + </option> |
| 92 | + ))} |
| 93 | + </Select> |
| 94 | + </FormGroup> |
| 95 | + |
| 96 | + <FormGroup> |
| 97 | + <Label htmlFor="message">Message *</Label> |
| 98 | + <TextArea |
| 99 | + id="message" |
| 100 | + rows={5} |
| 101 | + value={formData.message} |
| 102 | + onChange={(e) => updateField("message", e.target.value)} |
| 103 | + $hasError={!!errors.message} |
| 104 | + disabled={isSubmitting} |
| 105 | + placeholder="Please describe how we can help you..." |
| 106 | + /> |
| 107 | + {errors.message && <ErrorText>{errors.message}</ErrorText>} |
| 108 | + </FormGroup> |
| 109 | + |
| 110 | + <SubmitButton type="submit" disabled={isSubmitting}> |
| 111 | + {isSubmitting ? "Sending..." : "Send Message"} |
| 112 | + </SubmitButton> |
| 113 | + </Form> |
| 114 | + </FormContainer> |
| 115 | + ); |
| 116 | +}; |
| 117 | + |
| 118 | +const FormContainer = styled.div` |
| 119 | + display: flex; |
| 120 | + flex-direction: column; |
| 121 | + border: 1px solid ${THEME_COLORS.boxBorder}; |
| 122 | + border-radius: var(--box-border-radius); |
| 123 | + padding: 40px; |
| 124 | + backdrop-filter: blur(2px); |
| 125 | + background-image: linear-gradient( |
| 126 | + to right bottom, |
| 127 | + #379dc83d, |
| 128 | + #2b80ad3d, |
| 129 | + #2263903d, |
| 130 | + #1a48743d, |
| 131 | + #112f573d |
| 132 | + ); |
| 133 | + max-width: 600px; |
| 134 | + margin: 0 auto; |
| 135 | +`; |
| 136 | + |
| 137 | +const FormTitle = styled.h3` |
| 138 | + margin: 0 0 16px 0; |
| 139 | + color: ${THEME_COLORS.heading}; |
| 140 | + font-family: ${FONT_FAMILY_HEADING}; |
| 141 | +`; |
| 142 | + |
| 143 | +const FormDescription = styled.p.attrs({ |
| 144 | + className: "text-2", |
| 145 | +})` |
| 146 | + margin: 0 0 32px 0; |
| 147 | + color: ${THEME_COLORS.text}; |
| 148 | +`; |
| 149 | + |
| 150 | +const Form = styled.form` |
| 151 | + display: flex; |
| 152 | + flex-direction: column; |
| 153 | + gap: 24px; |
| 154 | +`; |
| 155 | + |
| 156 | +const FormGroup = styled.div` |
| 157 | + display: flex; |
| 158 | + flex-direction: column; |
| 159 | +`; |
| 160 | + |
| 161 | +const Label = styled.label` |
| 162 | + margin-bottom: 8px; |
| 163 | + color: ${THEME_COLORS.text}; |
| 164 | + font-weight: 500; |
| 165 | + font-size: 0.875rem; |
| 166 | +`; |
| 167 | + |
| 168 | +const baseInputStyles = ` |
| 169 | + padding: 12px 16px; |
| 170 | + border: 1px solid ${THEME_COLORS.boxBorder}; |
| 171 | + border-radius: var(--border-radius); |
| 172 | + background-color: rgba(255, 255, 255, 0.05); |
| 173 | + color: ${THEME_COLORS.text}; |
| 174 | + font-size: 1rem; |
| 175 | + transition: border-color 0.2s ease-in-out, background-color 0.2s ease-in-out; |
| 176 | +
|
| 177 | + &:focus { |
| 178 | + outline: none; |
| 179 | + border-color: ${THEME_COLORS.primary}; |
| 180 | + background-color: rgba(255, 255, 255, 0.08); |
| 181 | + } |
| 182 | +
|
| 183 | + &:disabled { |
| 184 | + opacity: 0.6; |
| 185 | + cursor: not-allowed; |
| 186 | + } |
| 187 | +
|
| 188 | + &::placeholder { |
| 189 | + color: ${THEME_COLORS.text}80; |
| 190 | + } |
| 191 | +`; |
| 192 | + |
| 193 | +const Input = styled.input<{ $hasError?: boolean }>` |
| 194 | + ${baseInputStyles} |
| 195 | +
|
| 196 | + ${({ $hasError }) => |
| 197 | + $hasError && |
| 198 | + ` |
| 199 | + border-color: #ef4444; |
| 200 | +
|
| 201 | + &:focus { |
| 202 | + border-color: #ef4444; |
| 203 | + } |
| 204 | + `} |
| 205 | +`; |
| 206 | + |
| 207 | +const TextArea = styled.textarea<{ $hasError?: boolean }>` |
| 208 | + ${baseInputStyles} |
| 209 | + resize: vertical; |
| 210 | + min-height: 120px; |
| 211 | +
|
| 212 | + ${({ $hasError }) => |
| 213 | + $hasError && |
| 214 | + ` |
| 215 | + border-color: #ef4444; |
| 216 | +
|
| 217 | + &:focus { |
| 218 | + border-color: #ef4444; |
| 219 | + } |
| 220 | + `} |
| 221 | +`; |
| 222 | + |
| 223 | +const Select = styled.select` |
| 224 | + ${baseInputStyles} |
| 225 | + cursor: pointer; |
| 226 | +`; |
| 227 | + |
| 228 | +const ErrorText = styled.span` |
| 229 | + margin-top: 4px; |
| 230 | + color: #ef4444; |
| 231 | + font-size: 0.875rem; |
| 232 | +`; |
| 233 | + |
| 234 | +const SubmitButton = styled(Button)` |
| 235 | + align-self: flex-start; |
| 236 | + padding: 12px 32px; |
| 237 | + font-size: 1rem; |
| 238 | + font-weight: 600; |
| 239 | + margin-top: 16px; |
| 240 | +
|
| 241 | + &:disabled { |
| 242 | + opacity: 0.6; |
| 243 | + cursor: not-allowed; |
| 244 | + } |
| 245 | +`; |
0 commit comments