-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from kakao-tech-campus-2nd-step3/Feature/이미지API-
- Loading branch information
Showing
17 changed files
with
191 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,68 @@ | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { needAuthDefaultApi } from "@api/axiosInstance"; | ||
import { externalApi, needAuthDefaultApi } from "@api/axiosInstance"; | ||
|
||
type ImageKeyPrefix = "FARM" | "FARM_REVIEW" | "PRODUCT" | "PRODUCT_REVIEW" | "MEMBER_PROFILE" | "PRODUCT_INTRO"; | ||
|
||
const getImageUpdatePresignedUrl = async (keyPrefix: ImageKeyPrefix) => | ||
needAuthDefaultApi.get("/api/s3/presigned-url-put", { params: { keyPrefix } }); | ||
|
||
const putImage2S3 = async (presignedUrl: string, image: File) => { | ||
const formData = new FormData(); | ||
formData.append("image", image as Blob); | ||
|
||
return externalApi.put(presignedUrl, formData, { | ||
headers: { | ||
"Content-Type": "multipart/form-data", | ||
}, | ||
}); | ||
}; | ||
|
||
const putImage = async (type: ImageKeyPrefix, image: File) => { | ||
const getPresignedUrlResponse = await getImageUpdatePresignedUrl(type); | ||
if (!getPresignedUrlResponse.data) { | ||
throw new Error("Failed to get presigned URL"); | ||
} | ||
const { presignedPutUrl, keyName, objectUrl } = getPresignedUrlResponse.data; | ||
const putImageResponse = await putImage2S3(presignedPutUrl, image); | ||
if (putImageResponse.status === 200) { | ||
return { | ||
imageUrl: objectUrl, | ||
objectKey: keyName, | ||
}; | ||
} | ||
throw new Error("Failed to put image"); | ||
}; | ||
|
||
type PostImageProps = { | ||
type: ImageKeyPrefix; | ||
image: File; | ||
referenceId: number; | ||
}; | ||
const usePostImage = () => { | ||
const fetcher = (img: string) => { | ||
const formData = new FormData(); | ||
formData.append("image", img); | ||
return needAuthDefaultApi.post("/api/images", formData).then(res => res.data); | ||
}; | ||
const fetcher = async ({ type, image, referenceId }: PostImageProps) => | ||
needAuthDefaultApi.post("/api/images", { | ||
...(await putImage(type, image)), | ||
type, | ||
referenceId, | ||
}); | ||
|
||
return useMutation({ | ||
mutationFn: fetcher, | ||
onSuccess: data => data, | ||
}); | ||
}; | ||
|
||
const usePostImages = () => null; | ||
const usePostImages = () => { | ||
const fetcher = async (images: PostImageProps[]) => { | ||
const promises = images.map(image => putImage(image.type, image.image)); | ||
const results = await Promise.all(promises); | ||
return needAuthDefaultApi.post("/api/images", results); | ||
}; | ||
|
||
return useMutation({ | ||
mutationFn: fetcher, | ||
onSuccess: data => data, | ||
}); | ||
}; | ||
|
||
export { usePostImage, usePostImages }; |
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 was deleted.
Oops, something went wrong.
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
35 changes: 22 additions & 13 deletions
35
src/components/features/StorePage/ProductList/ProductFilter/index.tsx
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,24 +1,33 @@ | ||
import { Divider, Flex, Text } from "@chakra-ui/react"; | ||
import { useGetProductCategories } from "@api/categoryApi"; | ||
import ProductGroupView from "@components/features/StorePage/ProductList/ProductFilter/ProductGroupView"; | ||
import { ProductFilterItem } from "@components/features/StorePage/ProductList/ProductFilter/type"; | ||
import productGroup from "@constants/productGroup"; | ||
import useFilters, { UseFilters } from "@hooks/useFilters"; | ||
import { encodeCategory } from "@utils/categoryParser"; | ||
|
||
type ProductFilterProps = { | ||
items?: ProductFilterItem[]; | ||
filterState?: UseFilters; | ||
}; | ||
|
||
const ProductFilter = ({ items = [], filterState = useFilters() }: ProductFilterProps) => ( | ||
<Flex direction="column" w="250px" h="100%" pt="10"> | ||
<Text w="full" py="5" fontSize="xl" fontWeight="bold"> | ||
필터 | ||
</Text> | ||
<Divider /> | ||
{productGroup.map(group => ( | ||
<ProductGroupView key={group.group} group={group} items={items} filterState={filterState} /> | ||
))} | ||
</Flex> | ||
); | ||
const ProductFilter = ({ filterState = useFilters() }: ProductFilterProps) => { | ||
const { data: productCategories } = useGetProductCategories(); | ||
|
||
return ( | ||
<Flex direction="column" w="250px" h="100%" pt="10"> | ||
<Text w="full" py="5" fontSize="xl" fontWeight="bold"> | ||
필터 | ||
</Text> | ||
<Divider /> | ||
{productGroup.map(group => ( | ||
<ProductGroupView | ||
key={group.group} | ||
group={group} | ||
items={(productCategories || []).map(encodeCategory)} | ||
filterState={filterState} | ||
/> | ||
))} | ||
</Flex> | ||
); | ||
}; | ||
|
||
export default ProductFilter; |
12 changes: 9 additions & 3 deletions
12
src/components/features/StorePage/ProductList/ProductsView.tsx
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,16 +1,22 @@ | ||
import { useGetProducts } from "@api/productApi"; | ||
import GridView, { GridViewProps } from "@components/ItemView/GridView"; | ||
import ProductCard from "@components/features/StorePage/ProductCard"; | ||
import mockProducts from "@mocks/mockItem/mockProducts"; | ||
import { Product } from "@type/index"; | ||
|
||
type ProductsViewProps = { | ||
filters: string[]; | ||
} & Omit<GridViewProps<Product>, "items" | "ItemComponent">; | ||
|
||
const ProductsView = ({ filters, ...props }: ProductsViewProps) => { | ||
const filteredProducts = mockProducts.filter(() => filters.length === 0); | ||
const { data: products } = useGetProducts(filters[0] ? Number(filters[0]) : 0); | ||
|
||
return <GridView items={filteredProducts} ItemComponent={ProductCard} {...props} />; | ||
return ( | ||
<GridView | ||
items={(products || []).map((p: Product) => ({ ...p, link: `/store/${p.id}` }))} | ||
ItemComponent={ProductCard} | ||
{...props} | ||
/> | ||
); | ||
}; | ||
|
||
export default ProductsView; |
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
Oops, something went wrong.