Skip to content

Commit

Permalink
Add action creators for selectCards, unselectCards, and clearSelected…
Browse files Browse the repository at this point in the history
…Cards.

Part of #688. Part of #174.
  • Loading branch information
jkomoros committed Mar 31, 2024
1 parent 5aec2c4 commit 7e5bf41
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ export const UPDATE_COLLECTION = 'UPDATE_COLLECTION';
export const UPDATE_RENDER_OFFSET = 'UPDATE_RENDER_OFFSET';
export const UPDATE_COLLECTION_SHAPSHOT = 'UPDATE_COLLECTION_SHAPSHOT';
export const RANDOMIZE_SALT = 'RANDOMIZE_SALT';
export const SELECT_CARDS = 'SELECT_CARDS';
export const UNSELECT_CARDS = 'UNSELECT_CARDS';
export const CLEAR_SELECTED_CARDS = 'CLEAR_SELECTED_CARDS';
//Comments
export const COMMENTS_UPDATE_THREADS = 'COMMENTS_UPDATE_THREADS';
export const COMMENTS_UPDATE_MESSAGES = 'COMMENTS_UPDATE_MESSAGES';
Expand Down Expand Up @@ -371,6 +374,20 @@ type ActionRandomizeSalt = {
type: typeof RANDOMIZE_SALT
};

type ActionSelectCards = {
type: typeof SELECT_CARDS,
cards: CardID[]
};

type ActionUnselectCards = {
type: typeof UNSELECT_CARDS,
cards: CardID[]
};

type ActionClearSelectedCards = {
type: typeof CLEAR_SELECTED_CARDS
};

type ActionCommentsUpdateThreads = {
type: typeof COMMENTS_UPDATE_THREADS,
threads: CommentThreads
Expand Down Expand Up @@ -974,6 +991,9 @@ export type SomeAction = ActionAIRequestStarted
| ActionUpdateRenderOffset
| ActionUpdateCollectionSnapshot
| ActionRandomizeSalt
| ActionSelectCards
| ActionUnselectCards
| ActionClearSelectedCards
| ActionCommentsUpdateThreads
| ActionCommentsUpdateMessages
| ActionUpdateCards
Expand Down
23 changes: 23 additions & 0 deletions src/actions/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@ import {
} from '../types.js';

import {
CLEAR_SELECTED_CARDS,
RANDOMIZE_SALT,
SELECT_CARDS,
SHOW_CARD,
SomeAction,
UNSELECT_CARDS,
UPDATE_COLLECTION,
UPDATE_COLLECTION_SHAPSHOT,
UPDATE_RENDER_OFFSET
Expand Down Expand Up @@ -493,4 +496,24 @@ export const waitForFinalCollection = async (description : CollectionDescription
if (!collection) throw new Error('We somehow settled without a collection');

return collection;
};

export const selectCards = (cards : CardID[]) : SomeAction => {
return {
type: SELECT_CARDS,
cards
};
};

export const unselectCards = (cards : CardID[]) : SomeAction => {
return {
type: UNSELECT_CARDS,
cards
};
};

export const clearSelectedCards = () : SomeAction => {
return {
type: CLEAR_SELECTED_CARDS
};
};
21 changes: 21 additions & 0 deletions src/reducers/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import {
UPDATE_READS,
UPDATE_READING_LIST,
SomeAction,
SELECT_CARDS,
UNSELECT_CARDS,
CLEAR_SELECTED_CARDS,
} from '../actions.js';

import {
Expand Down Expand Up @@ -111,6 +114,24 @@ const app = (state : CollectionState = INITIAL_STATE, action : SomeAction) : Col
...state,
randomSalt: randomString(16)
};
case SELECT_CARDS:
return {
...state,
selectedCards: {
...state.selectedCards,
...Object.fromEntries(action.cards.map(id => [id, true]))
}
};
case UNSELECT_CARDS:
return {
...state,
selectedCards: Object.fromEntries(Object.entries(state.selectedCards).filter(([id, _]) => !action.cards.includes(id)))
};
case CLEAR_SELECTED_CARDS:
return {
...state,
selectedCards: {}
};
default:
return state;
}
Expand Down

0 comments on commit 7e5bf41

Please sign in to comment.