-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
140 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,5 @@ export interface PresignedUrlResponse { | |
urls: { | ||
file_path: string, | ||
pre_signed_url: string, | ||
url: string | ||
}[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,139 @@ | ||
import { NoticeWrite, NoticeEdit } from "./request"; | ||
import { MutationOptions, useMutation } from "react-query"; | ||
import { instance } from "../axios"; | ||
import { NoticeListResponse } from "./response"; | ||
import { useEffect, useState } from "react"; | ||
import { NoticeDetailResponse } from "./response"; | ||
import { NoticeWrite, NoticeEdit } from './request'; | ||
import { MutationOptions, useMutation } from 'react-query'; | ||
import { instance } from '../axios'; | ||
import { NoticeListResponse } from './response'; | ||
import { useEffect, useState } from 'react'; | ||
import { NoticeDetailResponse } from './response'; | ||
import { useToastStore } from '@team-return/design-system'; | ||
|
||
const router = '/notices'; | ||
|
||
/** 공지사항 작성 */ | ||
export const useNoticeWriteData = () => { | ||
return useMutation( | ||
async (noticeData: NoticeWrite) => { | ||
const { data } = await instance.post(`/notices`, noticeData); | ||
return data; | ||
}, | ||
{ | ||
onError: (error: Error) => { | ||
console.error("notice write error:", error.message); | ||
} | ||
} | ||
) | ||
} | ||
const { append } = useToastStore(); | ||
return useMutation( | ||
async (noticeData: NoticeWrite) => { | ||
const { data } = await instance.post(`${router}`, noticeData); | ||
return data; | ||
}, | ||
{ | ||
onError: () => { | ||
append({ | ||
title: '공지 작성에 실패했습니다.', | ||
message: '', | ||
type: 'RED', | ||
}); | ||
}, | ||
} | ||
); | ||
}; | ||
|
||
/** 공지사항 수정 */ | ||
export const useNoticeEditData = (noticeId: string) => { | ||
return useMutation( | ||
async (noticeData: NoticeEdit) => { | ||
const { data } = await instance.patch(`/notices/${noticeId}`, noticeData); | ||
return data; | ||
}, | ||
{ | ||
onError: (error: Error) => { | ||
console.error("notice edit error: ", error); | ||
|
||
} | ||
} | ||
) | ||
} | ||
const { append } = useToastStore(); | ||
return useMutation( | ||
async (noticeData: NoticeEdit) => { | ||
const { data } = await instance.patch( | ||
`${router}/${noticeId}`, | ||
noticeData | ||
); | ||
return data; | ||
}, | ||
{ | ||
onError: () => { | ||
append({ | ||
title: '공지 수정에 실패했습니다.', | ||
message: '', | ||
type: 'RED', | ||
}); | ||
}, | ||
} | ||
); | ||
}; | ||
|
||
/** 공지사항 상세보기 조회 */ | ||
export const useNoticeDetailData = (noticeId: string) => { | ||
const [noticeDetail, setNoticeDetail] = useState<NoticeDetailResponse | null>(null); | ||
|
||
useEffect(() => { | ||
const fetchNoticeDetail = async () => { | ||
|
||
try { | ||
const response = await instance.get(`${process.env.REACT_APP_BASE_URL}/notices/${noticeId}`); | ||
const data = response.data; | ||
const [noticeDetail, setNoticeDetail] = | ||
useState<NoticeDetailResponse | null>(null); | ||
const { append } = useToastStore(); | ||
|
||
const fetchNoticeDetail = (noticeId: string) => { | ||
return instance | ||
.get(`${router}/${noticeId}`) | ||
.then((response) => { | ||
const data = response.data; | ||
const fetchedNoticeDetail: NoticeDetailResponse = { | ||
title: data.title, | ||
content: data.content, | ||
created_at: new Date(data.created_at).toISOString(), | ||
attachments: data.attachments.map((attachment: any) => ({ | ||
url: attachment.url, | ||
type: attachment.type, | ||
})), | ||
}; | ||
setNoticeDetail(fetchedNoticeDetail); | ||
}) | ||
.catch(() => { | ||
append({ | ||
title: '공지 상세보기에 실패했습니다.', | ||
message: '', | ||
type: 'RED', | ||
}); | ||
}); | ||
}; | ||
|
||
const fetchedNoticeDetail: NoticeDetailResponse = { | ||
title: data.title, | ||
content: data.content, | ||
created_at: new Date(data.created_at).toISOString(), | ||
attachments: data.attachments.map((attachment: any) => ({ | ||
url: attachment.url, | ||
type: attachment.type | ||
})) | ||
}; | ||
setNoticeDetail(fetchedNoticeDetail); | ||
} catch (error: any) { | ||
console.error('notice detail error:', error.message); | ||
} | ||
}; | ||
fetchNoticeDetail(); | ||
}, [noticeId]); | ||
useState(() => { | ||
fetchNoticeDetail(noticeId); | ||
}); | ||
|
||
return { noticeDetail }; | ||
return { noticeDetail }; | ||
}; | ||
|
||
/** 공지사항 리스트 조희 */ | ||
export const useNoticeListData = () => { | ||
const [notices, setNotices] = useState<NoticeListResponse[]>([]); | ||
const [notices, setNotices] = useState<NoticeListResponse[]>([]); | ||
const { append } = useToastStore(); | ||
|
||
useEffect(() => { | ||
const fetchNoticeList = async () => { | ||
try { | ||
const response = await instance.get(`${process.env.REACT_APP_BASE_URL}/notices`); | ||
const data = response.data; | ||
const fetchNoticeList = () => { | ||
return instance | ||
.get(`${router}`) | ||
.then((response) => { | ||
const data = response.data; | ||
|
||
const fetchedNotices: NoticeListResponse[] = data.notices.map((notice: any) => ({ | ||
id: notice.id, | ||
title: notice.title, | ||
created_at: new Date(notice.created_at).toISOString() | ||
})); | ||
setNotices(fetchedNotices); | ||
} catch (error: any) { | ||
console.error('notice list error: ', error.message); | ||
} | ||
}; | ||
fetchNoticeList(); | ||
}, []); | ||
const fetchedNotices: NoticeListResponse[] = data.notices.map( | ||
(notice: any) => ({ | ||
id: notice.id, | ||
title: notice.title, | ||
created_at: new Date(notice.created_at).toISOString(), | ||
}) | ||
); | ||
setNotices(fetchedNotices); | ||
}) | ||
.catch(() => { | ||
append({ | ||
title: '공지 상세보기에 실패했습니다.', | ||
message: '', | ||
type: 'RED', | ||
}); | ||
}); | ||
}; | ||
|
||
return { notices }; | ||
} | ||
useEffect(() => { | ||
fetchNoticeList(); | ||
}, []); | ||
|
||
return { notices }; | ||
}; | ||
|
||
/** 공지사항 삭제 */ | ||
export const useDeleteNotice = (noticeId: string, options: MutationOptions) => { | ||
return useMutation( | ||
async () => instance.delete(`${process.env.REACT_APP_BASE_URL}/notices/${noticeId}`, { data: { "notice-id": noticeId } }), | ||
{ | ||
...options | ||
} | ||
) | ||
} | ||
return useMutation( | ||
async () => | ||
instance.delete(`${router}/${noticeId}`, { | ||
data: { 'notice-id': noticeId }, | ||
}), | ||
{ | ||
...options, | ||
} | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.