|
| 1 | +import { useAlert } from '@dhis2/app-runtime' |
| 2 | +import i18n from '@dhis2/d2-i18n' |
| 3 | +import { |
| 4 | + Modal, |
| 5 | + ModalTitle, |
| 6 | + ModalContent, |
| 7 | + ModalActions, |
| 8 | + ButtonStrip, |
| 9 | + Button, |
| 10 | +} from '@dhis2/ui' |
| 11 | +import isEmpty from 'lodash/isEmpty' |
| 12 | +import PropTypes from 'prop-types' |
| 13 | +import React, { useState } from 'react' |
| 14 | +import { useLatestRelease, useUpdateVersions } from '../../hooks' |
| 15 | +import { DeleteButton } from '../Button' |
| 16 | + |
| 17 | +export const DeleteVersion = ({ version, versionList, handleList }) => { |
| 18 | + const release = useLatestRelease() |
| 19 | + const { mutateVersion, mutateList } = useUpdateVersions() |
| 20 | + const [openModal, setOpen] = useState(false) |
| 21 | + const { show } = useAlert( |
| 22 | + ({ version, success }) => |
| 23 | + success |
| 24 | + ? i18n.t( |
| 25 | + 'The version {{version}} has been successfully deleted.', |
| 26 | + { version: version } |
| 27 | + ) |
| 28 | + : i18n.t('Error occurred while deleting version {{version}}.', { |
| 29 | + version: version, |
| 30 | + }), |
| 31 | + ({ success }) => (success ? { success: true } : { critical: true }) |
| 32 | + ) |
| 33 | + |
| 34 | + const handleButton = () => setOpen(true) |
| 35 | + |
| 36 | + const handleDelete = () => { |
| 37 | + const filteredList = versionList.filter((e) => e.version !== version) |
| 38 | + const updatedList = !isEmpty(filteredList) ? filteredList : [] |
| 39 | + |
| 40 | + const updatePromises = [ |
| 41 | + mutateVersion({ |
| 42 | + version: { |
| 43 | + downloadURL: |
| 44 | + updatedList[0]?.downloadURL || release.downloadURL, |
| 45 | + version: updatedList[0]?.version || release.version, |
| 46 | + }, |
| 47 | + }), |
| 48 | + mutateList({ versionList: updatedList }), |
| 49 | + ] |
| 50 | + |
| 51 | + Promise.all(updatePromises) |
| 52 | + .then(() => { |
| 53 | + handleList(updatedList) |
| 54 | + handleClose() |
| 55 | + show({ version: version, success: true }) |
| 56 | + }) |
| 57 | + .catch(() => { |
| 58 | + show({ version: version, success: false }) |
| 59 | + }) |
| 60 | + } |
| 61 | + |
| 62 | + const handleClose = () => setOpen(false) |
| 63 | + |
| 64 | + return ( |
| 65 | + <> |
| 66 | + <DeleteButton onClick={handleButton} small /> |
| 67 | + {openModal && ( |
| 68 | + <DeleteModal |
| 69 | + version={version} |
| 70 | + onHandleClose={handleClose} |
| 71 | + onHandleDelete={handleDelete} |
| 72 | + /> |
| 73 | + )} |
| 74 | + </> |
| 75 | + ) |
| 76 | +} |
| 77 | + |
| 78 | +DeleteVersion.propTypes = { |
| 79 | + handleList: PropTypes.func, |
| 80 | + version: PropTypes.string, |
| 81 | + versionList: PropTypes.array, |
| 82 | +} |
| 83 | + |
| 84 | +const DeleteModal = ({ version, onHandleClose, onHandleDelete }) => ( |
| 85 | + <> |
| 86 | + <Modal position="middle" onClose={onHandleClose} small> |
| 87 | + <ModalTitle>{i18n.t('Delete app version')}</ModalTitle> |
| 88 | + <ModalContent> |
| 89 | + <p>{i18n.t('Deleting an app version cannot be undone.')}</p> |
| 90 | + <p> |
| 91 | + {i18n.t( |
| 92 | + 'Are you sure you want to delete app version {{version}}?', |
| 93 | + { version: version } |
| 94 | + )} |
| 95 | + </p> |
| 96 | + </ModalContent> |
| 97 | + <ModalActions> |
| 98 | + <ButtonStrip end> |
| 99 | + <Button onClick={onHandleClose}>{i18n.t('Cancel')}</Button> |
| 100 | + <Button onClick={onHandleDelete} destructive> |
| 101 | + {i18n.t('Delete')} |
| 102 | + </Button> |
| 103 | + </ButtonStrip> |
| 104 | + </ModalActions> |
| 105 | + </Modal> |
| 106 | + </> |
| 107 | +) |
| 108 | + |
| 109 | +DeleteModal.propTypes = { |
| 110 | + version: PropTypes.string, |
| 111 | + onHandleClose: PropTypes.func, |
| 112 | + onHandleDelete: PropTypes.func, |
| 113 | +} |
0 commit comments