Skip to content

Commit

Permalink
[Fix/BAR-252] 메인 페이지의 type searchParam에 저장 (#80)
Browse files Browse the repository at this point in the history
* fix: main 페이지의 type url에 저장

* refactor: 쿼리파람 tab으로 변경
  • Loading branch information
miro-ring authored Feb 23, 2024
1 parent ff379ec commit cfba340
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
7 changes: 5 additions & 2 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';

import { ROUTES } from '@constants/routes';
import { STORAGE_KEY } from '@models/storage';

export const middleware = async (request: NextRequest) => {
Expand All @@ -9,15 +10,17 @@ export const middleware = async (request: NextRequest) => {
if (request.nextUrl.pathname.startsWith('/terms')) return;

if (refreshToken?.value && request.nextUrl.pathname === '/') {
return NextResponse.redirect(new URL('/main', request.url));
return NextResponse.redirect(
new URL(`${ROUTES.MAIN}?tab=write`, request.url),
);
}

if (
!refreshToken &&
request.nextUrl.pathname !== '/' &&
!request.nextUrl.pathname.startsWith('/redirect')
) {
return NextResponse.redirect(new URL('/', request.url));
return NextResponse.redirect(new URL(ROUTES.LANDING, request.url));
}
};

Expand Down
13 changes: 9 additions & 4 deletions pages/main/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { useEffect, useRef, useState } from 'react';
import { useRouter, useSearchParams } from 'next/navigation';
import { useEffect, useRef } from 'react';

import WriteInput from '@components/Input/WriteInput';
import Layout from '@components/Layout';
import MainPageTab from '@components/Layout/MainPageTab';
import { ROUTES } from '@constants/routes';
import WriteGuide from '@domain/끄적이는/components/Guide';
import TemporalMemoHistoryTable from '@domain/끄적이는/components/History';
import TodayTemoralMemos from '@domain/끄적이는/components/Today';
Expand All @@ -16,6 +18,9 @@ import useGetMyProfile from '@queries/useGetMyProfile';
import { COLORS } from '@styles/tokens';

const MainPage = () => {
const router = useRouter();
const searchParams = useSearchParams();

useGetMyProfile();

const writeContentRef = useRef<HTMLDivElement>(null);
Expand All @@ -24,7 +29,7 @@ const MainPage = () => {
const { mutate: submitTemporalMemo } = useCreateTemporalMemo();
const { data: memoFolders } = useGetMemoFolders();

const [selectedTab, setSelectedTab] = useState('끄적이는');
const selectedTab = searchParams.get('tab') || 'write';

useEffect(() => {
handleScroll();
Expand All @@ -37,7 +42,7 @@ const MainPage = () => {
};

const handleTabSelect = (selectedTab: string) => {
setSelectedTab(selectedTab);
router.push(`${ROUTES.MAIN}?tab=${selectedTab}`);
};

const handleSubmit = () => {
Expand All @@ -48,7 +53,7 @@ const MainPage = () => {
};

const backgroundColor =
selectedTab === '참고하는' ? COLORS['Grey/100'] : undefined;
selectedTab === 'refer' ? COLORS['Grey/100'] : undefined;

return (
<Layout backgroundColor={backgroundColor}>
Expand Down
2 changes: 1 addition & 1 deletion pages/redirect/[authType]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const Bridge = () => {
saveToken({ accessToken, refreshToken });
localStorage.setItem(STORAGE_KEY.RECENT_LOGIN_TYPE, authType);

router.replace(ROUTES.MAIN);
router.replace(`${ROUTES.MAIN}?tab=write`);
})();
}, [router.query, router]);

Expand Down
8 changes: 4 additions & 4 deletions src/components/Layout/MainPageTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const MainPageTab = ({
<Tooltip hasArrow>
<Tooltip.Trigger>
<Tabs.Trigger
value="끄적이는"
value="write"
icon={{ default: 'pencilDefault', active: 'pencilActive' }}
>
끄적이는
Expand All @@ -40,7 +40,7 @@ const MainPageTab = ({
<Tooltip hasArrow>
<Tooltip.Trigger>
<Tabs.Trigger
value="참고하는"
value="refer"
icon={{ default: 'templateDefault', active: 'templateActive' }}
>
참고하는
Expand All @@ -52,8 +52,8 @@ const MainPageTab = ({
</div>

<section className={styles.tabWrapper}>
<Tabs.Content value="끄적이는">{write}</Tabs.Content>
<Tabs.Content value="참고하는">{refer}</Tabs.Content>
<Tabs.Content value="write">{write}</Tabs.Content>
<Tabs.Content value="refer">{refer}</Tabs.Content>
</section>
</Tabs>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/Layout/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const Header = ({ type }: HeaderProps) => {
return (
<header className={styles.header}>
<Link
href={refreshToken ? ROUTES.MAIN : ROUTES.LANDING}
href={refreshToken ? `${ROUTES.MAIN}?tab=write` : ROUTES.LANDING}
className={styles.logo}
>
<Logo />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ const NotFoundArchiveCard = () => {
자주 사용하는 글과 템플릿을 저장해보세요.
</span>
</div>
<Button state="enabled" size="M" onClick={() => router.push(ROUTES.MAIN)}>
<Button
state="enabled"
size="M"
onClick={() => router.push(`${ROUTES.MAIN}?tab=refer`)}
>
문장 템플릿 보러가기
</Button>
</div>
Expand Down

0 comments on commit cfba340

Please sign in to comment.