Skip to content

Commit 9775e2d

Browse files
committed
add formstatus type and statusmessagecontent component
1 parent 159e878 commit 9775e2d

File tree

1 file changed

+52
-23
lines changed

1 file changed

+52
-23
lines changed

src/components/contact-form/ContactFormModal.tsx

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,70 @@ import FormField from '../common/FormField';
88
import Spinner from '../common/Spinner';
99

1010
/**
11-
* StatusMessage Component
11+
* FormStatus
12+
*
13+
* Type for form submission status
1214
*
13-
* A component that displays feedback messages based on the current form submission status.
15+
* Values:
16+
* - `sending`: Form is being sent
17+
* - `error`: Form submission failed
18+
* - `timeout`: Form submission timed out
19+
* - `success`: Form submission successful
20+
* - `idle`: No form submission in progress
21+
*/
22+
type FormStatus = 'sending' | 'error' | 'timeout' | 'success' | 'idle';
23+
24+
/**
25+
* StatusMessage Component
1426
*
27+
* A functional component that displays a status message.
28+
*
1529
* Features:
16-
* - Renders a spinner when the form submission is in progress (`status: "sending"`).
17-
* - Shows error messages for failed submissions (`status: "error"` or `"timeout"`).
18-
* - Displays a success message when the form is successfully submitted (`status: "success"`).
30+
* - Displays content from StatusMessageContent based on the status prop.
31+
* - Uses a grid layout to center the content.
1932
*
2033
* Props:
21-
* - `status` ('sending' | 'error' | 'timeout' | 'success'): The current status of the form submission.
34+
* - `status` (`FormStatus`): The status of the form submission.
2235
*/
23-
const StatusMessage: React.FC<{ status: 'sending' | 'error' | 'timeout' | 'success' }> = ({ status }) => {
24-
const paragraphStyle = "py-2 text-md font-medium text-text"; // Common paragraph style
25-
26-
const content: Record<'sending' | 'error' | 'timeout' | 'success', JSX.Element> = {
27-
sending: <div className="grid place-items-center py-2 align-middle"><Spinner /></div>,
28-
error: <p className={paragraphStyle}>Failed to send your message, please try again.</p>,
29-
timeout: <p className={paragraphStyle}>Please check your connection status and try again.</p>,
30-
success: <p className={paragraphStyle}>Thank you for reaching out. I will get back to you soon.</p>,
31-
};
36+
const StatusMessage: React.FC<{status: FormStatus}> = ({status}) => {
3237

3338
return (
3439
<div className="grid place-items-center text-center justify-center items-center align-middle">
35-
{content[status]}
40+
<StatusMessageContent status={status} />
3641
</div>
3742
);
3843
};
3944

45+
/**
46+
* StatusMessageContent Component
47+
*
48+
* Sub-component of StatusMessage that displays the content of the status message.
49+
*
50+
* Features:
51+
* - Displays different content based on the status prop.
52+
*
53+
* Props:
54+
* - `status` (`FormStatus`): The status of the form submission.
55+
*/
56+
const StatusMessageContent: React.FC<{status: FormStatus}> = ({status}) => {
57+
const paragraphStyle = "py-2 text-md font-medium text-text"; // Common paragraph style
58+
59+
switch (status) {
60+
case 'sending':
61+
return <div className="grid place-items-center py-2 align-middle"><Spinner /></div>;
62+
case 'error':
63+
return <p className={paragraphStyle}>Failed to send your message, please try again.</p>;
64+
case 'timeout':
65+
return <p className={paragraphStyle}>Please check your connection status and try again.</p>;
66+
case 'success':
67+
return <p className={paragraphStyle}>Thank you for reaching out. I will get back to you soon.</p>;
68+
default:
69+
return null;
70+
}
71+
}
72+
73+
74+
4075
/**
4176
* ContactFormModal Component
4277
*
@@ -62,13 +97,7 @@ const ContactFormModal: React.FC = () => {
6297

6398
const form = useRef<HTMLFormElement>(null); // Ref for the form element
6499

65-
const [status, setStatus] = useState<
66-
'sending' |
67-
'success' |
68-
'error' |
69-
'timeout' |
70-
'idle'>
71-
('idle'); // set idle as default status
100+
const [status, setStatus] = useState<FormStatus>('idle'); // set idle as default status
72101

73102
// if form is submitted, set to true to prevent further submissions
74103
const [formSubmitted, setFormSubmitted] = useState(false);

0 commit comments

Comments
 (0)