Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/protocols' into RocketChat#755
Browse files Browse the repository at this point in the history
  • Loading branch information
Scarvis committed Nov 11, 2020
2 parents 18f6313 + 8e792b5 commit c86c1cd
Show file tree
Hide file tree
Showing 13 changed files with 439 additions and 410 deletions.
38 changes: 34 additions & 4 deletions app/models/server/models/Protocols.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,22 @@ class Protocols extends Base {
sectionData._id = _id;

const data = this.findOne({ _id: protocolId });
data.sections = data.sections ? [...data.sections, sectionData] : [sectionData];

if (data.sections) {
let internalNum = 0;
data.sections.forEach((section) => {
if (section.inum > internalNum) {
internalNum = section.inum;
}
})
internalNum++;
sectionData.inum = internalNum;
data.sections = [...data.sections, sectionData];
} else {
sectionData.inum = 1;
data.sections = [sectionData];
}

data._updatedAt = new Date();
this.update({ _id: protocolId }, { $set: { ...data } });

Expand All @@ -50,7 +65,7 @@ class Protocols extends Base {
if (data.sections) {
data.sections = data.sections.map((section) => {
if (section._id === sectionData._id) {
return { ...sectionData, items: section.items };
return { ...sectionData, inum: section.inum, items: section.items };
}
return section;
});
Expand All @@ -73,7 +88,22 @@ class Protocols extends Base {

data.sections.forEach((section) => {
if (section._id === sectionId) {
section.items = section.items ? [...section.items, item] : [item];

if (section.items) {
let internalNum = 0;
section.items.forEach((item) => {
if (item.inum > internalNum) {
internalNum = item.inum;
}
})
internalNum++;
item.inum = internalNum;
section.items = [...section.items, item];
} else {
item.inum = 1;
section.items = [item];
}

}
});

Expand Down Expand Up @@ -107,7 +137,7 @@ class Protocols extends Base {
if (section._id === sectionId) {
section.items = section.items.map((item) => {
if (item._id === itemData._id) {
return { ...itemData };
return { ...itemData, inum: item.inum };
}
return item;
})
Expand Down
4 changes: 2 additions & 2 deletions app/protocols/client/views/AddItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ export function AddItem({ goToNew, close, onChange, ...props }) {
expireAt
);
dispatchToastMessage({ type: 'success', message: t('Item_Added_Successfully') });
goToNew(sectionId, result)();
close();
onChange();
} catch (error) {
dispatchToastMessage({ type: 'error', message: error });
}
}, [dispatchToastMessage, goToNew, number, name, responsible, expireAt, onChange, saveAction, t]);
}, [dispatchToastMessage, close, number, name, responsible, expireAt, onChange, saveAction, t]);

return <VerticalBar.ScrollableContent {...props}>
<Field>
Expand Down
4 changes: 2 additions & 2 deletions app/protocols/client/views/AddSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ export function AddSection({ goToNew, close, onChange, ...props }) {
name
);
dispatchToastMessage({ type: 'success', message: t('Section_Added_Successfully') });
goToNew(result)();
close()
onChange();
} catch (error) {
dispatchToastMessage({ type: 'error', message: error });
}
}, [dispatchToastMessage, goToNew, number, name, onChange, saveAction, t]);
}, [dispatchToastMessage, close, number, name, onChange, saveAction, t]);

return <VerticalBar.ScrollableContent {...props}>
<Field>
Expand Down
69 changes: 2 additions & 67 deletions app/protocols/client/views/EditItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import {
Button,
ButtonGroup,
Field,
Icon,
Skeleton,
Throbber,
InputBox,
TextInput,
Modal
} from '@rocket.chat/fuselage';
import DatePicker, { registerLocale } from 'react-datepicker';
import ru from 'date-fns/locale/ru';
Expand All @@ -24,50 +22,10 @@ import { useMethod } from '../../../../client/contexts/ServerContext';
import { useToastMessageDispatch } from '../../../../client/contexts/ToastMessagesContext';
import { useEndpointDataExperimental, ENDPOINT_STATES } from '../../../../client/hooks/useEndpointDataExperimental';
import { validateItemData, createItemData } from './lib';
import { useSetModal } from '../../../../client/contexts/ModalContext';
import VerticalBar from '../../../../client/components/basic/VerticalBar';

require('react-datepicker/dist/react-datepicker.css');

