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