forked from ricardovasconcelos/Vueflix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.js
59 lines (50 loc) · 1.39 KB
/
store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
let list = window.localStorage.getItem("list");
let movieWatched = window.localStorage.getItem("movieWatched");
export default new Vuex.Store({
state: {
list: list ? JSON.parse(list) : [],
movieWatched: movieWatched ? JSON.parse(movieWatched) : []
},
mutations: {
addToMyList(state, movie) {
let existMovie = state.list.find(
current => current.imdbID == movie.imdbID
);
if (!existMovie) {
state.list.push(movie);
}
this.commit("saveData");
},
saveData(state) {
window.localStorage.setItem("list", JSON.stringify(state.list));
},
removeData(state, movie) {
let index = state.list.indexOf(movie);
state.list.splice(index, 1);
this.commit("saveData");
},
markAsWatched(state, movieID) {
let movieMarked = state.movieWatched.find(
current => current.imdbID == movieID.imdbID
);
if (!movieMarked) {
state.movieWatched.push(movieID);
}
this.commit("markMovie");
},
markMovie(state) {
window.localStorage.setItem(
"movieWatched",
JSON.stringify(state.movieWatched)
);
},
removeWatched(state, movie) {
let WatchedRemove = state.movieWatched.indexOf(movie);
state.movieWatched.splice(WatchedRemove, 1);
this.commit("markMovie");
}
}
});