forked from virusseq/portal-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: invite-users inviting users to project * feat: invite-users changing working to add users
- Loading branch information
1 parent
2d6ac26
commit 93f252c
Showing
3 changed files
with
156 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import { Button, Form, Modal, Select, SelectProps } from 'antd'; | ||
import { FC, useEffect, useState } from 'react'; | ||
import { UserAddOutlined } from '@ant-design/icons'; | ||
import { useRouter } from 'next/router'; | ||
|
||
import { authorizedApiRequest } from '@/global/utils/api'; | ||
import { API_ROUTES_PATHS, HttpMethods } from '@/global/utils/constants'; | ||
import toast, { ToastType } from '@/global/utils/toast'; | ||
|
||
type User = { | ||
value: string; | ||
label: string; | ||
}; | ||
|
||
function convertToTableData(responseData: []): User[] { | ||
return responseData.map((element: any) => { | ||
return { | ||
value: element?.id, | ||
label: `${element?.firstName} ${element?.lastName}`, | ||
}; | ||
}); | ||
} | ||
|
||
const ProjectInviteUsers: FC = () => { | ||
const [isModalOpen, setIsModalOpen] = useState(false); | ||
const [users, setUsers] = useState<SelectProps['options']>([]); | ||
const [loading, setloading] = useState(false); | ||
const [selectedUsers, setSelectedUsers] = useState<string[]>([]); | ||
|
||
useEffect(() => { | ||
authorizedApiRequest(HttpMethods.GET, API_ROUTES_PATHS.USERS) | ||
.then((data) => { | ||
setUsers(convertToTableData(data?.users)); | ||
}) | ||
.catch((error) => { | ||
console.log(error); | ||
}); | ||
}, []); | ||
|
||
const router = useRouter().query; | ||
const projectId = router['project-id']; | ||
|
||
const showModal = () => { | ||
setIsModalOpen(true); | ||
}; | ||
|
||
const handleOk = () => { | ||
setSelectedUsers([]); | ||
setIsModalOpen(false); | ||
}; | ||
|
||
const handleCancel = () => { | ||
setSelectedUsers([]); | ||
setIsModalOpen(false); | ||
}; | ||
|
||
const handleChange = (value: string[]) => { | ||
setSelectedUsers([...value]); | ||
}; | ||
|
||
const handleClear = () => { | ||
setSelectedUsers([]); | ||
}; | ||
|
||
const InviteUsers = () => { | ||
setloading(true); | ||
const uniqueUsers = new Set(selectedUsers); | ||
const users = [...uniqueUsers]; | ||
Promise.all( | ||
users.map((user) => | ||
authorizedApiRequest( | ||
HttpMethods.POST, | ||
`${API_ROUTES_PATHS.PROJECTS}/${projectId}/users/${user}`, | ||
) | ||
.then((data) => console.log()) | ||
.catch((error) => { | ||
console.log(error); | ||
throw error; | ||
}), | ||
), | ||
) | ||
.then((data) => { | ||
toast(ToastType.SUCCESS, 'Users are invited successfully'); | ||
setloading(false); | ||
handleOk(); | ||
}) | ||
.catch((error) => { | ||
console.log(error); | ||
setloading(false); | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Button onClick={() => showModal()} type="primary" icon={<UserAddOutlined />} ghost> | ||
Add users to project | ||
</Button> | ||
<Modal | ||
title="Add users to project" | ||
open={isModalOpen} | ||
onOk={handleOk} | ||
onCancel={handleCancel} | ||
footer={false} | ||
> | ||
<Form | ||
size="large" | ||
name="basic" | ||
style={{ minWidth: 400, margin: 'auto' }} | ||
onFinish={InviteUsers} | ||
layout="horizontal" | ||
autoComplete="off" | ||
labelCol={{ span: 7 }} | ||
> | ||
<Form.Item label="Users" rules={[{ required: true, message: 'Please select users' }]}> | ||
<Select | ||
mode="multiple" | ||
onClear={handleClear} | ||
value={selectedUsers} | ||
style={{ width: '100%' }} | ||
placeholder="Please select users to add" | ||
onChange={handleChange} | ||
options={users} | ||
showSearch | ||
optionFilterProp="label" | ||
/> | ||
</Form.Item> | ||
<Form.Item style={{ display: 'flex', justifyContent: 'end', width: '100%' }}> | ||
<Button | ||
disabled={!selectedUsers.length} | ||
loading={loading} | ||
size="large" | ||
block | ||
type="primary" | ||
htmlType="submit" | ||
> | ||
Add users | ||
</Button> | ||
</Form.Item> | ||
</Form> | ||
</Modal> | ||
</> | ||
); | ||
}; | ||
|
||
export default ProjectInviteUsers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters