/
index.js
142 lines (136 loc) · 4.21 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import Vue from "vue";
import Vuex from "vuex";
import Api from "@/services/api";
import snackbarModule from './snackbar';
import tagsModule from './tags';
Vue.use(Vuex);
export default new Vuex.Store({
modules: {
snackbar: snackbarModule,
tags: tagsModule
},
state: {
videos: [],
users: [],
currentUser: {}
},
mutations: {
SET_VIDEOS(state, videos) {
state.videos = videos;
},
SET_PLAYED_VIDEOS(state, playedVideos) {
Vue.set(state.currentUser, 'playedVideos', playedVideos);
},
SET_USERS(state, users) {
state.users = users;
},
MARK_VIDEO_PLAYED(state, videoId) {
let playedVideos = state.currentUser.playedVideos.concat(videoId);
state.currentUser.playedVideos = playedVideos;
},
ADD_VIDEO(state, video) {
let videos = state.videos.concat(video);
state.videos = videos;
},
DELETE_VIDEO(state, videoId){
let videos = state.videos.filter(v => v.id != videoId)
state.videos = videos;
},
EDIT_VIDEO(state, video) {
let v = state.videos.find(v => v.id == video.id)
v = video;
},
LOGOUT_USER(state) {
state.currentUser = {}
window.localStorage.currentUser = JSON.stringify({});
},
SET_CURRENT_USER(state, user) {
state.currentUser = user;
window.localStorage.currentUser = JSON.stringify(user);
},
},
actions: {
async loadVideos({commit, dispatch}){
let response = await Api().get('/videos');
let videos = response.data.data;
videos.forEach(v => {
v.attributes.tag_ids = v.relationships.tags.data.map(t => t.id);
});
commit('SET_VIDEOS', videos.map(v => v.attributes));
},
async loadUsers({commit}) {
let response = await Api().get('/users');
let users = response.data.data;
commit('SET_USERS', users.map(u => u.attributes));
},
async loadCurrentUser({commit, dispatch}) {
let user = JSON.parse(window.localStorage.currentUser);
commit('SET_CURRENT_USER', user);
dispatch('loadPlayedVideos', user.id);
},
async loadPlayedVideos({commit}, userId) {
let response = await Api().get(`users/${userId}`);
let user = response.data.data.attributes;
commit('SET_PLAYED_VIDEOS', user.played_video_ids)
},
markPlayed({commit, state}, videoId) {
if(state.currentUser.name){
commit('MARK_VIDEO_PLAYED', videoId);
Api().post(`/video_plays`, {
video_id: videoId
});
}
},
async createVideo({commit}, video) {
let response = await Api().post('/videos', video);
let savedVideo = response.data.data.attributes;
commit('ADD_VIDEO', savedVideo);
return savedVideo;
},
async deleteVideo({commit}, video) {
let response = await Api().delete(`/videos/${video.id}`);
if(response.status == 200 || response.status == 204){
commit('DELETE_VIDEO', video.id);
}
},
async editVideo({commit}, video) {
let response = await Api().put(`/videos/${video.id}`, video);
let newVideo = response.data.data.attributes;
commit('EDIT_VIDEO', newVideo);
return newVideo;
},
logoutUser({commit}) {
commit('LOGOUT_USER');
},
async loginUser({commit, dispatch}, loginInfo) {
try {
let response = await Api().post('/sessions', loginInfo);
let user = response.data.data.attributes;
commit('SET_CURRENT_USER', user);
dispatch('loadPlayedVideos', user.id);
return user;
} catch {
return {error: "Email/password combination was incorrect. Please try again."}
}
},
async registerUser({commit, dispatch}, registrationInfo) {
try {
let response = await Api().post('/users', registrationInfo);
let user = response.data.data.attributes;
commit('SET_CURRENT_USER', user);
dispatch('loadPlayedVideos', user.id);
return user;
} catch {
return { error: "There was an error. Please try again." }
}
},
},
getters: {
playedVideos: state => {
return state.currentUser.playedVideos || [];
},
isPlayed: (state, getters) => videoId => {
return getters.playedVideos.includes(videoId)
}
}
});