From 2cc5051ab4a8abd98c490f0b4d39e6eb9c1f5dc0 Mon Sep 17 00:00:00 2001 From: Kevin Thomas Date: Wed, 25 May 2022 21:09:41 -0700 Subject: [PATCH] Warn on unsaved changes --- package.json | 2 +- src/components/Noise.vue | 34 ++++++++++++- src/components/noise.js | 102 +++++++++++++++++++++++++++++---------- 3 files changed, 110 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 07c2514..df8a339 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "bugs": "https://github.com/kaythomas0/noisedash/issues", "license": "AGPL-3.0-or-later", "author": "Kay Thomas (https://kaythomas.dev)", - "version": "0.2.0", + "version": "0.4.0", "private": true, "scripts": { "serve": "vue-cli-service serve", diff --git a/src/components/Noise.vue b/src/components/Noise.vue index 97aadfb..54640de 100644 --- a/src/components/Noise.vue +++ b/src/components/Noise.vue @@ -66,7 +66,7 @@ class="mx-3 my-3" @click="updateProfile" > - Save Profile + {{ unsavedWork ? 'Save Profile*' : 'Save Profile' }} @@ -365,6 +365,36 @@ + + + + + Save Profile? + + + You have unsaved work on your current profile. Would you like to save it before switching? + + + + + Discard Changes + + + Save Profile + + + + diff --git a/src/components/noise.js b/src/components/noise.js index 2aee06e..9b0b59b 100644 --- a/src/components/noise.js +++ b/src/components/noise.js @@ -70,6 +70,11 @@ export default { recordedProfile: {}, recordingFileName: '', isRecordingValid: false, + unsavedWork: false, + saveProfileText: 'Save Profile', + unwatch: null, + confirmSwitchProfileDialog: false, + activeProfile: {}, errorSnackbar: false, errorSnackbarText: '', rules: { @@ -94,6 +99,26 @@ export default { } }) return samples + }, + changeableSettings: function () { + return [ + this.isTimerEnabled, + this.hours, + this.minutes, + this.seconds, + this.volume, + this.noiseColor, + this.isFilterEnabled, + this.filterType, + this.filterCutoff, + this.isLFOFilterCutoffEnabled, + this.lfoFilterCutoffFrequency, + this.lfoFilterCutoffRange, + this.isTremoloEnabled, + this.tremoloDepth, + this.isTimerEnabled, + this.loadedSamples + ] } }, created () { @@ -294,6 +319,7 @@ export default { if (response.status === 200) { this.profileDialog = false this.populateProfileItems(response.data.id) + this.unsavedWork = false this.infoSnackbarText = 'Profile Saved' this.infoSnackbar = true } @@ -322,6 +348,7 @@ export default { samples: this.loadedSamples }).then(response => { if (response.status === 200) { + this.unsavedWork = false this.infoSnackbarText = 'Profile Saved' this.infoSnackbar = true } @@ -332,33 +359,47 @@ export default { }) }, loadProfile () { - this.$http.get('/profiles/'.concat(this.selectedProfile.id)) - .then(response => { - if (response.status === 200) { - const profile = response.data.profile + if (this.unsavedWork) { + this.confirmSwitchProfileDialog = true + } else { + this.$http.get('/profiles/'.concat(this.selectedProfile.id)) + .then(response => { + if (response.status === 200) { + const profile = response.data.profile - this.isTimerEnabled = profile.isTimerEnabled - this.duration = profile.duration - this.volume = profile.volume - this.noiseColor = profile.noiseColor - this.isFilterEnabled = profile.isFilterEnabled - this.filterType = profile.filterType - this.filterCutoff = profile.filterCutoff - this.isLFOFilterCutoffEnabled = profile.isLFOFilterCutoffEnabled - this.lfoFilterCutoffFrequency = profile.lfoFilterCutoffFrequency - this.lfoFilterCutoffRange[0] = profile.lfoFilterCutoffLow - this.lfoFilterCutoffRange[1] = profile.lfoFilterCutoffHigh - this.isTremoloEnabled = profile.isTremoloEnabled - this.tremoloFrequency = profile.tremoloFrequency - this.tremoloDepth = profile.tremoloDepth + this.isTimerEnabled = profile.isTimerEnabled + this.duration = profile.duration + this.volume = profile.volume + this.noiseColor = profile.noiseColor + this.isFilterEnabled = profile.isFilterEnabled + this.filterType = profile.filterType + this.filterCutoff = profile.filterCutoff + this.isLFOFilterCutoffEnabled = profile.isLFOFilterCutoffEnabled + this.lfoFilterCutoffFrequency = profile.lfoFilterCutoffFrequency + this.lfoFilterCutoffRange[0] = profile.lfoFilterCutoffLow + this.lfoFilterCutoffRange[1] = profile.lfoFilterCutoffHigh + this.isTremoloEnabled = profile.isTremoloEnabled + this.tremoloFrequency = profile.tremoloFrequency + this.tremoloDepth = profile.tremoloDepth - this.loadedSamples = profile.samples - } - }) - .catch(() => { - this.errorSnackbarText = 'Error Loading Profile' - this.errorSnackbar = true - }) + this.loadedSamples = profile.samples + + this.activeProfile = profile + + if (this.unwatch) { + this.unwatch() + } + + this.unwatch = this.$watch('changeableSettings', function () { + this.unsavedWork = true + }) + } + }) + .catch(() => { + this.errorSnackbarText = 'Error Loading Profile' + this.errorSnackbar = true + }) + } }, deleteProfile () { this.$http.delete('/profiles/'.concat(this.selectedProfile.id)) @@ -749,6 +790,17 @@ export default { clearInterval(this.recordingInterval) this.recordingDialog = false this.stop() + }, + discardChanges () { + this.unsavedWork = false + this.loadProfile() + this.confirmSwitchProfileDialog = false + }, + saveChanges () { + // Set active profile back to previously selected one before saving + this.selectedProfile = this.profileItems.find(p => p.text === this.activeProfile.name) + this.updateProfile() + this.confirmSwitchProfileDialog = false } } }