-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 서비스로직을 훅안으로 들고가서 훅에서 주는 함수로 모든 행동 처리하도록 변경 - 로더분리
- Loading branch information
Showing
13 changed files
with
122 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,13 @@ | ||
import { addProduct, uploadProductImage } from "@service/product"; | ||
import { PageWrapper } from "@components/Page"; | ||
import ProductForm from "./components/ProductForm"; | ||
import useProductActions from "./components/useProductActions"; | ||
|
||
export default function ItemAddPage() { | ||
async function handleSubmit(data) { | ||
try { | ||
if (data.images) { | ||
const imgFormData = new FormData(); | ||
imgFormData.append("image", data.images); | ||
|
||
const { url } = await uploadProductImage(imgFormData); | ||
data.images = [url]; | ||
} | ||
|
||
await addProduct(data); | ||
} catch (err) { | ||
throw err; | ||
} | ||
} | ||
const { handleProductAdd } = useProductActions(); | ||
|
||
return ( | ||
<PageWrapper> | ||
<ProductForm onProductSubmit={handleSubmit} /> | ||
<ProductForm onProductSubmit={handleProductAdd} /> | ||
</PageWrapper> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { getProduct } from "@service/product"; | ||
import { getComments } from "@service/comments"; | ||
|
||
export default async function loader({ params }) { | ||
const { id } = params; | ||
|
||
// 1. 첫번째 시도 : Promise.all | ||
// 두 요청이 병렬로 시작은 가능하나, 두 요청이 모두 끝나야 반환하므로, 원하는 케이스가 아님 | ||
|
||
// 2. 두번째 시도 : 하나만 await | ||
// getProduct를 먼저 await하고 getProdcutComments는 promise상태로 담아서 보내기 | ||
// getProduct의 await 때문에 코멘트 데이터 요청이 동시에 이뤄지지가 않음 (서비스 호출함수에서 임의로 시간지연으로 테스트해봄) | ||
//const detail = await getProduct(id); | ||
//const comments = getProductComments({ productId: id, limit: 5 }); | ||
|
||
// 3. 해결한 방법 | ||
// 두 요청을 거의 동시에 보내도록 우선 await없이 promise를 각 변수에 할당함 | ||
// 여기서 미리 await을 작성하면 그 요청을 기다린후 다음줄의 코드가 실행되어서 동시에 요청이 안됨 | ||
const detail = getProduct(id); | ||
const comments = getComments("products", { productId: id, limit: 5 }); | ||
|
||
// detail에 담긴 promise를 기다린후에 리턴 | ||
return { detail: await detail, comments }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { getProduct } from "@service/product"; | ||
|
||
export default async function loader({ params }) { | ||
const { id } = params; | ||
const detail = await getProduct(id); | ||
|
||
return { detail }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { | ||
addProduct, | ||
deleteProduct, | ||
modifyProduct, | ||
toggleLike, | ||
uploadProductImage, | ||
} from "@service/product"; | ||
|
||
export default function useProductActions(productId) { | ||
async function handleLike(flag) { | ||
return toggleLike(productId, flag); | ||
} | ||
|
||
async function handleProductAdd(formData) { | ||
try { | ||
if (formData.images) { | ||
const imgFormData = new FormData(); | ||
imgFormData.append("image", formData.images); | ||
|
||
const { url } = await uploadProductImage(imgFormData); | ||
formData.images = [url]; | ||
} | ||
|
||
await addProduct(formData); | ||
} catch (err) { | ||
throw err; | ||
} | ||
} | ||
|
||
async function handleProductModify(prevData, formData) { | ||
try { | ||
if (formData.images !== prevData.images[0]) { | ||
const imgFormData = new FormData(); | ||
imgFormData.append("image", formData.images); | ||
|
||
const { url } = await uploadProductImage(imgFormData); | ||
formData.images = [url]; | ||
} | ||
|
||
await modifyProduct(productId, formData); | ||
} catch (err) { | ||
throw err; | ||
} | ||
} | ||
|
||
async function handleProductDelete() { | ||
return deleteProduct(productId); | ||
} | ||
|
||
return { | ||
handleLike, | ||
handleProductAdd, | ||
handleProductModify, | ||
handleProductDelete, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters