Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified client/.env.sample
100755 → 100644
Empty file.
126 changes: 88 additions & 38 deletions client/src/Hooks/useAssistantPage.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { useState, useEffect, useContext } from "react";
import { message } from "antd";
import { AssistantContext } from "../contexts/AssistantContext";
import { SEARCH_ALL_ORGANIZATIONAL_ASSISTANTS_SLUG, SEARCH_ALL_USER_CREATED_ASSISTANTS_SLUG } from "../constants/Api_constants";
import {
SEARCH_ALL_ORGANIZATIONAL_ASSISTANTS_SLUG,
SEARCH_ALL_USER_CREATED_ASSISTANTS_SLUG,
} from "../constants/Api_constants";
import { axiosSecureInstance } from "../api/axios";
import { getUserID } from "../Utility/service";
import { SEARCH_ASSISTANTS } from "../api/assistant_api_constant";
import { deleteAssistant, fetchAllAssistants, fetchAssistantStatisticsForUser, fetchAssistantsCreatedByUser, fetchTeams, updateAssistant, updateAssistantTeams } from "../api/assistant";
import {
deleteAssistant,
fetchAllAssistants,
fetchAssistantStatisticsForUser,
fetchAssistantsCreatedByUser,
fetchTeams,
updateAssistant,
updateAssistantTeams,
} from "../api/assistant";

