diff --git a/apps/OpenSign/src/components/AddDepartment.js b/apps/OpenSign/src/components/AddTeam.js similarity index 64% rename from apps/OpenSign/src/components/AddDepartment.js rename to apps/OpenSign/src/components/AddTeam.js index 6af89e6f0..3c784738c 100644 --- a/apps/OpenSign/src/components/AddDepartment.js +++ b/apps/OpenSign/src/components/AddTeam.js @@ -1,38 +1,43 @@ import React, { useEffect, useState } from "react"; import Parse from "parse"; import Title from "./Title"; -import Alert from "../primitives/Alert"; import Loader from "../primitives/Loader"; -const AddDepartment = (props) => { +const AddTeam = (props) => { const [formdata, setFormdata] = useState({ name: "", - department: "" + team: "" }); const [isLoader, setIsLoader] = useState(false); - const [isAlert, setIsAlert] = useState({ type: "success", msg: "" }); - const [departmentList, setDepartmentList] = useState([]); + const [teamList, setTeamList] = useState([]); useEffect(() => { - getDepartmentList(); + getTeamList(); }, []); - const getDepartmentList = async () => { + const getTeamList = async () => { + setIsLoader(true); try { const extUser = JSON.parse(localStorage.getItem("Extand_Class"))?.[0]; - const department = new Parse.Query("contracts_Departments"); - department.equalTo("OrganizationId", { + const teamCls = new Parse.Query("contracts_Teams"); + teamCls.equalTo("OrganizationId", { __type: "Pointer", className: "contracts_Organizations", objectId: extUser.OrganizationId.objectId }); - department.equalTo("IsActive", true); - const departmentRes = await department.find(); - if (departmentRes.length > 0) { - const _departmentRes = JSON.parse(JSON.stringify(departmentRes)); - setDepartmentList(_departmentRes); + teamCls.equalTo("IsActive", true); + const teamRes = await teamCls.find(); + if (teamRes.length > 0) { + const _teamRes = JSON.parse(JSON.stringify(teamRes)); + const allUsersteam = _teamRes.find((x) => x.Name === "All Users"); + if (allUsersteam) { + setFormdata({ team: allUsersteam.objectId }); + } + setTeamList(_teamRes); } } catch (err) { - console.log("Err in fetch top level departmentlist", err); + console.log("Err in fetch top level teamList", err); + } finally { + setIsLoader(false); } }; @@ -45,24 +50,24 @@ const AddDepartment = (props) => { e.stopPropagation(); // Extracting values except for the 'name' key let updatedAncestors = []; - if (formdata.department) { - const Ancestors = departmentList.find( - (x) => x.objectId === formdata.department + if (formdata.team) { + const Ancestors = teamList.find( + (x) => x.objectId === formdata.team )?.Ancestors; if (Ancestors && Ancestors.length > 0) { updatedAncestors = Ancestors.map((x) => ({ __type: "Pointer", - className: "contracts_Departments", + className: "contracts_Teams", objectId: x.objectId })); } else { - const AllUser = departmentList.find((x) => x.objectId === "All Users"); + const AllUser = teamList.find((x) => x.objectId === "All Users"); updatedAncestors = [ AllUser, { __type: "Pointer", - className: "contracts_Departments", - objectId: formdata.department + className: "contracts_Teams", + objectId: formdata.team } ]; } @@ -70,45 +75,43 @@ const AddDepartment = (props) => { try { const localUser = JSON.parse(localStorage.getItem("Extand_Class"))?.[0]; setIsLoader(true); - const department = new Parse.Query("contracts_Departments"); - department.equalTo("Name", formdata.name); + const team = new Parse.Query("contracts_Teams"); + team.equalTo("Name", formdata.name); if (updatedAncestors.length > 0) { const ParentId = updatedAncestors[updatedAncestors.length - 1]; - department.equalTo("ParentId", ParentId); + team.equalTo("ParentId", ParentId); } if (localUser && localUser.OrganizationId) { - department.equalTo("OrganizationId", { + team.equalTo("OrganizationId", { __type: "Pointer", className: "contracts_Organizations", objectId: localUser.OrganizationId.objectId }); } - const isDepartment = await department.first(); - if (isDepartment) { - setIsAlert({ type: "info", msg: "Department already exists." }); + const isTeam = await team.first(); + if (isTeam) { + props.setIsAlert({ type: "info", msg: "Teams already exists." }); setIsLoader(false); } else { - const newDepartment = new Parse.Object("contracts_Departments"); - newDepartment.set("Name", formdata.name); + const newTeam = new Parse.Object("contracts_Teams"); + newTeam.set("Name", formdata.name); if (updatedAncestors.length > 0) { const ParentId = updatedAncestors[updatedAncestors.length - 1]; - newDepartment.set("ParentId", ParentId); - newDepartment.set("Ancestors", updatedAncestors); + newTeam.set("ParentId", ParentId); + newTeam.set("Ancestors", updatedAncestors); } if (localUser && localUser.OrganizationId) { - newDepartment.set("OrganizationId", { + newTeam.set("OrganizationId", { __type: "Pointer", className: "contracts_Organizations", objectId: localUser.OrganizationId.objectId }); } - const newdepartmentRes = await newDepartment.save(); + const newTeamRes = await newTeam.save(); if (updatedAncestors.length > 0) { - const ParentId = departmentList.find( - (x) => x.objectId === formdata.department - ); + const ParentId = teamList.find((x) => x.objectId === formdata.team); props.handleTeamInfo({ - objectId: newdepartmentRes.id, + objectId: newTeamRes.id, Name: formdata.name, ParentId: ParentId, Ancestors: updatedAncestors, @@ -116,7 +119,7 @@ const AddDepartment = (props) => { }); } else { props.handleTeamInfo({ - objectId: newdepartmentRes.id, + objectId: newTeamRes.id, Name: formdata.name, ParentId: "", Ancestors: "", @@ -129,20 +132,18 @@ const AddDepartment = (props) => { } setFormdata({ name: "", - department: { name: "", objectId: "" } + team: { name: "", objectId: "" } }); - setIsAlert({ + props.setIsAlert({ type: "success", - msg: "Department created successfully." + msg: "Team created successfully." }); } } catch (err) { - console.log("err in save department", err); - setIsAlert({ type: "danger", msg: "Something went wrong." }); + console.log("err in save team", err); + props.setIsAlert({ type: "danger", msg: "Something went wrong." }); } finally { - setTimeout(() => { - setIsAlert({ type: "success", msg: "" }); - }, 1500); + setTimeout(() => props.setIsAlert({ type: "success", msg: "" }), 1500); setIsLoader(false); } }; @@ -151,7 +152,7 @@ const AddDepartment = (props) => { const handleReset = () => { setFormdata({ name: "", - department: { name: "", objectId: "" } + team: { name: "", objectId: "" } }); }; const handleChange = (e) => { @@ -160,12 +161,7 @@ const AddDepartment = (props) => { return (