diff --git a/front/js/helpers.js b/front/js/helpers.js index 4814bd8..77ca005 100644 --- a/front/js/helpers.js +++ b/front/js/helpers.js @@ -83,3 +83,74 @@ export function apiGet(url, options = {}) { }, }).then((r) => r.json()); } + +export function isEqual(db, ed) { + let isObject = (x) => + typeof x === "object" && !Array.isArray(x) && x !== null; + let isArray = (x) => x?.constructor === Array; + let isEmpty = (x) => x === null || x === undefined || x == []; + const db_value = db ?? null; + const ed_value = ed ?? null; + + if (isObject(db_value)) { + let keys = Object.keys(db_value); + return keys.reduce((acc, k) => { + if (acc === false) { + return false; + } + return isEqual(db_value?.[k], ed_value?.[k]); + }, true); + } + if (isArray(db_value)) { + return db_value.reduce((c, v, i) => { + if (c === false) { + return false; + } + return isEqual(v, ed_value?.[i]); + }, true); + } + + if (isEmpty(db_value) && isEmpty(ed_value)) { + return true; + } + + if (db_value == ed_value) { + return true; + } + + return false; + + // const ok = Object.keys, + // tx = typeof x, + // ty = typeof y; + // return x && y && tx === "object" && tx === ty + // ? ok(x).length === ok(y).length && + // ok(x).every((key) => isEqual(x[key], y[key])) + // : x === y; +} + +export function debounce(fn, delay) { + let timer; + return (...args) => { + clearTimeout(timer); + timer = setTimeout(() => fn(...args), delay); + }; +} + +export function arrayUnique(array) { + return array.filter((value, index) => array.indexOf(value) === index); +} + +export function arrayUniqueBy(items, uniqueBy) { + const ids = new Set(items.map((item) => item[uniqueBy])); + return [...ids].map((id) => items.find((i) => i[uniqueBy] === id)); +} +export function arrayUniqueCb(items, aFilter) { + const cache = new Set(); + return items.filter((item) => { + const cacheValue = aFilter(item); + if (cache.has(cacheValue)) return false; + cache.add(cacheValue); + return true; + }); +} diff --git a/front/js/svelte/Channel.svelte b/front/js/svelte/Channel.svelte index 0d55838..b408ec4 100644 --- a/front/js/svelte/Channel.svelte +++ b/front/js/svelte/Channel.svelte @@ -25,10 +25,8 @@ // export let layout; export let channel; - export let axios; export let readableSchemas; - setContext("axios", axios); setContext("channel", channel); setContext( "readableSchemas", diff --git a/front/js/svelte/account/Profile.svelte b/front/js/svelte/account/Profile.svelte index 180fbfb..b160077 100644 --- a/front/js/svelte/account/Profile.svelte +++ b/front/js/svelte/account/Profile.svelte @@ -2,8 +2,9 @@ import ErrorAlert from "../common/ErrorAlert.svelte"; import SpinnerButton from "../common/SpinnerButton.svelte"; import Avatar from "./Avatar.svelte"; - import {getContext} from "svelte"; + import { getContext } from "svelte"; import SuccessAlert from "../common/SuccessAlert.svelte"; + import { apiPost } from "../../helpers"; const user = getContext("user"); const channel = getContext("channel"); @@ -16,16 +17,15 @@ e.preventDefault(); errorMessage = ""; - axios - .post(channel.lucentUrl + "/account/update-name", { - name: name, - }) + apiPost(channel.lucentUrl + "/account/update-name", { + name: name, + }) .then((response) => { successAlert.show(); }) .catch((error) => { errorMessage = error.response?.data.error; - console.log({errorMessage}); + console.log({ errorMessage }); }); } @@ -33,55 +33,55 @@ e.preventDefault(); errorMessage = ""; - axios - .post(channel.lucentUrl + "/account/update-email", { - email: email, - }) + apiPost(channel.lucentUrl + "/account/update-email", { + email: email, + }) .then((response) => { successAlert.show(); }) .catch((error) => { errorMessage = error.response?.data.error; - console.log({errorMessage}); + console.log({ errorMessage }); }); } -