Revert SearchInput logic to a simplified one + Redirect on results on page reload

This commit is contained in:
2020-10-04 15:36:29 +02:00
parent 47cb0bbdac
commit c459863a0e
2 changed files with 11 additions and 69 deletions

View File

@@ -1,37 +1,13 @@
<template> <template>
<div> <div>
<label>Type any movie title you want:</label> <label>Type any movie title you want:</label>
<a-auto-complete <a-input-search
v-model="search"
:data-source="dataSource"
size="large"
style="width: 100%"
placeholder="Ex: Star Wars" placeholder="Ex: Star Wars"
:allowClear="true" enter-button
:autoFocus="true" v-model="search"
not-found-content="0 Result" size="large"
:loading="fetching"
@select="onSelect"
@search="handleSearch" @search="handleSearch"
> />
<a-input-search enter-button size="large" />
<template slot="dataSource">
<a-select-option
v-for="(opt, index) in dataSource"
:key="index"
:label="opt.text"
:value="opt.value"
>
<div class="item-content">
<img class="item-thumbnail" :src="opt.poster" :alt="opt.text" />
<div class="item-infos">
{{ opt.text }}
<span>{{ opt.year }}</span>
</div>
</div>
</a-select-option>
</template>
</a-auto-complete>
<a-alert <a-alert
v-if="!search || search.length < 3" v-if="!search || search.length < 3"
message="Type at least 3 letters." message="Type at least 3 letters."
@@ -46,50 +22,13 @@ import { groupBy } from "lodash";
export default { export default {
data() { data() {
return { return {
search: "", search: ""
fetching: false,
dataSource: []
}; };
}, },
methods: { methods: {
async getMovies(value) {
if (value && value.length >= 3) {
this.fetching = true;
const result = await this.$axios.$get("", {
params: {
apikey: "a4bf96a7",
s: value,
type: "movie"
}
});
if (result.Response === "True") {
const data = result.Search.map(movie => ({
text: movie.Title,
value: movie.imdbID,
poster: movie.Poster,
year: movie.Year
}));
this.dataSource = data;
this.fetching = false;
} else if (result.Response === "False") {
this.dataSource = [];
}
}
},
handleSearch(value) { handleSearch(value) {
this.getMovies(value); this.$store.commit("setSearch", value);
}, this.$router.push("/results");
handleChange(value) {
this.search = value;
this.getMovies(value);
},
onSelect(value) {
console.log(value);
//TODO: Fetch the selected Movies and redirect to result page
} }
} }
}; };

View File

@@ -0,0 +1,3 @@
export default function({ store, redirect, route }) {
if (store.state.search == "") return redirect("/");
}