diff --git a/src/modules/users/Users.tsx b/src/modules/users/Users.tsx index c40cc79..814c4c9 100644 --- a/src/modules/users/Users.tsx +++ b/src/modules/users/Users.tsx @@ -4,14 +4,16 @@ import { SearchIcon, PlusIcon } from '@heroicons/react/solid'; import { CogIcon, TrashIcon } from '@heroicons/react/outline'; import { useUsers } from 'src/services/users'; import { Table } from 'src/components'; -import { debounce } from 'lodash'; +import _, { debounce } from 'lodash'; import { useAuth } from 'src/services/auth'; import { UserModal } from '../../components/UserModal'; +import { MultipleUsersModal } from './components'; export const Users: React.FC = () => { const [selectedRowsIds, setSelectedRowsIds] = useState({}); const [configureModal, setConfigureModal] = useState(false); + const [multipleUsersModal, setMultipleUsersModal] = useState(false); const [userId, setUserId] = useState(null); const [search, setSearch] = useState(''); const { users, loadUsers, userTableLoading } = useUsers(); @@ -106,6 +108,14 @@ export const Users: React.FC = () => { Add new user + setMultipleUsersModal(true)} + type="button" + className="inline-flex items-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-primary-700 hover:bg-primary-800 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-800" + > + + Add new users + )} @@ -166,6 +176,13 @@ export const Users: React.FC = () => { {configureModal && ( )} + {multipleUsersModal && ( + setMultipleUsersModal(false)} + onUpload={_.noop} + /> + )} ); diff --git a/src/modules/users/components/MultipleUsersModal/MultipleUsersModal.tsx b/src/modules/users/components/MultipleUsersModal/MultipleUsersModal.tsx new file mode 100644 index 0000000..f57e28d --- /dev/null +++ b/src/modules/users/components/MultipleUsersModal/MultipleUsersModal.tsx @@ -0,0 +1,124 @@ +import React, { ChangeEvent, ChangeEventHandler, useEffect, useState } from 'react'; +import _ from 'lodash'; +// import { TrashIcon } from '@heroicons/react/outline'; +// import { useFieldArray, useForm, useWatch } from 'react-hook-form'; +import { Modal, Tabs } from 'src/components'; +// import { Input, Select } from 'src/components/Form'; +import { useUsers } from 'src/services/users'; +import { useAuth } from 'src/services/auth'; +import { MultipleUsersModalProps } from './types'; +import { csvFileToArray } from './utils'; + +export const MultipleUsersModal = ({ open, onClose, onUpload }: MultipleUsersModalProps) => { + const [file, setFile] = useState(); + const { userModalLoading } = useUsers(); + const { currentUser, isAdmin } = useAuth(); + + const handleOnChange = (e: ChangeEvent) => { + setFile(_.get(e.target, 'files[0]')); + console.log(_.get(e.target, 'files[0]')); + }; + + const handleSubmit = () => { + // e.preventDefault(); + + if (file) { + const formData = new FormData(); + formData.append('image', file, file.name); + + // console.log('csv to array', csvFileToArray(file)); + console.log('submit', formData); + } else { + console.log('nothing in file?'); + } + }; + + const renderUpload = () => { + return ( + + Please upload CSV file using , as a delimiter + + + IMPORT CSV + + + ); + }; + + const renderMultilineInput = () => { + return ( + + + Enter details here... + + + ); + }; + + const handleSave = async () => { + // try { + // if (userId) { + // await handleSubmit((data) => editUserById(data))(); + // } else { + // await handleSubmit((data) => createNewUser(data))(); + // } + // } catch (e: any) { + // // Continue + // } + + onUpload(); + + onClose(); + }; + + const handleClose = () => { + onClose(); + }; + + return ( + <> + + // + // Delete + // + // ) + // } + useCancelButton + > + + + + + Add new users + + + + + + + + + > + ); +}; diff --git a/src/modules/users/components/MultipleUsersModal/index.ts b/src/modules/users/components/MultipleUsersModal/index.ts new file mode 100644 index 0000000..b8c3b1a --- /dev/null +++ b/src/modules/users/components/MultipleUsersModal/index.ts @@ -0,0 +1 @@ +export { MultipleUsersModal } from './MultipleUsersModal'; diff --git a/src/modules/users/components/MultipleUsersModal/types.ts b/src/modules/users/components/MultipleUsersModal/types.ts new file mode 100644 index 0000000..2a3a280 --- /dev/null +++ b/src/modules/users/components/MultipleUsersModal/types.ts @@ -0,0 +1,5 @@ +export type MultipleUsersModalProps = { + open: boolean; + onClose: () => void; + onUpload: () => void; +}; diff --git a/src/modules/users/components/MultipleUsersModal/utils.ts b/src/modules/users/components/MultipleUsersModal/utils.ts new file mode 100644 index 0000000..7934b5d --- /dev/null +++ b/src/modules/users/components/MultipleUsersModal/utils.ts @@ -0,0 +1,14 @@ +import { User } from 'src/services/users'; + +export const csvFileToArray = (csvData: string) => { + const csvRows = csvData.slice(csvData.indexOf('\n') + 1).split('\n'); + + const array = csvRows.map((i) => { + const values = i.split(','); + const email = values[0]; + const name = values[1]; + return { email, name, app_roles: [], preferredUsername: '', status: '', id: '' } as User; + }); + + return array; +}; diff --git a/src/modules/users/components/index.ts b/src/modules/users/components/index.ts new file mode 100644 index 0000000..b8c3b1a --- /dev/null +++ b/src/modules/users/components/index.ts @@ -0,0 +1 @@ +export { MultipleUsersModal } from './MultipleUsersModal';