diff --git a/src/app/event-setup/components/add-participants.tsx b/src/app/event-setup/components/add-participants.tsx index b2cc5f7..ded4c0d 100644 --- a/src/app/event-setup/components/add-participants.tsx +++ b/src/app/event-setup/components/add-participants.tsx @@ -1,67 +1,14 @@ "use client"; -import { toast } from "sonner"; -import { nanoid } from "nanoid"; -import React, { useState } from "react"; -import { db } from "@/config/firebase"; -import { Attendee } from "@/lib/types"; -import { doc, setDoc } from "firebase/firestore"; - -import AdminPermDialog from "./admin-perm-dialog"; -import { useParams } from "next/navigation"; -import { XlsxForm } from "./xlsx-form"; +import React from "react"; import JsonForm from "./json-form"; +import { XlsxForm } from "./xlsx-form"; +import ImportParticipantsDialog from "./import-participants-dialog"; export default function AddParticipants() { - const [attendees, setAttendees] = useState(""); - const [parsedAttendees, setParsedAttendees] = useState([]); - const [loading, setLoading] = useState(false); - const [openAdminModal, setAdminModal] = useState(false); - - const params = useParams<{ name: string }>(); - - const handleImport = () => { - setLoading(true); - if (parsedAttendees.length === 0) { - toast.error("No attendees to import"); - return; - } - - try { - parsedAttendees.forEach(async (attendee) => { - try { - await setDoc( - doc(db, `${params.name}/data/certificates`, nanoid(10)), - { - firstName: attendee.firstName, - lastName: attendee.lastName, - email: attendee.email, - }, - ); - } catch (err: any) { - toast.error(err.message); - } - }); - - setTimeout(() => { - setParsedAttendees([]); - setAttendees(""); - }, 500); - - toast.success("Certificates added"); - } catch (err: any) { - toast.error(err.message); - } - setLoading(false); - }; - return (
- +
diff --git a/src/app/event-setup/components/import-participants-dialog.tsx b/src/app/event-setup/components/import-participants-dialog.tsx new file mode 100644 index 0000000..2e05e80 --- /dev/null +++ b/src/app/event-setup/components/import-participants-dialog.tsx @@ -0,0 +1,122 @@ +"use client"; + +import { toast } from "sonner"; +import { nanoid } from "nanoid"; +import { db } from "@/config/firebase"; +import React, { useState } from "react"; +import { useParams } from "next/navigation"; +import { doc, setDoc } from "firebase/firestore"; +import AdminPermDialog from "./admin-perm-dialog"; +import { useAttendeesStore } from "@/hooks/use-attendees-store"; + +import { AlertDialog, AlertDialogContent } from "@/components/ui/alert-dialog"; + +import { Icons } from "@/components/icons"; +import { Button } from "@/components/ui/button"; +import { useDialogStore } from "@/hooks/use-dialog-store"; + +export default function ImportParticipantsDialog() { + const params = useParams<{ name: string }>(); + + const [openAdminModal, setAdminModal] = useState(false); + const [loading, setLoading] = useState(false); + + const { importDialog, setImportDialog } = useDialogStore(); + const { parsedAttendees, setParsedAttendees } = useAttendeesStore(); + + const handleImport = () => { + setLoading(true); + if (parsedAttendees.length === 0) { + toast.error("No attendees to import"); + return; + } + + try { + parsedAttendees.forEach(async (attendee) => { + try { + await setDoc( + doc(db, `${params.name}/data/certificates`, nanoid(10)), + { + firstName: attendee.firstName, + lastName: attendee.lastName, + email: attendee.email, + }, + ); + } catch (err: any) { + toast.error(err.message); + } + }); + + setTimeout(() => { + setParsedAttendees([]); + }, 500); + + toast.success("Certificates added"); + } catch (err: any) { + toast.error(err.message); + } + setLoading(false); + }; + + const cancelImport = () => { + setImportDialog(false); + setParsedAttendees([]); + }; + + return ( + <> + + + + + {parsedAttendees.length > 0 && ( + <> +

+ Total certificates to be added: {parsedAttendees.length} +

+
+ {parsedAttendees.map((m) => { + return ( +
+
+

Email:

+

First Name:

+

Last Name:

+
+
+

{m.email}

+

{m.firstName}

+

{m.lastName}

+
+
+ ); + })} +
+ +
+ + +
+ + )} +
+
+ + ); +} diff --git a/src/app/event-setup/components/json-form.tsx b/src/app/event-setup/components/json-form.tsx index e1174e9..ad6f091 100644 --- a/src/app/event-setup/components/json-form.tsx +++ b/src/app/event-setup/components/json-form.tsx @@ -2,20 +2,21 @@ import { z } from "zod"; import { toast } from "sonner"; -import { nanoid } from "nanoid"; import React, { useState } from "react"; import { Attendee } from "@/lib/types"; import { Separator } from "@/components/ui/separator"; import { Textarea } from "@/components/ui/textarea"; import { Button } from "@/components/ui/button"; -import { Icons } from "@/components/icons"; +import { useDialogStore } from "@/hooks/use-dialog-store"; +import { useAttendeesStore } from "@/hooks/use-attendees-store"; export default function JsonForm() { const [attendees, setAttendees] = useState(""); - const [parsedAttendees, setParsedAttendees] = useState([]); const [loading, setLoading] = useState(false); - const [openAdminModal, setAdminModal] = useState(false); + + const { setParsedAttendees } = useAttendeesStore(); + const { setImportDialog } = useDialogStore(); const handleParse = () => { setLoading(true); @@ -43,6 +44,7 @@ export default function JsonForm() { toast.success("Parsed successfully"); setParsedAttendees(_parsedAttendees); + setImportDialog(true); } catch { toast.error("Invalid JSON format"); } @@ -50,20 +52,21 @@ export default function JsonForm() { }; return ( -
-
-
-

Import via JSON

-

- Strict: - Make sure to follow the same JSON Format -

-
- -

Sample JSON

-
-          
-            {`[
+    <>
+      
+        
+
+

Import via JSON

+

+ Strict: + Make sure to follow the same JSON Format +

+
+ +

Sample JSON

+
+            
+              {`[
   {
     "firstName": "John",
     "lastName": "Doe",
@@ -75,65 +78,28 @@ export default function JsonForm() {
     "email": "sally@smith.com"
   }
 ]`}
-          
-        
+
+
-
-