diff --git a/src/components/DataSync.svelte b/src/components/DataSync.svelte index d5ce6ed7..782753ec 100644 --- a/src/components/DataSync.svelte +++ b/src/components/DataSync.svelte @@ -7,6 +7,7 @@ import { getLocalSaveJson, updateSave, updateTime, UPDATE_TIME_KEY } from '../stores/saveManager'; import SyncConflictModal from '../components/SyncConflictModal.svelte'; + import { pushToast } from '../stores/toast'; const { open: openModal, close: closeModal } = getContext('simple-modal'); @@ -25,6 +26,10 @@ onMount(() => { startSync(); + + window.onerror = function () { + handleError(); + }; }); function startSync() { @@ -42,6 +47,7 @@ driveLoading.set(false); driveError.set(true); synced.set(true); + pushToast('Drive sync not available right now 😔', 'error'); } function handleClientLoad() { @@ -114,6 +120,7 @@ ); } else { synced.set(true); + pushToast('Data has been synced!'); } } catch (err) { console.error(err); @@ -178,7 +185,7 @@ } async function getData() { - console.log('reading remote file'); + console.log('reading remote file now'); try { const { result } = await gapi.client.drive.files.get({ @@ -186,10 +193,10 @@ alt: 'media', }); - console.log(result); + console.log('get file res', result); return result; } catch (err) { - console.error(err); + console.error('get file res error', err); handleError(); } } @@ -227,10 +234,7 @@ }, function (error) { console.error(error); - driveSignedIn.set(false); - driveLoading.set(false); - driveError.set(true); - synced.set(true); + handleError(); }, ); } diff --git a/src/components/Toast.svelte b/src/components/Toast.svelte new file mode 100644 index 00000000..8b092af7 --- /dev/null +++ b/src/components/Toast.svelte @@ -0,0 +1,21 @@ + + +
+ {#each $toasts as toast (toast._id)} +
+ {toast.msg} +
+ {/each} +
diff --git a/src/routes/_layout.svelte b/src/routes/_layout.svelte index 51893977..b5bb052b 100644 --- a/src/routes/_layout.svelte +++ b/src/routes/_layout.svelte @@ -15,6 +15,7 @@ import { checkLocalSave } from '../stores/saveManager'; import TodoData from '../components/TodoData.svelte'; import SettingData from '../components/SettingData.svelte'; + import Toast from '../components/Toast.svelte'; export let segment; @@ -78,6 +79,7 @@ +
diff --git a/src/stores/saveManager.js b/src/stores/saveManager.js index 9df9aa6d..bd6c30a1 100644 --- a/src/stores/saveManager.js +++ b/src/stores/saveManager.js @@ -3,6 +3,7 @@ import { writable } from 'svelte/store'; import debounce from 'lodash/debounce'; import { synced, saveId, localModified, lastSyncTime } from './dataSync'; +import { pushToast } from './toast'; export const updateTime = writable(null); export const fromRemote = writable(false); @@ -36,8 +37,12 @@ async function saveData(data) { }); synced.set(true); localModified.set(false); + + pushToast('Data has been synced!') } catch (err) { console.error(err); + pushToast('Error when uploading your data!', 'error') + synced.set(true); } } diff --git a/src/stores/toast.js b/src/stores/toast.js new file mode 100644 index 00000000..7fc5e5cc --- /dev/null +++ b/src/stores/toast.js @@ -0,0 +1,29 @@ +import { writable } from 'svelte/store'; + +export const toasts = writable([]); + +let toastId = 0; + +const unshiftToast = () => { + toasts.update((val) => { + return val.slice(1); + }); +}; + +export const pushToast = (msg = '', type = 'default') => { + toasts.update((val) => { + const updatedToasts = [ + ...val, + { + _id: ++toastId, + msg, + type, + }, + ]; + setTimeout(() => { + unshiftToast(); + }, 3500); + + return updatedToasts; + }); +};