Skip to content

Commit

Permalink
Merge pull request #1652 from ever-co/bug/active-task-non-manager-mem…
Browse files Browse the repository at this point in the history
…bers

Bug/active task non manager members
  • Loading branch information
evereq authored Oct 26, 2023
2 parents 06dfb71 + 634826a commit ca3a6b8
Show file tree
Hide file tree
Showing 2 changed files with 263 additions and 221 deletions.
209 changes: 112 additions & 97 deletions apps/mobile/app/services/hooks/features/useTeamMemberCard.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,52 @@
import { useCallback, useMemo, useRef, useState } from 'react';
import { useSyncRef } from '../useSyncRef';
import { useTeamTasks } from './useTeamTasks';
import cloneDeep from 'lodash/cloneDeep';
import { useStores } from '../../../models';
import { IOrganizationTeamList } from '../../interfaces/IOrganizationTeam';
import useAuthenticateUser from './useAuthentificateUser';
import { useIsMemberManager } from './useIsMemberManager';
import { ITeamTask } from '../../interfaces/ITask';
import { Nullable } from '../../interfaces/hooks';
import { useOrganizationTeam } from '../useOrganization';
import { useCallback, useMemo, useRef, useState } from "react"
import { useSyncRef } from "../useSyncRef"
import { useTeamTasks } from "./useTeamTasks"
import cloneDeep from "lodash/cloneDeep"
import { useStores } from "../../../models"
import { IOrganizationTeamList } from "../../interfaces/IOrganizationTeam"
import useAuthenticateUser from "./useAuthentificateUser"

Check warning on line 7 in apps/mobile/app/services/hooks/features/useTeamMemberCard.ts

View workflow job for this annotation

GitHub Actions / Cspell

Unknown word (Authentificate)
import { useIsMemberManager } from "./useIsMemberManager"
import { ITeamTask } from "../../interfaces/ITask"
import { Nullable } from "../../interfaces/hooks"
import { useOrganizationTeam } from "../useOrganization"

/**
* It returns a bunch of data about a team member, including whether or not the user is the team
* manager, whether or not the user is the authenticated user, and the last task the user worked on
* @param {IOrganizationTeamList['members'][number] | undefined} member -
* IOrganizationTeamList['members'][number] | undefined
*/
export function useTeamMemberCard(member: IOrganizationTeamList['members'][number] | undefined) {
export function useTeamMemberCard(member: IOrganizationTeamList["members"][number] | undefined) {
const {
TaskStore: { activeTaskId, setActiveTaskId, activeTask, teamTasks: tasks, tasksStatisticsState },
teamStore: { activeTeam, activeTeamId }
} = useStores();
const { updateTask, setActiveTeamTask, deleteEmployeeFromTasks } = useTeamTasks();
TaskStore: {
activeTaskId,
setActiveTaskId,
setActiveTask,
activeTask,
teamTasks: tasks,
tasksStatisticsState,
},
teamStore: { activeTeam, activeTeamId },
} = useStores()
const { updateTask, setActiveTeamTask, deleteEmployeeFromTasks } = useTeamTasks()

const publicTeam = false;
const publicTeam = false

const { user: authUser, isTeamManager: isAuthTeamManager } = useAuthenticateUser();
const { user: authUser, isTeamManager: isAuthTeamManager } = useAuthenticateUser()

const activeTeamTask = activeTask;
const activeTeamTask = activeTask

const { onUpdateOrganizationTeam } = useOrganizationTeam();
const { onUpdateOrganizationTeam } = useOrganizationTeam()

const activeTeamRef = useSyncRef(activeTeam);
const activeTeamRef = useSyncRef(activeTeam)

const memberUser = member?.employee.user;
const memberUser = member?.employee.user

// const memberUserRef = useSyncRef(memberUser);
const isAuthUser = member?.employee.userId === authUser?.id;
const { isTeamManager, isTeamCreator } = useIsMemberManager(memberUser);
const isAuthUser = member?.employee.userId === authUser?.id
const { isTeamManager, isTeamCreator } = useIsMemberManager(memberUser)

const memberTaskRef = useRef<Nullable<ITeamTask>>(null);
const memberTaskRef = useRef<Nullable<ITeamTask>>(null)

const setActiveUserTask = useCallback(
(task: ITeamTask | null) => {
Expand All @@ -48,170 +55,178 @@ export function useTeamMemberCard(member: IOrganizationTeamList['members'][numbe
// taskId: task.id,
// userId: authUser.id,
// })
setActiveTaskId(task.id);
setActiveTaskId(task.id)
}
},
[authUser]
);
[authUser],
)

memberTaskRef.current = useMemo(() => {
let cTask;
let find;
let cTask
let find

if (!member) {
return null;
return null
}

if (activeTaskId && isAuthUser) {
cTask = tasks.find((t) => activeTaskId === t.id || publicTeam);
find = cTask;
cTask = tasks.find((t) => activeTaskId === t.id || publicTeam)
find = cTask
} else if (member.lastWorkedTask) {
cTask = tasks.find((t) => t.id === member.lastWorkedTask?.id);
find = cTask?.members.some((m) => m.id === member.employee.id);
cTask = tasks.find((t) => t.id === member.lastWorkedTask?.id)
find = cTask?.members.some((m) => m.id === member.employee.id)
} else {
cTask = tasks.find((t) => t.members.some((m) => m.userId === member.employee.userId));
find = cTask?.members.some((m) => m.id === member.employee.id);
cTask = tasks.find((t) => t.members.some((m) => m.userId === member.employee.userId))
find = cTask?.members.some((m) => m.id === member.employee.id)
}

if (isAuthUser && member.lastWorkedTask && !activeTaskId) {
setActiveUserTask(member.lastWorkedTask);
setActiveUserTask(member.lastWorkedTask)
setActiveTask(member.lastWorkedTask)
} else if (isAuthUser && find && cTask && !activeTaskId) {
setActiveUserTask(cTask);
setActiveUserTask(cTask)
setActiveTask(cTask)
}

const responseTask = find ? cloneDeep(cTask) : null;
const responseTask = find ? cloneDeep(cTask) : null

if (responseTask) {
const taskStatistics =
tasksStatisticsState?.all.find((statistics) => statistics.id === responseTask.id) || [];
responseTask.totalWorkedTime = taskStatistics?.duration || 0;
tasksStatisticsState?.all.find((statistics) => statistics.id === responseTask.id) ||
[]
responseTask.totalWorkedTime = taskStatistics?.duration || 0
}

return responseTask;
}, [activeTeamTask, isAuthUser, authUser, member, tasks, publicTeam]);
return responseTask
}, [activeTeamTask, activeTaskId, isAuthUser, authUser, member, tasks, publicTeam])

