Skip to content

Commit

Permalink
Update addReferenceToCard to allow updating an existing reference to …
Browse files Browse the repository at this point in the history
…set a new value.

Part of #682.
  • Loading branch information
jkomoros committed Dec 9, 2023
1 parent feeb46e commit a40436a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/actions/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,7 @@ export const selectCardToReference = (referenceType : ReferenceType) : ThunkSome
};

export const addReferenceToCard = (cardID : CardID, referenceType : ReferenceType, value? : string) : ThunkSomeAction => (dispatch, getState) => {

const state = getState();

const editingCard = selectEditingCard(state);
Expand All @@ -840,7 +841,7 @@ export const addReferenceToCard = (cardID : CardID, referenceType : ReferenceTyp
return;
}

const reason = referencesNonModifying(editingCard).mayNotSetCardReferenceReason(state, cardID, referenceType);
const reason = referencesNonModifying(editingCard).mayNotSetCardReferenceReason(state, cardID, referenceType, value);

if (reason) {
console.warn(reason);
Expand Down
13 changes: 11 additions & 2 deletions src/references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ class ReferencesAccessor {

//Returns a string describing why that reference may not be set, or '' if
//it's legal.
mayNotSetCardReferenceReason(state : State, cardID : CardID, referenceType : ReferenceType) : string {
mayNotSetCardReferenceReason(state : State, cardID : CardID, referenceType : ReferenceType, value? : string) : string {

if (!value) value = '';

if (this._cardObj.id == cardID) {
return 'The card references itself which is not allowed';
Expand All @@ -361,7 +363,14 @@ class ReferencesAccessor {
const baseType = referenceTypeConfig.subTypeOf || referenceType;

if (REFERENCE_TYPES_EQUIVALENCE_CLASSES[baseType][referenceType] && this.typeClassArray(baseType).some(id => id == cardID)) {
return 'The editing card already has a ' + baseType + ' reference (or subtype) to that card';
//TODO: could this logic be simpler if it just checked "if the reason the typeClassArray conflicts is because this referenceType already exists, that's fine"?
const currentValue = this._referencesInfo[cardID]?.[referenceType];

if( currentValue != undefined && currentValue != value) {
//This is a special case; the reference already exists, yes, but we're changing its value, and that's OK.
} else {
return 'The editing card already has a ' + baseType + ' reference (or subtype) to that card';
}
}

//if the reference type doesn't have a toCardTypeAllowList then any of them
Expand Down

0 comments on commit a40436a

Please sign in to comment.