const DeleteWarningModal = ({ onDelete, onCancel, ...props }) => {
const t = useTranslation();
return <Modal {...props}>
<Modal.Header>
<Icon color='danger' name='modal-warning' size={20}/>
<Modal.Title>{t('Are_you_sure')}</Modal.Title>
<Modal.Close onClick={onCancel}/>
</Modal.Header>
<Modal.Content fontScale='p1'>
{t('Item_Delete_Warning')}
</Modal.Content>
<Modal.Footer>
<ButtonGroup align='end'>
<Button ghost onClick={onCancel}>{t('Cancel')}</Button>
<Button primary danger onClick={onDelete}>{t('Delete')}</Button>
</ButtonGroup>
</Modal.Footer>
</Modal>;
};

const SuccessModal = ({ onClose, ...props }) => {
const t = useTranslation();
return <Modal {...props}>
<Modal.Header>
<Icon color='success' name='checkmark-circled' size={20}/>
<Modal.Title>{t('Deleted')}</Modal.Title>
<Modal.Close onClick={onClose}/>
</Modal.Header>
<Modal.Content fontScale='p1'>
{t('Item_Has_Been_Deleted')}
</Modal.Content>
<Modal.Footer>
<ButtonGroup align='end'>
<Button primary onClick={onClose}>{t('Ok')}</Button>
</ButtonGroup>
</Modal.Footer>
</Modal>;
};