/**
* Give the manager role to the member
*/
const makeMemberManager = useCallback(() => {
const employeeId = member?.employee?.id;
const employeeId = member?.employee?.id

if (!activeTeamRef.current || !employeeId) return;
const team = activeTeamRef.current;
if (!activeTeamRef.current || !employeeId) return
const team = activeTeamRef.current

onUpdateOrganizationTeam({
id: activeTeamId,
data: {
...activeTeam,
managerIds: team.members
.filter((r) => r.role && r.role.name === 'MANAGER')
.filter((r) => r.role && r.role.name === "MANAGER")
.map((r) => r.employee.id)
.concat(employeeId)
}
});
}, [onUpdateOrganizationTeam, member, activeTeamRef]);
.concat(employeeId),
},
})
}, [onUpdateOrganizationTeam, member, activeTeamRef])

/**
* remove manager role to the member
*/
const unMakeMemberManager = useCallback(() => {
const employeeId = member?.employee?.id;
const employeeId = member?.employee?.id

if (!activeTeamRef.current || !employeeId) return;
const team = activeTeamRef.current;
if (!activeTeamRef.current || !employeeId) return
const team = activeTeamRef.current

onUpdateOrganizationTeam({
id: activeTeamId,
data: {
...activeTeam,
managerIds: team.members
.filter((r) => r.role && r.role.name === 'MANAGER')
.filter((r) => r.role && r.role.name === "MANAGER")
.filter((r) => r.employee.id !== employeeId)
.map((r) => r.employee.id)
.filter((value, index, array) => array.indexOf(value) === index) // To make the array Unique list of ids
}
});
}, [onUpdateOrganizationTeam, member, activeTeamRef]);
.filter((value, index, array) => array.indexOf(value) === index), // To make the array Unique list of ids
},
})
}, [onUpdateOrganizationTeam, member, activeTeamRef])