const initialLoaderState = {
ASSISTANT_UPDATING: false,
Expand All @@ -21,30 +31,35 @@ const useAssistantPage = () => {
const [adminUserAssistants, setAdminUserAssistants] = useState([]);
const [userAssistants, setUserAssistants] = useState([]);
const [assistants, setAssistants] = useState([]);
const [functionCallingAssistants, setFunctionCallingAssistants] = useState(
[]
);
const [teamList, setTeamList] = useState([]);
const [loader, setLoader] = useState({
...initialLoaderState,
});
const [totalCount, setTotalCount] = useState()
const [orgAssistantSearchQuery, setOrgAssistantSearchQuery] = useState('')
const [personalAssistantSearchQuery, setPersonalAssistantSearchQuery] = useState('')
const [totalCount, setTotalCount] = useState();
const [orgAssistantSearchQuery, setOrgAssistantSearchQuery] = useState("");
const [personalAssistantSearchQuery, setPersonalAssistantSearchQuery] =
useState("");

const { triggerRefetchAssistants } = useContext(AssistantContext);

useEffect(() => {
handleFetchUserCreatedAssistants(1);
handleFetchUserAssistantStats();
handleFetchFunctionCallingAssistants(1);
handleFetchAllAssistants(1);
handleFetchTeams();
}, []);

useEffect(()=>{
handleFetchAllAssistants(1, orgAssistantSearchQuery)
},[orgAssistantSearchQuery])
useEffect(() => {
handleFetchAllAssistants(1, orgAssistantSearchQuery);
}, [orgAssistantSearchQuery]);

useEffect(()=>{
handleFetchUserCreatedAssistants(1, personalAssistantSearchQuery)
},[personalAssistantSearchQuery])
useEffect(() => {
handleFetchUserCreatedAssistants(1, personalAssistantSearchQuery);
}, [personalAssistantSearchQuery]);

const updateLoader = (newState) => {
setLoader((prevLoader) => ({ ...prevLoader, ...newState }));
Expand All @@ -62,7 +77,11 @@ const useAssistantPage = () => {

// API FUNCTIONS

const handleAssignTeamToAssistant = async (assistantId, teamIds, successCb) => {
const handleAssignTeamToAssistant = async (
assistantId,
teamIds,
successCb
) => {
updateLoader({ ASSISTANT_UPDATING: assistantId });
try {
const payload = { teamIds };
Expand All @@ -71,6 +90,7 @@ const useAssistantPage = () => {
if (response) {
handleShowMessage(response.message);
handleFetchUserCreatedAssistants();
handleFetchFunctionCallingAssistants();
handleFetchAllAssistants(1);
successCb();
}
Expand All @@ -86,9 +106,10 @@ const useAssistantPage = () => {
updateLoader({ ASSISTANT_UPDATING: assistantId });
const response = await updateAssistant(assistantId, data);

if(response) {
if (response) {
handleFetchUserCreatedAssistants();
handleFetchAllAssistants(1);
handleFetchFunctionCallingAssistants();
handleShowMessage(response.message);
triggerRefetchAssistants();
}
Expand All @@ -99,13 +120,19 @@ const useAssistantPage = () => {
}
};

const handleFetchUserCreatedAssistants = async (page, personalAssistantSearchQuery) => {

const handleFetchUserCreatedAssistants = async (
page,
personalAssistantSearchQuery
) => {
try {
updateLoader({ ASSISTANT_LOADING: true });
const response = await fetchAssistantsCreatedByUser(page, false , personalAssistantSearchQuery);
const response = await fetchAssistantsCreatedByUser(
page,
false,
personalAssistantSearchQuery
);

if(response) {
if (response) {
setAdminUserAssistants(response.data);
setTotalCount(response.meta.total);
}
Expand All @@ -121,7 +148,7 @@ const useAssistantPage = () => {
updateLoader({ ASSISTANT_STATS_LOADING: true });
const response = await fetchAssistantStatisticsForUser();

if(response) {
if (response) {
setUserAssistants(
response.data.map((item) => {
return {
Expand All @@ -140,15 +167,31 @@ const useAssistantPage = () => {
}
};

//fetching all functionCalling assistants
const handleFetchFunctionCallingAssistants = async () => {
try {
const response = await axiosSecureInstance.get(
`/api/assistants/users/createdFunctionCalling`
);

const result = response.data.assistants;

setFunctionCallingAssistants(result);
} catch (error) {
console.log(error);
}
};

const handleDeleteAssistant = async (assistantId) => {
updateLoader({ ASSISTANT_DELETING: assistantId });
try {
const response = await deleteAssistant(assistantId);

if(response) {
if (response) {
handleFetchUserCreatedAssistants();
handleFetchUserAssistantStats();
handleFetchAllAssistants(1)
handleFetchFunctionCallingAssistants();
handleFetchAllAssistants(1);
handleShowMessage(response.message);
triggerRefetchAssistants();
}
Expand All @@ -161,12 +204,15 @@ const useAssistantPage = () => {
};

const handleFetchAllAssistants = async (page, orgAssistantSearchQuery) => {

try {
updateLoader({ ALL_ASSISTANT_LOADING: true });
const response = await fetchAllAssistants(page, 10, orgAssistantSearchQuery);
const response = await fetchAllAssistants(
page,
10,
orgAssistantSearchQuery
);

if(response) {
if (response) {
setAssistants(response);
}
} catch (error) {
Expand All @@ -181,7 +227,7 @@ const useAssistantPage = () => {
updateLoader({ TEAM_LOADING: true });
const response = await fetchTeams();

if(response) {
if (response) {
setTeamList(response.data);
}
} catch (error) {
Expand All @@ -194,33 +240,35 @@ const useAssistantPage = () => {
const searchOrganizationalAssistants = async (searchQuery) => {
try {
updateLoader({ ALL_ASSISTANT_LOADING: true });
const response = await axiosSecureInstance.get(SEARCH_ALL_ORGANIZATIONAL_ASSISTANTS_SLUG(searchQuery))
setAssistants(response.data)
const response = await axiosSecureInstance.get(
SEARCH_ALL_ORGANIZATIONAL_ASSISTANTS_SLUG(searchQuery)
);
setAssistants(response.data);
updateLoader({ ALL_ASSISTANT_LOADING: false });
} catch (error) {
console.log(error)
console.log(error);
updateLoader({ ALL_ASSISTANT_LOADING: false });
}

}
};

const searchPersonalAssistants = async (searchQuery) => {
try {
updateLoader({ ASSISTANT_LOADING: true });
const response = await axiosSecureInstance.get(SEARCH_ALL_USER_CREATED_ASSISTANTS_SLUG(getUserID(), searchQuery))
setAdminUserAssistants(response.data?.assistants)
const response = await axiosSecureInstance.get(
SEARCH_ALL_USER_CREATED_ASSISTANTS_SLUG(getUserID(), searchQuery)
);
setAdminUserAssistants(response.data?.assistants);
updateLoader({ ASSISTANT_LOADING: false });
} catch (error) {
console.log(error)
console.log(error);
updateLoader({ ASSISTANT_LOADING: false });
}

}


};

return {
setAdminUserAssistants,
setFunctionCallingAssistants,
functionCallingAssistants,
adminUserAssistants,
totalCount,
userAssistants,
Expand All @@ -230,8 +278,10 @@ const useAssistantPage = () => {
loader,
handleAssignTeamToAssistant,
handleUpdateAssistant,
// handleUpdateFunctionCallingAssistant,
handleFetchUserCreatedAssistants,
handleFetchUserAssistantStats,
handleFetchFunctionCallingAssistants,
handleDeleteAssistant,
handleFetchAllAssistants,
handleFetchTeams,
Expand All @@ -243,7 +293,7 @@ const useAssistantPage = () => {
setOrgAssistantSearchQuery,
//Search query for personal assistants
personalAssistantSearchQuery,
setPersonalAssistantSearchQuery
setPersonalAssistantSearchQuery,
};
};

Expand Down
5 changes: 4 additions & 1 deletion client/src/Pages/Layout/AssistantLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import { Footer, Header, Sidebar } from "../../component";
import { isLoggedIn } from "../../Utility/service";
import AssistantSidebar from "../../component/layout/AssistantSidebar/AssistantSidebar";
import AssistantContextProvider from "../../contexts/AssistantContext";
import { AssistantFetchContextProvider } from "../../contexts/AssistantsFetchContext";

const AssistantLayout = () => {
const location = useLocation();

return isLoggedIn() ? (
<SidebarContextProvider>
<AssistantFetchContextProvider>
<SidebarContextProvider>
<AssistantContextProvider>
<PromptTemplateContextProvider>
<main className="d-flex flex-nowrap">
Expand All @@ -31,6 +33,7 @@ const AssistantLayout = () => {
</PromptTemplateContextProvider>
</AssistantContextProvider>
</SidebarContextProvider>
</AssistantFetchContextProvider>
) : (
<Navigate to="/login" state={{ from: location }} replace />
);
Expand Down
3 changes: 3 additions & 0 deletions client/src/Pages/Layout/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import AssistantContextProvider from "../../contexts/AssistantContext";
import { Footer, Header} from "../../component";
import { isLoggedIn } from "../../Utility/service";
import NewSidebar from "../../component/layout/NewSidebar/NewSidebar";
import { AssistantFetchContextProvider } from "../../contexts/AssistantsFetchContext";

const Layout = () => {
const location = useLocation();
return isLoggedIn() ? (
<SidebarContextProvider>
<AssistantFetchContextProvider>
<AssistantContextProvider>
<PromptTemplateContextProvider>
<main className="d-flex flex-nowrap">
Expand All @@ -25,6 +27,7 @@ const Layout = () => {
</main>
</PromptTemplateContextProvider>
</AssistantContextProvider>
</AssistantFetchContextProvider>
</SidebarContextProvider>
) : (
<Navigate to="/login" state={{ from: location }} replace />
Expand Down
Loading