Skip to content
Snippets Groups Projects
Commit 2c1456c1 authored by Kyle Doherty's avatar Kyle Doherty Committed by GitHub
Browse files

Allow for custom setCollection function in MoveToCollection (#4935)

* allow for custom setCollection function in MoveToCollection

* clean up default action
parent 062207fe
Branches
Tags
No related merge requests found
......@@ -288,6 +288,10 @@ export default class QueryHeader extends Component {
<MoveToCollection
questionId={this.props.card.id}
initialCollectionId={this.props.card && this.props.card.collection_id}
setCollection={(questionId, collection) => {
this.props.onSetCardAttribute('collection', collection)
this.props.onSetCardAttribute('collection_id', collection.id)
}}
/>
</ModalWithTrigger>
]);
......
......@@ -18,7 +18,7 @@ const mapStateToProps = (state, props) => ({
const mapDispatchToProps = {
loadCollections,
setCollection
defaultSetCollection: setCollection
}
@connect(mapStateToProps, mapDispatchToProps)
......@@ -26,27 +26,29 @@ export default class MoveToCollection extends Component {
constructor(props) {
super(props);
this.state = {
collectionId: props.initialCollectionId
currentCollection: { id: props.initialCollectionId }
}
}
componentWillMount() {
this.props.loadCollections()
}
async onMove(collectionId) {
async onMove(collection) {
try {
this.setState({ error: null })
await this.props.setCollection(this.props.questionId, collectionId, true);
const setCollection = this.props.setCollection || this.props.defaultSetCollection
await setCollection(this.props.questionId, collection, true);
this.props.onClose();
} catch (e) {
this.setState({ error: e })
} catch (error) {
this.setState({ error })
}
}
render() {
const { onClose } = this.props;
const { collectionId, error } = this.state;
const { currentCollection, error } = this.state;
return (
<ModalContent
title="Which collection should this be in?"
......@@ -58,7 +60,7 @@ export default class MoveToCollection extends Component {
<Button className="mr1" onClick={onClose}>
Cancel
</Button>
<Button primary disabled={collectionId === undefined} onClick={() => this.onMove(collectionId)}>
<Button primary disabled={currentCollection.id === undefined} onClick={() => this.onMove(currentCollection)}>
Move
</Button>
</div>
......@@ -71,9 +73,9 @@ export default class MoveToCollection extends Component {
<ol className="List text-brand ml-auto mr-auto" style={{ width: 520 }}>
{ [{ name: "None", id: null }].concat(collections).map((collection, index) =>
<li
className={cx("List-item flex align-center cursor-pointer mb1 p1", { "List-item--selected": collection.id === collectionId })}
className={cx("List-item flex align-center cursor-pointer mb1 p1", { "List-item--selected": collection.id === currentCollection.id })}
key={index}
onClick={() => this.setState({ collectionId: collection.id })}
onClick={() => this.setState({ currentCollection: collection })}
>
<Icon
className="Icon mr2"
......@@ -93,3 +95,4 @@ export default class MoveToCollection extends Component {
)
}
}
......@@ -162,9 +162,10 @@ export const setLabeled = createThunkAction(SET_LABELED, (cardId, labelId, label
const getCardCollectionId = (state, cardId) => getIn(state, ["questions", "entities", "cards", cardId, "collection_id"])
export const setCollection = createThunkAction(SET_COLLECTION, (cardId, collectionId, undoable = false) => {
export const setCollection = createThunkAction(SET_COLLECTION, (cardId, collection, undoable = false) => {
return async (dispatch, getState) => {
const state = getState();
const collectionId = collection.id;
if (cardId == null) {
// bulk move
let selected = getSelectedEntities(getState());
......@@ -178,6 +179,7 @@ export const setCollection = createThunkAction(SET_COLLECTION, (cardId, collecti
selected.map(item => dispatch(setCollection(item.id, collectionId)));
} else {
const collection = _.findWhere(state.collections.collections, { id: collectionId });
if (undoable) {
dispatch(addUndo(createUndo(
"moved",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment