From b1feefc8643227faecef92f81ff9f62cf07ddd9d Mon Sep 17 00:00:00 2001 From: secondl1ght Date: Mon, 18 Dec 2023 22:04:30 -0700 Subject: [PATCH] move clearing tables into function --- src/lib/sync/areas.ts | 44 ++--------------------- src/lib/sync/clearTables.ts | 23 ++++++++++++ src/lib/sync/elements.ts | 48 ++----------------------- src/lib/sync/events.ts | 44 ++--------------------- src/lib/sync/reports.ts | 71 ++----------------------------------- src/lib/sync/users.ts | 65 ++------------------------------- 6 files changed, 38 insertions(+), 257 deletions(-) create mode 100644 src/lib/sync/clearTables.ts diff --git a/src/lib/sync/areas.ts b/src/lib/sync/areas.ts index b17b5cd8..43a3e6ef 100644 --- a/src/lib/sync/areas.ts +++ b/src/lib/sync/areas.ts @@ -1,4 +1,5 @@ import { areaError, areas } from '$lib/store'; +import { clearTables } from '$lib/sync/clearTables'; import type { Area } from '$lib/types'; import axios from 'axios'; import axiosRetry from 'axios-retry'; @@ -9,47 +10,8 @@ axiosRetry(axios, { retries: 3 }); const limit = 500; export const areasSync = async () => { - // clear v1 table if present - await localforage - .getItem('areas') - .then(function (value) { - if (value) { - localforage - .removeItem('areas') - .then(function () { - console.log('Key is cleared!'); - }) - .catch(function (err) { - areaError.set('Could not clear areas locally, please try again or contact BTC Map.'); - console.log(err); - }); - } - }) - .catch(function (err) { - areaError.set('Could not check areas locally, please try again or contact BTC Map.'); - console.log(err); - }); - - // clear v2 table if present - await localforage - .getItem('areas_v2') - .then(function (value) { - if (value) { - localforage - .removeItem('areas_v2') - .then(function () { - console.log('Key is cleared!'); - }) - .catch(function (err) { - areaError.set('Could not clear areas locally, please try again or contact BTC Map.'); - console.log(err); - }); - } - }) - .catch(function (err) { - areaError.set('Could not check areas locally, please try again or contact BTC Map.'); - console.log(err); - }); + // clear tables if present + clearTables(['areas', 'areas_v2']); // get areas from local await localforage diff --git a/src/lib/sync/clearTables.ts b/src/lib/sync/clearTables.ts new file mode 100644 index 00000000..c7fa4597 --- /dev/null +++ b/src/lib/sync/clearTables.ts @@ -0,0 +1,23 @@ +import localforage from 'localforage'; + +export const clearTables = (tables: string[]) => { + for (const table of tables) { + localforage + .getItem(table) + .then(function (value) { + if (value) { + localforage + .removeItem(table) + .then(function () { + console.log('Key is cleared!'); + }) + .catch(function (err) { + console.log(err); + }); + } + }) + .catch(function (err) { + console.log(err); + }); + } +}; diff --git a/src/lib/sync/elements.ts b/src/lib/sync/elements.ts index d10d6d39..8daafcd6 100644 --- a/src/lib/sync/elements.ts +++ b/src/lib/sync/elements.ts @@ -1,4 +1,5 @@ import { elementError, elements, elementsSyncCount, mapUpdates } from '$lib/store'; +import { clearTables } from '$lib/sync/clearTables'; import type { Element } from '$lib/types'; import axios from 'axios'; import axiosRetry from 'axios-retry'; @@ -10,51 +11,8 @@ axiosRetry(axios, { retries: 3 }); const limit = 5000; export const elementsSync = async () => { - // clear v1 table if present - await localforage - .getItem('elements') - .then(function (value) { - if (value) { - localforage - .removeItem('elements') - .then(function () { - console.log('Key is cleared!'); - }) - .catch(function (err) { - elementError.set( - 'Could not clear elements locally, please try again or contact BTC Map.' - ); - console.log(err); - }); - } - }) - .catch(function (err) { - elementError.set('Could not check elements locally, please try again or contact BTC Map.'); - console.log(err); - }); - - // clear v2 table if present - await localforage - .getItem('elements_v2') - .then(function (value) { - if (value) { - localforage - .removeItem('elements_v2') - .then(function () { - console.log('Key is cleared!'); - }) - .catch(function (err) { - elementError.set( - 'Could not clear elements locally, please try again or contact BTC Map.' - ); - console.log(err); - }); - } - }) - .catch(function (err) { - elementError.set('Could not check elements locally, please try again or contact BTC Map.'); - console.log(err); - }); + // clear tables if present + clearTables(['elements', 'elements_v2']); // get elements from local await localforage diff --git a/src/lib/sync/events.ts b/src/lib/sync/events.ts index c82e4ac5..2e215779 100644 --- a/src/lib/sync/events.ts +++ b/src/lib/sync/events.ts @@ -1,4 +1,5 @@ import { eventError, events } from '$lib/store'; +import { clearTables } from '$lib/sync/clearTables'; import type { Event } from '$lib/types'; import axios from 'axios'; import axiosRetry from 'axios-retry'; @@ -9,47 +10,8 @@ axiosRetry(axios, { retries: 3 }); const limit = 50000; export const eventsSync = async () => { - // clear v1 table if present - await localforage - .getItem('events') - .then(function (value) { - if (value) { - localforage - .removeItem('events') - .then(function () { - console.log('Key is cleared!'); - }) - .catch(function (err) { - eventError.set('Could not clear events locally, please try again or contact BTC Map.'); - console.log(err); - }); - } - }) - .catch(function (err) { - eventError.set('Could not check events locally, please try again or contact BTC Map.'); - console.log(err); - }); - - // clear v2 table if present - await localforage - .getItem('events_v2') - .then(function (value) { - if (value) { - localforage - .removeItem('events_v2') - .then(function () { - console.log('Key is cleared!'); - }) - .catch(function (err) { - eventError.set('Could not clear events locally, please try again or contact BTC Map.'); - console.log(err); - }); - } - }) - .catch(function (err) { - eventError.set('Could not check events locally, please try again or contact BTC Map.'); - console.log(err); - }); + // clear tables if present + clearTables(['events', 'events_v2']); // get events from local await localforage diff --git a/src/lib/sync/reports.ts b/src/lib/sync/reports.ts index 95107781..0093ba81 100644 --- a/src/lib/sync/reports.ts +++ b/src/lib/sync/reports.ts @@ -1,4 +1,5 @@ import { reportError, reports } from '$lib/store'; +import { clearTables } from '$lib/sync/clearTables'; import type { Report } from '$lib/types'; import axios from 'axios'; import axiosRetry from 'axios-retry'; @@ -9,74 +10,8 @@ axiosRetry(axios, { retries: 3 }); const limit = 20000; export const reportsSync = async () => { - // clear v1 table if present - await localforage - .getItem('reports') - .then(function (value) { - if (value) { - localforage - .removeItem('reports') - .then(function () { - console.log('Key is cleared!'); - }) - .catch(function (err) { - reportError.set( - 'Could not clear reports locally, please try again or contact BTC Map.' - ); - console.log(err); - }); - } - }) - .catch(function (err) { - reportError.set('Could not check reports locally, please try again or contact BTC Map.'); - console.log(err); - }); - - // clear v2 table if present - await localforage - .getItem('reports_v2') - .then(function (value) { - if (value) { - localforage - .removeItem('reports_v2') - .then(function () { - console.log('Key is cleared!'); - }) - .catch(function (err) { - reportError.set( - 'Could not clear reports locally, please try again or contact BTC Map.' - ); - console.log(err); - }); - } - }) - .catch(function (err) { - reportError.set('Could not check reports locally, please try again or contact BTC Map.'); - console.log(err); - }); - - // clear v3 table if present - await localforage - .getItem('reports_v3') - .then(function (value) { - if (value) { - localforage - .removeItem('reports_v3') - .then(function () { - console.log('Key is cleared!'); - }) - .catch(function (err) { - reportError.set( - 'Could not clear reports locally, please try again or contact BTC Map.' - ); - console.log(err); - }); - } - }) - .catch(function (err) { - reportError.set('Could not check reports locally, please try again or contact BTC Map.'); - console.log(err); - }); + // clear tables if present + clearTables(['reports', 'reports_v2', 'reports_v3']); // get reports from local await localforage diff --git a/src/lib/sync/users.ts b/src/lib/sync/users.ts index 91108648..aeaf4870 100644 --- a/src/lib/sync/users.ts +++ b/src/lib/sync/users.ts @@ -1,4 +1,5 @@ import { userError, users } from '$lib/store'; +import { clearTables } from '$lib/sync/clearTables'; import type { User } from '$lib/types'; import axios from 'axios'; import axiosRetry from 'axios-retry'; @@ -9,68 +10,8 @@ axiosRetry(axios, { retries: 3 }); const limit = 7500; export const usersSync = async () => { - // clear potentially broken users v1 sync due to top level ID changing from string to int - await localforage - .getItem('users') - .then(function (value) { - if (value) { - localforage - .removeItem('users') - .then(function () { - console.log('Key is cleared!'); - }) - .catch(function (err) { - userError.set('Could not clear users locally, please try again or contact BTC Map.'); - console.log(err); - }); - } - }) - .catch(function (err) { - userError.set('Could not check users locally, please try again or contact BTC Map.'); - console.log(err); - }); - - // clear v2 table if present - await localforage - .getItem('users_v2') - .then(function (value) { - if (value) { - localforage - .removeItem('users_v2') - .then(function () { - console.log('Key is cleared!'); - }) - .catch(function (err) { - userError.set('Could not clear users locally, please try again or contact BTC Map.'); - console.log(err); - }); - } - }) - .catch(function (err) { - userError.set('Could not check users locally, please try again or contact BTC Map.'); - console.log(err); - }); - - // clear v3 table if present - await localforage - .getItem('users_v3') - .then(function (value) { - if (value) { - localforage - .removeItem('users_v3') - .then(function () { - console.log('Key is cleared!'); - }) - .catch(function (err) { - userError.set('Could not clear users locally, please try again or contact BTC Map.'); - console.log(err); - }); - } - }) - .catch(function (err) { - userError.set('Could not check users locally, please try again or contact BTC Map.'); - console.log(err); - }); + // clear tables if present + clearTables(['users', 'users_v2', 'users_v3']); // get users from local await localforage