Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
fix(schedule): hide publish schedule when not connected to congregati…
Browse files Browse the repository at this point in the history
…on account
  • Loading branch information
rhahao authored Dec 3, 2022
1 parent 6444035 commit 8ec8695
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 42 deletions.
14 changes: 10 additions & 4 deletions src/features/schedules/ScheduleCard.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { useSetRecoilState } from 'recoil';
import { useRecoilValue, useSetRecoilState } from 'recoil';
import { useTranslation } from 'react-i18next';
import Box from '@mui/material/Box';
import DeleteIcon from '@mui/icons-material/Delete';
Expand All @@ -23,6 +23,7 @@ import {
isPublishOpenState,
s89DataState,
} from '../../states/schedule';
import { congAccountConnectedState } from '../../states/congregation';

const ScheduleCard = ({ schedule }) => {
const navigate = useNavigate();
Expand All @@ -37,6 +38,8 @@ const ScheduleCard = ({ schedule }) => {
const setS89Data = useSetRecoilState(s89DataState);
const setPublishPocket = useSetRecoilState(isPublishOpenState);

const isCongAccountConnected = useRecoilValue(congAccountConnectedState);

const [anchorPrintEl, setAnchorPrintEl] = useState(null);

const openPrint = Boolean(anchorPrintEl);
Expand Down Expand Up @@ -140,9 +143,12 @@ const ScheduleCard = ({ schedule }) => {
</MenuItem>
</Menu>

<IconButton onClick={handlePublishPocket}>
<SendIcon color="info" sx={{ fontSize: '35px' }} />
</IconButton>
{isCongAccountConnected && (
<IconButton onClick={handlePublishPocket}>
<SendIcon color="info" sx={{ fontSize: '35px' }} />
</IconButton>
)}

<IconButton onClick={handleDeleteSchedule}>
<DeleteIcon color="error" sx={{ fontSize: '35px' }} />
</IconButton>
Expand Down
74 changes: 36 additions & 38 deletions src/features/schedules/SchedulePublish.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,28 @@ const SchedulePublish = () => {
[setOpen]
);

const publishSchedulePocket = useCallback(async () => {
try {
cancel.current = false;

const dataPocket = await dbBuildScheduleForShare(currentSchedule.value);
const { cong_schedule, cong_sourceMaterial } = dataPocket;

if (apiHost !== '') {
const res = await fetch(`${apiHost}api/congregations/${congID}/schedule`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
visitorid: visitorID,
email: userEmail,
},
body: JSON.stringify({ cong_schedule, cong_sourceMaterial }),
});

if (!cancel.current) {
useEffect(() => {
const abortCont = new AbortController();

const publish = async () => {
try {
cancel.current = false;

const dataPocket = await dbBuildScheduleForShare(currentSchedule.value);
const { cong_schedule, cong_sourceMaterial } = dataPocket;

if (apiHost !== '') {
const res = await fetch(`${apiHost}api/congregations/${congID}/schedule`, {
method: 'POST',
signal: abortCont.signal,
headers: {
'Content-Type': 'application/json',
visitorid: visitorID,
email: userEmail,
},
body: JSON.stringify({ cong_schedule, cong_sourceMaterial }),
});

const data = await res.json();

if (res.status === 200) {
Expand All @@ -74,17 +77,22 @@ const SchedulePublish = () => {
setAppSnackOpen(true);
handleClose(false);
}
} catch (err) {
if (err.name !== 'AbortError') {
setAppMessage(err.message);
setAppSeverity('error');
setAppSnackOpen(true);
handleClose();
}
}
} catch (err) {
if (!cancel.current) {
setAppMessage(err.message);
setAppSeverity('error');
setAppSnackOpen(true);
handleClose();
}
}
};

publish();

return () => {
abortCont.abort();
};
}, [
cancel,
apiHost,
congID,
currentSchedule,
Expand All @@ -97,16 +105,6 @@ const SchedulePublish = () => {
visitorID,
]);

useEffect(() => {
publishSchedulePocket();
}, [publishSchedulePocket]);

useEffect(() => {
return () => {
cancel.current = true;
};
}, []);

return (
<Box>
<Dialog open={open} aria-labelledby="dialog-title-publish" onClose={handleClose}>
Expand Down

0 comments on commit 8ec8695

Please sign in to comment.