Skip to content

Commit

Permalink
Refactor where the logic to commit the multi edit dialog lives.
Browse files Browse the repository at this point in the history
Part of #688. Part of #174.
  • Loading branch information
jkomoros committed Mar 30, 2024
1 parent 142696f commit cb41875
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
25 changes: 24 additions & 1 deletion src/actions/multiedit.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {
selectIsEditing
selectIsEditing, selectMultiEditReferencesDiff, selectSelectedCards
} from '../selectors.js';

import {
ThunkSomeAction
} from '../store.js';

import {
CardDiff,
CardID,
ReferenceType
} from '../types.js';
Expand All @@ -18,6 +19,7 @@ import {
MULTI_EDIT_DIALOG_REMOVE_REFERENCE,
SomeAction
} from '../actions.js';
import { modifyCards } from './data.js';

export const openMultiEditDialog = () : ThunkSomeAction => (dispatch, getState) => {

Expand All @@ -38,6 +40,27 @@ export const closeMultiEditDialog = () : SomeAction => {
};
};

export const commitMultiEditDialog = () : ThunkSomeAction => (dispatch, getState) => {
const state = getState();
const referencesDiff = selectMultiEditReferencesDiff(state);
if (referencesDiff.length == 0) {
//If there's nothing to do, we can close the dialog now.
dispatch(closeMultiEditDialog());
return;
}

//There's a change to make. modifyCardsSuccess will close the dialog.

const update : CardDiff = {
references_diff: referencesDiff
};

const selectedCards = selectSelectedCards(state);

dispatch(modifyCards(selectedCards, update, false, false));

};

export const addReference = (cardID : CardID, referenceType : ReferenceType) : SomeAction => {
return {
type: MULTI_EDIT_DIALOG_ADD_REFERENCE,
Expand Down
18 changes: 3 additions & 15 deletions src/components/multi-edit-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ import { ButtonSharedStyles } from './button-shared-styles.js';
import {
closeMultiEditDialog,
removeReference,
addReference
addReference,
commitMultiEditDialog
} from '../actions/multiedit.js';

import {
modifyCards
} from '../actions/data.js';

import {
selectCardToReference
} from '../actions/editor.js';
Expand Down Expand Up @@ -176,16 +173,7 @@ class MultiEditDialog extends connect(store)(DialogElement) {
}

_handleDoneClicked() {
if (this._referencesDiff.length) {
const update = {
references_diff: this._referencesDiff,
};
store.dispatch(modifyCards(this._selectedCards, update, false, false));
//We'll close the dialog when modifyCardSuccess is triggered.
return;
}
//If we didn't have anything to do, close it now.
this._shouldClose();
store.dispatch(commitMultiEditDialog());
}

override _shouldClose() {
Expand Down

0 comments on commit cb41875

Please sign in to comment.