-
Notifications
You must be signed in to change notification settings - Fork 3
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 #180 from Codeit-Part4-SFJs/jh/modal
feat: 비교하기 페이지 공유 버튼 구현
- Loading branch information
Showing
2 changed files
with
113 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
'use client'; | ||
|
||
import { useState } from 'react'; | ||
import { Icon } from '@/shared/ui/Icon'; | ||
import { Toast } from '@/components/@common'; | ||
import { usePathname } from 'next/navigation'; | ||
import { ProductDetailData } from './types'; | ||
|
||
declare global { | ||
// eslint-disable-next-line no-unused-vars | ||
interface Window { | ||
Kakao: any; | ||
} | ||
} | ||
|
||
interface ShareIconProps { | ||
product1: ProductDetailData; | ||
product2: ProductDetailData; | ||
} | ||
|
||
export const ShareIcon = ({ product1, product2 }: ShareIconProps) => { | ||
const pathname = usePathname(); | ||
|
||
const url = `${process.env.NEXT_PUBLIC_FE_URL}${pathname}?product1=${product1.id}&product2=${product2.id}`; | ||
|
||
const handleKakaoClick = () => { | ||
if (!window.Kakao?.isInitialized()) { | ||
window.Kakao.init(process.env.NEXT_PUBLIC_KAKAO_JAVASCRIPT_KEY); | ||
} | ||
|
||
window.Kakao.Share.sendDefault({ | ||
objectType: 'feed', | ||
content: { | ||
title: 'WDYTA', | ||
description: `지금 ${product1.name} 상품과 ${product2.name} 상품을 비교해보세요`, | ||
imageUrl: | ||
'https://sprint-fe-project.s3.ap-northeast-2.amazonaws.com/Mogazoa/user/168/1719131911147/123IMG_3465.png', | ||
link: { | ||
mobileWebUrl: url, | ||
webUrl: url, | ||
}, | ||
}, | ||
buttons: [ | ||
{ | ||
title: '상품 비교하러 가기', | ||
link: { | ||
mobileWebUrl: url, | ||
webUrl: url, | ||
}, | ||
}, | ||
], | ||
}); | ||
}; | ||
|
||
const [copyUrlStatus, setCopyUrlStatus] = useState('idle'); | ||
|
||
const handleUrlButtonClick = async (copyUrl: string) => { | ||
setCopyUrlStatus('fetching'); | ||
try { | ||
await navigator.clipboard.writeText(copyUrl); | ||
setCopyUrlStatus('success'); | ||
setTimeout(() => { | ||
setCopyUrlStatus('idle'); | ||
}, 3000); | ||
} catch (error) { | ||
setCopyUrlStatus('error'); | ||
setTimeout(() => { | ||
setCopyUrlStatus('idle'); | ||
}, 3000); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="relative flex shrink-0 gap-[10px]"> | ||
<button | ||
onClick={handleKakaoClick} | ||
className="flex justify-center items-center bg-black-25 rounded-md mobile:w-[24px] mobile:h-[24px] md:w-[24px] md:h-[24px] lg:w-[28px] lg:h-[28px]" | ||
type="button" | ||
> | ||
<Icon | ||
name="KakaoIcon" | ||
className="mobile:w-[14px] mobile:h-[14px] md:w-[14px] md:h-[14px] lg:w-[18px] lg:h-[18px] fill-gray-6E" | ||
/> | ||
</button> | ||
<button | ||
className="flex justify-center items-center bg-black-25 rounded-md mobile:w-[24px] mobile:h-[24px] md:w-[24px] md:h-[24px] lg:w-[28px] lg:h-[28px] disabled:cursor-not-allowed" | ||
type="button" | ||
disabled={copyUrlStatus !== 'idle'} | ||
onClick={() => handleUrlButtonClick(url)} | ||
> | ||
<Icon | ||
name="ShareIcon" | ||
className="mobile:w-[14px] mobile:h-[14px] md:w-[14px] md:h-[14px] lg:w-[18px] lg:h-[18px] fill-gray-6E" | ||
/> | ||
</button> | ||
{copyUrlStatus === 'success' && ( | ||
<div className="absolute mobile:top-[-70px] right-0 md:top-[-70px] lg:top-[-80px]"> | ||
<Toast text="URL이 복사되었습니다" /> | ||
</div> | ||
)} | ||
{copyUrlStatus === 'error' && ( | ||
<div className="absolute mobile:top-[-70px] right-0 md:top-[-70px] lg:top-[-80px]"> | ||
<Toast text="URL 복사에 실패했습니다" /> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; |