Skip to content

Commit

Permalink
videos module
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreybiles committed Oct 7, 2019
1 parent c3c17c1 commit 2ec7e3b
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/App.vue
Expand Up @@ -42,7 +42,7 @@ import { mapState } from 'vuex';
export default {
name: 'App',
created(){
this.$store.dispatch('loadVideos');
this.$store.dispatch('videos/loadAll');
this.$store.dispatch('loadCurrentUser');
this.$store.dispatch('tags/loadAll');
},
Expand Down
47 changes: 3 additions & 44 deletions src/store/index.js
Expand Up @@ -3,23 +3,21 @@ import Vuex from "vuex";
import Api from "@/services/api";
import snackbarModule from './snackbar';
import tagsModule from './tags';
import videosModule from './videos';

Vue.use(Vuex);

export default new Vuex.Store({
modules: {
snackbar: snackbarModule,
tags: tagsModule
tags: tagsModule,
videos: videosModule
},
state: {
videos: [],
users: [],
currentUser: {}
},
mutations: {
SET_VIDEOS(state, videos) {
state.videos = videos;
},
SET_PLAYED_VIDEOS(state, playedVideos) {
Vue.set(state.currentUser, 'playedVideos', playedVideos);
},
Expand All @@ -30,18 +28,6 @@ export default new Vuex.Store({
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({});
Expand All @@ -52,15 +38,6 @@ export default new Vuex.Store({
},
},
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;
Expand All @@ -84,24 +61,6 @@ export default new Vuex.Store({
});
}
},
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');
},
Expand Down
54 changes: 54 additions & 0 deletions src/store/videos.js
@@ -0,0 +1,54 @@
import Api from "@/services/api";

export default {
namespaced: true,
state: {
videos: [],
},
mutations: {
SET_VIDEOS(state, videos) {
state.videos = videos;
},
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;
},
},
actions: {
async loadAll({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 create({commit}, video) {
let response = await Api().post('/videos', video);
let savedVideo = response.data.data.attributes;
commit('ADD_VIDEO', savedVideo);
return savedVideo;
},
async delete({commit}, video) {
let response = await Api().delete(`/videos/${video.id}`);
if(response.status == 200 || response.status == 204){
commit('DELETE_VIDEO', video.id);
}
},
async edit({commit}, video) {
let response = await Api().put(`/videos/${video.id}`, video);
let newVideo = response.data.data.attributes;
commit('EDIT_VIDEO', newVideo);
return newVideo;
},
}
}
2 changes: 1 addition & 1 deletion src/views/AdminVideoCreate.vue
Expand Up @@ -28,7 +28,7 @@
},
methods: {
async createVideo() {
let video = await this.$store.dispatch('createVideo', this.video);
let video = await this.$store.dispatch('videos/create', this.video);
this.$store.dispatch('snackbar/setSnackbar', {text: `You have successfully created a new video, ${video.name}.`});
this.$router.push({ name: 'video-watch', params: {id: video.id}});
}
Expand Down
6 changes: 4 additions & 2 deletions src/views/AdminVideoEdit.vue
Expand Up @@ -13,14 +13,16 @@
VideoEditForm,
},
computed: {
...mapState(['videos']),
...mapState({
videos: state => state.videos.videos
}),
video(){
return this.videos.find(v => v.id == this.$route.params.id) || {};
}
},
methods: {
async saveVideo() {
let video = await this.$store.dispatch('editVideo', this.video);
let video = await this.$store.dispatch('videos/edit', this.video);
this.$store.dispatch('snackbar/setSnackbar', {text: `You have successfully edited your video, ${video.name}.`});
this.$router.push({name: 'admin-video-list'});
},
Expand Down
6 changes: 4 additions & 2 deletions src/views/AdminVideoList.vue
Expand Up @@ -25,7 +25,9 @@
import { mapState } from 'vuex';
export default {
computed: {
...mapState(['videos'])
...mapState({
videos: state => state.videos.videos
})
},
filters: {
abbreviate(text) {
Expand All @@ -39,7 +41,7 @@
deleteVideo(video) {
let response = confirm(`Are you sure you want to delete ${video.name}`)
if(response){
this.$store.dispatch('deleteVideo', video);
this.$store.dispatch('videos/delete', video);
this.$store.dispatch('snackbar/setSnackbar', {text: `You have successfully deleted your video, ${video.name}.`});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/views/AdminVideoShow.vue
Expand Up @@ -22,7 +22,7 @@
export default {
computed: {
...mapState({
videos: 'videos',
videos: state => state.videos.videos,
tags: state => state.tags.tags
}),
...mapGetters({
Expand Down
4 changes: 3 additions & 1 deletion src/views/Home.vue
Expand Up @@ -23,7 +23,9 @@ export default {
},
methods: {},
computed: {
...mapState(['videos']),
...mapState({
videos: state => state.videos.videos
}),
}
};
</script>
Expand Down
4 changes: 3 additions & 1 deletion src/views/TagVideoList.vue
Expand Up @@ -21,7 +21,9 @@ export default {
VideoListVideo
},
computed: {
...mapState(['videos']),
...mapState({
videos: state => state.videos.videos
}),
...mapGetters({
getTag: 'tags/get'
}),
Expand Down
5 changes: 4 additions & 1 deletion src/views/VideoWatch.vue
Expand Up @@ -48,11 +48,14 @@ export default {
video(){
return this.videos.find(vid => vid.id == this.$route.params.id) || {}
},
...mapState(['videos', 'currentUser']),
...mapGetters({
getTag: 'tags/get',
isPlayed: 'isPlayed'
}),
...mapState({
videos: state => state.videos.videos,
currentUser: 'currentUser'
}),
playerOptions(){
return {
language: 'en',
Expand Down

0 comments on commit 2ec7e3b

Please sign in to comment.