| 3358 |
stevensc |
1 |
import React, { useEffect, useState } from 'react'
|
|
|
2 |
import { useDispatch } from 'react-redux'
|
|
|
3 |
import { Backdrop } from '@mui/material'
|
|
|
4 |
|
|
|
5 |
import { useHabitsUrls } from '@hooks'
|
|
|
6 |
import { useDailyLogCategories } from './use-daily-log-categories'
|
|
|
7 |
import { savePreferences } from '@services/habits/daily-log'
|
|
|
8 |
import { addNotification } from '@store/notification/notification.actions'
|
|
|
9 |
|
|
|
10 |
import Widget from '@components/UI/Widget'
|
|
|
11 |
import DailyLogForm from './daily-log-form'
|
|
|
12 |
|
|
|
13 |
export default function DailyLog() {
|
|
|
14 |
const [show, setShow] = useState(false)
|
|
|
15 |
const [dailyMediaContent, setDailyMediaContent] = useState(null)
|
|
|
16 |
const dispatch = useDispatch()
|
|
|
17 |
|
|
|
18 |
const { selectEmoji, categories } = useDailyLogCategories()
|
|
|
19 |
const { links } = useHabitsUrls()
|
|
|
20 |
|
|
|
21 |
const allCategoriesSelected =
|
|
|
22 |
categories.length && categories.every((cat) => cat.selected)
|
|
|
23 |
|
|
|
24 |
const saveDailyLog = async (log) => {
|
|
|
25 |
try {
|
|
|
26 |
const mediaResponse = await savePreferences(
|
|
|
27 |
links.link_aspect_daily_log,
|
|
|
28 |
log
|
|
|
29 |
)
|
|
|
30 |
setDailyMediaContent(mediaResponse)
|
|
|
31 |
setShow(false)
|
|
|
32 |
} catch (error) {
|
|
|
33 |
dispatch(addNotification({ style: 'danger', msg: error.message }))
|
|
|
34 |
}
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
useEffect(() => {
|
|
|
38 |
if (!allCategoriesSelected) setShow(true)
|
|
|
39 |
}, [allCategoriesSelected])
|
|
|
40 |
|
|
|
41 |
return (
|
|
|
42 |
<Backdrop sx={{ color: '#fff', zIndex: 1250 }} open={show}>
|
|
|
43 |
<Widget styles={{ maxWidth: '800px' }}>
|
|
|
44 |
<Widget.Body>
|
|
|
45 |
{dailyMediaContent ? (
|
|
|
46 |
''
|
|
|
47 |
) : (
|
|
|
48 |
<DailyLogForm
|
|
|
49 |
categories={categories}
|
|
|
50 |
onSelect={selectEmoji}
|
|
|
51 |
onSubmit={saveDailyLog}
|
|
|
52 |
/>
|
|
|
53 |
)}
|
|
|
54 |
</Widget.Body>
|
|
|
55 |
</Widget>
|
|
|
56 |
</Backdrop>
|
|
|
57 |
)
|
|
|
58 |
}
|