-
Notifications
You must be signed in to change notification settings - Fork 2
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 #379 from gsainfoteam/342-feature-push-noti-is-now…
…-delayed-15-min
- Loading branch information
Showing
10 changed files
with
209 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
}, | ||
"editor.formatOnSave": false, | ||
"cSpell.words": [ | ||
"Swal", | ||
"Zabo", | ||
"Ziggle" | ||
], | ||
|
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,5 @@ | ||
import { ziggleApi } from '..'; | ||
import type { Notice } from './notice'; | ||
|
||
export const sendNoticeAlarm = ({ id }: Pick<Notice, 'id'>) => | ||
ziggleApi.post(`/notice/${id}/alarm`).then((res) => res.data); |
152 changes: 152 additions & 0 deletions
152
.../[lng]/(with-page-layout)/(with-sidebar-layout)/notice/[id]/SendPushNotificationAlert.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 |
---|---|---|
@@ -0,0 +1,152 @@ | ||
'use client'; | ||
|
||
import { useSession } from 'next-auth/react'; | ||
import { useCallback, useEffect, useMemo, useState } from 'react'; | ||
import Swal from 'sweetalert2'; | ||
|
||
import { NoticeDetail } from '@/api/notice/notice'; | ||
import { sendNoticeAlarm } from '@/api/notice/send-alarm'; | ||
import { PropsWithLng } from '@/app/i18next'; | ||
import { useTranslation } from '@/app/i18next/client'; | ||
|
||
import { getTimeDiff } from './getTimeDiff'; | ||
|
||
interface SendPushAlarmProps | ||
extends Pick<NoticeDetail, 'id' | 'author' | 'publishedAt'> {} | ||
|
||
const SendPushAlarm = ({ | ||
id, | ||
author, | ||
publishedAt, | ||
lng, | ||
}: PropsWithLng<SendPushAlarmProps>): JSX.Element | null => { | ||
const { t } = useTranslation(lng); | ||
|
||
const [isManuallyAlarmed, setIsManuallyAlarmed] = useState(false); | ||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
const handleSendPushNotification = useCallback(async () => { | ||
if (isLoading) return; | ||
const result = await Swal.fire({ | ||
text: t('write.alerts.sendPushNotice'), | ||
icon: 'question', | ||
showCancelButton: true, | ||
confirmButtonText: t('alertResponse.confirm'), | ||
cancelButtonText: t('alertResponse.cancel'), | ||
}); | ||
|
||
if (!result.isConfirmed) return; | ||
setIsLoading(true); | ||
|
||
try { | ||
Swal.fire({ | ||
text: t('write.alerts.sendingAlarmNotice'), | ||
icon: 'info', | ||
showConfirmButton: false, | ||
allowOutsideClick: false, | ||
}); | ||
|
||
const newNotice = await sendNoticeAlarm({ id }).catch(() => null); | ||
|
||
if (!newNotice) throw new Error('No newNotice returned'); | ||
|
||
setIsManuallyAlarmed(true); | ||
|
||
Swal.fire({ | ||
text: t('write.alerts.sendPushNoticeSuccess'), | ||
icon: 'success', | ||
confirmButtonText: t('alertResponse.confirm'), | ||
}); | ||
} catch (error) { | ||
Swal.fire({ | ||
text: t('write.alerts.sendPushNoticeFail'), | ||
icon: 'error', | ||
confirmButtonText: t('alertResponse.confirm'), | ||
}); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}, [t, id, isLoading]); | ||
|
||
const { data: user } = useSession(); | ||
const isMyNotice = user?.user.uuid === author.uuid; | ||
|
||
const MINUTE = 60000; | ||
const TEN_SECONDS = 10000; | ||
const ONE_SECOND = 1000; | ||
|
||
const getIntervalDuration = (minutes: number, seconds: number) => { | ||
if (minutes > 1) return MINUTE; | ||
if (seconds > 15) return TEN_SECONDS; | ||
return ONE_SECOND; | ||
}; | ||
|
||
const [timeRemaining, setTimeRemaining] = useState(getTimeDiff(publishedAt)); | ||
useEffect(() => { | ||
let isSubscribed = true; | ||
if ( | ||
isManuallyAlarmed || | ||
timeRemaining.minutes < 0 || | ||
timeRemaining.seconds < 0 | ||
) { | ||
return; | ||
} | ||
|
||
const intervalDuration = getIntervalDuration( | ||
timeRemaining.minutes, | ||
timeRemaining.seconds | ||
); | ||
|
||
const interval = setInterval(() => { | ||
if (isSubscribed) { | ||
const newTimeRemaining = getTimeDiff(publishedAt); | ||
if ( | ||
newTimeRemaining.minutes !== timeRemaining.minutes || | ||
newTimeRemaining.seconds !== timeRemaining.seconds | ||
) { | ||
setTimeRemaining(newTimeRemaining); | ||
} | ||
} | ||
}, intervalDuration); | ||
|
||
return () => { | ||
isSubscribed = false; | ||
clearInterval(interval); | ||
}; | ||
}, [timeRemaining, publishedAt, isManuallyAlarmed]); | ||
|
||
const isEditable = timeRemaining.minutes >= 0 && timeRemaining.seconds >= 0; | ||
|
||
const showComponent = useMemo( | ||
() => isMyNotice && isEditable && !isManuallyAlarmed, | ||
[isMyNotice, isEditable, isManuallyAlarmed], | ||
); | ||
|
||
return ( | ||
<div | ||
className={`transform transition-all duration-1000 ease-in-out ${ | ||
showComponent ? 'max-h-screen' : 'max-h-0 overflow-hidden' | ||
}`} | ||
> | ||
<div | ||
className={`inline-flex w-full items-start justify-start gap-1.5 rounded-[15px] bg-[#fff4f0] px-5 py-[15px] font-normal text-primary`} | ||
> | ||
<span>{t('zabo.sentPushNotificationAlert.title')} </span> | ||
<span | ||
className="cursor-pointer font-bold underline" | ||
onClick={handleSendPushNotification} | ||
tabIndex={0} | ||
onKeyDown={(e) => { | ||
if (e.key === 'Enter' || e.key === ' ') { | ||
handleSendPushNotification(); | ||
} | ||
}} | ||
> | ||
{t('zabo.sentPushNotificationAlert.action')} | ||
</span> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SendPushAlarm; |
14 changes: 14 additions & 0 deletions
14
src/app/[lng]/(with-page-layout)/(with-sidebar-layout)/notice/[id]/getTimeDiff.ts
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,14 @@ | ||
import dayjs, { Dayjs } from 'dayjs'; | ||
|
||
export const isServer = typeof window === 'undefined'; | ||
|
||
const CLIENT_SERVER_TIME_OFFSET_SECONDS = 10 | ||
export const getTimeDiff = (createdAt: Dayjs | string) => { | ||
const currentTime = dayjs(); | ||
const diffInSeconds = dayjs(createdAt) | ||
.subtract(CLIENT_SERVER_TIME_OFFSET_SECONDS, 'second') | ||
.diff(currentTime, 'second'); | ||
const minutes = Math.floor(diffInSeconds / 60); | ||
const seconds = diffInSeconds % 60; | ||
return { minutes, seconds }; | ||
}; |
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