Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: #102 프로젝트 상태 변경 API를 이용하여 상태 변경 기능 구현 #106

Merged
merged 4 commits into from
Sep 5, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ModalLayout from '@layouts/ModalLayout';
import ModalPortal from '@components/modal/ModalPortal';
import ModalProjectStatusForm from '@components/modal/project-status/ModalProjectStatusForm';
import ModalFormButton from '@components/modal/ModalFormButton';
import { useUpdateStatus } from '@hooks/query/useStatusQuery';

import type { SubmitHandler } from 'react-hook-form';
import type { Project } from '@/types/ProjectType';
Expand All @@ -18,10 +19,12 @@ export default function UpdateModalProjectStatus({
statusId,
onClose: handleClose,
}: UpdateModalProjectStatusProps) {
// ToDo: 상태 수정을 위한 네트워크 로직 추가
const updateMutation = useUpdateStatus(project.projectId, statusId);

// ToDo: Error 처리 추가
const handleSubmit: SubmitHandler<ProjectStatusForm> = async (data) => {
console.log(statusId, '수정 폼 제출');
console.log(data);
updateMutation.mutate(data);
updateMutation.reset();
handleClose();
};

Expand Down
20 changes: 19 additions & 1 deletion src/hooks/query/useStatusQuery.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { queryClient } from '@hooks/query/queryClient';
import { useMemo } from 'react';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import useToast from '@hooks/useToast';
import { PROJECT_STATUS_COLORS } from '@constants/projectStatus';
import { createStatus, getStatusList } from '@services/statusService';
import { createStatus, getStatusList, updateStatus } from '@services/statusService';

import type { Project } from '@/types/ProjectType';
import type { ProjectStatus, ProjectStatusForm, UsableColor } from '@/types/ProjectStatusType';
Expand Down Expand Up @@ -108,3 +109,20 @@ export function useCreateStatus(projectId: Project['projectId']) {

return mutation;
}

export function useUpdateStatus(projectId: Project['projectId'], statusId: ProjectStatus['statusId']) {
const { toastSuccess } = useToast();
const queryClient = useQueryClient();

const mutation = useMutation({
mutationFn: (formData: ProjectStatusForm) => updateStatus(projectId, statusId, formData),
onSuccess: () => {
toastSuccess('프로젝트 상태를 수정했습니다.');
queryClient.invalidateQueries({
queryKey: ['projects', projectId, 'statuses'],
});
},
});

return mutation;
}
18 changes: 18 additions & 0 deletions src/mocks/services/statusServiceHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ const statusServiceHandler = [

return new HttpResponse(null, { status: 200 });
}),
// 프로젝트 상태 수정 API
http.patch(`${BASE_URL}/project/:projectId/status/:statusId`, async ({ request, params }) => {
const accessToken = request.headers.get('Authorization');
const { projectId, statusId } = params;
const formData = (await request.json()) as ProjectStatusForm;

if (!accessToken) return new HttpResponse(null, { status: 401 });

const status = STATUS_DUMMY.find(
(status) => status.projectId === Number(projectId) && status.statusId === Number(statusId),
);
if (!status) return new HttpResponse(null, { status: 404 });

status.statusName = formData.statusName;
status.colorCode = formData.colorCode;
status.sortOrder = formData.sortOrder;
return new HttpResponse(null, { status: 204 });
}),
];

export default statusServiceHandler;
20 changes: 20 additions & 0 deletions src/services/statusService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,23 @@ export async function createStatus(
) {
return authAxios.post(`/project/${projectId}/status`, formData, axiosConfig);
}

/**
* 프로젝트 상태 수정 API
*
* @export
* @async
* @param {Project['projectId']} projectId - 프로젝트 ID
* @param {ProjectStatus['statusId']} statusId - 프로젝트 상태 ID
* @param {ProjectStatusForm} formData - 상태 정보 객체
* @param {AxiosRequestConfig} [axiosConfig={}] - axios 요청 옵션 설정 객체
* @returns {Promise<AxiosResponse<void>>}
*/
export async function updateStatus(
projectId: Project['projectId'],
statusId: ProjectStatus['statusId'],
formData: ProjectStatusForm,
axiosConfig: AxiosRequestConfig = {},
) {
return authAxios.patch(`/project/${projectId}/status/${statusId}`, formData, axiosConfig);
}