From 93f252c71878bf55d06aae7b47f181d4dd391d0b Mon Sep 17 00:00:00 2001 From: "Vuyani M. Shabangu" Date: Thu, 25 Jul 2024 09:45:14 +0200 Subject: [PATCH] feat: invite-users (#44) * feat: invite-users inviting users to project * feat: invite-users changing working to add users --- components/ProjectDetails/index.tsx | 10 +- components/ProjectInviteUsers/index.tsx | 145 ++++++++++++++++++++++++ global/utils/api.ts | 7 +- 3 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 components/ProjectInviteUsers/index.tsx diff --git a/components/ProjectDetails/index.tsx b/components/ProjectDetails/index.tsx index 8159e1c..7873dbc 100644 --- a/components/ProjectDetails/index.tsx +++ b/components/ProjectDetails/index.tsx @@ -20,9 +20,9 @@ */ import React, { useState, useEffect } from 'react'; -import { Card, Divider, Modal, Button, message } from 'antd'; +import { Card, Divider, Modal } from 'antd'; import { useRouter } from 'next/router'; -import { EditOutlined, DeleteOutlined, UserAddOutlined } from '@ant-design/icons'; +import { EditOutlined, DeleteOutlined } from '@ant-design/icons'; import { Toaster } from 'react-hot-toast'; import { authorizedApiRequest } from '@/global/utils/api'; @@ -31,6 +31,7 @@ import toast, { ToastType } from '@/global/utils/toast'; import useAuthContext from '../../global/hooks/useAuthContext'; import UpdateProject from '../ProjectUpdate'; +import ProjectInviteUsers from '../ProjectInviteUsers'; const CardDivColumn: React.CSSProperties = { display: 'flex', @@ -69,6 +70,7 @@ function convertToTableData(responseData: any): Project { studyCount: responseData?.study_count, }; } + const ProjectDetails: React.FC = () => { const [origin, setOrigin] = useState(''); const [loading, setLoading] = useState(true); @@ -201,9 +203,7 @@ const ProjectDetails: React.FC = () => { {project?.description} - + } /> diff --git a/components/ProjectInviteUsers/index.tsx b/components/ProjectInviteUsers/index.tsx new file mode 100644 index 0000000..27e703c --- /dev/null +++ b/components/ProjectInviteUsers/index.tsx @@ -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([]); + const [loading, setloading] = useState(false); + const [selectedUsers, setSelectedUsers] = useState([]); + + 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 ( + <> + + +
+ +