Removed event bus and switched to vuex. Implemented vue dropzone.

This commit is contained in:
krzysiej
2022-06-15 15:19:46 +02:00
parent 5350c5c46b
commit 4536195a4c
16 changed files with 151 additions and 88 deletions

View File

@@ -0,0 +1,68 @@
import FileApi from "../../api/file";
import {
REMOVE_FILE_FROM_LIST,
FETCHING_FILES,
FETCHING_FILES_SUCCESS,
FETCHING_FILES_ERROR
} from '../mutation-types.js'
export default {
namespaced: true,
state: {
isLoading: false,
error: null,
files: []
},
getters: {
isLoading(state) {
return state.isLoading;
},
hasError(state) {
return state.error !== null;
},
error(state) {
return state.error;
}
},
mutations: {
[FETCHING_FILES](state) {
state.isLoading = true;
state.error = null;
},
[FETCHING_FILES_SUCCESS](state, files) {
state.isLoading = false;
state.error = null;
state.files = files;
},
[FETCHING_FILES_ERROR](state, error) {
state.isLoading = false;
state.error = error;
state.files = [];
},
[REMOVE_FILE_FROM_LIST](state, fileId) {
const index = state.files.findIndex(file => file.id === fileId);
state.files.splice(index, 1);
}
},
actions: {
async deleteFile({commit}, fileId) {
try {
await FileApi.deleteFile(fileId);
commit(REMOVE_FILE_FROM_LIST, fileId)
} catch (error) {
commit(FETCHING_FILES_ERROR, error);
}
},
async getFiles({commit}, bookId) {
commit(FETCHING_FILES);
try {
let response = await FileApi.getFiles(bookId);
commit(FETCHING_FILES_SUCCESS, response.data);
return response.data;
} catch (error) {
commit(FETCHING_FILES_ERROR, error);
}
},
}
};