Migrated progress bar and progress editor to vuex.
This commit is contained in:
@@ -1,20 +1,14 @@
|
||||
import Vue from 'vue'
|
||||
import Vuex from 'vuex'
|
||||
import booksmodule from './modules/books';
|
||||
import bookprogressmodule from './modules/bookprogress';
|
||||
|
||||
Vue.use(Vuex)
|
||||
|
||||
export default new Vuex.Store({
|
||||
state: {
|
||||
count: 0
|
||||
},
|
||||
mutations: {
|
||||
increment(state) {
|
||||
state.count++
|
||||
}
|
||||
},
|
||||
modules:{
|
||||
booksmodule
|
||||
booksmodule,
|
||||
bookprogressmodule
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
70
assets/js/store/modules/bookprogress.js
Normal file
70
assets/js/store/modules/bookprogress.js
Normal file
@@ -0,0 +1,70 @@
|
||||
import BookApi from "../../api/book";
|
||||
import {
|
||||
UPDATING_PROGRESS,
|
||||
UPDATING_PROGRESS_SUCCESS,
|
||||
UPDATING_PROGRESS_ERROR,
|
||||
TURN_ON_EDIT_MODE,
|
||||
TURN_OFF_EDIT_MODE
|
||||
} from '../mutation-types.js'
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
state: {
|
||||
isUpdating: false,
|
||||
error: null,
|
||||
progress: null,
|
||||
editMode: false
|
||||
},
|
||||
getters: {
|
||||
isEditMode(state) {
|
||||
return state.editMode;
|
||||
},
|
||||
isUpdating(state) {
|
||||
return state.isUpdating;
|
||||
},
|
||||
hasError(state) {
|
||||
return state.error !== null;
|
||||
},
|
||||
error(state) {
|
||||
return state.error;
|
||||
},
|
||||
progress(state) {
|
||||
return state.progress;
|
||||
},
|
||||
},
|
||||
mutations: {
|
||||
[UPDATING_PROGRESS](state) {
|
||||
state.isUpdating = true;
|
||||
state.error = null;
|
||||
state.progress = null;
|
||||
},
|
||||
[UPDATING_PROGRESS_SUCCESS](state, progress) {
|
||||
state.isLoading = false;
|
||||
state.error = null;
|
||||
state.progress = progress;
|
||||
},
|
||||
[UPDATING_PROGRESS_ERROR](state, error) {
|
||||
state.isLoading = false;
|
||||
state.error = error;
|
||||
state.progress = null;
|
||||
},
|
||||
[TURN_ON_EDIT_MODE](state) {
|
||||
state.editMode = true;
|
||||
},
|
||||
[TURN_OFF_EDIT_MODE](state) {
|
||||
state.editMode = false;
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
async updateProgress({commit}, data) {
|
||||
commit(UPDATING_PROGRESS);
|
||||
try {
|
||||
let response = await BookApi.bookUpdateProgress(data.bookId, data.progress);
|
||||
commit(UPDATING_PROGRESS_SUCCESS, response.data.progress);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
commit(UPDATING_PROGRESS_ERROR, error);
|
||||
}
|
||||
},
|
||||
}
|
||||
};
|
||||
@@ -3,7 +3,9 @@ import BookApi from "../../api/book";
|
||||
const
|
||||
FETCHING_BOOKS = "FETCHING_BOOKS",
|
||||
FETCHING_BOOKS_SUCCESS = "FETCHING_BOOKS_SUCCESS",
|
||||
FETCHING_BOOKS_ERROR = "FETCHING_BOOKS_ERROR";
|
||||
FETCHING_BOOKS_ERROR = "FETCHING_BOOKS_ERROR",
|
||||
SETTING_SEARCH_TERM = "SETTING_SEARCH_TERM"
|
||||
;
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
@@ -28,9 +30,15 @@ export default {
|
||||
},
|
||||
books(state) {
|
||||
return state.books;
|
||||
},
|
||||
isSearching(state) {
|
||||
return !!state.searchTitle;
|
||||
}
|
||||
},
|
||||
mutations: {
|
||||
[SETTING_SEARCH_TERM](state, searchTitle) {
|
||||
state.searchTitle = searchTitle;
|
||||
},
|
||||
[FETCHING_BOOKS](state) {
|
||||
state.isLoading = true;
|
||||
state.error = null;
|
||||
@@ -51,8 +59,7 @@ export default {
|
||||
async findAll({commit, state}, searchTitle) {
|
||||
commit(FETCHING_BOOKS);
|
||||
try {
|
||||
state.searchTitle = searchTitle;
|
||||
console.info(state.searchTitle);
|
||||
commit(SETTING_SEARCH_TERM, searchTitle);
|
||||
let response = await BookApi.booksFetch(state.searchTitle);
|
||||
commit(FETCHING_BOOKS_SUCCESS, response.data);
|
||||
return response.data;
|
||||
|
||||
7
assets/js/store/mutation-types.js
Normal file
7
assets/js/store/mutation-types.js
Normal file
@@ -0,0 +1,7 @@
|
||||
export const
|
||||
UPDATING_PROGRESS = "UPDATING_PROGRESS",
|
||||
UPDATING_PROGRESS_SUCCESS = "UPDATING_PROGRESS_SUCCESS",
|
||||
UPDATING_PROGRESS_ERROR = "UPDATING_PROGRESS_ERROR",
|
||||
TURN_ON_EDIT_MODE = "TURN_ON_EDIT_MODE",
|
||||
TURN_OFF_EDIT_MODE = "TURN_OFF_EDIT_MODE"
|
||||
;
|
||||
Reference in New Issue
Block a user