-
Notifications
You must be signed in to change notification settings - Fork 20
[김희진] sprint 8 #72
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
The head ref may contain hidden characters: "React-\uAE40\uD76C\uC9C4-sprint4"
[김희진] sprint 8 #72
Changes from all commits
a6078af
bc52a21
1bfc745
5aeb67b
03cae43
e91dbc4
ed3d169
a2378cc
f509000
f6bf6a4
1461f32
7c820a5
c42c8da
ae862ec
d9eb65f
9a4c9ee
a278c7b
05d1ef2
a672e6c
fe89ba4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import api from "./index"; | ||
|
|
||
| export interface Comment { | ||
| id: number; | ||
| content: string; | ||
| createdAt: string; | ||
| updatedAt: string; | ||
| writer: { | ||
| id: number; | ||
| image: string | null; | ||
| nickname: string; | ||
| }; | ||
| } | ||
|
|
||
| export async function getComments( | ||
| productId: string, | ||
| limit: number = 3 | ||
| ): Promise<Comment[]> { | ||
| const response = await api.get( | ||
| `/products/${productId}/comments?limit=${limit}` | ||
| ); | ||
|
|
||
| return response.data.list; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import axios from "axios"; | ||
|
|
||
| const BASE_URL = process.env.REACT_APP_BASE_URL; | ||
|
|
||
| const api = axios.create({ | ||
| baseURL: BASE_URL, | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| }); | ||
|
|
||
| export default api; |
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,40 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { AddItem } from "../components/pages/AddItemPage/AddItemPage"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import api from "./index"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| export interface Items { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| id: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| images: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| price: number; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| favoriteCount: number; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| interface Params { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| page: number; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| pageSize: number; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| orderBy: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| keyword: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| export interface DetailItem extends Omit<AddItem, "images"> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| images: string | null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| favoriteCount: number; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| createdAt: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| updatedAt: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ownerNickname: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| export async function getProducts(params: Params) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { page, pageSize, orderBy, keyword } = params; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const response = await api.get(`/products`, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| params: { page, pageSize, orderBy, keyword }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| return response.data; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+27
to
+34
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 함수 내부는 다음과 같이 작성해볼 수 있어요:
Suggested change
희진님께서 getProducts({
page: 1,
pageSize: 4,
orderBy: "favorite",
keyword: "",
}).then((result: BestItem) => {
if (!result) return;
const sortedBestItems = [...result.list].slice(0, 4);
setBestItems(sortedBestItems);
});
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 외 고려해야할 점현재 제 코드는
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (참조) 변경 코드 전문:diff --git a/src/api/products.tsx b/src/api/products.tsx
index 42c1f45..9c3e19a 100644
--- a/src/api/products.tsx
+++ b/src/api/products.tsx
@@ -1,3 +1,5 @@
+import { AllItem } from "../components/Items/AllItems/AllItems";
+import { BestItem } from "../components/Items/BestItems/BestItems";
import { AddItem } from "../components/pages/AddItemPage/AddItemPage";
import api from "./index";
@@ -9,13 +11,20 @@ export interface Items {
favoriteCount: number;
}
-interface Params {
+interface RecentParams {
page: number;
pageSize: number;
- orderBy: string;
+ orderBy: "recent" | "favorite";
keyword: string;
}
+interface FavoriteParams {
+ page: number;
+ pageSize: number;
+ orderBy: "favorite";
+ keyword: "";
+}
+
export interface DetailItem extends Omit<AddItem, "images"> {
images: string | null;
favoriteCount: number;
@@ -24,9 +33,20 @@ export interface DetailItem extends Omit<AddItem, "images"> {
ownerNickname: string;
}
-export async function getProducts(params: Params) {
+export async function getProducts(params: RecentParams): Promise<AllItem>;
+export async function getProducts(params: FavoriteParams): Promise<BestItem>;
+export async function getProducts(params: RecentParams | FavoriteParams) {
const { page, pageSize, orderBy, keyword } = params;
- const response = await api.get(`/products`, {
+
+ if (orderBy === "favorite" && keyword === "") {
+ const response = await api.get<BestItem>(`/products`, {
+ params: { page, pageSize, orderBy, keyword },
+ });
+
+ return response.data;
+ }
+
+ const response = await api.get<AllItem>(`/products`, {
params: { page, pageSize, orderBy, keyword },
});
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 추가로, 오버로딩과 관련된 예시 코드를 작성해봤어요 ! 한 번 확인해보세요 😊:interface Cat {
status: "live" | "deaded"
inWater: false
}
interface Fish {
status: "live" | "deaded"
inWater: true
}
type CatResponse = "is Cat"
type FishResponse = "is Fish"
function getAnimal(param: Cat): CatResponse
function getAnimal(param: Fish): FishResponse
function getAnimal(param: Cat | Fish) {
if (param.inWater) {
return "is Fish"
}
return "is Cat"
}
const who = getAnimal({
status: "live",
inWater: false
}) // who의 타입은 "is Cat" |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| export async function getProductInfo(productId: string): Promise<DetailItem> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const response = await api.get(`/products/${productId}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| return response.data; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
근본적인 문제는 "모호함" 이라고 사료됩니다.
같은 엔드포인트이지만
favorite,recent에 따라 기대하는 반환값이 다르기에 생겨난 문제로 보입니다 !기능의 목적이 다르며, 기대하는 반환되는 값도 다릅니다.
이럴 때 'A' 값이 들어오면 'AA' 값이 반환됨을 컴파일 단계에서 확정을 해줘야 하는데.
현재는 'A' 값이 들어오면 'AA'이거나 'BB'일 수도 있습니다.
이럴 때는 타입 가드를 통하여 타입을 확정지을 수도 있겠으나,
오버로딩(
Overloading)을 통하여 해결할 수도 있어요.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
다음과 같이 작성해볼 수 있습니다:
이를 테면,
getProducts가 "어떤 값"을 받는지에 따라 반환되는 타입을 명시할 수 있습니다.