From 11bc7a13bc510d4afb6446b3135c03a3c08473c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Delattre?= Date: Thu, 4 Feb 2021 11:53:24 +0100 Subject: [PATCH] Add a button to refresh a show In the show list and in the show details. --- frontend/js/components/buttons/refresh.js | 21 +++++++++ .../js/components/shows/details/header.js | 47 ++++++++++++------- frontend/js/components/shows/list.js | 8 +++- frontend/js/reducers/show.js | 8 ++++ frontend/js/reducers/shows.js | 4 +- 5 files changed, 69 insertions(+), 19 deletions(-) create mode 100644 frontend/js/components/buttons/refresh.js diff --git a/frontend/js/components/buttons/refresh.js b/frontend/js/components/buttons/refresh.js new file mode 100644 index 0000000..050a131 --- /dev/null +++ b/frontend/js/components/buttons/refresh.js @@ -0,0 +1,21 @@ +import React from "react"; +import PropTypes from "prop-types"; + +export const Refresh = ({ onClick, loading, kind }) => { + return ( + + ); +}; +Refresh.propTypes = { + onClick: PropTypes.func.isRequired, + loading: PropTypes.bool.isRequired, + kind: PropTypes.string.isRequired, +}; +Refresh.defaultProps = { + onClick: () => {}, + loading: false, + kind: "secondary", +}; diff --git a/frontend/js/components/shows/details/header.js b/frontend/js/components/shows/details/header.js index b5647ae..0ee2945 100644 --- a/frontend/js/components/shows/details/header.js +++ b/frontend/js/components/shows/details/header.js @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useEffect } from "react"; import { useDispatch, useSelector } from "react-redux"; import { Plot } from "../../details/plot"; @@ -8,27 +8,35 @@ import { Title } from "../../details/title"; import { TrackingLabel } from "../../details/tracking"; import { ImdbBadge } from "../../buttons/imdb"; +import { Refresh } from "../../buttons/refresh"; -import { showWishlistToggle } from "../../../actions/shows"; +import { + showWishlistToggle, + fetchShowDetails, + getShowDetails, +} from "../../../actions/shows"; export const Header = () => { const dispatch = useDispatch(); - const posterUrl = useSelector((state) => state.show.show.poster_url); - const title = useSelector((state) => state.show.show.title); - const imdbId = useSelector((state) => state.show.show.imdb_id); - const year = useSelector((state) => state.show.show.year); - const rating = useSelector((state) => state.show.show.rating); - const plot = useSelector((state) => state.show.show.plot); + const { + poster_url: posterUrl, + title, + imdb_id: imdbId, + year, + rating, + plot, + tracked_season: trackedSeason, + tracked_episode: trackedEpisode, + fetchingDetails, + } = useSelector((state) => state.show.show); - const trackedSeason = useSelector((state) => state.show.show.tracked_season); - const trackedEpisode = useSelector( - (state) => state.show.show.tracked_episode - ); - const wishlisted = useSelector( - (state) => - state.show.show.tracked_season !== null && - state.show.show.tracked_episode !== null - ); + useEffect(() => { + if (fetchingDetails === false) { + dispatch(fetchShowDetails(imdbId)); + } + }, [fetchingDetails, imdbId, dispatch]); + + const wishlisted = trackedSeason !== null && trackedEpisode !== null; return (
@@ -55,6 +63,11 @@ export const Header = () => {

+ dispatch(getShowDetails(imdbId))} + loading={fetchingDetails} + kind="info" + />

{ switch (match.path) { @@ -92,11 +94,15 @@ const ShowList = ({ match, history }) => { + dispatch(getShowDetails(selectedShow.imdb_id))} + loading={selectedShow.fetchingDetails} + />

diff --git a/frontend/js/reducers/show.js b/frontend/js/reducers/show.js index 0dcd45f..dc52f2f 100644 --- a/frontend/js/reducers/show.js +++ b/frontend/js/reducers/show.js @@ -68,6 +68,14 @@ export default (state = defaultState, action) => break; } + case "SHOW_GET_DETAILS_PENDING": + draft.show.fetchingDetails = true; + break; + + case "SHOW_GET_DETAILS_FULFILLED": + draft.show.fetchingDetails = false; + break; + case "SHOW_UPDATE_STORE_WISHLIST": // TODO: check with we give the imdb in the payload draft.show.tracked_season = action.payload.season; // eslint-disable-line camelcase diff --git a/frontend/js/reducers/shows.js b/frontend/js/reducers/shows.js index 5408027..9e6f426 100644 --- a/frontend/js/reducers/shows.js +++ b/frontend/js/reducers/shows.js @@ -46,7 +46,9 @@ export default (state = defaultState, action) => break; case "SHOW_GET_DETAILS_PENDING": - draft.shows.get(action.payload.main.imdbId).fetchingDetails = true; + if (draft.shows.get(action.payload.main.imdbId)) { + draft.shows.get(action.payload.main.imdbId).fetchingDetails = true; + } break; case "SHOW_GET_DETAILS_FULFILLED":