diff --git a/epictrack-api/src/api/models/dashboard_seach_options.py b/epictrack-api/src/api/models/dashboard_seach_options.py index 35047ed4b..424837929 100644 --- a/epictrack-api/src/api/models/dashboard_seach_options.py +++ b/epictrack-api/src/api/models/dashboard_seach_options.py @@ -15,3 +15,4 @@ class WorkplanDashboardSearchOptions: # pylint: disable=too-many-instance-attri regions: Optional[List[int]] project_types: Optional[List[int]] work_types: Optional[List[int]] + staff_id: Optional[int] diff --git a/epictrack-api/src/api/models/work.py b/epictrack-api/src/api/models/work.py index be8efa3aa..bcad609d4 100644 --- a/epictrack-api/src/api/models/work.py +++ b/epictrack-api/src/api/models/work.py @@ -139,6 +139,8 @@ def filter_by_search_criteria(cls, query, search_filters: WorkplanDashboardSearc if not search_filters: return query + query = cls._filter_by_staff_id(query, search_filters.staff_id) + query = cls._filter_by_search_text(query, search_filters.text) query = cls._filter_by_eao_team(query, search_filters.teams) @@ -156,11 +158,7 @@ def filter_by_search_criteria(cls, query, search_filters: WorkplanDashboardSearc @classmethod def _filter_by_staff_id(cls, query, staff_id): if staff_id: - subquery = ( - cls.query - .filter(StaffWorkRole.staff_id == staff_id) - .exists() - ) + subquery = exists().where(and_(Work.id == StaffWorkRole.work_id, StaffWorkRole.staff_id == staff_id)) query = query.filter(subquery) return query diff --git a/epictrack-api/src/api/resources/work.py b/epictrack-api/src/api/resources/work.py index ded13d087..bce81c7f4 100644 --- a/epictrack-api/src/api/resources/work.py +++ b/epictrack-api/src/api/resources/work.py @@ -78,6 +78,7 @@ def get(): project_types=list(map(int, args.getlist('project_types[]'))), work_types=list(map(int, args.getlist('work_types[]'))), text=args.get('text', None, str), + staff_id=args.get('staff_id', None, int), ) works = WorkService.fetch_all_work_plans(pagination_options, search_options) return jsonify(works), HTTPStatus.OK diff --git a/epictrack-web/src/components/myWorkplans/Filters/AssigneeToggle.tsx b/epictrack-web/src/components/myWorkplans/Filters/AssigneeToggle.tsx index 10cc7c14e..62b573d54 100644 --- a/epictrack-web/src/components/myWorkplans/Filters/AssigneeToggle.tsx +++ b/epictrack-web/src/components/myWorkplans/Filters/AssigneeToggle.tsx @@ -1,14 +1,44 @@ -import React from "react"; -import { FormControlLabel, Stack, Switch } from "@mui/material"; +import React, { useContext, useEffect, useState } from "react"; +import { Stack } from "@mui/material"; import { useAppSelector } from "../../../hooks"; import { ETCaption2 } from "../../shared"; import { Palette } from "../../../styles/theme"; import { CustomSwitch } from "../../shared/CustomSwitch"; +import staffService from "../../../services/staffService/staffService"; +import { MyWorkplansContext } from "../MyWorkPlanContext"; export const AssigneeToggle = () => { const user = useAppSelector((state) => state.user.userDetail); + const { setSearchOptions } = useContext(MyWorkplansContext); + + const [isUsersWorkPlans, setIsUsersWorkPlans] = useState(false); + const [staffId, setStaffId] = useState(null); + const [loading, setLoading] = useState(true); + + const getStaffInfo = async () => { + if (!user.email) return; + + try { + const response = await staffService.getByEmail(user.email); + setStaffId(response.data.id); + setLoading(false); + } catch (error) { + console.log(error); + } + }; + + useEffect(() => { + getStaffInfo(); + }, [isUsersWorkPlans]); + + const handleToggleChange = (checked: boolean) => { + setIsUsersWorkPlans(checked); + setSearchOptions((prev) => ({ + ...prev, + staff_id: checked ? staffId : null, + })); + }; - const [isUsersWorkPlans, setIsUsersWorkPlans] = React.useState(false); return ( <> @@ -20,7 +50,8 @@ export const AssigneeToggle = () => { setIsUsersWorkPlans(!isUsersWorkPlans)} + onChange={() => handleToggleChange(!isUsersWorkPlans)} + disabled={loading} /> ); diff --git a/epictrack-web/src/components/myWorkplans/MyWorkPlanContext.tsx b/epictrack-web/src/components/myWorkplans/MyWorkPlanContext.tsx index 88e08d437..7640683a6 100644 --- a/epictrack-web/src/components/myWorkplans/MyWorkPlanContext.tsx +++ b/epictrack-web/src/components/myWorkplans/MyWorkPlanContext.tsx @@ -31,6 +31,7 @@ export interface WorkPlanSearchOptions { project_types: string[]; work_types: string[]; text: string; + staff_id: number | null; } const PAGE_SIZE = 6; @@ -52,6 +53,7 @@ export const MyWorkplansProvider = ({ project_types: [], work_types: [], text: "", + staff_id: null, }); const fetchWorkplans = async (page: number, shouldAppend = false) => { diff --git a/epictrack-web/src/services/staffService/staffService.ts b/epictrack-web/src/services/staffService/staffService.ts index f091f5730..cdd56fff6 100644 --- a/epictrack-web/src/services/staffService/staffService.ts +++ b/epictrack-web/src/services/staffService/staffService.ts @@ -1,5 +1,6 @@ import http from "../../apiManager/http-request-handler"; import Endpoints from "../../constants/api-endpoint"; +import { Staff } from "../../models/staff"; import { MasterBase } from "../../models/type"; import ServiceBase from "../common/serviceBase"; @@ -42,7 +43,7 @@ class StaffService implements ServiceBase { } async getByEmail(email: string) { - return await http.GetRequest(Endpoints.Staffs.STAFFS + `/${email}`); + return await http.GetRequest(Endpoints.Staffs.STAFFS + `/${email}`); } }