Browse Source

refactor: changed import input's to select elements

Kristian Vos 4 years ago
parent
commit
6db924ec05

+ 16 - 0
backend/logic/accountSchema.js

@@ -1,6 +1,8 @@
 'use strict';
 
 const async = require("async");
+const fs = require("fs");
+const path = require("path");
 
 const coreClass = require("../core");
 
@@ -22,6 +24,8 @@ module.exports = class extends coreClass {
 			this.accountSchemaSchema = await this.mongoModule.schema("accountSchema");
 			this.accountSchemaModel = await this.mongoModule.model("accountSchema");
 
+			this.schemaDirectoryPath = path.join(__dirname, "..", "schemas", "account");
+
 			resolve();
 		})
 	}
@@ -93,6 +97,18 @@ module.exports = class extends coreClass {
 		});
 	}
 
+	async listSchemasInDirectory() {
+		return new Promise(async (resolve, reject) => {
+			try { await this._validateHook(); } catch { return; }
+
+			fs.readdir(this.schemaDirectoryPath, (err, files) => {
+				if (err) reject(new Error(err.message))
+				else resolve(files.map(file => file.substring(0, file.length - 3)));
+			});
+				
+		});
+	}
+
 	async import(name) {
 		return new Promise(async (resolve, reject) => {
 			try { await this._validateHook(); } catch { return; }

+ 16 - 0
backend/logic/convertSchema.js

@@ -1,6 +1,8 @@
 'use strict';
 
 const async = require("async");
+const fs = require("fs");
+const path = require("path");
 
 const coreClass = require("../core");
 
@@ -22,6 +24,8 @@ module.exports = class extends coreClass {
 			this.convertSchemaSchema = await this.mongoModule.schema("convertSchema");
 			this.convertSchemaModel = await this.mongoModule.model("convertSchema");
 
+			this.schemaDirectoryPath = path.join(__dirname, "..", "schemas", "convert");
+
 			resolve();
 		})
 	}
@@ -72,6 +76,18 @@ module.exports = class extends coreClass {
 		});
 	}
 
+	async listSchemasInDirectory() {
+		return new Promise(async (resolve, reject) => {
+			try { await this._validateHook(); } catch { return; }
+
+			fs.readdir(this.schemaDirectoryPath, (err, files) => {
+				if (err) reject(new Error(err.message))
+				else resolve(files.map(file => file.substring(0, file.length - 3)));
+			});
+				
+		});
+	}
+
 	async import(name) {
 		return new Promise(async (resolve, reject) => {
 			try { await this._validateHook(); } catch { return; }

+ 14 - 0
backend/logic/io/namespaces/accountSchema.js

@@ -81,6 +81,20 @@ module.exports = {
 		});
 	},
 
+	"listSchemasInDirectory": (cb) => {
+		accountSchemaModule.listSchemasInDirectory().then((schemasInDirectory) => {
+			cb({
+				status: "success",
+				schemasInDirectory
+			});
+		}).catch(err => {
+			cb({
+				status: "failure",
+				error: err.message
+			});
+		});
+	},
+
 	"import": (cb, name) => {
 		accountSchemaModule.import(name).then(() => {
 			cb({

+ 14 - 0
backend/logic/io/namespaces/convertSchema.js

@@ -55,6 +55,20 @@ module.exports = {
 		});
 	},
 
+	"listSchemasInDirectory": (cb) => {
+		convertSchemaModule.listSchemasInDirectory().then((schemasInDirectory) => {
+			cb({
+				status: "success",
+				schemasInDirectory
+			});
+		}).catch(err => {
+			cb({
+				status: "failure",
+				error: err.message
+			});
+		});
+	},
+
 	"import": (cb, name) => {
 		convertSchemaModule.import(name).then(() => {
 			cb({

+ 1 - 1
frontend/vue/App.vue

@@ -71,7 +71,7 @@ main {
 	}
 }
 
-input {
+input, select {
 	padding: 8px 12px;
 }
 </style>

+ 8 - 1
frontend/vue/pages/ConvertAccounts.vue

@@ -31,7 +31,9 @@
 		<h1>Convert schemas</h1>
 		<hr/>
 		<br/>
-		<input v-model="importConvertSchemaName"/>
+		<select v-model="importConvertSchemaName">
+			<option v-for="schema in schemasInDirectory" :value="schema">{{ schema }}</option>
+		</select>
 		<button @click="importConvertSchema()" class="button">Import convert schema</button>
 		<br/>
 		<br/>
@@ -65,6 +67,7 @@ export default {
 			convertSchemas: [],
 			versions: [],
 			selectedAccounts: [],
+			schemasInDirectory: [],
 			convertingAccounts: false,
 			accountsFields: [
 				{
@@ -197,6 +200,10 @@ export default {
 			socket.emit("convertSchema.getAll", res => {
 				this.convertSchemas = res.schemas;
 			});
+
+			socket.emit("convertSchema.listSchemasInDirectory", res => {
+				this.schemasInDirectory = res.schemasInDirectory;
+			});
 		});
 	}
 };

+ 8 - 1
frontend/vue/pages/Schemas.vue

@@ -3,7 +3,9 @@
 		<h1>Schemas</h1>
 		<hr/>
 		<br/>
-		<input v-model="importAccountSchemaName"/>
+		<select v-model="importAccountSchemaName">
+			<option v-for="schema in schemasInDirectory" :value="schema">{{ schema }}</option>
+		</select>
 		<button @click="importAccountSchema()" class="button">Import account schema</button>
 		<br/>
 		<br/>
@@ -34,6 +36,7 @@ export default {
 	data: () => {
 		return {
 			importAccountSchemaName: "",
+			schemasInDirectory: [],
 			schemas: [],
 			fields: [
 				{
@@ -83,6 +86,10 @@ export default {
 			this.socket.emit("accountSchema.getAll", res => {
 				this.schemas = res.schemas;
 			});
+
+			socket.emit("accountSchema.listSchemasInDirectory", res => {
+				this.schemasInDirectory = res.schemasInDirectory;
+			});
 		});
 	}
 };