Skip to content

Commit

Permalink
fix(persons): check if adding deleted visiting speaker
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao committed Sep 19, 2023
1 parent 5db6479 commit d997285
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
35 changes: 35 additions & 0 deletions src/classes/VisitingSpeakers.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ VisitingSpeakersClass.prototype.add = async function ({
Speaker.email = is_self ? '' : email;
Speaker.phone = is_self ? '' : phone;

// remove sibling record
cong.cong_speakers = cong.cong_speakers.filter((speaker) => speaker.person_uid !== person_uid);

// add record to db
const newSpeakers = cong.cong_speakers.concat({
person_uid: Speaker.person_uid,
Expand Down Expand Up @@ -503,4 +506,36 @@ VisitingSpeakersClass.prototype.getCongregationsApprovedNew = async function ()
return result;
};

VisitingSpeakersClass.prototype.deleteSpeaker = async function ({ person_uid, cong_number }) {
const currentCong = this.getCongregation(cong_number);
const speaker = currentCong.cong_speakers.find((record) => record.person_uid === person_uid);
speaker.is_deleted = true;

const isSelf = currentCong.cong_number === +Setting.cong_number;

const newSpeakers = currentCong.cong_speakers.map((speaker) => {
const tmp = structuredClone(speaker);

delete tmp.cong_name;
delete tmp.cong_number;
delete tmp.cong_id;
delete tmp.is_local;

if (isSelf) {
tmp.person_displayName = '';
tmp.person_name = '';
tmp.is_elder = false;
tmp.is_ms = false;
tmp.email = '';
tmp.phone = '';
}

return tmp;
});

await appDb.visiting_speakers.update(cong_number, {
cong_speakers: newSpeakers,
});
};

export const VisitingSpeakers = new VisitingSpeakersClass();
23 changes: 22 additions & 1 deletion src/features/visitingSpeakers/OutgoingSpeaker.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import { useSetRecoilState } from 'recoil';
import { useTranslation } from 'react-i18next';
import Box from '@mui/material/Box';
import DeleteIcon from '@mui/icons-material/Delete';
import IconButton from '@mui/material/IconButton';
import TextField from '@mui/material/TextField';
import SpeakerTalk from './SpeakerTalk';
import { VisitingSpeakers } from '../../classes/VisitingSpeakers';
import { refreshScreenState } from '../../states/main';

const OutgoingSpeaker = ({ speaker }) => {
const { t } = useTranslation('ui');

const setScreenRefresh = useSetRecoilState(refreshScreenState);

const handleDeleteVisitingSpeaker = async () => {
await VisitingSpeakers.deleteSpeaker({
person_uid: speaker.person_uid,
cong_number: speaker.cong_number,
});
setScreenRefresh((prev) => !prev);
};

return (
<Box
sx={{
Expand All @@ -15,6 +30,7 @@ const OutgoingSpeaker = ({ speaker }) => {
alignItems: 'flex-start',
gap: '15px',
flexWrap: 'wrap',
width: '100%',
}}
>
<TextField
Expand All @@ -28,7 +44,12 @@ const OutgoingSpeaker = ({ speaker }) => {
readOnly: true,
}}
/>
<SpeakerTalk speaker={speaker} />
<Box sx={{ display: 'flex', flexWrap: 'nowrap', flexGrow: 1, minWidth: '300px', gap: '10px' }}>
<SpeakerTalk speaker={speaker} />
<IconButton color="error" onClick={handleDeleteVisitingSpeaker}>
<DeleteIcon sx={{ fontSize: '25px' }} />
</IconButton>
</Box>
</Box>
);
};
Expand Down

0 comments on commit d997285

Please sign in to comment.