Skip to content

Commit

Permalink
feat: remove task form plan requests
Browse files Browse the repository at this point in the history
  • Loading branch information
GloireMutaliko21 committed May 9, 2024
1 parent 764a6be commit e3d2dd1
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
28 changes: 28 additions & 0 deletions apps/web/app/api/daily-plan/remove-task/[planId]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ICreateDailyPlan, INextParams } from '@app/interfaces';
import { authenticatedGuard } from '@app/services/server/guards/authenticated-guard-app';
import { removeTaskFromPlanRequest } from '@app/services/server/requests';
import { NextResponse } from 'next/server';

export async function PUT(req: Request, { params }: INextParams) {
const res = new NextResponse();

const { planId } = params;
if (!planId) {
return;
}

const { $res, user, tenantId, organizationId, access_token } = await authenticatedGuard(req, res);
if (!user) return $res('Unauthorized');

const body = (await req.json()) as unknown as Partial<ICreateDailyPlan>;

const response = await removeTaskFromPlanRequest({
data: body,
organizationId,
planId,
tenantId,
bearer_token: access_token
});

return $res(response.data);
}
18 changes: 17 additions & 1 deletion apps/web/app/hooks/features/useDailyPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
getAllDayPlansAPI,
getDayPlansByEmployeeAPI,
getPlansByTaskAPI,
removeTaskFromPlanAPI,
updateDailyPlanAPI
} from '@app/services/client/api';
import { ICreateDailyPlan, IDailyPlan, IEmployee, ITeamTask } from '@app/interfaces';
Expand All @@ -30,6 +31,8 @@ export function useDailyPlan() {
const { loading: updateDailyPlanLoading, queryCall: updateQueryCall } = useQuery(updateDailyPlanAPI);
const { loading: getPlansByTaskLoading, queryCall: getPlansByTaskQueryCall } = useQuery(getPlansByTaskAPI);
const { loading: addTaskToPlanLoading, queryCall: addTaskToPlanQueryCall } = useQuery(addTaskToPlanAPI);
const { loading: removeTaskFromPlanLoading, queryCall: removeTAskFromPlanQueryCall } =
useQuery(removeTaskFromPlanAPI);

const [dailyPlan, setDailyPlan] = useRecoilState(dailyPlanListState);
const [profileDailyPlans, setProfileDailyPlans] = useRecoilState(profileDailyPlanListState);
Expand Down Expand Up @@ -105,6 +108,16 @@ export function useDailyPlan() {
[addTaskToPlanQueryCall, profileDailyPlans.items, profileDailyPlans.total, setProfileDailyPlans]
);

const removeTaskFromPlan = useCallback(
async (data: Partial<ICreateDailyPlan>, planId: IDailyPlan['id']) => {
const updated = profileDailyPlans.items.filter((plan) => plan.id != planId);
const res = await removeTAskFromPlanQueryCall(data, planId);
setProfileDailyPlans({ total: profileDailyPlans.total, items: [...updated, res.data] });
return res;
},
[profileDailyPlans.items, profileDailyPlans.total, removeTAskFromPlanQueryCall, setProfileDailyPlans]
);

return {
dailyPlan,
profileDailyPlans,
Expand All @@ -131,6 +144,9 @@ export function useDailyPlan() {
updateDailyPlanLoading,

addTaskToPlan,
addTaskToPlanLoading
addTaskToPlanLoading,

removeTaskFromPlan,
removeTaskFromPlanLoading
};
}
13 changes: 13 additions & 0 deletions apps/web/app/services/client/api/daily-plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,16 @@ export function addTaskToPlanAPI(data: { employeeId: IEmployee['id']; taskId: IT

return put<IDailyPlan>(`/daily-plan/add-task/${planId}?${query}`, data, { tenantId });
}

export function removeTaskFromPlanAPI(data: Partial<ICreateDailyPlan>, planId: IDailyPlan['id']) {
const organizationId = getOrganizationIdCookie();
const tenantId = getTenantIdCookie();

const obj = {
'where[organizationId]': organizationId
} as Record<string, string>;

const query = qs.stringify(obj);

return put<IDailyPlan>(`/daily-plan/task/${planId}?${query}`, data, { tenantId });
}
30 changes: 29 additions & 1 deletion apps/web/app/services/server/requests/daily-plan.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import qs from 'qs';
import { ICreateDailyPlan, IDailyPlan } from '@app/interfaces/IDailyPlan';
import { serverFetch } from '../fetch';
import { IEmployee, ITeamTask } from '@app/interfaces';
import { IEmployee, IOrganization, ITeamTask } from '@app/interfaces';

export function getAllDayPlans({
organizationId,
Expand Down Expand Up @@ -153,3 +153,31 @@ export function addTaskToDailyPlanRequest({
tenantId
});
}

export function removeTaskFromPlanRequest({
planId,
data,
bearer_token,
tenantId,
organizationId
}: {
planId: IDailyPlan['id'];
data: Partial<ICreateDailyPlan>;
bearer_token?: string;
tenantId: any;
organizationId: IOrganization['id'];
}) {
const obj = {
'where[organizationId]': organizationId
} as Record<string, string>;

const query = qs.stringify(obj);

return serverFetch<IDailyPlan>({
method: 'PUT',
path: `/daily-plan/task/${planId}?${query}`,
body: data,
bearer_token,
tenantId
});
}

0 comments on commit e3d2dd1

Please sign in to comment.