-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow user to delete all user input in a space
closes #184
- Loading branch information
1 parent
ceb5983
commit f5ac61d
Showing
13 changed files
with
235 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const _ = require('lodash'); | ||
const { CLEARED_USER_INPUT_CHANNEL } = require('../config/channels'); | ||
const { ERROR_GENERAL } = require('../config/errors'); | ||
const { SPACES_COLLECTION } = require('../db'); | ||
const logger = require('../logger'); | ||
|
||
const clearUserInput = (mainWindow, db) => async (event, { id }) => { | ||
try { | ||
logger.debug(`clearing user input for space ${id}`); | ||
|
||
// get handle to the space | ||
const spaceHandle = db.get(SPACES_COLLECTION).find({ id }); | ||
|
||
// get handle to regular items | ||
const regularItems = spaceHandle | ||
.get('phases') | ||
// we only care about phases with items | ||
.filter(phase => phase.items) | ||
// ensure all items are in the same level of the array | ||
.flatMap(phase => phase.items); | ||
|
||
// get handle to tools | ||
const tools = spaceHandle.get('items'); | ||
|
||
// remove user input in items within phases then | ||
// remove user input in tools | ||
[regularItems, tools].forEach(handle => { | ||
// we only care about items with app instances | ||
handle | ||
.filter(item => item.appInstance) | ||
// ensure all app instances are in the same level | ||
.flatMap(item => item.appInstance) | ||
// user input is saved inside resources | ||
.filter(appInstance => appInstance.resources) | ||
// iterate through app instances to be able to delete | ||
// reference to the original resource array and thus | ||
// mutate the db object | ||
.forEach(appInstance => { | ||
const resources = _.get(appInstance, 'resources'); | ||
// we should not remove resources marked as public | ||
_.remove(resources, resource => resource.visibility !== 'public'); | ||
}) | ||
.write(); | ||
}); | ||
|
||
// we need to return the value of the mutated | ||
// space object to the frontend | ||
const space = spaceHandle.value(); | ||
|
||
mainWindow.webContents.send(CLEARED_USER_INPUT_CHANNEL, space); | ||
} catch (err) { | ||
mainWindow.webContents.send(CLEARED_USER_INPUT_CHANNEL, ERROR_GENERAL); | ||
} | ||
}; | ||
|
||
module.exports = clearUserInput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
const { dialog } = require('electron'); | ||
const { | ||
RESPOND_CLEAR_USER_INPUT_PROMPT_CHANNEL, | ||
} = require('../config/channels'); | ||
|
||
const showClearUserInputChannel = mainWindow => () => { | ||
const options = { | ||
type: 'warning', | ||
buttons: ['Cancel', 'Clear'], | ||
defaultId: 0, | ||
cancelId: 0, | ||
message: | ||
'Are you sure you want to clear all of the user input in this space?', | ||
}; | ||
dialog.showMessageBox(mainWindow, options, respond => { | ||
mainWindow.webContents.send( | ||
RESPOND_CLEAR_USER_INPUT_PROMPT_CHANNEL, | ||
respond | ||
); | ||
}); | ||
}; | ||
|
||
module.exports = showClearUserInputChannel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { withTranslation } from 'react-i18next'; | ||
import Tooltip from '@material-ui/core/Tooltip'; | ||
import { connect } from 'react-redux'; | ||
import IconButton from '@material-ui/core/IconButton/IconButton'; | ||
import { withStyles } from '@material-ui/core'; | ||
import ClearAllIcon from '@material-ui/icons/ClearAll'; | ||
import Styles from '../../Styles'; | ||
import { clearUserInput } from '../../actions'; | ||
|
||
class ClearButton extends Component { | ||
static propTypes = { | ||
classes: PropTypes.shape({ | ||
button: PropTypes.string, | ||
}).isRequired, | ||
t: PropTypes.func.isRequired, | ||
id: PropTypes.string.isRequired, | ||
dispatchClearUserInput: PropTypes.func.isRequired, | ||
}; | ||
|
||
handleClearUserInput = () => { | ||
const { id, dispatchClearUserInput } = this.props; | ||
dispatchClearUserInput({ id }); | ||
}; | ||
|
||
render() { | ||
const { classes, t } = this.props; | ||
return ( | ||
<Tooltip title={t('Clear all of the user input in this space.')}> | ||
<IconButton | ||
color="inherit" | ||
className={classes.button} | ||
onClick={this.handleClearUserInput} | ||
> | ||
<ClearAllIcon /> | ||
</IconButton> | ||
</Tooltip> | ||
); | ||
} | ||
} | ||
|
||
const mapDispatchToProps = { | ||
dispatchClearUserInput: clearUserInput, | ||
}; | ||
|
||
const StyledComponent = withStyles(Styles)(ClearButton); | ||
|
||
const TranslatedComponent = withTranslation()(StyledComponent); | ||
|
||
const ConnectedComponent = connect( | ||
null, | ||
mapDispatchToProps | ||
)(TranslatedComponent); | ||
|
||
export default ConnectedComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.