/**
* Remove member from team API call
*/
const removeMemberFromTeam = useCallback(() => {
const employeeId = member?.employee?.id;
const employeeId = member?.employee?.id

if (!activeTeamRef.current || !employeeId) return;
const team = activeTeamRef.current;
if (!activeTeamRef.current || !employeeId) return
const team = activeTeamRef.current

deleteEmployeeFromTasks(employeeId, team.id); // Unassign all the task
deleteEmployeeFromTasks(employeeId, team.id) // Unassign all the task
onUpdateOrganizationTeam({
id: activeTeamId,
data: {
...activeTeam,
// remove from members
memberIds: team.members.filter((r) => r.employee.id !== employeeId).map((r) => r.employee.id),
memberIds: team.members
.filter((r) => r.employee.id !== employeeId)
.map((r) => r.employee.id),

// remove from managers
managerIds: team.members
.filter((r) => r.role && r.role.name === 'MANAGER')
.filter((r) => r.role && r.role.name === "MANAGER")
.filter((r) => r.employee.id !== employeeId)
.map((r) => r.employee.id)
}
});
}, [onUpdateOrganizationTeam, member, activeTeamRef]);
.map((r) => r.employee.id),
},
})
}, [onUpdateOrganizationTeam, member, activeTeamRef])

/**
* Assign task to the member
*/
const assignTask = useCallback(
(task: ITeamTask) => {
if (!member?.employeeId) {
return Promise.resolve();
return Promise.resolve()
}

return updateTask(
{
...task,
members: [...task.members, (member?.employeeId ? { id: member?.employeeId } : {}) as any]
members: [
...task.members,
(member?.employeeId ? { id: member?.employeeId } : {}) as any,
],
},
task.id
task.id,
).then(() => {
if (isAuthUser && !activeTeamTask) {
setActiveTeamTask(task);
setActiveTeamTask(task)
}
});
})
},
[updateTask, member, isAuthUser, setActiveTeamTask, activeTeamTask]
);
[updateTask, member, isAuthUser, setActiveTeamTask, activeTeamTask],
)

/**
* Returns all tasks not assigned to the member
*/
const memberUnassignTasks = useMemo(() => {
if (!memberUser) return [];
if (!memberUser) return []

return tasks.filter((task) => {
return !task.members.some((m) => m.userId === memberUser.id);
});
}, [tasks, memberUser, assignTask]);
return !task.members.some((m) => m.userId === memberUser.id)
})
}, [tasks, memberUser, assignTask])

const unassignTask = useCallback(
(task: ITeamTask) => {
if (!member?.employeeId) {
return Promise.resolve();
return Promise.resolve()
}

return updateTask(
{
...task,
members: task.members.filter((m) => m.id !== member.employeeId)
members: task.members.filter((m) => m.id !== member.employeeId),
},
task.id
task.id,
).finally(() => {
isAuthUser && setActiveTeamTask(null);
});
isAuthUser && setActiveTeamTask(null)
})
},
[updateTask, member, isAuthUser, setActiveTeamTask]
);
[updateTask, member, isAuthUser, setActiveTeamTask],
)

return {
assignTask,
Expand All @@ -227,14 +242,14 @@ export function useTeamMemberCard(member: IOrganizationTeamList['members'][numbe
unMakeMemberManager,
isTeamCreator,
unassignTask,
isTeamOwner: activeTeam?.createdBy?.id === memberUser?.id
};
isTeamOwner: activeTeam?.createdBy?.id === memberUser?.id,
}
}

export function useTMCardTaskEdit(task: Nullable<ITeamTask>) {
const [editMode, setEditMode] = useState(false);
const [estimateEditMode, setEstimateEditMode] = useState(false);
const [loading, setLoading] = useState(false);
const [editMode, setEditMode] = useState(false)
const [estimateEditMode, setEstimateEditMode] = useState(false)
const [loading, setLoading] = useState(false)

return {
editMode,
Expand All @@ -243,13 +258,13 @@ export function useTMCardTaskEdit(task: Nullable<ITeamTask>) {
estimateEditMode,
setEstimateEditMode,
loading,
setLoading
};
setLoading,
}
}

export type I_TMCardTaskEditHook = ReturnType<typeof useTMCardTaskEdit>;
export type I_TMCardTaskEditHook = ReturnType<typeof useTMCardTaskEdit>

export type I_TeamMemberCardHook = ReturnType<typeof useTeamMemberCard>;
export type I_TeamMemberCardHook = ReturnType<typeof useTeamMemberCard>
// function useOrganizationTeams(): {
// activeTeam: any
// updateOrganizationTeam: any
Expand Down
Loading

0 comments on commit ca3a6b8

Please sign in to comment.