From df58a801258878567c6c12347650c0bef56ad6ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Delattre?= Date: Fri, 6 Jan 2017 22:01:33 +0100 Subject: [PATCH] Add show search --- src/internal/shows/handlers.go | 15 +++++++++++---- src/main.go | 2 +- src/public/js/actions/actionCreators.js | 7 +++++++ src/public/js/app.js | 5 ++++- src/public/js/components/navbar.js | 23 ++++++++++++++++++++++- src/public/js/components/shows/list.js | 19 ++++++++++++++++++- src/public/js/reducers/shows.js | 5 +++++ 7 files changed, 68 insertions(+), 8 deletions(-) diff --git a/src/internal/shows/handlers.go b/src/internal/shows/handlers.go index 88f52b9..9453eec 100644 --- a/src/internal/shows/handlers.go +++ b/src/internal/shows/handlers.go @@ -1,6 +1,7 @@ package shows import ( + "encoding/json" "errors" "net/http" @@ -27,15 +28,21 @@ func GetDetailsHandler(env *web.Env, w http.ResponseWriter, r *http.Request) err // SearchShow will search a show func SearchShow(env *web.Env, w http.ResponseWriter, r *http.Request) error { - key := r.FormValue("key") - if key == "" { + var data struct { + Key string `json:"key"` + } + if err := json.NewDecoder(r.Body).Decode(&data); err != nil { + return err + } + + if data.Key == "" { return env.RenderError(w, errors.New("no given key")) } var shows []*polochon.Show searchers := env.Config.ShowSearchers for _, searcher := range searchers { - result, err := searcher.SearchShow(key, env.Log) + result, err := searcher.SearchShow(data.Key, env.Log) if err != nil { env.Log.Errorf("error while searching show : %s", err) continue @@ -43,7 +50,7 @@ func SearchShow(env *web.Env, w http.ResponseWriter, r *http.Request) error { shows = append(shows, result...) } - env.Log.Debugf("got %d shows doing search %q", len(shows), key) + env.Log.Debugf("got %d shows doing search %q", len(shows), data.Key) showList := []*Show{} for _, s := range shows { show := New(s.ImdbID) diff --git a/src/main.go b/src/main.go index 0653411..58db0ce 100644 --- a/src/main.go +++ b/src/main.go @@ -86,7 +86,7 @@ func main() { env.Handle("/shows/{id:tt[0-9]+}", shows.GetDetailsHandler) env.Handle("/shows/refresh", extmedias.RefreshShows) env.Handle("/shows/explore", extmedias.ExploreShows) - env.Handle("/shows/search", shows.SearchShow) + env.Handle("/shows/search", shows.SearchShow).Methods("POST") n := negroni.Classic() n.Use(authMiddleware) diff --git a/src/public/js/actions/actionCreators.js b/src/public/js/actions/actionCreators.js index 06194cb..9e96a87 100644 --- a/src/public/js/actions/actionCreators.js +++ b/src/public/js/actions/actionCreators.js @@ -112,6 +112,13 @@ export function fetchShows(url) { ) } +export function searchShows(search) { + return request( + 'SEARCH_SHOWS', + configureAxios().post('/shows/search', search) + ) +} + export function fetchShowDetails(imdbId) { return request( 'SHOW_FETCH_DETAILS', diff --git a/src/public/js/app.js b/src/public/js/app.js index 8b071cb..bcd32ce 100644 --- a/src/public/js/app.js +++ b/src/public/js/app.js @@ -90,10 +90,12 @@ const MovieListSearch = (props) => ( const ShowListPopular = (props) => ( ) - const ShowDetailsView = (props) => ( ) +const ShowListSearch = (props) => ( + +) ReactDOM.render(( @@ -106,6 +108,7 @@ ReactDOM.render(( + diff --git a/src/public/js/components/navbar.js b/src/public/js/components/navbar.js index 2fb6907..a21ec94 100644 --- a/src/public/js/components/navbar.js +++ b/src/public/js/components/navbar.js @@ -10,19 +10,28 @@ export default class NavBar extends React.Component { constructor(props) { super(props); this.handleMovieSearch = this.handleMovieSearch.bind(this); + this.handleShowSearch = this.handleShowSearch.bind(this); } handleMovieSearch() { this.props.router.push(`/movies/search/${encodeURI(this.props.movieStore.search)}`) } + handleShowSearch() { + this.props.router.push(`/shows/search/${encodeURI(this.props.showStore.search)}`) + } render() { const username = this.props.userStore.username; const isLoggedIn = username !== "" ? true : false; const location = this.props.router.getCurrentLocation().pathname; let displayMovieSearch = false; + let displayShowSearch = false; if (isLoggedIn && location.indexOf("movies") > -1) { displayMovieSearch = true; } + if (isLoggedIn && location.indexOf("shows") > -1) + { + displayShowSearch = true; + } return (
@@ -72,7 +81,19 @@ export default class NavBar extends React.Component { + + + } + {displayShowSearch && + +
this.handleShowSearch()}> + diff --git a/src/public/js/components/shows/list.js b/src/public/js/components/shows/list.js index 1d83f0c..0dec71a 100644 --- a/src/public/js/components/shows/list.js +++ b/src/public/js/components/shows/list.js @@ -20,7 +20,24 @@ function ShowButtons(props) { export default class ShowList extends React.Component { componentWillMount() { - this.props.fetchShows(this.props.showsUrl); + if (this.props.showsUrl) { + this.props.fetchShows(this.props.showsUrl); + } else if (this.props.params && this.props.params.search != "") { + this.props.searchShows({ + key: this.props.params.search + }); + } + } + componentWillUpdate(nextProps, nextState) { + if (!nextProps.params || nextProps.params.search === "") { + return + } + if (this.props.params.search === nextProps.params.search) { + return + } + this.props.searchShows({ + key: nextProps.params.search + }); } render() { const shows = this.props.showStore.shows; diff --git a/src/public/js/reducers/shows.js b/src/public/js/reducers/shows.js index 43ed2af..dbdbdc7 100644 --- a/src/public/js/reducers/shows.js +++ b/src/public/js/reducers/shows.js @@ -6,6 +6,7 @@ const defaultState = { show: { seasons: [], }, + search: "", }; export default function showStore(state = defaultState, action) { @@ -25,6 +26,10 @@ export default function showStore(state = defaultState, action) { show: sortEpisodes(action.payload.data), }) return state; + case 'SEARCH_SHOWS_FULFILLED': + return Object.assign({}, state, { + shows: action.payload.data, + }) case 'SELECT_SHOW': // Don't select the show if we're fetching another show's details if (state.fetchingDetails) {