From 60e70f4b34808e0760f6722b41bc1f206128c429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Ytterstr=C3=B6m?= Date: Tue, 23 Apr 2019 14:58:20 +0200 Subject: [PATCH] Add selected album actions and reducers Default state is an empty object ({}). SELECT_ALBUM will update state with an album. UNSELECT_ALBUM resets state to default state. --- src/actions/index.js | 15 ++++++++++++++- src/reducers/index.js | 2 ++ src/reducers/selected-album.js | 12 ++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/reducers/selected-album.js diff --git a/src/actions/index.js b/src/actions/index.js index cec9b1f..efa37b2 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -2,6 +2,8 @@ export const SET_VISIBILITY_FILTER = 'SET_VISIBILITY_FILTER'; export const SET_SORT_KEY = 'SET_SORT_KEY'; export const LOAD_ALBUMS = 'LOAD_ALBUMS'; export const LOAD_ALBUMS_OK = 'LOAD_ALBUMS_OK'; +export const SELECT_ALBUM = 'SELECT_ALBUM'; +export const UNSELECT_ALBUM = 'UNSELECT_ALBUM'; export const setVisibilityFilter = filter => ({ type: SET_VISIBILITY_FILTER, @@ -22,4 +24,15 @@ export const albumsLoadedOk = albums => ({ payload: { albums, }, -}) \ No newline at end of file +}) + +export const selectAlbum = album => ({ + type: SELECT_ALBUM, + payload: { + album + }, +}); + +export const unselectAlbum = () => ({ + type: UNSELECT_ALBUM, +}); diff --git a/src/reducers/index.js b/src/reducers/index.js index 60f09f2..dd3626b 100644 --- a/src/reducers/index.js +++ b/src/reducers/index.js @@ -2,9 +2,11 @@ import { combineReducers } from 'redux'; import albums from './albums'; import visibilityFilter from './visibility-filter'; import sortKey from "./sort-key"; +import selectedAlbum from './selected-album'; export default combineReducers({ albums, visibilityFilter, sortKey, + selectedAlbum, }); diff --git a/src/reducers/selected-album.js b/src/reducers/selected-album.js new file mode 100644 index 0000000..a25ec92 --- /dev/null +++ b/src/reducers/selected-album.js @@ -0,0 +1,12 @@ +import { SELECT_ALBUM, UNSELECT_ALBUM } from '../actions'; + +export default (state = {}, action) => { + switch (action.type) { + case SELECT_ALBUM: + return action.payload.album; + case UNSELECT_ALBUM: + return {}; + default: + return state; + } +};