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

refactor: Applied clean code ~ chapter3 (#99) #100

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/client/src/lib/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@ export const validateUser = async (
// user validation OK.
return true;
} else {
console.error("TOKEN is not valid!");
return false;
}
} catch (e) {
console.error("ERROR in `users/validate/");
console.log(e.response);
const errResponse = e.response.data;
if (errResponse.code === -21) {
const refreshResult = await refresh();
Expand Down
102 changes: 55 additions & 47 deletions src/client/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const Home: FunctionComponent = () => {
const router = useRouter();
const { user, setUser } = useContext(UserContext);

const [isOpen, setIsOpen] = useState<boolean>(false);
const [pickedProductInfo, setPickedProductInfo] =
const [isOpenWishModal, setIsOpenWishModal] = useState<boolean>(false);
const [userClickedProduct, setUserClickedProduct] =
useState<HomeProductInfoRes>();

const { data: homeData, mutate } = useSWR<HomeRes>(
Expand All @@ -44,27 +44,29 @@ const Home: FunctionComponent = () => {
},
);

const onHandleClickWish = useCallback(async (obj: HomeProductInfoRes) => {
try {
const res = await validateUser(setUser);
if (!res) {
// user Not logined.
const onHandleUserClickedWish = useCallback(
async (clickedProduct: HomeProductInfoRes) => {
try {
const res = await validateUser(setUser);
if (!res) {
router.push("/login");
} else {
setIsOpenWishModal(true);
setUserClickedProduct(clickedProduct);
}
} catch (e) {
router.push("/login");
} else {
setIsOpen(true);
setPickedProductInfo(obj);
}
} catch (e) {
router.push("/login");
}
}, []);
},
[],
);

const onPostWish = useCallback(
async (size: string) => {
const token = getToken("accessToken");
try {
const res = await axios.post(
`${process.env.END_POINT_PRODUCT}/wish/${pickedProductInfo.id}/${size}`,
`${process.env.END_POINT_PRODUCT}/wish/${userClickedProduct.id}/${size}`,
{},
{
headers: {
Expand All @@ -78,36 +80,42 @@ const Home: FunctionComponent = () => {
`Something wrong when posting. error code ${res.status}}`,
);
} else {
if (
pickedProductInfo.wishList &&
pickedProductInfo.wishList.includes(size)
) {
setPickedProductInfo({
...pickedProductInfo,
wishList: [
...pickedProductInfo.wishList.filter((data) => data !== size),
],
});
} else {
if (pickedProductInfo.wishList !== null) {
setPickedProductInfo({
...pickedProductInfo,
wishList: [...pickedProductInfo.wishList, size],
});
} else {
setPickedProductInfo({ ...pickedProductInfo, wishList: [size] });
}
}
toggleUserClickWish(size);
mutate();
}
} catch (err) {
console.error(err);
// errResponse.code && alert(`${process.env.ERROR_code[parseInt(errResponse.code)]}`);
}
},
[pickedProductInfo],
[userClickedProduct],
);

const toggleUserClickWish = (size: string) => {
if (
userClickedProduct.wishList &&
userClickedProduct.wishList.includes(size)
) {
setUserClickedProduct({
...userClickedProduct,
wishList: [
...userClickedProduct.wishList.filter((data) => data !== size),
],
});
} else {
userClickedProduct.wishList !== null &&
setUserClickedProduct({
...userClickedProduct,
wishList: [...userClickedProduct.wishList, size],
});
userClickedProduct.wishList === null &&
setUserClickedProduct({
...userClickedProduct,
wishList: [size],
});
}
};

useEffect(() => {
mutate();
}, [user]);
Expand Down Expand Up @@ -145,7 +153,7 @@ const Home: FunctionComponent = () => {
productInfos={
homeData.recommendedItems ? homeData.recommendedItems : []
}
onHandleWishClick={onHandleClickWish}
onHandleWishClick={onHandleUserClickedWish}
/>
)}
{homeData &&
Expand All @@ -157,23 +165,23 @@ const Home: FunctionComponent = () => {
header={product.header}
detail={product.detail}
productInfo={product.products}
onHandleWishClick={onHandleClickWish}
onHandleWishClick={onHandleUserClickedWish}
/>
))}
</HomeTemplate>
<Modal
category="wish"
onClose={() => setIsOpen(false)}
show={isOpen}
onClose={() => setIsOpenWishModal(false)}
show={isOpenWishModal}
title="관심 상품 추가"
>
{pickedProductInfo && (
{userClickedProduct && (
<>
<ProductSmallInfo
imgSrc={pickedProductInfo.imageUrls[0]}
backgroundColor={pickedProductInfo.backgroundColor}
productName={pickedProductInfo.originalName}
productNameKor={pickedProductInfo.translatedName}
imgSrc={userClickedProduct.imageUrls[0]}
backgroundColor={userClickedProduct.backgroundColor}
productName={userClickedProduct.originalName}
productNameKor={userClickedProduct.translatedName}
style={{
borderBottom: "1px solid #eeeeee",
paddingBottom: "10px",
Expand All @@ -183,9 +191,9 @@ const Home: FunctionComponent = () => {
<ProductSizeSelectGrid
category="wish"
activeSizeOption={
pickedProductInfo.wishList && pickedProductInfo.wishList
userClickedProduct.wishList && userClickedProduct.wishList
}
datas={pickedProductInfo.sizes}
datas={userClickedProduct.sizes}
onClick={(size) => onPostWish(size)}
/>
</>
Expand Down