Skip to content

Commit

Permalink
[리팩토링, Fix] 코드 4차 리팩토링 (#66)
Browse files Browse the repository at this point in the history
* refactor: 랜덤 덕담생성 시점 이관

* remove: 불필요한 코드 제거
  • Loading branch information
dahye1013 authored Oct 8, 2022
1 parent 71f1898 commit d1139e9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
9 changes: 7 additions & 2 deletions pages/attitude/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ import { AttitudeRange, MessageBox, MessageItem } from '@/components/attitude';
import { Button, Description, Title } from '@/components/common';
import { Bottom } from '@/components/layout';
import useAttitudeHooks from '@/shared/hooks/useAttitudeHooks';
import useDuckdam from '@/shared/hooks/useDuckdam';
import theme from '@/styles/theme';

const Attitude = () => {
const { message1, message2, askMessage, politeLevel, setPoliteRange } =
useAttitudeHooks();

const { addNewDuckDam } = useDuckdam();

const router = useRouter();

const handleAddNewDuckDam = () => {
router.push(`load/?politeLevel=${politeLevel}`);
const handleAddNewDuckDam = async () => {
const id = await addNewDuckDam(politeLevel);
router.push(`load/?id=${id}`);
};

return (
Expand Down
19 changes: 8 additions & 11 deletions pages/load/index.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { useRouter } from 'next/router';
import { useEffect } from 'react';

import { LoadingLogo, Title } from '@/components/common';
import useDuckdam from '@/shared/hooks/useDuckdam';

const Load = () => {
const {
addNewDuckDam: addNewDuckDamHandler,
hasPoliteLevel,
getPoliteLevel,
} = useDuckdam();
const router = useRouter();
const { id } = router.query;

useEffect(() => {
const politeLevel = getPoliteLevel();
const timer = setTimeout(() => router.push(`/result/${id}`), 3000);

if (hasPoliteLevel(politeLevel)) {
addNewDuckDamHandler(politeLevel);
}
}, [addNewDuckDamHandler, getPoliteLevel, hasPoliteLevel]);
return () => {
clearTimeout(timer);
};
}, [id, router]);

return (
<>
Expand Down
15 changes: 2 additions & 13 deletions shared/hooks/useDuckdam.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import axios from 'axios';
import { useRouter } from 'next/router';

import type { PoliteKey } from '@/shared/types/DuckDam';
import { randomNewDuckDam } from '@/shared/utils/duckdamGenerator';

const useDuckdam = () => {
const router = useRouter();

const addNewDuckDam = async (politeLevel: PoliteKey) => {
const newDuckDam = {
...randomNewDuckDam(politeLevel),
Expand All @@ -20,19 +17,11 @@ const useDuckdam = () => {
});

if (status === 200) {
setTimeout(() => router.push(`/result/${data}`), 3000);
return data;
}
};

const hasPoliteLevel = (politeLevel: unknown): politeLevel is PoliteKey => {
return politeLevel !== undefined && typeof politeLevel === 'string';
};

const getPoliteLevel = () => {
return router.query.politeLevel;
};

return { addNewDuckDam, hasPoliteLevel, getPoliteLevel };
return { addNewDuckDam };
};

export default useDuckdam;

1 comment on commit d1139e9

@vercel
Copy link

@vercel vercel bot commented on d1139e9 Oct 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.