Skip to content

Commit

Permalink
이미지 뷰어 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
HyunJinNo committed Jul 21, 2024
1 parent 4d2a4d8 commit 716c5f7
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 81 deletions.
81 changes: 81 additions & 0 deletions src/components/informations/detail/ImageList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { useDragScrollType } from "@/hooks/useDragScroll";
import Image from "next/image";
import { Dispatch, SetStateAction } from "react";
import ImageViewer from "./ImageViewer";

interface Props {
scrollHook: useDragScrollType;
images: Array<Readonly<{ imageStatus: string; address: string }>>;
mainImageUrl: string;
viewerVisible: boolean;
setMainImageUrl: Dispatch<SetStateAction<string>>;
openViewer: () => void;
closeViewer: () => void;
}

const ImageList = ({
scrollHook,
images,
mainImageUrl,
viewerVisible,
setMainImageUrl,
openViewer,
closeViewer,
}: Props) => {
return (
<div className="dark:opacity-65">
{viewerVisible && (
<ImageViewer imageUrl={mainImageUrl} closeViewer={closeViewer} />
)}
<div className="relative h-[26.0625rem] w-full max-[744px]:h-[19.125rem]">
<Image
className="cursor-pointer rounded-2xl border-[0.0625rem] hover:border-main"
src={mainImageUrl}
alt={"/background"}
fill={true}
style={{
objectFit: "cover",
}}
onClick={() => openViewer()}
/>
</div>
<div
className="flex flex-row items-center gap-[0.875rem] overflow-x-auto pt-[0.875rem]"
ref={scrollHook.listRef}
onMouseDown={(e) => {
e.preventDefault();
scrollHook.onDragStart(e);
}}
onMouseMove={scrollHook.onDragMove}
onMouseUp={scrollHook.onDragEnd}
onMouseLeave={scrollHook.onDragEnd}
onTouchStart={scrollHook.onTouchStart}
onTouchMove={scrollHook.onTouchMove}
onTouchEnd={scrollHook.onTouchEnd}
>
{images.map((image, index) => (
<div
key={index}
className="relative h-[6.6875rem] w-[6.6875rem] rounded-lg border-[0.0625rem]"
onClick={() => {
setMainImageUrl(image.address);
}}
onTouchEnd={() => {
setMainImageUrl(image.address);
}}
>
<Image
className="cursor-pointer rounded-lg"
src={image.address}
alt={"/background"}
fill={true}
style={{ objectFit: "cover" }}
/>
</div>
))}
</div>
</div>
);
};

export default ImageList;
71 changes: 17 additions & 54 deletions src/components/informations/detail/ImageViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,63 +1,26 @@
import { useDragScrollType } from "@/hooks/useDragScroll";
import Image from "next/image";
import { MdClose } from "react-icons/md";

interface Props {
images: Array<Readonly<{ imageStatus: string; address: string }>>;
mainImageIndex: number;
scrollHook: useDragScrollType;
setMainImageIndex: (index: number) => void;
imageUrl: string;
closeViewer: () => void;
}

