1- import { useState } from "react" ;
1+ import { useState , useRef , useEffect } from "react" ;
22import {
33 Box ,
44 Button ,
55 Input ,
66 Text ,
7+ useToast ,
8+ Card ,
79} from "@chakra-ui/react" ;
810import { useNavigate } from "react-router-dom" ;
9-
11+ import {
12+ SelectInputComponent ,
13+ } from "./formComponents.tsx" ;
1014interface FormFrontDeskProps {
1115 onFormSubmitSuccess : ( ) => void ;
1216}
@@ -15,8 +19,14 @@ import { useBackendContext } from "../../contexts/hooks/useBackendContext";
1519function FormFrontDesk ( { onFormSubmitSuccess } : FormFrontDeskProps ) {
1620 const { backend } = useBackendContext ( ) ;
1721 const [ error , setError ] = useState < string | null > ( null ) ;
22+ const [ month , setMonth ] = useState < string > ( "" ) ;
23+ const [ year , setYear ] = useState < string > ( "" ) ;
24+ const [ cms , setCms ] = useState <
25+ { id : string ; firstName : string ; lastName : string ; role : string } [ ]
26+ > ( [ ] ) ;
27+
1828 const [ formData , setFormData ] = useState ( {
19- date : "" ,
29+ case_manager : "" ,
2030 total_office_visits : "" ,
2131 total_calls : "" ,
2232 total_unduplicated_calls : "" ,
@@ -28,12 +38,13 @@ function FormFrontDesk({ onFormSubmitSuccess }: FormFrontDeskProps) {
2838 total_visits_placentia_pantry : "" ,
2939 total_served_placentia_neighborhood : "" ,
3040 total_served_placentia_pantry : "" ,
41+
3142 } ) ;
3243 const navigate = useNavigate ( ) ;
33-
34-
44+ const formRef = useRef < HTMLFormElement > ( null ) ;
45+ const toast = useToast ( ) ;
3546 const generalFields = [
36- { name : "date" , label : "Date" , subtitle : "" } ,
47+
3748 { name : "total_office_visits" , label : "Total Office Visits" , subtitle : "" } ,
3849 { name : "total_calls" , label : "Total # of Calls" , subtitle : "(including children in custody)" } ,
3950 { name : "total_unduplicated_calls" , label : "Total # of unduplicated calls" , subtitle : "(including children in custody)" } ,
@@ -54,17 +65,39 @@ function FormFrontDesk({ onFormSubmitSuccess }: FormFrontDeskProps) {
5465 ]
5566
5667
57- const handleChange = ( event ) => {
68+ const handleChange = ( event : { target : { name : any ; value : any ; } ; } ) => {
5869 const name = event . target . name ;
5970 const value = event . target . value ;
60- setFormData ( ( prevState ) => ( { ...prevState , [ name ] : value } ) ) ;
71+ if ( name === "month" ) {
72+ setMonth ( value ) ;
73+ } else if ( name === "year" ) {
74+ setYear ( value ) ;
75+ }
76+ else
77+ setFormData ( ( prevState ) => ( { ...prevState , [ name ] : value } ) ) ;
78+
6179 } ;
6280
6381 const handleSubmit = async ( e : React . FormEvent ) => {
6482 e . preventDefault ( ) ;
83+
84+ for ( const key in formData ) {
85+ if ( formData [ key ] === "" ) {
86+ toast ( {
87+ title : "Missing Information" ,
88+ description : "Please fill out all required fields before submitting." ,
89+ status : "warning" ,
90+ duration : 9000 ,
91+ isClosable : true ,
92+ } ) ;
93+ return ;
94+ }
95+ }
96+
6597 try {
6698 const monthlyStatData = {
67- date : formData . date ,
99+ date : `${ year } -${ ( '0' + month ) . slice ( - 2 ) } -01` ,
100+ case_manager : formData . case_manager ,
68101 total_office_visits : parseInt ( formData . total_office_visits || "0" , 10 ) ,
69102 total_calls : parseInt ( formData . total_calls || "0" , 10 ) ,
70103 //This one isn't in the figma but its in the schema, idk what its supposed to be, just placeholder for now
@@ -80,10 +113,17 @@ function FormFrontDesk({ onFormSubmitSuccess }: FormFrontDeskProps) {
80113 total_served_placentia_pantry : parseInt ( formData . total_served_placentia_pantry || "0" , 10 ) ,
81114
82115 } ;
83-
116+
84117 await backend . post ( "/frontDesk" , monthlyStatData ) ;
118+ toast ( {
119+ title : "Successfully submitted form" ,
120+ description : `Intake Statistics Form - ${ new Date ( ) . toLocaleString ( ) } ` ,
121+ status : "success" ,
122+ duration : 9000 ,
123+ isClosable : true ,
124+ } ) ;
85125 setFormData ( {
86- date : "" ,
126+ case_manager : "" ,
87127 total_office_visits : "" ,
88128 total_calls : "" ,
89129 total_unduplicated_calls : "" ,
@@ -102,15 +142,95 @@ function FormFrontDesk({ onFormSubmitSuccess }: FormFrontDeskProps) {
102142 } catch ( error ) {
103143 console . error ( "Error submitting form:" , error ) ;
104144 setError ( "Failed to submit form. Please try again." ) ;
145+ toast ( {
146+ title : "Submission failed" ,
147+ description : error . message || "An unexpected error occurred." ,
148+ status : "error" ,
149+ duration : 9000 ,
150+ isClosable : true ,
151+ } ) ;
105152 }
106153 } ;
107154
155+ useEffect ( ( ) => {
156+ const fetchCaseManagers = async ( ) => {
157+ try {
158+ const response = await backend . get ( "/casemanagers" ) ;
159+ setCms ( response . data ) ;
160+ } catch ( error ) {
161+ console . error ( "Error fetching users:" , error ) ;
162+ }
163+ } ;
164+ fetchCaseManagers ( ) ;
165+ } , [ backend ] ) ;
166+
108167 return (
109168 < >
110- < Box maxW = "700px" marginX = { "20%" } >
111- < Text justifySelf = "center" fontSize = { 30 } paddingBottom = { "100px" } >
169+ < Box backgroundColor = { "gray.100" } width = { "100vw" } zIndex = { - 1 } top = { 0 } left = { 0 } pt = { 10 } pb = { 10 } >
170+ < Card maxW = "100%" marginX = { "20%" } justifyContent = { "center" } backgroundColor = { "white" } padding = { 10 } >
171+
172+ < Text justifySelf = "center" text-align = "center" fontSize = { 30 } paddingBottom = { "50px" } >
112173 < b > Front Desk Monthly Statistics Form</ b >
113174 </ Text >
175+ < form
176+ ref = { formRef }
177+ onSubmit = { handleSubmit }
178+ >
179+ < Box key = "date" display = "flex" flexDirection = "row" gap = "20px" p = { 2 } >
180+ < Box key = "month" display = "flex" flexDirection = "column" gap = "20px" p = { 2 } >
181+ < Text width = "100%" > Month</ Text >
182+ < Input
183+ type = "number"
184+ width = '100%'
185+ height = "30px"
186+ name = "month"
187+ value = { month }
188+ onChange = { handleChange }
189+ placeholder = "Type Here"
190+ maxLength = { 2 }
191+ />
192+ </ Box >
193+ < Box key = "year" display = "flex" flexDirection = "column" gap = "20px" p = { 2 } >
194+ < Text width = "100%" > Year</ Text >
195+ < Input
196+ type = "number"
197+ width = '100%'
198+ height = "30px"
199+ name = "year"
200+ value = { year }
201+ onChange = { handleChange }
202+ placeholder = "Type Here"
203+ maxLength = { 4 }
204+ />
205+ </ Box >
206+
207+
208+ < SelectInputComponent
209+ label = "Case Manager"
210+ name = "caseManager"
211+ value = { formData . case_manager || "" }
212+ onChange = { ( e ) => {
213+ const selectedCaseManager = cms . find (
214+ ( user ) => `${ user . firstName } ${ user . lastName } ` === e . target . value
215+ ) ;
216+ setFormData ( ( prev ) => ( {
217+ ...prev ,
218+ case_manager : e . target . value , // Set the case manager's name
219+ cmId : selectedCaseManager ?. id || null , // Set the case manager's ID
220+ } ) ) ;
221+ } }
222+ options = { cms
223+ . filter ( ( user ) => user . role === "case manager" )
224+ . map ( ( user ) => ( {
225+ key : user . id ,
226+ label : `${ user . firstName } ${ user . lastName } ` ,
227+ value : `${ user . firstName } ${ user . lastName } ` ,
228+ } ) ) }
229+ placeholder = "Select Case Manager"
230+ width = "70%"
231+ />
232+ </ Box >
233+
114234 { generalFields . map ( ( { name, label } ) => (
115235 < Box
116236 key = { name }
@@ -121,7 +241,7 @@ function FormFrontDesk({ onFormSubmitSuccess }: FormFrontDeskProps) {
121241 >
122242 < Text width = "50%" > { label } </ Text >
123243 < Input
124- type = { name === "date" ? "date" : " number"}
244+ type = " number"
125245 width = '50%'
126246 height = "30px"
127247 name = { name }
@@ -131,7 +251,7 @@ function FormFrontDesk({ onFormSubmitSuccess }: FormFrontDeskProps) {
131251 />
132252 </ Box >
133253 ) ) }
134- < Text fontWeight = { "bold" } p = { 2 } >
254+ < Text fontWeight = { "bold" } p = { 2 } paddingTop = { "30px" } >
135255 Huntington Beach (HB)
136256 </ Text >
137257 { hbFields . map ( ( { name, label } ) => (
@@ -154,7 +274,7 @@ function FormFrontDesk({ onFormSubmitSuccess }: FormFrontDeskProps) {
154274 />
155275 </ Box >
156276 ) ) }
157- < Text fontWeight = { "bold" } p = { 2 } >
277+ < Text fontWeight = { "bold" } p = { 2 } paddingTop = { "30px" } >
158278 Placentia
159279 </ Text >
160280 { placentiaFields . map ( ( { name, label } ) => (
@@ -181,13 +301,14 @@ function FormFrontDesk({ onFormSubmitSuccess }: FormFrontDeskProps) {
181301 < Button colorScheme = "gray" mr = { 3 } onClick = { ( ) => navigate ( "/forms-hub" ) } >
182302 Cancel
183303 </ Button >
184- < Button colorScheme = "blue" mr = { 3 } onClick = { handleSubmit } >
304+ < Button colorScheme = "blue" mr = { 3 } type = "submit" >
185305 Submit
186306 </ Button >
187307 </ Box >
308+ </ form >
309+ </ Card >
188310 </ Box >
189311
190-
191312 </ >
192313 ) ;
193314}
0 commit comments