|
| 1 | +import {defineMessages, FormattedMessage, injectIntl, intlShape} from 'react-intl'; |
| 2 | +import React from 'react'; |
| 3 | +import PropTypes from 'prop-types'; |
| 4 | +import classNames from 'classnames'; |
| 5 | + |
| 6 | +import Box from '../box/box.jsx'; |
| 7 | +import ReactModal from 'react-modal'; |
| 8 | +import deleteIcon from './icon--delete.svg'; |
| 9 | +import undoIcon from './icon--undo.svg'; |
| 10 | +import arrowLeftIcon from './icon--arrow-left.svg'; |
| 11 | +import arrowRightIcon from './icon--arrow-right.svg'; |
| 12 | + |
| 13 | +import styles from './delete-confirmation-prompt.css'; |
| 14 | + |
| 15 | +// TODO: Parametrize from outside if we want more custom messaging |
| 16 | +const messages = defineMessages({ |
| 17 | + shouldDeleteSpriteMessage: { |
| 18 | + defaultMessage: 'Are you sure you want to delete this sprite?', |
| 19 | + description: 'Message to indicate whether selected sprite should be deleted.', |
| 20 | + id: 'gui.gui.shouldDeleteSprite' |
| 21 | + }, |
| 22 | + shouldDeleteCostumeMessage: { |
| 23 | + defaultMessage: 'Are you sure you want to delete this costume?', |
| 24 | + description: 'Message to indicate whether selected costume should be deleted.', |
| 25 | + id: 'gui.gui.shouldDeleteCostume' |
| 26 | + }, |
| 27 | + shouldDeleteSoundMessage: { |
| 28 | + defaultMessage: 'Are you sure you want to delete this sound?', |
| 29 | + description: 'Message to indicate whether selected sound should be deleted.', |
| 30 | + id: 'gui.gui.shouldDeleteSound' |
| 31 | + }, |
| 32 | + confirmOption: { |
| 33 | + defaultMessage: 'yes', |
| 34 | + description: 'Yes - should delete the sprite', |
| 35 | + id: 'gui.gui.confirm' |
| 36 | + }, |
| 37 | + cancelOption: { |
| 38 | + defaultMessage: 'no', |
| 39 | + description: 'No - cancel deletion', |
| 40 | + id: 'gui.gui.cancel' |
| 41 | + }, |
| 42 | + confirmDeletionHeading: { |
| 43 | + defaultMessage: 'Confirm Asset Deletion', |
| 44 | + description: 'Heading of confirmation prompt to delete asset', |
| 45 | + id: 'gui.gui.deleteAssetHeading' |
| 46 | + } |
| 47 | +}); |
| 48 | + |
| 49 | +const modalWidth = 300; |
| 50 | +const calculateModalPosition = (relativeElemRef, modalPosition) => { |
| 51 | + const refPosition = relativeElemRef.getBoundingClientRect(); |
| 52 | + |
| 53 | + if (modalPosition === 'left') { |
| 54 | + return { |
| 55 | + top: refPosition.top - refPosition.height, |
| 56 | + left: refPosition.left - modalWidth - 25 |
| 57 | + }; |
| 58 | + } |
| 59 | + |
| 60 | + if (modalPosition === 'right') { |
| 61 | + return { |
| 62 | + top: refPosition.top - refPosition.height, |
| 63 | + left: refPosition.right + 25 |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + return {}; |
| 68 | +}; |
| 69 | + |
| 70 | +const getMessage = entityType => { |
| 71 | + if (entityType === 'COSTUME') { |
| 72 | + return messages.shouldDeleteCostumeMessage; |
| 73 | + } |
| 74 | + |
| 75 | + if (entityType === 'SOUND') { |
| 76 | + return messages.shouldDeleteSoundMessage; |
| 77 | + } |
| 78 | + |
| 79 | + return messages.shouldDeleteSpriteMessage; |
| 80 | +}; |
| 81 | + |
| 82 | +const DeleteConfirmationPrompt = ({ |
| 83 | + intl, |
| 84 | + onCancel, |
| 85 | + onOk, |
| 86 | + modalPosition, |
| 87 | + entityType, |
| 88 | + relativeElemRef |
| 89 | +}) => { |
| 90 | + const modalPositionValues = calculateModalPosition(relativeElemRef, modalPosition); |
| 91 | + |
| 92 | + return (<ReactModal |
| 93 | + isOpen |
| 94 | + // We have to inline the styles, since a part |
| 95 | + // of them are dynamically generated |
| 96 | + style={{ |
| 97 | + content: { |
| 98 | + ...modalPositionValues, |
| 99 | + width: modalWidth, |
| 100 | + border: 'none', |
| 101 | + height: 'fit-content', |
| 102 | + backgroundColor: 'transparent', |
| 103 | + padding: 0, |
| 104 | + margin: 0, |
| 105 | + position: 'absolute', |
| 106 | + overflowX: 'hidden', |
| 107 | + zIndex: 1000 |
| 108 | + }, |
| 109 | + overlay: { |
| 110 | + position: 'fixed', |
| 111 | + top: 0, |
| 112 | + left: 0, |
| 113 | + right: 0, |
| 114 | + bottom: 0, |
| 115 | + zIndex: 510, |
| 116 | + backgroundColor: 'transparent' |
| 117 | + } |
| 118 | + }} |
| 119 | + contentLabel={intl.formatMessage(messages.confirmDeletionHeading)} |
| 120 | + onRequestClose={onCancel} |
| 121 | + > |
| 122 | + <Box className={styles.modalContainer}> |
| 123 | + { modalPosition === 'right' ? |
| 124 | + <Box className={classNames(styles.arrowContainer, styles.arrowContainerLeft)}> |
| 125 | + <img |
| 126 | + className={styles.deleteIcon} |
| 127 | + src={arrowLeftIcon} |
| 128 | + /> |
| 129 | + </Box> : null } |
| 130 | + <Box className={styles.body}> |
| 131 | + <Box className={styles.label}> |
| 132 | + <FormattedMessage {...getMessage(entityType)} /> |
| 133 | + </Box> |
| 134 | + <Box className={styles.buttonRow}> |
| 135 | + <button |
| 136 | + className={styles.okButton} |
| 137 | + onClick={onOk} |
| 138 | + role="button" |
| 139 | + > |
| 140 | + <img |
| 141 | + className={styles.deleteIcon} |
| 142 | + src={deleteIcon} |
| 143 | + /> |
| 144 | + <div className={styles.message}> |
| 145 | + <FormattedMessage {...messages.confirmOption} /> |
| 146 | + </div> |
| 147 | + </button> |
| 148 | + <button |
| 149 | + className={styles.cancelButton} |
| 150 | + onClick={onCancel} |
| 151 | + role="button" |
| 152 | + > |
| 153 | + <img |
| 154 | + className={styles.deleteIcon} |
| 155 | + src={undoIcon} |
| 156 | + /> |
| 157 | + <div className={styles.message}> |
| 158 | + <FormattedMessage {...messages.cancelOption} /> |
| 159 | + </div> |
| 160 | + </button> |
| 161 | + </Box> |
| 162 | + </Box> |
| 163 | + {modalPosition === 'left' ? |
| 164 | + <Box className={classNames(styles.arrowContainer, styles.arrowContainerRight)}> |
| 165 | + <img |
| 166 | + className={styles.deleteIcon} |
| 167 | + src={arrowRightIcon} |
| 168 | + /> |
| 169 | + </Box> : null } |
| 170 | + </Box> |
| 171 | + </ReactModal>); |
| 172 | +}; |
| 173 | + |
| 174 | +DeleteConfirmationPrompt.propTypes = { |
| 175 | + onOk: PropTypes.func.isRequired, |
| 176 | + onCancel: PropTypes.func.isRequired, |
| 177 | + relativeElemRef: PropTypes.object, |
| 178 | + entityType: PropTypes.string, |
| 179 | + modalPosition: PropTypes.string, |
| 180 | + intl: intlShape.isRequired |
| 181 | +}; |
| 182 | + |
| 183 | +const DeleteConfirmationPromptIntl = injectIntl(DeleteConfirmationPrompt); |
| 184 | + |
| 185 | +export default DeleteConfirmationPromptIntl; |
0 commit comments