Skip to content

Commit

Permalink
Merge pull request #5 from shimdokite/admin
Browse files Browse the repository at this point in the history
[FE] ♻️ 관리자 모드 리팩토링
  • Loading branch information
shimdokite authored Mar 29, 2024
2 parents 8bd60c3 + 1c27020 commit a287a8e
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 29 deletions.
18 changes: 17 additions & 1 deletion client/src/app/admin/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import CheckInquiry from '@/components/admin/CheckInquiry';
'use client';

import { useRouter } from 'next/navigation';

import useUserStore from '@/stores/userStore';

import { CheckInquiry } from '@/components/admin';

import { ADMIN_USER_ID } from '@/constants/values';

export default function Admin() {
const router = useRouter();

const { userId } = useUserStore();

if (userId !== ADMIN_USER_ID) {
router.replace('/');
}

return (
<div className="h-full flex justify-center items-center">
<CheckInquiry />
Expand Down
16 changes: 16 additions & 0 deletions client/src/app/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { NextRequest, NextResponse } from 'next/server';

import useUserStore from '@/stores/userStore';

import { ADMIN_USER_ID } from '@/constants/values';

export function middleware(request: NextRequest) {
const { userId } = useUserStore();

if (userId !== ADMIN_USER_ID && request.nextUrl.pathname.startsWith('/admin'))
return NextResponse.redirect(new URL('/', request.url));
}

export const cofig = {
matcher: '/admin',
};
44 changes: 21 additions & 23 deletions client/src/components/admin/ChatModal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Chat } from '../inquiry';
import { CommonButton, Modal, ModalPortal } from '../common';
import { CommonButton, Modal } from '../common';

interface ChatModalProps {
close: () => void;
Expand All @@ -8,30 +8,28 @@ interface ChatModalProps {

export default function ChatModal({ close, handleAnswered }: ChatModalProps) {
return (
<ModalPortal>
<Modal>
<div className="px-2 py-2 mx-2 flex flex-col justify-center items-center border-2 rounded-lg border-brown-50 bg-brown-20">
<Chat role="admin" />
<Modal>
<div className="px-2 py-2 mx-2 flex flex-col justify-center items-center border-2 rounded-lg border-brown-50 bg-brown-20">
<Chat role="admin" />

<div className="flex justify-center items-center">
<CommonButton
type="button"
size="md"
className="mx-1 my-2 max-[580px]:text-[16px]"
onClose={() => close()}>
종료하기
</CommonButton>
<div className="flex justify-center items-center">
<CommonButton
type="button"
size="md"
className="mx-1 my-2 max-[580px]:text-[16px]"
onClose={() => close()}>
종료하기
</CommonButton>

<CommonButton
type="button"
size="md"
className="mx-1 my-2 max-[580px]:text-[16px]"
onClose={handleAnswered}>
메일 전송
</CommonButton>
</div>
<CommonButton
type="button"
size="md"
className="mx-1 my-2 max-[580px]:text-[16px]"
onClose={() => handleAnswered()}>
메일 전송
</CommonButton>
</div>
</Modal>
</ModalPortal>
</div>
</Modal>
);
}
8 changes: 7 additions & 1 deletion client/src/components/admin/CheckInquiry.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
'use client';

import { createPortal } from 'react-dom';

import useModalStore, { ModalType } from '@/stores/modalStore';

import useUpdateChatAnsweredMutation from '@/hooks/mutation/useUpdateChatAnsweredMutation';
import useModal from '@/hooks/useModal';

import { ChatModal, InquiryListForm } from '.';

export default function CheckInquiry() {
const { isOpen, type, close } = useModalStore();

const { portalElement } = useModal(isOpen);
const { mutate: onAnswered } = useUpdateChatAnsweredMutation();

const handleAnswered = () => {
Expand All @@ -27,7 +31,9 @@ export default function CheckInquiry() {
<InquiryListForm />
</div>

{isOpen && renderModal(type)}
{isOpen && portalElement
? createPortal(renderModal(type), portalElement)
: null}
</div>
);
}
9 changes: 5 additions & 4 deletions client/src/components/admin/InquiryListForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Pagination } from '.';
import { ErrorMessage, LoadingMessage } from '../common';

import { REPORT_TITLE, REPORT_TITLE_STYLE } from '@/constants/contents';
import { ChatList } from '@/types/data';

export default function InquiryListForm() {
const { open, changeType } = useModalStore();
Expand All @@ -34,9 +35,9 @@ export default function InquiryListForm() {
if (current.key === 'entry')
return (
setRoomId(`${chatRoomId}`),
setTitle(title),
changeType('ChatModal'),
open(),
setTitle(title)
open()
);
};

Expand Down Expand Up @@ -70,7 +71,7 @@ export default function InquiryListForm() {

<tbody className="bg-white-10">
{isSuccess &&
chatList.map((list: any) => (
chatList.map((list: ChatList) => (
<tr
key={list.chatRoomId}
className="border-b-[1px] border-brown-90">
Expand All @@ -86,7 +87,7 @@ export default function InquiryListForm() {
}>
{current.key === 'entry'
? '입장하기'
: `${list[current.key]}`}
: `${list[current.key as keyof ChatList]}`}
</td>
))}
</tr>
Expand Down
1 change: 1 addition & 0 deletions client/src/components/admin/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { MouseEventHandler } from 'react';

interface PaginationProps {
page: number;

onFirstPage: MouseEventHandler<HTMLDivElement>;
onLastPage: MouseEventHandler<HTMLDivElement>;
onPreviousPage: MouseEventHandler<HTMLButtonElement>;
Expand Down
1 change: 1 addition & 0 deletions client/src/stores/chatStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const useChatStore = create<ChatState>((set) => ({
title: '',
roomId: '',
questionerId: '',

isOpen: false,

setChatList: (chatList) => {
Expand Down

0 comments on commit a287a8e

Please sign in to comment.