diff --git a/src/app/routes/BasicRoutes.tsx b/src/app/routes/BasicRoutes.tsx index af764be..99206de 100644 --- a/src/app/routes/BasicRoutes.tsx +++ b/src/app/routes/BasicRoutes.tsx @@ -11,6 +11,7 @@ import Workspace from 'features/workspace'; import EditWorkspace from 'features/EditWorkspace'; import EditProject from 'features/EditProject'; import WorkspaceMembers from 'features/workspace-members '; +import WorkspaceAddMember from 'features/WorkspaceAddMember'; const BasicRoutes = () => { return ( @@ -24,6 +25,7 @@ const BasicRoutes = () => { } /> }/> }/> + } /> } /> {/* } /> */} diff --git a/src/features/WorkspaceAddMember/index.tsx b/src/features/WorkspaceAddMember/index.tsx index de46672..2075c46 100644 --- a/src/features/WorkspaceAddMember/index.tsx +++ b/src/features/WorkspaceAddMember/index.tsx @@ -1,9 +1,9 @@ import { getAllUser, getUser } from 'app/api/user'; import React, { ChangeEvent, useContext, useEffect, useState } from 'react'; -import { useNavigate } from 'react-router-dom'; +import { useNavigate, useParams } from 'react-router-dom'; import { useQuery } from 'react-query'; import toast from 'react-hot-toast'; -import { addOrg, addOrgMembers, getAllOrgs } from 'app/api/organization'; +import { addOrg, addOrgMembers, getAllOrgs, getOrgMembers } from 'app/api/organization'; import { uploadIcon } from 'app/api/file'; import './index.scss'; @@ -14,35 +14,27 @@ const WorkspaceAddMember = () => { const token = localStorage.getItem('token'); const userContext = useContext(UserContext); - const [selectedFile, setSelectedFile] = useState(null); - const [name, SetName] = useState(null); - const [description, setDiscription] = useState(null); - const [validDescription, setValidDescription] = useState(true); - const [validName, setValidName] = useState(false); - const [uniqueName, setUniqueName] = useState(false); const [members, setMembers] = useState([]); const [memberName, setMemberName] = useState(null); - + const [orgMembers, setOrgMembers] = useState([]) const [users, setUsers] = useState([]); - const [orgs, setOrgs] = useState([]); + const {spaceName} = useParams() const dataFetch = async () => { try { - if (token) { + if (token&&spaceName) { const users_aray: string[] = []; - const org_aray: string[] = []; + const allUser = await getAllUser(token); - const allOrgs = await getAllOrgs(token); + const orgMembers = await getOrgMembers(token,spaceName) allUser.data.users.forEach((user) => { users_aray.push(user.username); }); + setOrgMembers(Object.keys(orgMembers.data.members)) - allOrgs.data.organizations.forEach((org) => { - org_aray.push(org.name); - }); setUsers(users_aray); - setOrgs(org_aray); + } } catch (e) {} }; @@ -51,52 +43,17 @@ const WorkspaceAddMember = () => { dataFetch(); }, []); - const allowedFieTypes = ['image/jpeg', 'image/jpg', 'image/png']; - - const handleFileChange = (event: ChangeEvent) => { - const file = event.target.files?.[0]; - if (file) { - if (allowedFieTypes.includes(file.type)) { - setSelectedFile(file); - } else { - setSelectedFile(null); - toast.error('Invalid file type'); - } - } - }; - function valid_name(str: string): boolean { - // Define a regular expression for special characters (excluding letters, digits, and spaces) - const specialCharacters = /^[a-zA-Z0-9_-]+$/; - // Check if the string contains any special characters - return specialCharacters.test(str) && !str.endsWith('-userspace'); - } - - function isUniqueName(str: string): boolean { - return !orgs.includes(str); - } - - const handleNameChange = (event: ChangeEvent) => { - SetName(event.target.value); - setUniqueName(() => isUniqueName(event.target.value)); - setValidName(() => valid_name(event.target.value)); - }; - - const handleDesriptionChange = (event: ChangeEvent) => { - setDiscription(event.target.value); - if (description?.length) { - setValidDescription(description.length < 200); - } - }; const addMembers = () => { if (memberName) { if ( users.includes(memberName) && memberName != userContext?.username && - !members.includes(memberName) + !members.includes(memberName)&& + !orgMembers.includes(memberName) ) { setMembers([...members, memberName]); setMemberName(null); @@ -120,36 +77,23 @@ const WorkspaceAddMember = () => { }; const SubmitHandler = async (): Promise => { if ( - description && - token && - name && - validName && - uniqueName && - validDescription + + token + ) { const func = async (): Promise => { - const dataRes = await addOrg(token, { - name: name, - description: description, - }); - - try { - if (selectedFile != null) { - const fileRes = uploadIcon(token, name, selectedFile); - } - } catch (e) {} - if (members.length > 0) { + if (members.length > 0&&spaceName) { try { - const addMmebersRes = await addOrgMembers(token, name, members); + const addMmebersRes = await addOrgMembers(token, spaceName, members); } catch (e) {} } navigate('/'); }; toast.promise(func(), { - loading: 'Saving Workspace', - success: Workspace saved, - error: Could not save, + loading: 'On Progress', + success: Members added, + error: Could not add, }); } else { toast.error('Invalid inputs'); @@ -159,60 +103,10 @@ const WorkspaceAddMember = () => { return (
+ +
- -
- - -

Supported formats: JPEG, JPG, PNG

-

Selected File: {selectedFile?.name}

-
-
-
- - - {!name ?

Name feild should not be empty

: <>} - {!validName && name ?

Not a valid name

: <>} - {!uniqueName && name ? ( -

Name already taken. Try another name

- ) : ( - <> - )} -
-
- - - {!description ?

Description feild should not be empty

: <>} - {!validDescription ? ( -

Characters length should be less than 200

- ) : ( - <> - )} +
)=>{ const new_role= event.target.value - console.log("helo") if(token&&spaceName&&orgMembers&&new_role!=role){ - console.log("chu") - console.log({[name]:new_role}) - + const func= async()=>{ console.log({[name]:new_role})