Skip to content

Commit

Permalink
feat: 리뷰 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
eejx0 committed May 27, 2024
1 parent 5f91be1 commit fc95634
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 135 deletions.
6 changes: 1 addition & 5 deletions src/Apis/Files/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import { PresignedUrlResponse } from "./response";
import { instance } from "../axios";
import axios from "axios";

export type PresignedUrlType =
| "LOGO_IMAGE"
| "EXTENSION_FILE";

export const usePresignedUrl = (type: PresignedUrlType) => {
export const usePresignedUrl = () => {
const res = useMutation(
async (attachments: File[] ) => {
const files = attachments.map((item) => ({
Expand Down
1 change: 0 additions & 1 deletion src/Apis/Files/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ export interface PresignedUrlResponse {
urls: {
file_path: string,
pre_signed_url: string,
url: string
}[]
}
201 changes: 118 additions & 83 deletions src/Apis/Notices/index.ts
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,
}
);
};
16 changes: 10 additions & 6 deletions src/Components/Notice/AttachedBox/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Icon } from '@team-return/design-system';
import { Icon, useToastStore } from '@team-return/design-system';
import { AttachmentResponse } from '../../../Apis/Notices/response';
import * as _ from './style';
import axios from 'axios';
Expand All @@ -11,26 +11,30 @@ export function AttachedBox({ props }: PropsType) {
const file_name_regex = (url: string) => {
return url.replace(/(?:.*?-){5}(.*)/, '$1').replaceAll('+', ' ');
};
const { append } = useToastStore();

const downLoadFile = async (attachment: AttachmentResponse) => {
try {
const response = await axios.get(
const { data } = await axios.get(
`${process.env.REACT_APP_FILE_URL}${attachment.url}`,
{
responseType: 'blob',
}
);

const url = window.URL.createObjectURL(new Blob([response.data]));
const url = window.URL.createObjectURL(new Blob([data]));
const a = document.createElement('a');
a.href = url;
a.download = file_name_regex(attachment.url);
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
} catch (error) {
console.error('파일 다운로드 에러: ', error);
} catch {
append({
title: '파일 다운로드에 실패했습니다.',
message: '',
type: 'RED',
});
}
};

Expand Down
7 changes: 1 addition & 6 deletions src/Pages/NoticePage/NoticeDetailPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { useDeleteNotice } from '../../../Apis/Notices';
import * as _ from './style';
import { AttachedBox } from '../../../Components/Notice/AttachedBox';
import { Link } from 'react-router-dom';
import { useEffect } from 'react';

export function NoticeDetailPage() {
const { id } = useParams<{ id: string }>();
Expand All @@ -30,10 +29,6 @@ export function NoticeDetailPage() {

const date = noticeDetail?.created_at.substring(0, 10);

useEffect(() => {
console.log(noticeDetail);
}, [noticeDetail]);

return (
<>
<Header />
Expand Down Expand Up @@ -68,7 +63,7 @@ export function NoticeDetailPage() {
icon="EditPencil"
color={'gray90'}
size={26}
></Icon>
/>
</_.IconBox>
</Link>
</_.IconWrapper>
Expand Down
35 changes: 5 additions & 30 deletions src/Pages/NoticePage/NoticeEditPage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { Header } from '../../../Components/Header';
import * as _ from './style';
import { ChangeEvent, useEffect, useRef, useState } from 'react';
import { useEffect, useState } from 'react';
import { Button } from '@team-return/design-system';
import { usePresignedUrl } from '../../../Apis/Files';
import { useLocation, useNavigate, useParams } from 'react-router-dom';
import { AttachmentRequest } from '../../../Apis/Notices/request';
import { Icon } from '@team-return/design-system';
import { useNoticeEditData } from '../../../Apis/Notices';
import { useForm } from '../../../Hooks/useForm';

export function NoticeEditPage() {
const fileInputRef = useRef<HTMLInputElement>(null);
const location = useLocation();
const [inputCount, setInputCount] = useState<number>(0);

Expand All @@ -31,7 +29,7 @@ export function NoticeEditPage() {
const { id } = useParams<{ id: string }>();

const { mutate: editNotice } = useNoticeEditData(id || '');
const { mutate: getPresignedUrl, data } = usePresignedUrl('EXTENSION_FILE');
const { mutate: getPresignedUrl, data } = usePresignedUrl();

useEffect(() => {
if (data) {
Expand All @@ -52,25 +50,6 @@ export function NoticeEditPage() {
}
}, [presignedUrls]);

const handleAddFileClick = () => {
if (fileInputRef.current) {
fileInputRef.current.click();
}
};

const handleFileChange = (e: ChangeEvent<HTMLInputElement>) => {
if (e.target.files) {
const newAttachments = Array.from(e.target.files);
getPresignedUrl(newAttachments);
}
};

const handleFileDelete = (index: number) => {
const newAttachments = [...noticeEditForm.editAttachments];
newAttachments.splice(index, 1);
setEditAttachments(newAttachments);
};

const handleNoticeSubmit = () => {
editNotice({
title: noticeEditForm.editTitle,
Expand All @@ -80,10 +59,6 @@ export function NoticeEditPage() {
navigate('/Notice');
};

const file_name_regex = (url: string) => {
return url?.replace(/(?:.*?-){5}(.*)/, '$1').replaceAll('+', ' ');
};

return (
<>
<Header />
Expand Down Expand Up @@ -113,9 +88,9 @@ export function NoticeEditPage() {
value={noticeEditForm.editContent}
onChange={handleChange}
name="editContent"
onInput={(e: any) =>
setInputCount(e.target.value.length)
}
onInput={(
e: React.ChangeEvent<HTMLTextAreaElement>
) => setInputCount(e.target.value.length)}
as="textarea"
/>
<_.InputCount>{inputCount}자/1000</_.InputCount>
Expand Down
Loading

0 comments on commit fc95634

Please sign in to comment.