Browse Source

refactor: moved creating blank accounts to the backend

Kristian Vos 4 years ago
parent
commit
935bd61a00

+ 30 - 0
backend/logic/account.js

@@ -29,6 +29,36 @@ module.exports = class extends coreClass {
 		})
 	}
 
+	async createEmptyAccount(version) {
+		return new Promise(async (resolve, reject) => {
+			try { await this._validateHook(); } catch { return; }
+
+			let schema = await this.accountSchemaModule.getByVersion(version).catch(reject);
+			if (!schema) return reject(new Error("No schema found."));
+
+			let account = {};
+
+			account.fields = {};
+			account.version = version;
+
+			schema.fields.forEach(field => {
+				let defaultObject = {};
+				field.fieldTypes.forEach(fieldType => {
+					if (fieldType.type === "text" || fieldType.type === "select") defaultObject[fieldType.fieldTypeId] = "";
+					else if (fieldType.type === "checkbox") defaultObject[fieldType.fieldTypeId] = false;
+				});
+				
+				account.fields[field.fieldId] = [];
+
+				for(let i = 0; i < field.minEntries; i++) {
+					account.fields[field.fieldId].push(defaultObject);
+				}
+			});
+
+			resolve(account);
+		});
+	}
+
 	async getAll() {
 		return new Promise(async (resolve, reject) => {
 			try { await this._validateHook(); } catch { return; }

+ 13 - 0
backend/logic/io/namespaces/account.js

@@ -5,6 +5,19 @@ const mongoModule = moduleManager.modules["mongo"];
 const utilModule = moduleManager.modules["util"];
 
 module.exports = {
+	"createEmptyAccount": async (cb, version) => {
+		accountModule.createEmptyAccount(version).then(account => {
+			cb({
+				status: "success",
+				account
+			});
+		}).catch(err => {
+			cb({
+				status: "failure"
+			});
+		});
+	},
+
 	"getAll": async cb => {
 		accountModule.getAll().then(accounts => {
 			cb({

+ 7 - 40
frontend/vue/components/AccountForm.vue

@@ -64,31 +64,6 @@ export default {
 			return () => {
 				this.$set(this.account.fields, fieldId, this.$refs[fieldId][0].entries);
 			};
-		},
-		initializeAccount() {
-			if (!this.initialAccount) {
-				this.$set(this.account, "fields", {});
-				this.$set(this.account, "version", this.schema.version);
-
-				this.fields.forEach(field => {
-					let defaultObject = {};
-					field.fieldTypes.forEach(fieldType => {
-						if (fieldType.type === "text" || fieldType.type === "select") defaultObject[fieldType.fieldTypeId] = "";
-						else if (fieldType.type === "checkbox") defaultObject[fieldType.fieldTypeId] = false;
-					});
-					
-					this.$set(this.account.fields, field.fieldId, []);
-
-					for(let i = 0; i < field.minEntries; i++) {
-						this.account.fields[field.fieldId].push(defaultObject);
-					}
-				});
-
-				this.templateAccount = this.account;
-			} else {
-				this.account = this.initialAccount;
-				this.templateAccount = this.initialAccount;
-			}
 		}
 	},
 	props: {
@@ -100,21 +75,13 @@ export default {
 		io.getSocket(socket => {
 			this.socket = socket;
 
-			if (this.initialAccount) {
-				socket.emit("accountSchema.getByVersion", this.initialAccount.version, res => {
-					this.fields = res.schema.fields;
-					this.dependencies = (res.schema.dependencies) ? res.schema.dependencies : {};
-					this.initializeAccount();
-				});
-			} else {
-				socket.emit("accountSchema.getLatest", res => {
-					this.fields = res.schema.fields;
-					this.schema = res.schema;
-					this.dependencies = (res.schema.dependencies) ? res.schema.dependencies : {};
-					this.initializeAccount();
-				});
-			}
-
+			socket.emit("accountSchema.getByVersion", this.initialAccount.version, res => {
+				this.fields = res.schema.fields;
+				this.dependencies = (res.schema.dependencies) ? res.schema.dependencies : {};
+				this.account = this.initialAccount;
+				this.templateAccount = this.initialAccount;
+			});
+			
 			socket.emit("util.getAutosuggest", res => {
 				this.autosuggest = res.autosuggest;
 			});

+ 8 - 2
frontend/vue/pages/AddAccount.vue

@@ -3,7 +3,7 @@
 		<h1>Add account</h1>
 		<hr/>
 		<br/>
-		<account-form :onSubmit="onSubmit"/>
+		<account-form v-if="account.version" :onSubmit="onSubmit" :initialAccount="account"/>
 	</main>
 </template>
 
@@ -16,7 +16,7 @@ export default {
 	components: { AccountForm },
 	data: () => {
 		return {
-			
+			account: {}
 		}
 	},
 	methods: {
@@ -32,6 +32,12 @@ export default {
 	mounted() {
 		io.getSocket(socket => {
 			this.socket = socket;
+
+			this.socket.emit("accountSchema.getLatest", res => {
+				this.socket.emit("account.createEmptyAccount", res.schema.version, res => {
+					this.account = res.account;
+				});
+			});
 		});
 	}
 };