Adds update backup functionality

This commit is contained in:
Alicia Sykes 2021-05-24 17:39:05 +01:00
parent 44b2594dfa
commit bdb6e310e8
2 changed files with 29 additions and 7 deletions

View File

@ -64,7 +64,7 @@ import Button from '@/components/FormElements/Button';
import Input from '@/components/FormElements/Input'; import Input from '@/components/FormElements/Input';
import IconBackup from '@/assets/interface-icons/config-backup.svg'; import IconBackup from '@/assets/interface-icons/config-backup.svg';
import IconRestore from '@/assets/interface-icons/config-restore.svg'; import IconRestore from '@/assets/interface-icons/config-restore.svg';
import { backup } from '@/utils/CloudBackup'; import { backup, update } from '@/utils/CloudBackup';
import { localStorageKeys } from '@/utils/defaults'; import { localStorageKeys } from '@/utils/defaults';
export default { export default {
@ -89,8 +89,10 @@ export default {
methods: { methods: {
checkPass() { checkPass() {
const savedHash = localStorage[localStorageKeys.BACKUP_HASH] || undefined; const savedHash = localStorage[localStorageKeys.BACKUP_HASH] || undefined;
if (!savedHash || savedHash === this.makeHash(this.backupPassword)) { if (!savedHash) {
this.makeBackup(); this.makeBackup();
} else if (savedHash === this.makeHash(this.backupPassword)) {
this.makeUpdate();
} else { } else {
this.showErrorMsg('Incorrect password. Please enter the password you used last time.'); this.showErrorMsg('Incorrect password. Please enter the password you used last time.');
} }
@ -101,17 +103,29 @@ export default {
if (!response.data || response.data.errorMsg || !response.data.backupId) { if (!response.data || response.data.errorMsg || !response.data.backupId) {
this.showErrorMsg(response.data.errorMsg || 'Error'); this.showErrorMsg(response.data.errorMsg || 'Error');
} else { // All clear, no error } else { // All clear, no error
this.updateAfterBackup(response.data.backupId); this.updateUiAfterBackup(response.data.backupId, false);
} }
}).catch(() => { }).catch(() => {
this.showErrorMsg('Unable to process request'); this.showErrorMsg('Unable to process request');
}); });
}, },
updateAfterBackup(backupId) { makeUpdate() {
update(this.config, this.backupPassword, this.backupId)
.then((response) => {
if (!response.data || response.data.errorMsg || !response.data.backupId) {
this.showErrorMsg(response.data.errorMsg || 'Error');
} else { // All clear, no error
this.updateUiAfterBackup(response.data.backupId, true);
}
}).catch(() => {
this.showErrorMsg('Unable to process request');
});
},
updateUiAfterBackup(backupId, isUpdate = false) {
const hash = this.makeHash(this.backupPassword); const hash = this.makeHash(this.backupPassword);
localStorage.setItem(localStorageKeys.BACKUP_ID, backupId); localStorage.setItem(localStorageKeys.BACKUP_ID, backupId);
localStorage.setItem(localStorageKeys.BACKUP_HASH, hash); localStorage.setItem(localStorageKeys.BACKUP_HASH, hash);
this.showSuccessMsg('Backup Completed Succesfully'); this.showSuccessMsg(`${isUpdate ? 'Update' : 'Backup'} Completed Succesfully`);
this.backupPassword = ''; this.backupPassword = '';
}, },
showErrorMsg(errorMsg) { showErrorMsg(errorMsg) {
@ -141,9 +155,8 @@ export default {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
width: fit-content; width: fit-content;
min-width: 400px;
margin: 0 auto 1rem auto; margin: 0 auto 1rem auto;
padding-bottom: 1rem; padding: 0 0.5rem 1rem 0.5rem;
&:first-child { &:first-child {
border-bottom: 1px dashed var(--config-settings-color); border-bottom: 1px dashed var(--config-settings-color);
} }

View File

@ -29,6 +29,15 @@ export const backup = (data, password) => {
}); });
}; };
/* Updates and existing backup */
export const update = (data, password, backupId) => {
return axios.put(ENDPOINT, {
backupId,
userData: encryptData(data, password),
subHash: makeSubHash(password),
});
};
/* Restores the backup */ /* Restores the backup */
export const restore = (backupId, password) => { export const restore = (backupId, password) => {
// return axios.get(ENDPOINT, { // return axios.get(ENDPOINT, {