Skip to content

Commit

Permalink
refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
dlaxcess committed Feb 1, 2024
1 parent 67a73c1 commit 3d97f56
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 83 deletions.
30 changes: 7 additions & 23 deletions svelte/components/Input/Config/Config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { UserConfig } from "./configTypes";

export default class Config {
// private _listeners: Record<string, Function[]>;

attributes: UserConfig;

propertyChangedEvent = new Event("propertyChanged");
Expand Down Expand Up @@ -81,39 +79,25 @@ export default class Config {
}

addField(namespace: string): void {
this.attributes[namespace].fields = [...this.attributes[namespace].fields, { key: "", value: "" }];
const fields = this.attributes[namespace].fieldsGroups[this.attributes[namespace].default.value].fields;
this.attributes[namespace].fieldsGroups[this.attributes[namespace].default.value].fields = [...fields, { key: "", value: "" }];
console.log("addField ~ this.attributes[namespace].fields:", this.attributes[namespace].fields);
// attributes[namespace].fields = [...attributes[namespace].fields, { key: "", value: "" }];

// this.dispatch("propertyChanged", { property: "myProperty", value: namespace });

document.dispatchEvent(this.propertyChangedEvent);
}

removeField(namespace: string, i: number): void {
if (this.attributes[namespace].fields.length > 1) {
this.attributes[namespace].fields = this.attributes[namespace].fields.filter((_, index) => i !== index);
const fields = this.attributes[namespace].fieldsGroups[this.attributes[namespace].default.value].fields;
if (fields.length > 1) {
this.attributes[namespace].fieldsGroups[this.attributes[namespace].default.value].fields = fields.filter((_, index) => i !== index);
} else {
this.attributes[namespace].fields[i] = { key: "", value: "" };
this.attributes[namespace].fieldsGroups[this.attributes[namespace].default.value].fields[i] = { key: "", value: "" };
}

document.dispatchEvent(this.propertyChangedEvent);
}

// dispatch(eventName: string, data?: any) {
// if (this._listeners[eventName]) {
// this._listeners[eventName].forEach(callback => callback(data));
// }
// }

// on(eventName: string, callback: Function) {
// if (!this._listeners[eventName]) {
// this._listeners[eventName] = [];
// }
// this._listeners[eventName].push(callback);
// }
}


//////////////////////////////////////////////
// actual localStorage storage structure
let storage = {
Expand Down
2 changes: 0 additions & 2 deletions svelte/components/Input/Config/ConfigFieldsLoop.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<script lang="ts">
export let attributesObject;
// export let attributes;
// export let attribute;
export let fieldsGroup;
export let namespace;
Expand Down
103 changes: 45 additions & 58 deletions svelte/components/Input/Config/InputConfigFields.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script lang="ts">
import type { Properties } from "@lib/common/types";
import type { UserConfig } from "./configTypes";
import { onMount } from "svelte";
Expand All @@ -11,9 +10,6 @@
// <InputConfigFields />
// Set User config
/////////////////////////////////////////////////
// let userConfig: { namespace: {fields: [{ key: "" , value: ""}]} };
let userConfig;
let attributesObject;
let attributes: UserConfig;
Expand Down Expand Up @@ -51,69 +47,61 @@
// if (!localConfigGet("gateway")) localConfigSet("gateway", BEE_GATEWAY_DEFAULT);
// if (!localConfigGet("batchId")) localConfigSet("batchId", BEE_BATCHID_DEFAULT);
// if (!localConfigGet("bzzChainId")) localConfigSet("bzzChainId", BZZ_CHAIN_ID_DEFAULT);
// Object.entries(localStorage)
// .filter(([namespaceKey, fields]) => namespaceKey.startsWith(`${krdNamespace}.`))
// .forEach(([namespaceKey, fields]) => {
// const namespace = localConfigGetKey(namespaceKey);
// console.log(".forEach ~ namespace:", namespace);
// let localFieldsObject = JSON.parse(fields);
// if (attributes[namespace].fields) {
// Object.entries(localFieldsObject).forEach(([key, value]) => {
// if (attributes[namespace].fields.some((field) => field.key === key)) {
// attributes[namespace].fields = attributes[namespace].fields.map((field) =>
// field.key === key ? { ...field, value: value.toString() } : field
// );
// } else {
// attributes[namespace].fields.push({ key, value: value.toString() });
// }
// });
// }
// });
// for (const namespace of Object.keys(attributes)) {
// const defaultValue = attributes[namespace].default.value;
// if (attributes[namespace].editable && attributes[namespace][defaultValue].fields.length == 0) {
// attributes[namespace][defaultValue].fields.push({ key: "", value: "" });
// }
// }
Object.entries(localStorage)
.filter(([namespaceKey, _]) => namespaceKey.startsWith(`${krdNamespace}.`))
.forEach(([namespaceKey, fields]) => {
const namespace = localConfigGetKey(namespaceKey);
console.log(".forEach ~ namespace:", namespace);
let localFieldsObject = JSON.parse(fields);
attributes[namespace].default.value = localFieldsObject.default;
Object.entries(localFieldsObject).forEach(([localFieldsObjectKey, ObjectEntries]) => {
Object.entries(ObjectEntries).forEach(([key, value]) => {
if (attributes[namespace].fieldsGroups[localFieldsObjectKey]?.fields.some((field) => field.key === key)) {
attributes[namespace].fieldsGroups[localFieldsObjectKey].fields = attributes[namespace].fieldsGroups[
localFieldsObjectKey
].fields.map((field) => (field.key === key ? { ...field, value: value.toString() } : field));
} else {
attributes[namespace].fieldsGroups[localFieldsObjectKey]?.fields.push({ key, value: value.toString() });
}
});
});
});
for (const namespace of Object.keys(attributes)) {
const defaultValue = attributes[namespace].default.value;
const defaultFieldsGroups = attributes[namespace].fieldsGroups[defaultValue];
if (defaultFieldsGroups.editable && defaultFieldsGroups.fields.length == 0) {
attributes[namespace].fieldsGroups[defaultValue].fields.push({ key: "", value: "" });
}
}
};
console.log("ATTRIBUTES LOADED:", attributes);
/////////////////////////
const saveConfig = () => {
if (typeof localStorage !== "undefined") {
for (const [namespace, fields] of Object.entries(userConfig)) {
let namespaceValues = Object.entries(fields).reduce((valuesObject, [key, value]) => {
valuesObject[key] = value;
return {
...valuesObject,
[key]: value
};
}, {});
localConfigSet(namespace, JSON.stringify(namespaceValues));
}
Object.entries(attributes).forEach(([namespace, attribute]) => {
const defaultValue = attribute.default.value;
let storageFields = {};
Object.entries(attribute.fieldsGroups).forEach(([key, fieldGroup]) => {
fieldGroup.fields.forEach((field) => {
if (field.key !== "" && field.value !== "")
storageFields[key] = { ...storageFields[key], [field.key]: field.value };
});
});
if (Object.keys(storageFields).length > 0) {
storageFields.default = defaultValue;
localConfigSet(namespace, JSON.stringify(storageFields));
}
});
}
};
// $: attributes && handleProperties();
// const handleProperties = () => {
// userConfig = {};
// Object.entries(attributes).forEach(([namespace, attribute]) => {
// console.log("fields ~ namespace:", namespace);
// console.log("fields ~ attribute:", attribute);
// const defaultValue = attribute.default.value;
// if (attribute[defaultValue].fields) {
// userConfig[namespace] = {[defaultValue]: {}};
// attribute[defaultValue].fields.forEach((field) => {
// if (field.key !== "" && field.value !== "") userConfig[namespace][defaultValue][field.key] = field.value;
// });
// }
// });
// console.log("userConfig refresh:", userConfig);
// };
$: console.log("attributes refresh:", attributes);
let open = false;
Expand All @@ -123,7 +111,6 @@

<div class="section">
{#if attributes}
<!-- <ConfigField bind:attributesObject bind:attributes /> -->
{#each Object.entries(attributes) as [namespace, attribute]}
{#if attribute.display}
<div class="titre">{attribute.display}</div>
Expand Down

0 comments on commit 3d97f56

Please sign in to comment.