|
1 |
| -import { useToast } from '@chakra-ui/react'; |
2 |
| -import { UserRole } from '@prisma/client'; |
3 |
| -import { useState } from 'react'; |
4 |
| -import { Importer, ImporterField } from 'react-csv-importer'; |
5 |
| -import 'react-csv-importer/dist/index.css'; |
6 |
| -import { trpc } from '../../utils/trpc'; |
7 |
| -import { EMAIL_REGEX } from '../../utils/constants'; |
8 |
| - |
9 |
| -interface ImportedUser { |
10 |
| - email: string; |
11 |
| - role: UserRole; |
| 1 | +import {Radio, RadioGroup, Stack, Text} from '@chakra-ui/react'; |
| 2 | +import {UserRole} from '@prisma/client'; |
| 3 | +import {useState} from 'react'; |
| 4 | +import {trpc} from '../../utils/trpc'; |
| 5 | +import {ImportNumberPossibilities, ImportNumberPossibilitiesType} from "../../utils/utils"; |
| 6 | +import SingleImporter from "./import/SingleImporter"; |
| 7 | +import BatchImporter from "./import/BatchImporter"; |
| 8 | + |
| 9 | +export interface ImportedUser { |
| 10 | + email: string; |
| 11 | + role: UserRole; |
12 | 12 | }
|
13 | 13 |
|
14 | 14 | const ImportUsers = () => {
|
15 |
| - const [invalidEmails, setInvalidEmails] = useState<string[]>([]); |
16 |
| - const [invlidRoles, setInvalidRoles] = useState<string[]>([]); |
17 |
| - const addUserMutation = trpc.user.addUsers.useMutation(); |
18 |
| - const toast = useToast(); |
19 |
| - |
20 |
| - const handleAddUsers = async (users: ImportedUser[]) => { |
21 |
| - await addUserMutation.mutateAsync(users); |
22 |
| - }; |
23 |
| - |
24 |
| - return ( |
25 |
| - <Importer |
26 |
| - // chunkSize={10000} |
27 |
| - assumeNoHeaders={false} |
28 |
| - restartable |
29 |
| - processChunk={async rows => { |
30 |
| - const users = rows.filter(user => { |
31 |
| - const userRole = user.role as string; |
32 |
| - const userEmail = user.email as string; |
33 |
| - |
34 |
| - // Check if userEmail is valid email |
35 |
| - if (!userEmail.match(EMAIL_REGEX)) { |
36 |
| - setInvalidEmails(prev => [...prev, userEmail]); |
37 |
| - return false; |
38 |
| - } |
39 |
| - |
40 |
| - // Check if user role is valid, meaning it's in the UserRole enum |
41 |
| - if (!Object.values(UserRole).includes(userRole.toUpperCase() as UserRole)) { |
42 |
| - setInvalidRoles(prev => [...prev, userRole]); |
43 |
| - return false; |
44 |
| - } |
45 |
| - |
46 |
| - return true; |
47 |
| - }); |
48 |
| - |
49 |
| - handleAddUsers(users as unknown as ImportedUser[]); |
50 |
| - }} |
51 |
| - onComplete={() => { |
52 |
| - if (invalidEmails.length > 0) { |
53 |
| - toast({ |
54 |
| - title: 'Invalid emails', |
55 |
| - description: `Added valid users. The following emails are invalid: ${invalidEmails.join(', ')}`, |
56 |
| - status: 'error', |
57 |
| - isClosable: true, |
58 |
| - position: 'top-right', |
59 |
| - }); |
60 |
| - } |
61 |
| - |
62 |
| - if (invlidRoles.length > 0) { |
63 |
| - toast({ |
64 |
| - title: 'Invalid roles', |
65 |
| - description: `Added valid usres. The following roles are invalid: ${invlidRoles.join(', ')}`, |
66 |
| - status: 'error', |
67 |
| - isClosable: true, |
68 |
| - position: 'top-right', |
69 |
| - }); |
70 |
| - } |
71 |
| - }} |
72 |
| - > |
73 |
| - <ImporterField name='email' label='Email' /> |
74 |
| - <ImporterField name='role' label='Role' /> |
75 |
| - </Importer> |
76 |
| - ); |
| 15 | + const [importType, setImportType] = useState<ImportNumberPossibilitiesType>(ImportNumberPossibilities.SINGLE_IMPORT); |
| 16 | + const addUserMutation = trpc.user.addUsers.useMutation(); |
| 17 | + |
| 18 | + const handleAddUsers = async (users: ImportedUser[]) => { |
| 19 | + await addUserMutation.mutateAsync(users); |
| 20 | + }; |
| 21 | + |
| 22 | + return ( |
| 23 | + <> |
| 24 | + <Text fontSize='xl' mb={1}>Import method</Text> |
| 25 | + <RadioGroup onChange={(val: ImportNumberPossibilitiesType) => setImportType(val)} value={importType}> |
| 26 | + <Stack spacing={5} direction='row'> |
| 27 | + <Radio value={ImportNumberPossibilities.SINGLE_IMPORT}>Single import</Radio> |
| 28 | + <Radio value={ImportNumberPossibilities.BATCH_IMPORT}>Batch import</Radio> |
| 29 | + </Stack> |
| 30 | + </RadioGroup> |
| 31 | + { |
| 32 | + importType === ImportNumberPossibilities.BATCH_IMPORT |
| 33 | + ? <BatchImporter handleAddUsers={handleAddUsers}/> |
| 34 | + : <SingleImporter handleAddUsers={handleAddUsers}/> |
| 35 | + } |
| 36 | + </> |
| 37 | + ); |
77 | 38 | };
|
78 | 39 |
|
79 | 40 | export default ImportUsers;
|
0 commit comments