Skip to content

Commit

Permalink
feat: added notification push
Browse files Browse the repository at this point in the history
  • Loading branch information
Molaryy committed Oct 3, 2023
1 parent 10825a5 commit ff7e60b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Bective</title>
<link rel="icon" href="/assets/bective-logo.png">
</head>
<body>
<div id="root"></div>
Expand Down
Binary file added public/assets/bective-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/assets/behance_logo_icon_257018.ico
Binary file not shown.
Binary file added public/assets/logo-bective.ico
Binary file not shown.
Binary file added public/assets/logo-bective.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions src/components/BreakTime.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useEffect, useState } from 'react'

const requestNotificationPermission = () => {
if (Notification.permission !== 'granted') {
Notification.requestPermission().then((permission: NotificationPermission) => {
if (permission !== 'granted') {
console.error('Notification permission granted.')
}
})
}
}

const sendNotification = () => {
const [canSendNotification, setCanSendNotification] = useState(true)

if (Notification.permission === 'granted' && canSendNotification) {
new Notification('Take a break time')
setCanSendNotification(false)
}
}

const BreakTime = () => {
useEffect(() => {
requestNotificationPermission()
}, [])
sendNotification()
return <></>
}

export default BreakTime
20 changes: 18 additions & 2 deletions src/components/Form/TimerFormHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FormEvent, useEffect, useState } from 'react'
import TimerForm from './TimerForm.tsx'
import pauseSound from '/assets/5-minutes-lofi.mp3'
import workSound from '/assets/Wakey-Wakey.mp3'
import BreakTime from '../BreakTime.tsx'

const TimerFormHandler = () => {
const [workTime, setWorkTime] = useState<string[]>(['00', '00', '00'])
Expand All @@ -12,6 +13,7 @@ const TimerFormHandler = () => {
const [startWorkSound, setStartWorkSound] = useState<boolean[]>([false, false])
const [storeWorkTime, setStoreWorkTime] = useState<string[] | null>(null)
const [storePauseTime, setStorePauseTime] = useState<string[] | null>(null)
const [alertNotification, setAlertNotification] = useState(false)

const handleFormSubmit = (event: FormEvent): void => {
event.preventDefault()
Expand All @@ -36,7 +38,7 @@ const TimerFormHandler = () => {
const handleContinueTimer = (): void => {
setIsWorkTimerRunning(workTime.toString() != storeWorkTime?.toString())
setIsPauseTimerRunning(pauseTime.toString() != storePauseTime?.toString())
!startPauseSound ? setStartPauseSound(true) : setStartPauseSound(false)
isWorkTimerRunning ? setStartPauseSound(false) : setStartPauseSound(true)
}

useEffect(() => {
Expand All @@ -49,6 +51,13 @@ const TimerFormHandler = () => {
return
}

if (alertNotification) {
setAlertNotification(false)
}
if (isPauseTimerRunning && !alertNotification) {
setAlertNotification(true)
}

const timeInterval: number = setInterval(() => {
if (isPauseTimerRunning) {
setStartWorkSound([true, false])
Expand Down Expand Up @@ -96,7 +105,14 @@ const TimerFormHandler = () => {
isWorking={isWorkTimerRunning}
isInPause={isPauseTimerRunning}
/>
{startPauseSound ? <audio controls src={pauseSound} autoPlay hidden /> : <></>}
{startPauseSound ? (
<>
<BreakTime />
<audio controls src={pauseSound} autoPlay hidden />
</>
) : (
<></>
)}
{startWorkSound[0] && startWorkSound[1] ? (
<audio controls src={workSound} autoPlay hidden />
) : (
Expand Down

0 comments on commit ff7e60b

Please sign in to comment.