From 18d0403172ae54f96ace32d9fdc043ecdaf4a8da Mon Sep 17 00:00:00 2001 From: Gamebrary Date: Tue, 25 Aug 2020 16:37:33 -0700 Subject: [PATCH] clean up wallpapers, move everything to store --- src/components/Settings.vue | 9 +- src/components/Settings/BoardSettings.vue | 15 +- src/components/Settings/FileSettings.vue | 157 --------------- src/components/Settings/WallpaperSettings.vue | 179 ++++++++++++++++++ src/pages/Board.vue | 4 +- src/store/actions.js | 70 ++++++- src/store/mutations.js | 14 ++ src/store/state.js | 1 + 8 files changed, 277 insertions(+), 172 deletions(-) delete mode 100644 src/components/Settings/FileSettings.vue create mode 100644 src/components/Settings/WallpaperSettings.vue diff --git a/src/components/Settings.vue b/src/components/Settings.vue index 5948bfba..aad0e494 100644 --- a/src/components/Settings.vue +++ b/src/components/Settings.vue @@ -15,13 +15,14 @@ - - + + + @@ -30,7 +31,7 @@ - - diff --git a/src/components/Settings/WallpaperSettings.vue b/src/components/Settings/WallpaperSettings.vue new file mode 100644 index 00000000..8f3e313f --- /dev/null +++ b/src/components/Settings/WallpaperSettings.vue @@ -0,0 +1,179 @@ + + + + + diff --git a/src/pages/Board.vue b/src/pages/Board.vue index 70ea3775..a768bf67 100755 --- a/src/pages/Board.vue +++ b/src/pages/Board.vue @@ -94,14 +94,14 @@ export default { async loadBoardWallpaper() { this.wallpaperUrl = this.board.wallpaper - ? await this.$store.dispatch('LOAD_FIRESTORE_FILE', this.board.wallpaper) + ? await this.$store.dispatch('LOAD_WALLPAPER', this.board.wallpaper) : null; }, loadBoardGames() { const { lists } = this.board; - if (lists.length === 0) { + if (lists && lists.length === 0) { this.$bvModal.show('add-list'); } diff --git a/src/store/actions.js b/src/store/actions.js index 61fc5a33..7c2f29f4 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -95,17 +95,18 @@ export default { }, - DELETE_FIRESTORE_FILE(context, path) { + DELETE_WALLPAPER({ commit }, { fullPath }) { return new Promise((resolve, reject) => { - firebase.storage().ref(path).delete() + firebase.storage().ref(fullPath).delete() .then(() => { + commit('REMOVE_WALLPAPER', fullPath); resolve(); }) .catch(reject); }); }, - LOAD_FIRESTORE_FILE(context, path) { + LOAD_WALLPAPER(context, path) { const storage = firebase.storage().ref(); return new Promise((resolve, reject) => { @@ -117,13 +118,72 @@ export default { }); }, - async LOAD_WALLPAPERS({ state }) { + // TODO: commit wallpapers to store + LOAD_WALLPAPERS({ state, commit }) { const storage = firebase.storage().ref(`${state.user.uid}/wallpapers`); return new Promise((resolve, reject) => { storage .listAll() - .then(({ items }) => resolve(items.map(({ fullPath }) => fullPath))) + .then(({ items }) => { + const wallpapers = items.map(({ fullPath, name }) => { + const wallpaper = { + fullPath, + name, + }; + + return wallpaper; + }); + + // TODO: refactor? there's gotta be a better way to do this + const fetchedUrls = []; + + wallpapers.forEach(({ fullPath }, index) => { + firebase.storage() + .ref() + .child(fullPath).getDownloadURL() + .then((url) => { + fetchedUrls.push(url); + + wallpapers[index].url = url; + + if (fetchedUrls.length === wallpapers.length) { + commit('SET_WALLPAPERS', wallpapers); + resolve(); + } + }) + .catch(reject); + }); + }) + .catch(reject); + }); + }, + + UPLOAD_WALLPAPER({ state, commit }, file) { + return new Promise((resolve, reject) => { + firebase.storage() + .ref(`${state.user.uid}/wallpapers/${file.name}`) + .put(file) + .then((response) => { + if (response.state === 'success') { + const { metadata: { fullPath, name } } = response; + + firebase.storage() + .ref() + .child(fullPath).getDownloadURL() + .then((url) => { + const wallpaper = { + fullPath, + name, + url, + }; + + commit('ADD_WALLPAPER', wallpaper); + resolve(); + }) + .catch(reject); + } + }) .catch(reject); }); }, diff --git a/src/store/mutations.js b/src/store/mutations.js index cb0ce962..0b3e205c 100755 --- a/src/store/mutations.js +++ b/src/store/mutations.js @@ -42,6 +42,20 @@ export default { state.boardGames = boardGames; }, + SET_WALLPAPERS(state, wallpapers) { + state.wallpapers = wallpapers; + }, + + REMOVE_WALLPAPER(state, fullPath) { + const wallpaperIndex = state.wallpapers.findIndex(wallpaper => wallpaper.fullPath === fullPath); + + state.wallpapers.splice(wallpaperIndex, 1); + }, + + ADD_WALLPAPER(state, wallpaper) { + state.wallpapers.push(wallpaper); + }, + ADD_BOARD(state, board) { state.boards.push(board); }, diff --git a/src/store/state.js b/src/store/state.js index acb361c1..d112b422 100755 --- a/src/store/state.js +++ b/src/store/state.js @@ -19,4 +19,5 @@ export default { publicGameData: {}, game: null, wallpaperUrl: null, + wallpapers: [], };