Skip to content

Commit

Permalink
Merge pull request #127 from softeerbootcamp4th/feat/#124/adminDateCh…
Browse files Browse the repository at this point in the history
…ange

Feat/#124/admin date change
  • Loading branch information
subsub-e authored Aug 19, 2024
2 parents 8bc83cf + e5e3abd commit 928e7ca
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 37 deletions.
41 changes: 30 additions & 11 deletions admin/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
import React from 'react';
import { Outlet, useLocation } from 'react-router-dom';
import React, { useEffect, useContext } from 'react';
import { Outlet, useLocation, useNavigate } from 'react-router-dom';
import AdminHeader from '@/components/header/AdminHeader';
import TabHeader from '@/components/header/TabHeader';
import { DateProvider } from '@/context/dateContext';
import { DateContext } from '@/context/dateContext';
import dateFormatting from '@/utils/dateFormatting';

function App() {
const location = useLocation();
const navigate = useNavigate();
const hideHeader = ['/login', '/error'].includes(location.pathname);
const { setDateInfo } = useContext(DateContext);

useEffect(() => {
if (location.pathname === '/') {
const today = new Date();
const formattedDate = dateFormatting(today);
navigate(`/${formattedDate}`);
}
}, [location, navigate]);

useEffect(() => {
const pathSegments = location.pathname.split('/');
const potentialDateSegment = pathSegments[1];

if (potentialDateSegment && !isNaN(Date.parse(potentialDateSegment))) {
const parsedDate = new Date(potentialDateSegment);
setDateInfo(dateFormatting(parsedDate));
}
}, [location.pathname, setDateInfo]);

return (
<DateProvider>
<div className="bg-[#DADADA] flex flex-col items-center min-w-[700px] text-nowrap">
{!hideHeader && <AdminHeader />}
{!hideHeader && <TabHeader />}
<div className="w-[90%] flex flex-col items-center pb-2000">
<Outlet />
</div>
<div className="bg-[#DADADA] flex flex-col items-center min-w-[700px] text-nowrap">
{!hideHeader && <AdminHeader />}
{!hideHeader && <TabHeader />}
<div className="w-[90%] flex flex-col items-center pb-2000">
<Outlet />
</div>
</DateProvider>
</div>
);
}

Expand Down
8 changes: 8 additions & 0 deletions admin/src/api/header/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { put, get } from '@/api/index';

const putEventSchedules = startDate =>
put('/admin/event-schedules', { startDate });

const getEventSchedules = () => get('/admin/event-schedules');

export { putEventSchedules, getEventSchedules };
54 changes: 48 additions & 6 deletions admin/src/components/header/AdminHeader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,59 @@ import NextDayArrow from '@/assets/icons/nextDayArrow.svg';
import PreviousDayArrow from '@/assets/icons/previousDayArrow.svg';
import dateFormatting from '@/utils/dateFormatting';
import { DateContext } from '@/context/dateContext';
import { useNavigate, useLocation } from 'react-router-dom';
import { putEventSchedules, getEventSchedules } from '@/api/header/index';

