|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { Button } from "@/components/ui/button"; |
| 4 | +import { toast } from "@/components/ui/use-toast"; |
| 5 | +import { parseInviteMembersCSV } from "@/lib/invite-team-members-csv-parser"; |
| 6 | +import { api } from "@/trpc/react"; |
| 7 | +import { type TypeZodInviteMemberArrayMutationSchema } from "@/trpc/routers/member-router/schema"; |
| 8 | +import { RiUploadLine } from "@remixicon/react"; |
| 9 | +import Link from "next/link"; |
| 10 | +import { useRouter } from "next/navigation"; |
| 11 | +import { useRef, useState } from "react"; |
| 12 | + |
| 13 | +type TeamMemberUploaderType = { |
| 14 | + setOpen: (val: boolean) => void; |
| 15 | +}; |
| 16 | + |
| 17 | +const TeamMemberUploader = ({ setOpen }: TeamMemberUploaderType) => { |
| 18 | + const [csvFile, setCSVFile] = useState<File[]>([]); |
| 19 | + const fileInputRef = useRef<HTMLInputElement>(null); |
| 20 | + |
| 21 | + const router = useRouter(); |
| 22 | + |
| 23 | + const inviteMember = api.member.inviteMember.useMutation({ |
| 24 | + onSuccess: () => { |
| 25 | + setOpen(false); |
| 26 | + toast({ |
| 27 | + variant: "default", |
| 28 | + title: "🎉 Invited!", |
| 29 | + description: "You have successfully invited the stakeholder.", |
| 30 | + }); |
| 31 | + router.refresh(); |
| 32 | + }, |
| 33 | + onError: (error) => { |
| 34 | + toast({ |
| 35 | + variant: "destructive", |
| 36 | + title: error.message, |
| 37 | + description: "", |
| 38 | + }); |
| 39 | + }, |
| 40 | + }); |
| 41 | + |
| 42 | + const onFileInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 43 | + const files = Array.from(e.currentTarget.files ?? []); |
| 44 | + setCSVFile(files); |
| 45 | + }; |
| 46 | + |
| 47 | + const onImport = async () => { |
| 48 | + try { |
| 49 | + if (!csvFile[0]) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + const parsedData = (await parseInviteMembersCSV( |
| 54 | + csvFile[0], |
| 55 | + )) as TypeZodInviteMemberArrayMutationSchema; |
| 56 | + await Promise.all( |
| 57 | + parsedData.map(async (data) => { |
| 58 | + await inviteMember.mutateAsync(data); |
| 59 | + }), |
| 60 | + ); |
| 61 | + setOpen(false); |
| 62 | + } catch (error) { |
| 63 | + console.error((error as Error).message); |
| 64 | + toast({ |
| 65 | + variant: "destructive", |
| 66 | + title: "Something went wrong!", |
| 67 | + description: |
| 68 | + "Please check the CSV file and make sure its according to our format", |
| 69 | + }); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + return ( |
| 74 | + <div className="space-y-4"> |
| 75 | + <div className="text-sm leading-6 text-neutral-600"> |
| 76 | + Please download the{" "} |
| 77 | + <Link |
| 78 | + download |
| 79 | + href="/sample-csv/captable-team-members-template.csv" |
| 80 | + target="_blank" |
| 81 | + rel="noopener noreferrer" |
| 82 | + className="rounded bg-gray-300/70 px-2 py-1 text-xs font-medium hover:bg-gray-400/50" |
| 83 | + > |
| 84 | + <span className="mr-1">sample csv file</span> |
| 85 | + <span aria-hidden="true"> ↓</span> |
| 86 | + </Link> |
| 87 | + , complete and upload it to import your existing or new team members. |
| 88 | + </div> |
| 89 | + |
| 90 | + <div |
| 91 | + className="flex h-24 cursor-pointer flex-col items-center justify-center gap-2 rounded-lg border border-gray-300" |
| 92 | + onClick={() => fileInputRef.current?.click()} |
| 93 | + > |
| 94 | + <RiUploadLine className="h-7 w-7 text-neutral-500" /> |
| 95 | + <span className="text-sm text-neutral-500"> |
| 96 | + {csvFile.length !== 0 ? csvFile[0]?.name : "Click here to import"} |
| 97 | + </span> |
| 98 | + <input |
| 99 | + onChange={onFileInputChange} |
| 100 | + type="file" |
| 101 | + ref={fileInputRef} |
| 102 | + accept=".csv" |
| 103 | + hidden |
| 104 | + /> |
| 105 | + </div> |
| 106 | + |
| 107 | + <div className="text-xs"> |
| 108 | + <Link |
| 109 | + target="_blank" |
| 110 | + rel="noopener noreferrer" |
| 111 | + href={""} |
| 112 | + className="text-teal-700 underline" |
| 113 | + > |
| 114 | + Learn more |
| 115 | + </Link>{" "} |
| 116 | + about the sample csv format |
| 117 | + </div> |
| 118 | + |
| 119 | + <Button onClick={onImport} className="ml-auto block"> |
| 120 | + Import |
| 121 | + </Button> |
| 122 | + </div> |
| 123 | + ); |
| 124 | +}; |
| 125 | + |
| 126 | +export default TeamMemberUploader; |
0 commit comments