export function EditItem({ protocolId, sectionId, _id, cache, onChange, ...props }) {
const query = useMemo(() => ({
query: JSON.stringify({ _id: protocolId }),
Expand All @@ -89,9 +47,6 @@ export function EditItem({ protocolId, sectionId, _id, cache, onChange, ...props
<Button disabled><Throbber inheritColor/></Button>
<Button primary disabled><Throbber inheritColor/></Button>
</ButtonGroup>
<ButtonGroup stretch w='full' mbs='x8'>
<Button primary danger disabled><Throbber inheritColor/></Button>
</ButtonGroup>
</Box>;
}

Expand All @@ -115,7 +70,6 @@ function EditItemWithData({ close, onChange, protocol, sectionId, itemId, ...pro
const [name, setName] = useState('');
const [responsible, setResponsible] = useState('');
const [expireAt, setExpireAt] = useState('');
const setModal = useSetModal();

useEffect(() => {
setNumber(previousNumber || '');
Expand All @@ -124,7 +78,6 @@ function EditItemWithData({ close, onChange, protocol, sectionId, itemId, ...pro
setExpireAt(previousExpireAt ? new Date(previousExpireAt) : '');
}, [previousNumber, previousName, previousResponsible, previousExpireAt, _id]);

const deleteItem = useMethod('deleteItem');
const insertOrUpdateItem = useMethod('insertOrUpdateItem');

const hasUnsavedChanges = useMemo(() => previousNumber !== number || previousName !== name || previousResponsible !== responsible || previousExpireAt !== expireAt,
Expand All @@ -141,20 +94,9 @@ function EditItemWithData({ close, onChange, protocol, sectionId, itemId, ...pro

const handleSave = useCallback(async () => {
saveAction(number, name, responsible, expireAt);
close();
onChange();
}, [saveAction, onChange]);

const onDeleteConfirm = useCallback(async () => {
try {
await deleteItem(protocol._id, sectionId, _id);
setModal(() => <SuccessModal onClose={() => { setModal(undefined); close(); onChange(); }}/>);
} catch (error) {
dispatchToastMessage({ type: 'error', message: error });
onChange();
}
}, [_id, close, deleteItem, dispatchToastMessage, onChange]);

const openConfirmDelete = () => setModal(() => <DeleteWarningModal onDelete={onDeleteConfirm} onCancel={() => setModal(undefined)}/>);
}, [saveAction, close, onChange]);

return <VerticalBar.ScrollableContent {...props}>
<Field>
Expand Down Expand Up @@ -206,12 +148,5 @@ function EditItemWithData({ close, onChange, protocol, sectionId, itemId, ...pro
</ButtonGroup>
</Field.Row>
</Field>
<Field>
<Field.Row>
<ButtonGroup stretch w='full'>
<Button primary danger onClick={openConfirmDelete}><Icon name='trash' mie='x4'/>{t('Delete')}</Button>
</ButtonGroup>
</Field.Row>
</Field>
</VerticalBar.ScrollableContent>;
}
69 changes: 2 additions & 67 deletions app/protocols/client/views/EditSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import {
Button,
ButtonGroup,
Field,
Icon,
Skeleton,
Throbber,
InputBox,
Modal
} from '@rocket.chat/fuselage';
import CKEditor from '@ckeditor/ckeditor5-react';
import ClassicEditor from '@ckeditor/ckeditor5-build-classic';
Expand All @@ -19,50 +17,10 @@ import { useMethod } from '../../../../client/contexts/ServerContext';
import { useToastMessageDispatch } from '../../../../client/contexts/ToastMessagesContext';
import { useEndpointDataExperimental, ENDPOINT_STATES } from '../../../../client/hooks/useEndpointDataExperimental';
import { validateSectionData, createSectionData } from './lib';
import { useSetModal } from '../../../../client/contexts/ModalContext';
import VerticalBar from '../../../../client/components/basic/VerticalBar';

require('react-datepicker/dist/react-datepicker.css');

const DeleteWarningModal = ({ onDelete, onCancel, ...props }) => {
const t = useTranslation();
return <Modal {...props}>
<Modal.Header>
<Icon color='danger' name='modal-warning' size={20}/>
<Modal.Title>{t('Are_you_sure')}</Modal.Title>
<Modal.Close onClick={onCancel}/>
</Modal.Header>
<Modal.Content fontScale='p1'>
{t('Section_Delete_Warning')}
</Modal.Content>
<Modal.Footer>
<ButtonGroup align='end'>
<Button ghost onClick={onCancel}>{t('Cancel')}</Button>
<Button primary danger onClick={onDelete}>{t('Delete')}</Button>
</ButtonGroup>
</Modal.Footer>
</Modal>;
};

const SuccessModal = ({ onClose, ...props }) => {
const t = useTranslation();
return <Modal {...props}>
<Modal.Header>
<Icon color='success' name='checkmark-circled' size={20}/>
<Modal.Title>{t('Deleted')}</Modal.Title>
<Modal.Close onClick={onClose}/>
</Modal.Header>
<Modal.Content fontScale='p1'>
{t('Section_Has_Been_Deleted')}
</Modal.Content>
<Modal.Footer>
<ButtonGroup align='end'>
<Button primary onClick={onClose}>{t('Ok')}</Button>
</ButtonGroup>
</Modal.Footer>
</Modal>;
};

export function EditSection({ protocolId, _id, cache, onChange, ...props }) {
const query = useMemo(() => ({
query: JSON.stringify({ _id: protocolId }),
Expand All @@ -80,9 +38,6 @@ export function EditSection({ protocolId, _id, cache, onChange, ...props }) {
<Button disabled><Throbber inheritColor/></Button>
<Button primary disabled><Throbber inheritColor/></Button>
</ButtonGroup>
<ButtonGroup stretch w='full' mbs='x8'>
<Button primary danger disabled><Throbber inheritColor/></Button>
</ButtonGroup>
</Box>;
}

Expand All @@ -104,14 +59,12 @@ function EditSectionWithData({ close, onChange, protocol, sectionId, ...props })

const [number, setNumber] = useState('');
const [name, setName] = useState('');
const setModal = useSetModal();

useEffect(() => {
setNumber(previousNumber || '');
setName(previousName || '');
}, [previousNumber, previousName, _id]);

const deleteSection = useMethod('deleteSection');
const insertOrUpdateSection = useMethod('insertOrUpdateSection');

const hasUnsavedChanges = useMemo(() => previousNumber !== number || previousName !== name,
Expand All @@ -128,20 +81,9 @@ function EditSectionWithData({ close, onChange, protocol, sectionId, ...props })

const handleSave = useCallback(async () => {
saveAction(number, name);
close();
onChange();
}, [saveAction, onChange]);

const onDeleteConfirm = useCallback(async () => {
try {
await deleteSection(protocol._id, _id);
setModal(() => <SuccessModal onClose={() => { setModal(undefined); close(); onChange(); }}/>);
} catch (error) {
dispatchToastMessage({ type: 'error', message: error });
onChange();
}
}, [_id, close, deleteSection, dispatchToastMessage, onChange]);

const openConfirmDelete = () => setModal(() => <DeleteWarningModal onDelete={onDeleteConfirm} onCancel={() => setModal(undefined)}/>);
}, [saveAction, close, onChange]);

return <VerticalBar.ScrollableContent {...props}>
<Field>
Expand Down Expand Up @@ -175,12 +117,5 @@ function EditSectionWithData({ close, onChange, protocol, sectionId, ...props })
</ButtonGroup>
</Field.Row>
</Field>
<Field>
<Field.Row>
<ButtonGroup stretch w='full'>
<Button primary danger onClick={openConfirmDelete}><Icon name='trash' mie='x4'/>{t('Delete')}</Button>
</ButtonGroup>
</Field.Row>
</Field>
</VerticalBar.ScrollableContent>;
}
Loading

0 comments on commit c86c1cd

Please sign in to comment.