-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: added identification hook and form
- Loading branch information
1 parent
0af7144
commit b0b70f9
Showing
14 changed files
with
295 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import { useEffect, useState } from "react"; | ||
import { Paperclip } from "lucide-react"; | ||
import { useSelector } from "react-redux"; | ||
import { store } from "@/store"; | ||
import { authApi, useAuthUserQuery, useUpdateProfileMutation } from "@/store/api"; | ||
import { toggleIdentificationForm } from "@/store/reducers/ui/global"; | ||
import { | ||
Accordion, | ||
AccordionContent, | ||
AccordionItem, | ||
AccordionTrigger, | ||
AlertDialog, | ||
AlertDialogContent, | ||
AlertDialogDescription, | ||
AlertDialogFooter, | ||
AlertDialogHeader, | ||
AlertDialogTitle, | ||
Button, | ||
Dropdown, | ||
Input, | ||
toast | ||
} from "@sliit-foss/bashaway-ui/components"; | ||
import { gender, mealPreference } from "@sliit-foss/bashaway-ui/constants"; | ||
|
||
const close = () => store.dispatch(toggleIdentificationForm(false)); | ||
|
||
const IdentificationForm = () => { | ||
const open = useSelector((store) => store.ui.global.showIdentificationForm); | ||
|
||
const { data: { data: team } = {} } = useAuthUserQuery(); | ||
|
||
const [updateProfile, { isLoading }] = useUpdateProfileMutation(); | ||
|
||
const [formData, setFormData] = useState(team?.members); | ||
const [idFiles, setIdFiles] = useState([]); | ||
|
||
useEffect(() => { | ||
if (team) setFormData(team.members); | ||
}, [team]); | ||
|
||
const handleSubmit = async () => { | ||
await updateProfile({ | ||
id: team._id, | ||
data: { | ||
members: formData | ||
} | ||
}) | ||
.unwrap() | ||
.then((data) => { | ||
store.dispatch(authApi.util.upsertQueryData("authUser", undefined, { data: data?.data })); | ||
close(); | ||
toast({ title: "Details recorded successfully" }); | ||
}); | ||
}; | ||
|
||
const onFileChange = (e, index) => { | ||
setIdFiles((prev) => { | ||
const newFiles = JSON.parse(JSON.stringify(prev)); | ||
newFiles[index] = e.target.files[0]; | ||
return newFiles; | ||
}); | ||
}; | ||
|
||
const onChange = (e, index) => { | ||
const newFormData = JSON.parse(JSON.stringify(formData)); | ||
newFormData[index][e.target.name] = e.target.value; | ||
setFormData(newFormData); | ||
}; | ||
|
||
return ( | ||
<AlertDialog | ||
open={open} | ||
onOpenChange={(open) => { | ||
if (!open) close(); | ||
}} | ||
> | ||
<AlertDialogContent> | ||
<form onSubmit={handleSubmit} className="flex flex-col gap-3"> | ||
<AlertDialogHeader> | ||
<AlertDialogTitle>Hello there!</AlertDialogTitle> | ||
<AlertDialogDescription> | ||
Congratulations on being selected to the final round of Bashaway 2023. However, before you can continue to | ||
use this platform to compete further, please fill in the following details. | ||
</AlertDialogDescription> | ||
</AlertDialogHeader> | ||
<Accordion type="single" collapsible> | ||
{formData?.map((member, index) => ( | ||
<AccordionItem key={index} value={`member-${index}`}> | ||
<AccordionTrigger>{member.name}</AccordionTrigger> | ||
<AccordionContent containerClassName="flex flex-col gap-3"> | ||
<Input | ||
placeholder="NIC *" | ||
name="nic" | ||
required | ||
className="sm:h-14" | ||
onChange={(e) => onChange(e, index)} | ||
/> | ||
<div className="flex flex-col md:flex-row gap-3"> | ||
<Dropdown | ||
filterkey="gender" | ||
label="Gender *" | ||
options={gender.options} | ||
className="sm:h-14" | ||
value={formData[index].gender} | ||
onChange={(e) => onChange(e, index)} | ||
/> | ||
<Dropdown | ||
filterkey="meal_preference" | ||
label="Meal Preference *" | ||
options={mealPreference.options} | ||
className="sm:h-14" | ||
value={formData[index].meal_preference} | ||
onChange={(e) => onChange(e, index)} | ||
/> | ||
</div> | ||
<Input | ||
placeholder="University ID (both sides) *" | ||
name="student_id_url" | ||
value={idFiles[index] ? idFiles[index].name : formData.student_id_url} | ||
required | ||
className="sm:h-14 cursor-pointer" | ||
onClick={() => document.getElementById(`id-upload-${index}`).click()} | ||
suffixIcon={<Paperclip className="text-gray-400 pointer-events-none" size={21} />} | ||
readOnly | ||
/> | ||
<input | ||
id={`id-upload-${index}`} | ||
type="file" | ||
className="hidden" | ||
onChange={(e) => onFileChange(e, index)} | ||
/> | ||
</AccordionContent> | ||
</AccordionItem> | ||
))} | ||
</Accordion> | ||
<AlertDialogFooter className="mt-4"> | ||
<Button type="submit" loading={isLoading}> | ||
Submit and continue | ||
</Button> | ||
</AlertDialogFooter> | ||
</form> | ||
</AlertDialogContent> | ||
</AlertDialog> | ||
); | ||
}; | ||
|
||
export default IdentificationForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useEffect } from "react"; | ||
import { useSelector } from "react-redux"; | ||
import { store } from "@/store"; | ||
import { useAuthUserQuery, useGetSettingsQuery } from "@/store/api"; | ||
import { toggleIdentificationForm } from "@/store/reducers/ui/global"; | ||
|
||
const useIdentification = () => { | ||
const { data: { data: settings } = {} } = useGetSettingsQuery(); | ||
const { data: { data: team } = {} } = useAuthUserQuery(); | ||
|
||
const open = useSelector((store) => store.ui.global.showIdentificationForm); | ||
|
||
useEffect(() => { | ||
if ( | ||
team && | ||
settings?.round_breakpoint && | ||
new Date() > new Date(settings?.round_breakpoint) && | ||
!team.eliminated && | ||
team.members.find((member) => !member.nic) && | ||
!open | ||
) { | ||
store.dispatch(toggleIdentificationForm(true)); | ||
} | ||
}, [settings]); | ||
}; | ||
|
||
export default useIdentification; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { default as useAuth } from "./auth"; | ||
export { default as useBreakpoint } from "./breakpoint"; | ||
export { default as useEffectOnce } from "./effect-once"; | ||
export { default as useIdentification } from "./identification"; | ||
export { default as useTitle } from "./title"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.