@@ -13,49 +13,47 @@ import { Input } from "@/components/ui/input";
1313import { Label } from "@/components/ui/label" ;
1414import { useRef , useState } from "react" ;
1515import { useCreateShareMutation } from "@/api/sharedApiSlice" ;
16- import TimetableErrorDialog from "./TimetableErrorDialog" ;
1716import TimetableSuccessDialog from "./TimetableSuccessDialog" ;
17+ import { Spinner } from "@/components/ui/spinner" ;
1818
19- interface ShareButtonProps {
19+ interface ShareDialogProps {
20+ open : boolean ;
21+ setOpen : ( open : boolean ) => void ;
2022 calendar_id : number ;
2123}
2224
23- const ShareButton = ( { calendar_id } : ShareButtonProps ) => {
25+ const ShareDialog = ( { open , setOpen , calendar_id } : ShareDialogProps ) => {
2426 const emailRef = useRef < HTMLInputElement > ( null ) ;
2527 const [ shareTimetable ] = useCreateShareMutation ( ) ;
28+
29+ const [ loading , setLoading ] = useState ( false ) ;
2630 const [ errorMessage , setErrorMessage ] = useState < string | null > ( null ) ;
2731 const [ successMessage , setSuccessMessage ] = useState < string | null > ( null ) ;
2832
2933 const handleShare = async ( ) => {
34+ setLoading ( true ) ;
3035 const email = emailRef . current ?. value ;
31-
3236 const { error } = await shareTimetable ( {
3337 shared_email : email ,
3438 calendar_id,
3539 } ) ;
3640 if ( error ) {
3741 const errorData = ( error as { data ?: { error ?: string } } ) . data ;
3842 setErrorMessage ( errorData ?. error ?? "Unknown error occurred" ) ;
39- return ;
4043 } else {
4144 setSuccessMessage ( "Timetable shared successfully!" ) ;
45+ setOpen ( false ) ;
46+ setErrorMessage ( null ) ;
4247 }
48+ setLoading ( false ) ;
4349 } ;
4450
45- return (
46- < Dialog >
47- < TimetableErrorDialog
48- errorMessage = { errorMessage }
49- setErrorMessage = { setErrorMessage }
50- />
51+ return ( < Dialog open = { open } onOpenChange = { ( ) => { setOpen ( ! open ) ; setErrorMessage ( null ) ; } } >
5152 < TimetableSuccessDialog
5253 successMessage = { successMessage }
5354 setSuccessMessage = { setSuccessMessage }
5455 />
5556 < DialogTrigger asChild >
56- < Button size = "sm" variant = "outline" >
57- Share
58- </ Button >
5957 </ DialogTrigger >
6058 < DialogContent className = "gap-5" >
6159 < DialogHeader >
@@ -64,6 +62,7 @@ const ShareButton = ({ calendar_id }: ShareButtonProps) => {
6462 Who would you like to share your timetable with?
6563 </ DialogDescription >
6664 </ DialogHeader >
65+ { loading && < Spinner /> }
6766 < Label htmlFor = "sharedEmail" >
6867 Enter the email of the person you want to share your timetable with
6968 </ Label >
@@ -74,17 +73,16 @@ const ShareButton = ({ calendar_id }: ShareButtonProps) => {
7473 placeholder = "Email"
7574 className = "w-full"
7675 />
76+ < DialogDescription className = "text-red-500" > { errorMessage } </ DialogDescription >
7777 < DialogFooter >
7878 < DialogClose asChild >
7979 < Button variant = "secondary" > Cancel</ Button >
8080 </ DialogClose >
81- < DialogClose asChild >
82- < Button onClick = { handleShare } > Share</ Button >
83- </ DialogClose >
81+ < Button onClick = { handleShare } > Share</ Button >
8482 </ DialogFooter >
8583 </ DialogContent >
8684 </ Dialog >
8785 ) ;
8886} ;
8987
90- export default ShareButton ;
88+ export default ShareDialog ;
0 commit comments