Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user fetch refactoring /#98 #100

Merged
merged 13 commits into from
May 11, 2024
7 changes: 4 additions & 3 deletions app/business/user/user.query.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { httpErrorHandler } from '@/app/utils/http/http-error-handler';
import { API_PATH } from '../api-path';
import { UserInfoResponse } from './user.type';

import { cookies } from 'next/headers';
import { isValidation } from '@/app/utils/zod/validation.util';
import { InitUserInfoResponseSchema, UserInfoResponseSchema } from './user.validation';
import { InitUserInfoResponse, UserInfoResponse } from './user.type';
import { UserInfoResponseSchema, InitUserInfoResponseSchema } from './user.validation';

export async function fetchUserInfo(): Promise<UserInfoResponse> {
export async function fetchUserInfo(): Promise<InitUserInfoResponse | UserInfoResponse> {
try {
const response = await fetch(`${API_PATH.user}`, {
headers: {
Expand Down
3 changes: 2 additions & 1 deletion app/business/user/user.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface MockUser {

export type SignInResponse = z.infer<typeof SignInResponseSchema>;

export type UserInfoResponse = z.infer<typeof UserInfoResponseSchema | typeof InitUserInfoResponseSchema>;
export type UserInfoResponse = z.infer<typeof UserInfoResponseSchema>;
export type InitUserInfoResponse = z.infer<typeof InitUserInfoResponseSchema>;

export type ValidateTokenResponse = z.infer<typeof ValidateTokenResponseSchema>;
84 changes: 48 additions & 36 deletions app/ui/user/user-info-card/user-info-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { getPercentage } from '@/app/utils/chart.util';
import PieChart from '../../view/molecule/pie-chart/pie-chart';
import { fetchUserInfo } from '@/app/business/user/user.query';
import { MAJOR_NOTATION } from '@/app/utils/key/result-category.key';
import { UserInfoResponseSchema } from '@/app/business/user/user.validation';
import { z } from 'zod';

async function UserInfoCard() {
const {
Expand All @@ -13,48 +11,62 @@ async function UserInfoCard() {
totalCredit,
takenCredit,
graduated,
} = (await fetchUserInfo()) as z.infer<typeof UserInfoResponseSchema>;
} = await fetchUserInfo();

const displaySeveralMajor = (notation: 'major' | 'title'): React.ReactNode => {
return majors.map((major, index) => {
const { major: majorName, majorType } = major;
if (majors) {
return majors.map((major, index) => {
const { major: majorName, majorType } = major;

return <li key={index}>{notation === 'title' ? MAJOR_NOTATION[majorType] : majorName}</li>;
});
return <li key={index}>{notation === 'title' ? MAJOR_NOTATION[majorType] : majorName}</li>;
});
}
};

return (
<>
<p className="font-bold text-sm md:text-xl">
์กธ์—…ํ•„์š”ํ•™์ ๋ณด๋‹ค <span className="text-point-blue">{totalCredit - takenCredit}</span>ํ•™์ ์ด ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค.
</p>
<div className="flex border-t-2 my-4 py-4 justify-between items-center">
<div className="flex font-medium text-xs md:text-lg gap-4 md:gap-14 ">
<ul className="text-gray-6 flex flex-col gap-1">
<li>์ด๋ฆ„</li>
<li>ํ•™๋ฒˆ</li>
{displaySeveralMajor('title')}
<li>์กธ์—…ํ•„์š”ํ•™์ </li>
<li>์ด ์ด์ˆ˜ ํ•™์ </li>
<li>์กธ์—…๊ฐ€๋Šฅ์—ฌ๋ถ€</li>
</ul>
<ul className="flex flex-col gap-1">
<li>{studentNumber}</li>
<li>{studentName}</li>
{displaySeveralMajor('major')}
<li>{totalCredit}</li>
<li>{takenCredit}</li>
<li>{graduated ? '๊ฐ€๋Šฅ' : '๋ถˆ๊ฐ€๋Šฅ'}</li>
</ul>
{studentName ? (
Copy link
Member

Choose a reason for hiding this comment

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

majors๋‚˜ studentName์œผ๋กœ ๋ถ„๊ธฐ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค
let isNotInitUser = major && studentName ....
์š”๋ก  ์‹์œผ๋กœ ๋ณ€์ˆ˜๋งŒ๋“  ํ›„ isNotInitUser ? ~~~ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋” ๊ฐ€๋…์„ฑ์ด ์˜ฌ๋ผ๊ฐ€์ง€ ์•Š์„๊นŒ์š”!?

Copy link
Collaborator

Choose a reason for hiding this comment

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

  • ์–ด์ œ ์ œ๊ฐ€ ์ƒ๊ฐํ–ˆ๋˜ ๋ฐฉ์‹์€ ์‚ฌ์šฉ์ž ์ •์˜ ํƒ€์ž… ๊ฐ€๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํƒ€์ž…์„ ํ™•์ •ํ•˜๊ณ  ๋ถ„๊ธฐ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ฐธ์กฐํ•œ ๋งํฌ๋Š” ์•„๋ž˜์— ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด ์–ด๋–จ๊นŒ์š”? ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ถ”์ƒํ™”ํ•˜๋ฉด init ์ƒํ™ฉ๊ณผ ๊ทธ๋ ‡์ง€ ์•Š์€ ์ƒํ™ฉ์„ ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์žˆ์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ํƒ€์ž… ํ™•์ •์œผ๋กœ ์ธํ•ด ๋”์šฑ ๋ช…ํ™•ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards

<>
<p className="font-bold text-sm md:text-xl">
์กธ์—…ํ•„์š”ํ•™์ ๋ณด๋‹ค <span className="text-point-blue">{totalCredit - takenCredit}</span>ํ•™์ ์ด ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค.
</p>
<div className="flex border-t-2 my-4 py-4 justify-between items-center">
<div className="flex font-medium text-xs md:text-lg gap-4 md:gap-14 ">
<ul className="text-gray-6 flex flex-col gap-1">
<li>์ด๋ฆ„</li>
<li>ํ•™๋ฒˆ</li>
{displaySeveralMajor('title')}
<li>์กธ์—…ํ•„์š”ํ•™์ </li>
<li>์ด ์ด์ˆ˜ ํ•™์ </li>
<li>์กธ์—…๊ฐ€๋Šฅ์—ฌ๋ถ€</li>
</ul>
<ul className="flex flex-col gap-1">
<li>{studentNumber}</li>
<li>{studentName}</li>
{displaySeveralMajor('major')}
<li>{totalCredit}</li>
<li>{takenCredit}</li>
<li>{graduated ? '๊ฐ€๋Šฅ' : '๋ถˆ๊ฐ€๋Šฅ'}</li>
</ul>
</div>
<div className="md:px-10">
<PieChart percentage={getPercentage(takenCredit, totalCredit)} />
</div>
</div>
<p className="text-gray-6 text-xs">
* ์„œ๋น„์Šค์˜ ๊ฒฐ๊ณผ๋Š” ๊ณต์‹์ ์ธ ํšจ๋ ฅ์„ ๊ฐ–์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ •ํ™•ํ•œ ์กธ์—…์‚ฌ์ •๊ฒฐ๊ณผ๋Š” ์†Œ์† ๋‹จ๊ณผ๋Œ€ ๊ตํ•™ํŒ€์—์„œ์˜ ํ™•์ธ์„
๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.
</p>
</>
) : (
<div className="h-44 text-gray-6 flex items-center justify-center">
<p className="text-center">
์„ฑ์ ํ‘œ PDF ์—…๋กœ๋“œ๋ฅผ ํ†ตํ•ด
<br />
์กธ์—…์‚ฌ์ • ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.
</p>
</div>
<div className="md:px-10">
<PieChart percentage={getPercentage(takenCredit, totalCredit)} />
</div>
</div>
<p className="text-gray-6 text-xs">
* ์„œ๋น„์Šค์˜ ๊ฒฐ๊ณผ๋Š” ๊ณต์‹์ ์ธ ํšจ๋ ฅ์„ ๊ฐ–์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ •ํ™•ํ•œ ์กธ์—…์‚ฌ์ •๊ฒฐ๊ณผ๋Š” ์†Œ์† ๋‹จ๊ณผ๋Œ€ ๊ตํ•™ํŒ€์—์„œ์˜ ํ™•์ธ์„
๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.
</p>
)}
</>
);
}
Expand Down
Loading