From b0b655d2470de8d338f7d9d229af19c021eae005 Mon Sep 17 00:00:00 2001 From: desperado1802 <124465103+desperado1802@users.noreply.github.com> Date: Wed, 25 Oct 2023 21:51:36 +0300 Subject: [PATCH] showing the tasks in last 24 hours dynamically, by adding missing param to API and giving dynamic data to show (#1645) --- .../logics/useProfileScreenLogic.ts | 105 +++++------ .../client/requests/organization-team.ts | 166 +++++++++--------- .../hooks/features/useAuthTeamTasks.ts | 54 +++--- 3 files changed, 173 insertions(+), 152 deletions(-) diff --git a/apps/mobile/app/screens/Authenticated/ProfileScreen/logics/useProfileScreenLogic.ts b/apps/mobile/app/screens/Authenticated/ProfileScreen/logics/useProfileScreenLogic.ts index 65c122fe6..0c250af84 100644 --- a/apps/mobile/app/screens/Authenticated/ProfileScreen/logics/useProfileScreenLogic.ts +++ b/apps/mobile/app/screens/Authenticated/ProfileScreen/logics/useProfileScreenLogic.ts @@ -1,94 +1,99 @@ -import { useCallback, useEffect, useMemo, useState } from 'react'; -import { useStores } from '../../../../models'; -import { useAuthTeamTasks } from '../../../../services/hooks/features/useAuthTeamTasks'; -import { useTeamTasks } from '../../../../services/hooks/features/useTeamTasks'; -import { ICreateTask, ITeamTask } from '../../../../services/interfaces/ITask'; -import { createTaskRequest } from '../../../../services/client/requests/tasks'; +import { useCallback, useEffect, useMemo, useState } from "react" +import { useStores } from "../../../../models" +import { useAuthTeamTasks } from "../../../../services/hooks/features/useAuthTeamTasks" +import { useTeamTasks } from "../../../../services/hooks/features/useTeamTasks" +import { ICreateTask, ITeamTask } from "../../../../services/interfaces/ITask" +import { createTaskRequest } from "../../../../services/client/requests/tasks" export const useProfileScreenLogic = ({ activeTab, - userId + userId, }: { - activeTab: 'worked' | 'assigned' | 'unassigned'; - userId: string; + activeTab: "worked" | "assigned" | "unassigned" + userId: string }) => { const { TaskStore: { activeTask }, authenticationStore: { user, authToken, tenantId, organizationId }, - teamStore: { activeTeam } - } = useStores(); - const { updateTask, isRefetching } = useTeamTasks(); + teamStore: { activeTeam }, + } = useStores() + const { updateTask, isRefetching } = useTeamTasks() - const [showCreateTeamModal, setShowCreateTeamModal] = useState(false); - const [isLoading, setIsLoading] = useState(true); + const [showCreateTeamModal, setShowCreateTeamModal] = useState(false) + const [isLoading, setIsLoading] = useState(true) - const members = activeTeam?.members || []; - const currentUserId = userId || user?.id; + const members = activeTeam?.members || [] + const currentUserId = userId || user?.id const matchUser = useMemo( () => members.find((m) => { - return m.employee.userId === userId; + return m.employee.userId === userId }) || members.find((m) => { - return m.employee.userId === user?.id; + return m.employee.userId === user?.id }), - [activeTeam, currentUserId] - ); + [activeTeam, currentUserId], + ) - const isAuthUser = user?.employee.userId === currentUserId; + const isAuthUser = user?.employee.userId === currentUserId const activeUserTeamTask = useMemo( () => (isAuthUser ? activeTask : matchUser?.lastWorkedTask), - [userId, isRefetching] - ); + [userId, isRefetching], + ) - const userProfile = isAuthUser ? user : matchUser?.employee.user; + const userProfile = isAuthUser ? user : matchUser?.employee.user - const employeeId = isAuthUser ? user?.employee.id : matchUser?.employeeId; + const employeeId = isAuthUser ? user?.employee.id : matchUser?.employeeId /* Filtering the tasks */ - const tasksGrouped = useAuthTeamTasks(userProfile); + const tasksGrouped = useAuthTeamTasks(userProfile) useEffect(() => { if (employeeId) { // getTasksStatsData(employeeId) } - }, [employeeId]); + }, [employeeId]) const assignTask = useCallback( (task: ITeamTask) => { if (!matchUser?.employeeId) { - return Promise.resolve(); + return Promise.resolve() } return updateTask( { ...task, - members: [...task.members, (matchUser?.employeeId ? { id: matchUser?.employeeId } : {}) as any] + members: [ + ...task.members, + (matchUser?.employeeId ? { id: matchUser?.employeeId } : {}) as any, + ], }, - task.id - ); + task.id, + ) }, - [updateTask, matchUser] - ); + [updateTask, matchUser], + ) const unassignTask = useCallback( (task: ITeamTask) => { if (!matchUser?.employeeId) { - return Promise.resolve(); + return Promise.resolve() } return updateTask( { ...task, - members: [...task.members.filter((member) => member.id !== matchUser?.employeeId)] + members: [ + ...task.members.filter((member) => member.id !== matchUser?.employeeId), + ], }, - task.id - ); + task.id, + ) }, - [updateTask, matchUser] - ); + [updateTask, matchUser], + ) const onCreateNewTask = useCallback( async (task: ICreateTask) => { @@ -98,17 +103,17 @@ export const useProfileScreenLogic = ({ organizationId, tenantId, teams: [{ id: activeTeam?.id }], - members: [{ id: employeeId }] + members: [{ id: employeeId }], }, - bearer_token: authToken - }); + bearer_token: authToken, + }) }, - [authToken] - ); + [authToken], + ) useEffect(() => { - setTimeout(() => setIsLoading(false), 2000); - }, []); + setTimeout(() => setIsLoading(false), 2000) + }, []) return { showCreateTeamModal, setShowCreateTeamModal, @@ -121,8 +126,8 @@ export const useProfileScreenLogic = ({ unassignTask, activeTab, onCreateNewTask, - isLoading - }; -}; + isLoading, + } +} -export type IUserProfile = ReturnType; +export type IUserProfile = ReturnType diff --git a/apps/mobile/app/services/client/requests/organization-team.ts b/apps/mobile/app/services/client/requests/organization-team.ts index a225f2172..d57e49bd0 100644 --- a/apps/mobile/app/services/client/requests/organization-team.ts +++ b/apps/mobile/app/services/client/requests/organization-team.ts @@ -1,39 +1,42 @@ /* eslint-disable camelcase */ -import moment from 'moment'; -import { PaginationResponse } from '../../interfaces/IDataResponse'; +import moment from "moment" +import { PaginationResponse } from "../../interfaces/IDataResponse" import { IOrganizationTeam, IOrganizationTeamCreate, IOrganizationTeamList, IOrganizationTeamUpdate, - IOrganizationTeamWithMStatus -} from '../../interfaces/IOrganizationTeam'; -import { serverFetch } from '../fetch'; + IOrganizationTeamWithMStatus, +} from "../../interfaces/IOrganizationTeam" +import { serverFetch } from "../fetch" -export function createOrganizationTeamRequest(datas: IOrganizationTeamCreate, bearer_token: string) { +export function createOrganizationTeamRequest( + datas: IOrganizationTeamCreate, + bearer_token: string, +) { return serverFetch({ - path: '/organization-team', - method: 'POST', + path: "/organization-team", + method: "POST", body: datas, - bearer_token - }); + bearer_token, + }) } export function updateOrganizationTeamRequest({ id, datas, - bearer_token + bearer_token, }: { - datas: IOrganizationTeamList | IOrganizationTeamCreate; - id: string; - bearer_token: string; + datas: IOrganizationTeamList | IOrganizationTeamCreate + id: string + bearer_token: string }) { return serverFetch({ path: `/organization-team/${id}`, - method: 'PUT', + method: "PUT", body: datas, - bearer_token - }); + bearer_token, + }) } export function getOrganizationTeamRequest( @@ -42,130 +45,131 @@ export function getOrganizationTeamRequest( tenantId, teamId, relations = [ - 'members', - 'members.role', - 'members.employee', - 'members.employee.user', - 'createdBy', - 'createdBy.employee', - 'projects' - ] + "members", + "members.role", + "members.employee", + "members.employee.user", + "createdBy", + "createdBy.employee", + "projects", + ], }: TeamRequestParams & { teamId: string }, - bearer_token: string + bearer_token: string, ) { const params = { organizationId, tenantId, // source: "BROWSER", - withLaskWorkedTask: 'true', - startDate: moment().startOf('day').toISOString(), - endDate: moment().endOf('day').toISOString() - } as { [x: string]: string }; + withLaskWorkedTask: "true", + startDate: moment().startOf("day").toISOString(), + endDate: moment().endOf("day").toISOString(), + includeOrganizationTeamId: "false", + } as { [x: string]: string } relations.forEach((rl, i) => { - params[`relations[${i}]`] = rl; - }); + params[`relations[${i}]`] = rl + }) - const queries = new URLSearchParams(params || {}); + const queries = new URLSearchParams(params || {}) return serverFetch({ path: `/organization-team/${teamId}?${queries.toString()}`, - method: 'GET', + method: "GET", bearer_token, - tenantId - }); + tenantId, + }) } type TeamRequestParams = { - organizationId: string; - tenantId: string; - relations?: string[]; -}; + organizationId: string + tenantId: string + relations?: string[] +} export function getAllOrganizationTeamRequest( { organizationId, tenantId, relations = [ - 'members', - 'members.role', - 'members.employee', - 'members.employee.user', - 'createdBy', - 'createdBy.employee' - ] + "members", + "members.role", + "members.employee", + "members.employee.user", + "createdBy", + "createdBy.employee", + ], }: TeamRequestParams, - bearer_token: string + bearer_token: string, ) { const params = { - 'where[organizationId]': organizationId, - 'where[tenantId]': tenantId, - source: 'BROWSER', - withLaskWorkedTask: 'true' - } as { [x: string]: string }; + "where[organizationId]": organizationId, + "where[tenantId]": tenantId, + source: "BROWSER", + withLaskWorkedTask: "true", + } as { [x: string]: string } relations.forEach((rl, i) => { - params[`relations[${i}]`] = rl; - }); + params[`relations[${i}]`] = rl + }) - const query = new URLSearchParams(params); + const query = new URLSearchParams(params) return serverFetch>({ path: `/organization-team?${query.toString()}`, - method: 'GET', + method: "GET", bearer_token, - tenantId - }); + tenantId, + }) } export function removeEmployeeOrganizationTeamRequest({ employeeId, bearer_token, - tenantId + tenantId, }: { - employeeId: string; - bearer_token: string; - tenantId: string; + employeeId: string + bearer_token: string + tenantId: string }) { return serverFetch({ path: `/organization-team-employee/${employeeId}`, - method: 'DELETE', + method: "DELETE", bearer_token, - tenantId - }); + tenantId, + }) } export function removeUserFromAllTeam({ userId, bearer_token, - tenantId + tenantId, }: { - userId: string; - bearer_token: string; - tenantId: string; + userId: string + bearer_token: string + tenantId: string }) { return serverFetch({ path: `/organization-team/teams/${userId}`, - method: 'DELETE', + method: "DELETE", bearer_token, - tenantId - }); + tenantId, + }) } export function deleteOrganizationTeamRequest({ id, bearer_token, tenantId, - organizationId + organizationId, }: { - id: string; - bearer_token: string; - tenantId: string; - organizationId: string; + id: string + bearer_token: string + tenantId: string + organizationId: string }) { return serverFetch({ path: `/organization-team/${id}?organizationId=${organizationId}`, - method: 'DELETE', + method: "DELETE", bearer_token, - tenantId - }); + tenantId, + }) } diff --git a/apps/mobile/app/services/hooks/features/useAuthTeamTasks.ts b/apps/mobile/app/services/hooks/features/useAuthTeamTasks.ts index a0b6c90e0..c57cf50c8 100644 --- a/apps/mobile/app/services/hooks/features/useAuthTeamTasks.ts +++ b/apps/mobile/app/services/hooks/features/useAuthTeamTasks.ts @@ -1,39 +1,51 @@ -import { useMemo } from 'react'; -import { useStores } from '../../../models'; -import { IUser } from '../../interfaces/interfaces/IUserData'; -import { useTeamTasks } from './useTeamTasks'; +import { useMemo } from "react" +import { useStores } from "../../../models" +import { IUser } from "../../interfaces/interfaces/IUserData" +import { useTeamTasks } from "./useTeamTasks" +import { useOrganizationTeam } from "../useOrganization" export function useAuthTeamTasks(user: IUser | undefined) { const { - TaskStore: { teamTasks, tasksStatisticsState } - } = useStores(); - const { isRefetching } = useTeamTasks(); + TaskStore: { teamTasks, tasksStatisticsState }, + } = useStores() + const { isRefetching } = useTeamTasks() - const statTasks = tasksStatisticsState; + const { currentTeam } = useOrganizationTeam() + const currentMember = currentTeam?.members?.find( + (member) => member.employee?.userId === user.id, + ) + + const statTasks = tasksStatisticsState const assignedTasks = useMemo(() => { - if (!user) return []; + if (!user) return [] return teamTasks?.filter((task) => { - return task?.members.some((m) => m.userId === user.id); - }); - }, [teamTasks, user, isRefetching]); + return task?.members.some((m) => m.userId === user.id) + }) + }, [teamTasks, user, isRefetching]) const unassignedTasks = useMemo(() => { - if (!user) return []; + if (!user) return [] return teamTasks?.filter((task) => { - return !task?.members.some((m) => m.userId === user.id); - }); - }, [teamTasks, user, isRefetching]); + return !task?.members.some((m) => m.userId === user.id) + }) + }, [teamTasks, user, isRefetching]) + + const totalTodayTasks = useMemo(() => { + return currentMember?.totalTodayTasks && currentMember?.totalTodayTasks.length + ? currentMember?.totalTodayTasks.map((task) => task.id) + : [] + }, [currentMember]) const workedTasks = useMemo(() => { return teamTasks?.filter((tsk) => { - return statTasks?.today.some((st) => st.id === tsk.id); - }); - }, [statTasks, teamTasks, isRefetching]); + return totalTodayTasks?.includes(tsk.id) + }) + }, [statTasks, teamTasks, isRefetching]) return { assignedTasks, unassignedTasks, - workedTasks - }; + workedTasks, + } }