const ImageViewer = ({ images, scrollHook, setMainImageIndex }: Props) => {
const ImageViewer = ({ imageUrl, closeViewer }: Props) => {
return (
<div className="dark:opacity-65">
<div className="relative h-[26.0625rem] w-full text-slate-200 max-[744px]:h-[19.125rem]">
<Image
className="rounded-2xl border-[0.0625rem]"
src={
images.filter((image) => image.imageStatus === "썸네일")[0].address
}
alt={"/background"}
fill={true}
style={{
objectFit: "cover",
}}
/>
</div>
<div
className="flex flex-row items-center gap-[0.875rem] overflow-x-auto pt-[0.875rem]"
ref={scrollHook.listRef}
onMouseDown={(e) => {
e.preventDefault();
scrollHook.onDragStart(e);
}}
onMouseMove={scrollHook.onDragMove}
onMouseUp={scrollHook.onDragEnd}
onMouseLeave={scrollHook.onDragEnd}
onTouchStart={scrollHook.onTouchStart}
onTouchMove={scrollHook.onTouchMove}
onTouchEnd={scrollHook.onTouchEnd}
>
{images.map((image, index) => (
<div
key={index}
className="relative h-[6.6875rem] w-[6.6875rem] rounded-lg border-[0.0625rem]"
onClick={() => {
setMainImageIndex(index);
}}
onTouchEnd={() => {
setMainImageIndex(index);
}}
>
<Image
className="cursor-pointer rounded-lg"
src={image.address}
alt={"/background"}
fill={true}
style={{ objectFit: "cover" }}
/>
</div>
))}
<div className="z-top fixed left-0 top-0 z-50 flex h-full w-full items-center justify-center bg-black/25">
<div className="relative flex h-[calc(100%_-_48px)] w-[calc(100%_-_48px)] flex-col gap-3 rounded-2xl bg-gray-900 p-6">
<div className="absolute right-5 top-4 cursor-pointer self-end rounded-md text-white hover:text-main dark:bg-slate-600">
<MdClose size={"2rem"} onClick={() => closeViewer()} />
</div>
<div className="relative mt-8 h-full w-full">
<Image
src={imageUrl}
alt="image"
fill={true}
style={{ objectFit: "contain" }}
/>
</div>
</div>
</div>
);
Expand Down
4 changes: 2 additions & 2 deletions src/components/informations/detail/InformationViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import Image from "next/image";
import { FaRegHeart } from "react-icons/fa";
import { TiLocation } from "react-icons/ti";
import KakaoMapLinkContainer from "@/containers/common/KakaoMapLinkContainer";
import ImageViewerContainer from "@/containers/informations/detail/ImageViewerContainer";
import { LuEye } from "react-icons/lu";
import { InformationDetailDto } from "@/types/InformationDto";
import ButtonListContainer from "@/containers/informations/detail/ButtonListContainer";
import ImageListContainer from "@/containers/informations/detail/ImageListContainer";

async function getInformation(id: number) {
const response = await fetch(
Expand Down Expand Up @@ -70,7 +70,7 @@ const InformationViewer = async ({ informationId }: Props) => {
</div>
</div>
<div className="h-[34.5rem] w-[29.375rem] max-[1024px]:w-full max-[744px]:h-[27.5625rem]">
<ImageViewerContainer images={data.imageResponses} />
<ImageListContainer images={data.imageResponses} />
</div>
<div className="flex h-[34.5rem] w-[29.375rem] flex-col overflow-y-auto px-[1.25rem] max-[1024px]:h-fit max-[1024px]:w-full max-[1024px]:px-0 max-[1024px]:pt-8">
<div className="max-[1024px]:hidden">
Expand Down
31 changes: 31 additions & 0 deletions src/containers/informations/detail/ImageListContainer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use client";

import ImageList from "@/components/informations/detail/ImageList";
import useDragScroll from "@/hooks/useDragScroll";
import { useState } from "react";

interface Props {
images: Array<Readonly<{ imageStatus: string; address: string }>>;
}

const ImageListContainer = ({ images }: Props) => {
const scrollHook = useDragScroll();
const [mainImageUrl, setMainImageUrl] = useState<string>(
images.find((image) => image.imageStatus === "썸네일")?.address ?? "",
);
const [viewerVisible, setViewerVisible] = useState<boolean>(false);

return (
<ImageList
scrollHook={scrollHook}
images={images}
mainImageUrl={mainImageUrl}
viewerVisible={viewerVisible}
setMainImageUrl={setMainImageUrl}
openViewer={() => setViewerVisible(true)}
closeViewer={() => setViewerVisible(false)}
/>
);
};

export default ImageListContainer;
25 changes: 0 additions & 25 deletions src/containers/informations/detail/ImageViewerContainer.tsx

This file was deleted.

0 comments on commit 716c5f7

Please sign in to comment.