|
| 1 | +import { AxiosError, AxiosResponse } from 'axios'; |
| 2 | +import { useMutation, useQuery } from 'react-query'; |
| 3 | + |
| 4 | +import type { NoticeArticle } from '@custom-types'; |
| 5 | + |
| 6 | +import axiosInstance from '@api/axiosInstance'; |
| 7 | + |
| 8 | +export type GetNoticeArticlesResponseData = { |
| 9 | + articles: Array<NoticeArticle>; |
| 10 | + currentPage: number; |
| 11 | + lastPage: number; |
| 12 | + totalCount: number; |
| 13 | +}; |
| 14 | + |
| 15 | +export type GetNoticeArticleResponseData = NoticeArticle; |
| 16 | + |
| 17 | +export type GetNoticeArticlesParams = { |
| 18 | + studyId: number; |
| 19 | + page?: number; |
| 20 | + size?: number; |
| 21 | +}; |
| 22 | + |
| 23 | +export type GetNoticeArticleParams = { |
| 24 | + studyId: number; |
| 25 | + articleId: number; |
| 26 | +}; |
| 27 | + |
| 28 | +export type PostNoticeArticleRequestParams = { |
| 29 | + studyId: number; |
| 30 | +}; |
| 31 | +export type PostNoticeArticleRequestBody = { |
| 32 | + title: string; |
| 33 | + content: string; |
| 34 | +}; |
| 35 | +export type PostNoticeArticleRequestVariables = PostNoticeArticleRequestParams & PostNoticeArticleRequestBody; |
| 36 | +export type PostNoticeArticleResponseData = { |
| 37 | + studyId: number; |
| 38 | + title: string; |
| 39 | + content: string; |
| 40 | +}; |
| 41 | + |
| 42 | +export type PutNoticeArticleRequestParams = { |
| 43 | + studyId: number; |
| 44 | + articleId: number; |
| 45 | +}; |
| 46 | +export type PutNoticeArticleRequestBody = { |
| 47 | + title: string; |
| 48 | + content: string; |
| 49 | +}; |
| 50 | +export type PutNoticeArticleRequestVariables = PutNoticeArticleRequestParams & PutNoticeArticleRequestBody; |
| 51 | + |
| 52 | +export type DeleteNoticeArticleRequestParams = { |
| 53 | + studyId: number; |
| 54 | + articleId: number; |
| 55 | +}; |
| 56 | + |
| 57 | +const getNoticeArticles = async ({ studyId, page = 1, size = 8 }: GetNoticeArticlesParams) => { |
| 58 | + // 서버쪽에서는 page를 0번부터 계산하기 때문에 page - 1을 해줘야 한다 |
| 59 | + const response = await axiosInstance.get<GetNoticeArticlesResponseData>( |
| 60 | + `/api/studies/${studyId}/notice/articles?page=${page - 1}&size=${size}`, |
| 61 | + ); |
| 62 | + const { totalCount, currentPage, lastPage } = response.data; |
| 63 | + |
| 64 | + response.data = { |
| 65 | + ...response.data, |
| 66 | + totalCount: Number(totalCount), |
| 67 | + currentPage: Number(currentPage) + 1, // page를 하나 늘려준다 서버에서 0으로 오기 때문이다 |
| 68 | + lastPage: Number(lastPage), |
| 69 | + }; |
| 70 | + |
| 71 | + return response.data; |
| 72 | +}; |
| 73 | + |
| 74 | +const getNoticeArticle = async ({ studyId, articleId }: GetNoticeArticleParams) => { |
| 75 | + // 서버쪽에서는 page를 0번부터 계산하기 때문에 page - 1을 해줘야 한다 |
| 76 | + const response = await axiosInstance.get<GetNoticeArticleResponseData>( |
| 77 | + `/api/studies/${studyId}/notice/articles/${articleId}`, |
| 78 | + ); |
| 79 | + return response.data; |
| 80 | +}; |
| 81 | + |
| 82 | +const postNoticeArticle = async ({ studyId, title, content }: PostNoticeArticleRequestVariables) => { |
| 83 | + const response = await axiosInstance.post<null, AxiosResponse<null>, PostNoticeArticleRequestBody>( |
| 84 | + `/api/studies/${studyId}/notice/articles`, |
| 85 | + { |
| 86 | + title, |
| 87 | + content, |
| 88 | + }, |
| 89 | + ); |
| 90 | + |
| 91 | + return response.data; |
| 92 | +}; |
| 93 | + |
| 94 | +const putNoticeArticle = async ({ studyId, title, content, articleId }: PutNoticeArticleRequestVariables) => { |
| 95 | + const response = await axiosInstance.put<null, AxiosResponse<null>, PutNoticeArticleRequestBody>( |
| 96 | + `/api/studies/${studyId}/notice/articles/${articleId}`, |
| 97 | + { |
| 98 | + title, |
| 99 | + content, |
| 100 | + }, |
| 101 | + ); |
| 102 | + |
| 103 | + return response.data; |
| 104 | +}; |
| 105 | + |
| 106 | +const deleteNoticeArticle = async ({ studyId, articleId }: DeleteNoticeArticleRequestParams) => { |
| 107 | + const response = await axiosInstance.delete<null, AxiosResponse<null>>( |
| 108 | + `/api/studies/${studyId}/notice/articles/${articleId}`, |
| 109 | + ); |
| 110 | + |
| 111 | + return response.data; |
| 112 | +}; |
| 113 | + |
| 114 | +export const useGetNoticeArticles = (studyId: number, page: number) => { |
| 115 | + return useQuery(['get-notice-articles', studyId, page], () => getNoticeArticles({ studyId, page })); |
| 116 | +}; |
| 117 | + |
| 118 | +export const useGetNoticeArticle = (studyId: number, articleId: number) => { |
| 119 | + return useQuery(['get-notice-article', studyId, articleId], () => getNoticeArticle({ studyId, articleId })); |
| 120 | +}; |
| 121 | + |
| 122 | +export const usePostNoticeArticle = () => { |
| 123 | + return useMutation<null, AxiosError, PostNoticeArticleRequestVariables>(postNoticeArticle); |
| 124 | +}; |
| 125 | + |
| 126 | +export const usePutNoticeArticle = () => { |
| 127 | + return useMutation<null, AxiosError, PutNoticeArticleRequestVariables>(putNoticeArticle); |
| 128 | +}; |
| 129 | + |
| 130 | +export const useDeleteNoticeArticle = () => { |
| 131 | + return useMutation<null, AxiosError, DeleteNoticeArticleRequestParams>(deleteNoticeArticle); |
| 132 | +}; |
0 commit comments