function AdminHeader() {
const { dateInfo, setDateInfo } = useContext(DateContext);
const location = useLocation();
const navigator = useNavigate();
const { dateInfo } = useContext(DateContext);
const [isNextDayDisabled, setIsNextDayDisabled] = useState(false);
const [isPreviousDayDisabled, setIsPreviousDayDisabled] = useState(false);
const [selectedDate, setSelectedDate] = useState('');

useEffect(() => {
setIsNextDayDisabled(dateInfo === '2024-08-31');
setIsPreviousDayDisabled(dateInfo === '2024-07-28');
const getDate = async () => {
const response = await getEventSchedules();
const startDate = new Date(response[0].date);
const finishDate = new Date(response[13].date);
const currentDate = new Date(dateInfo);
setIsPreviousDayDisabled(currentDate.getTime() === startDate.getTime());
setIsNextDayDisabled(currentDate.getTime() === finishDate.getTime());
};
getDate();
}, [dateInfo]);

const handleDateChange = event => {
setSelectedDate(event.target.value);
};

const handlePreviousDay = () => {
if (!isPreviousDayDisabled) {
const previousDay = new Date(dateInfo);
previousDay.setDate(previousDay.getDate() - 1);
setDateInfo(dateFormatting(previousDay));
const pathSegments = location.pathname.split('/');
const tabName = pathSegments[2];
navigator(`/${dateFormatting(previousDay)}/${tabName}`);
}
};

const handleNextDay = () => {
if (!isNextDayDisabled) {
const nextDay = new Date(dateInfo);
nextDay.setDate(nextDay.getDate() + 1);
setDateInfo(dateFormatting(nextDay));
const pathSegments = location.pathname.split('/');
const tabName = pathSegments[2];
navigator(`/${dateFormatting(nextDay)}/${tabName}`);
}
};

const handleSubmit = () => {
putEventSchedules(selectedDate);
};

return (
<div className="pt-1000 flex justify-between w-[90%]">
<div className="pt-1000 flex justify-between w-[90%] items-center">
<div className="set-center gap-500">
<span className="text-heading-2-bold text-neutral-black">
이벤트 관리
Expand All @@ -54,6 +79,23 @@ function AdminHeader() {
/>
</div>
</div>
<div>
<div className="flex gap-300">
<label htmlFor="date">시작일 설정: </label>
<input
type="date"
id="date"
value={selectedDate}
onChange={handleDateChange}
/>
<button
className="text-white bg-neutral-black"
onClick={handleSubmit}
>
전송
</button>
</div>
</div>
</div>
);
}
Expand Down
10 changes: 8 additions & 2 deletions admin/src/components/header/NavLinkItem.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import React from 'react';
import React, { useContext } from 'react';
import PropTypes from 'prop-types';
import { NavLink } from 'react-router-dom';
import { DateContext } from '@/context/dateContext';

function NavLinkItem({ path, value }) {
const { dateInfo } = useContext(DateContext);

const fullPath = `/${dateInfo}${path}`;

return (
<NavLink
to={path}
to={fullPath}
end
className={({ isActive }) =>
`${
isActive
Expand Down
2 changes: 1 addition & 1 deletion admin/src/components/header/TabHeader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import NavLinkItem from '@/components/header/NavLinkItem';
function TabHeader() {
return (
<div className="pt-1000 flex justify-between items-center w-[90%]">
<NavLinkItem path="/" value="미니퀴즈 질문" />
<NavLinkItem path="" value="미니퀴즈 질문" />
<NavLinkItem path="/miniQuizAnswer" value="미니퀴즈 답변" />
<NavLinkItem path="/draw" value="응모 결과" />
<NavLinkItem path="/reward" value="상품 목록" />
Expand Down
9 changes: 6 additions & 3 deletions admin/src/main.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import '@/styles/global.css';
import { DateProvider } from '@/context/dateContext';
import { RouterProvider } from 'react-router-dom';
import ReactDOM from 'react-dom/client';
import router from '@/router';
import '@/styles/global.css';

ReactDOM.createRoot(document.getElementById('root')).render(
<RouterProvider router={router} />,
<DateProvider>
<RouterProvider router={router} />,
</DateProvider>,
);
7 changes: 2 additions & 5 deletions admin/src/pages/UploadPrize/UploadPrize.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ function UploadPrize() {
const zipContent = await zip.loadAsync(file);
const fileKeys = Object.keys(zipContent.files);

// 허용된 확장자 목록
const allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'svg'];

// 디렉토리 파일이 포함되어 있거나, 허용되지 않은 확장자가 포함된 경우 체크
const hasInvalidFiles = fileKeys.some(key => {
if (zipContent.files[key].dir) {
Expand All @@ -142,9 +139,9 @@ function UploadPrize() {
}

const fileExt = key.split('.').pop().toLowerCase();
if (!allowedExtensions.includes(fileExt)) {
if ('jpg' !== fileExt) {
setErrorMessage(
`파일 형식은 'jpg', 'jpeg', 'png', 'gif', 'svg'만 가능합니다. ${fileExt} 해당 형식이 있습니다.`,
`파일 형식은 'jpg'만 가능합니다. ${fileExt} 형식 파일이 있습니다.`,
);
return true; // 허용되지 않은 확장자 포함
}
Expand Down
7 changes: 2 additions & 5 deletions admin/src/pages/UploadReward/UploadReward.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ function UploadReward() {
const zipContent = await zip.loadAsync(file);
const fileKeys = Object.keys(zipContent.files);

// 허용된 확장자 목록
const allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'svg'];

// 디렉토리 파일이 포함되어 있거나, 허용되지 않은 확장자가 포함된 경우 체크
const hasInvalidFiles = fileKeys.some(key => {
if (zipContent.files[key].dir) {
Expand All @@ -107,9 +104,9 @@ function UploadReward() {
}

const fileExt = key.split('.').pop().toLowerCase();
if (!allowedExtensions.includes(fileExt)) {
if ('jpg' !== fileExt) {
setErrorMessage(
`파일 형식은 'jpg', 'jpeg', 'png', 'gif', 'svg'만 가능합니다. ${fileExt} 해당 형식이 있습니다.`,
`파일 형식은 'jpg'만 가능합니다. ${fileExt} 형식 파일이 있습니다.`,
);
return true; // 허용되지 않은 확장자 포함
}
Expand Down
14 changes: 10 additions & 4 deletions admin/src/router.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ const router = createBrowserRouter([
{
path: '/',
element: <App />,
children: [
{
path: 'error',
element: <ErrorPage />,
},
],
},
{
path: '/:date',
element: <App />,
children: [
{
element: <ProtectedRoute />,
Expand Down Expand Up @@ -55,10 +65,6 @@ const router = createBrowserRouter([
path: 'uploadPrize',
element: <UploadPrize />,
},
{
path: 'error',
element: <ErrorPage />,
},
{
path: 'Indicator',
element: <Indicator />,
Expand Down

0 comments on commit 928e7ca

Please sign in to comment.