Skip to content

Commit

Permalink
Merge pull request #14 from Need2Watch/store-rework
Browse files Browse the repository at this point in the history
Store reworked & modularized
  • Loading branch information
RexusWolf authored Aug 24, 2020
2 parents 786aaa3 + 50f08dd commit 4c5bc0b
Show file tree
Hide file tree
Showing 19 changed files with 165 additions and 88 deletions.
2 changes: 1 addition & 1 deletion src/components/calendarView/N2wCalendar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default {
color: this.colors[this.rnd(0, this.colors.length - 1)],
});
}
this.$store.commit('scheduleEvents', events);
this.$store.dispatch('scheduledEvents/scheduleEvents', events);
this.events = events;
},
getEventColor(event) {
Expand Down
10 changes: 5 additions & 5 deletions src/components/mainView/N2wNavBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@
<v-icon color="secondary" size="30">mdi-calendar</v-icon>
</v-btn>
<v-btn
v-if="!this.loggedUser.user_id"
v-if="!this.loggedUser.userId"
height="100%"
tile
depressed
to="/signIn"
class="n2wgray y ml-3 secondary"
>SIGN IN</v-btn>
<v-btn
v-if="!this.loggedUser.user_id"
v-if="!this.loggedUser.userId"
height="100%"
tile
depressed
to="/signUp"
class="primary black--text"
>SIGN UP</v-btn>

<v-menu v-if="this.loggedUser.user_id" offset-y>
<v-menu v-if="this.loggedUser.userId" offset-y>
<template v-slot:activator="{ on }" v-bind:loggedUser="this.loggedUser">
<v-btn
color="transparent"
Expand Down Expand Up @@ -135,12 +135,12 @@ export default {
username: '',
email: '',
password: '',
user_id: '',
userId: '',
country: '',
city: '',
profilePicture: '',
};
this.$store.commit('loadUser', user);
this.$store.dispatch('loggedUser/loadUser', user);
this.$router.push('/signIn');
},
},
Expand Down
14 changes: 7 additions & 7 deletions src/components/mainView/N2wSearchBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ export default {
axios
.post('http://127.0.0.1:5000/movies/search', {
title: this.movieName,
user_id: this.loggedUser.user_id,
user_id: this.loggedUser.userId,
})
.then(function(response) {
.then(function (response) {
let movies = response.data;
previousThis.$store.commit('loadMovies', movies);
previousThis.$store.dispatch('movies/loadMovies', movies);
previousThis.$router.push('/search');
})
.catch(function(error) {
.catch(function (error) {
console.log(error);
});
},
Expand All @@ -55,13 +55,13 @@ export default {
// Lazily load input items
fetch('http://127.0.0.1/movies/search')
.then(res => res.json())
.then(res => {
.then((res) => res.json())
.then((res) => {
const { count, entries } = res;
this.count = count;
this.entries = entries;
})
.catch(err => {
.catch((err) => {
console.log(err);
})
.finally(() => (this.isLoading = false));
Expand Down
4 changes: 2 additions & 2 deletions src/components/mainView/N2wSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ export default {
username: '',
email: '',
password: '',
user_id: '',
userId: '',
country: '',
city: '',
profilePicture: '',
};
this.$store.commit('loadUser', user);
this.$store.dispatch('loggedUser/loadUser', user);
this.$router.push('/signIn');
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/components/moviesView/N2wCinemaCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default {
.get(route)
.then(function (response) {
let movie = response.data;
previousThis.$store.commit('loadMovie', movie);
previousThis.$store.dispatch('currentMovie/loadMovie', movie);
previousThis.$router.push('/movie');
})
.catch(function (error) {
Expand Down
10 changes: 5 additions & 5 deletions src/components/moviesView/N2wMovieCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<span :key="index" v-for="(item, index) in this.movie.genres">{{item.name}}|</span>
</v-card-subtitle>
<v-card-text class="headline mb-10">{{this.movie.overview}}</v-card-text>
<v-card-actions v-if="this.loggedUser.user_id" class="movieCardActions">
<v-card-actions v-if="this.loggedUser.userId" class="movieCardActions">
<v-btn
v-on:click="followMovie"
v-if="!this.movie.following"
Expand Down Expand Up @@ -64,7 +64,7 @@ export default {
const previousThis = this;
axios
.post('http://127.0.0.1:5000/movies/follow', {
user_id: previousThis.loggedUser.user_id,
user_id: previousThis.loggedUser.userId,
movie_id: previousThis.movie.movie_id,
})
.then(function () {
Expand All @@ -75,7 +75,7 @@ export default {
const previousThis = this;
axios
.post('http://127.0.0.1:5000/movies/watch', {
user_id: previousThis.loggedUser.user_id,
user_id: previousThis.loggedUser.userId,
movie_id: previousThis.movie.movie_id,
})
.then(function () {
Expand All @@ -86,7 +86,7 @@ export default {
const previousThis = this;
axios
.post('http://127.0.0.1:5000/movies/unfollow', {
user_id: previousThis.loggedUser.user_id,
user_id: previousThis.loggedUser.userId,
movie_id: previousThis.movie.movie_id,
})
.then(function () {
Expand All @@ -97,7 +97,7 @@ export default {
const previousThis = this;
axios
.post('http://127.0.0.1:5000/movies/unwatch', {
user_id: previousThis.loggedUser.user_id,
user_id: previousThis.loggedUser.userId,
movie_id: previousThis.movie.movie_id,
})
.then(function () {
Expand Down
2 changes: 1 addition & 1 deletion src/components/signView/N2wSignInForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default {
})
.then(function (response) {
let user = response.data;
previousThis.$store.commit('loadUser', user);
previousThis.$store.dispatch('loggedUser/loadUser', user);
previousThis.$router.push('/');
})
.catch(function (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/signView/N2wSignUpForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export default {
})
.then(function (response) {
let user = response.data;
previousThis.$store.commit('loadUser', user);
previousThis.$store.dispatch('loggedUser/loadUser', user);
previousThis.$router.push('/');
})
.catch(function (error) {
Expand Down
60 changes: 12 additions & 48 deletions src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,23 @@
import Vue from 'vue';
import Vuex from 'vuex';
import VuexPersistence from 'vuex-persist'
import VuexPersistence from 'vuex-persist';
import loggedUser from './modules/loggedUser.store';
import movies from './modules/movies.store';
import currentMovie from './modules/currentMovie.store';
import scheduledEvents from './modules/scheduledEvents.store';

Vue.use(Vuex);

const vuexLocal = new VuexPersistence({
storage: window.localStorage
})
storage: window.localStorage,
});

export default new Vuex.Store({
state: {
loggedUser: {
firstName: '',
lastName: '',
username: '',
email: '',
password: '',
user_id: '',
country: '',
city: '',
profilePicture: '',
},
movies: [],
currentMovie: {},
scheduledEvents: {},
},
getters: {},
mutations: {
loadUser(state, payload) {
state.loggedUser.firstName = payload.first_name;
state.loggedUser.lastName = payload.last_name;
state.loggedUser.username = payload.username;
state.loggedUser.email = payload.email;
state.loggedUser.password = payload.password;
state.loggedUser.user_id = payload.user_id;
state.loggedUser.country = payload.country;
state.loggedUser.city = payload.city;
state.loggedUser.profilePicture = payload.profile_picture;
},
loadMovies(state, payload) {
state.movies = payload;
},
loadMovie(state, payload) {
state.currentMovie = payload;
},
followMovie(state) {
state.currentMovie.following = !state.currentMovie.following;
},
watchMovie(state) {
state.currentMovie.watched = !state.currentMovie.watched;
},
scheduleEvents(state, payload) {
state.scheduledEvents = payload;
}
modules: {
loggedUser,
movies,
currentMovie,
scheduledEvents,
},
actions: {},
plugins: [vuexLocal.plugin],
});
34 changes: 34 additions & 0 deletions src/store/modules/currentMovie.store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const state = {
currentMovie: {},
};

const actions = {
loadMovie({ commit }, payload) {
commit('LOAD_MOVIE', payload);
},
followMovie({ commit }) {
commit('FOLLOW_MOVIES');
},
watchMovie({ commit }) {
commit('WATCH_MOVIES');
},
};

const mutations = {
LOAD_MOVIE(state, payload) {
this.state.currentMovie = payload;
},
FOLLOW_MOVIES(state) {
this.state.currentMovie.following = !state.currentMovie.following;
},
WATCH_MOVIES(state) {
this.state.currentMovie.watched = !state.currentMovie.watched;
},
};

export default {
namespaced: true,
actions,
state,
mutations,
};
38 changes: 38 additions & 0 deletions src/store/modules/loggedUser.store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const state = {
firstName: '',
lastName: '',
username: '',
email: '',
password: '',
userId: '',
country: '',
city: '',
profilePicture: '',
};

const actions = {
loadUser({ commit }, payload) {
commit('LOAD_USER', payload);
},
};

const mutations = {
LOAD_USER(state, payload) {
state.firstName = payload.first_name;
state.lastName = payload.last_name;
state.username = payload.username;
state.email = payload.email;
state.password = payload.password;
state.userId = payload.user_id;
state.country = payload.country;
state.city = payload.city;
state.profilePicture = payload.profile_picture;
},
};

export default {
namespaced: true,
actions,
state,
mutations,
};
22 changes: 22 additions & 0 deletions src/store/modules/movies.store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const state = {
movies: '',
};

const actions = {
loadMovies({ commit }, payload) {
commit('LOAD_MOVIES', payload);
},
};

const mutations = {
LOAD_MOVIES(state, payload) {
this.state.movies = payload;
},
};

export default {
namespaced: true,
actions,
state,
mutations,
};
22 changes: 22 additions & 0 deletions src/store/modules/scheduledEvents.store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const state = {
scheduledEvents: {},
};

const actions = {
scheduleEvents({ commit }, payload) {
commit('SCHEDULE_EVENTS', payload);
},
};

const mutations = {
SCHEDULE_EVENTS(state, payload) {
this.state.scheduledEvents = payload;
},
};

export default {
namespaced: true,
actions,
state,
mutations,
};
Empty file added src/store/mutations.js
Empty file.
6 changes: 3 additions & 3 deletions src/views/EditProfile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export default {
submitForm() {
const previousThis = this;
axios
.put('http://127.0.0.1:5000/users/' + this.loggedUser.user_id, {
user_id: this.loggedUser.user_id,
.put('http://127.0.0.1:5000/users/' + this.loggedUser.userId, {
user_id: this.loggedUser.userId,
username: this.username,
password: this.loggedUser.password,
first_name: this.firstName,
Expand All @@ -62,7 +62,7 @@ export default {
})
.then(function (response) {
let user = response.data;
previousThis.$store.commit('loadUser', user);
previousThis.$store.dispatch('loggedUser/loadUser', user);
previousThis.$router.push('/profile');
})
.catch(function (error) {
Expand Down
Loading

0 comments on commit 4c5bc0b

Please sign in to comment.