AccountForm.vue 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <form>
  3. <field
  4. v-for="field in fields"
  5. :name="field.name"
  6. :minEntries="field.minEntries"
  7. :maxEntries="field.maxEntries"
  8. :initialEntries="account.fields[field.fieldId]"
  9. :autosuggest="autosuggest"
  10. :key="field.fieldId"
  11. :ref="field.fieldId"
  12. :fieldTypes="field.fieldTypes"/>
  13. <button @click="submit()" type="button">
  14. Submit
  15. </button>
  16. </form>
  17. </template>
  18. <script>
  19. import Field from '../components/Field.vue';
  20. import io from "../../io.js";
  21. export default {
  22. components: { Field },
  23. data: function() {
  24. return {
  25. fields: [],
  26. account: {},
  27. autosuggest: {}
  28. };
  29. },
  30. methods: {
  31. submit() {
  32. let account = JSON.parse(JSON.stringify(this.account));
  33. let fields = {};
  34. Object.keys(account.fields).forEach(fieldId => {
  35. fields[fieldId] = this.$refs[fieldId][0].entries;
  36. });
  37. account.fields = fields;
  38. this.onSubmit(account);
  39. }
  40. },
  41. props: {
  42. onSubmit: Function,
  43. initialAccount: Object
  44. },
  45. mounted() {
  46. io.getSocket(socket => {
  47. this.socket = socket;
  48. socket.emit("getAccountSchema", res => {
  49. this.fields = res.schema.fields;
  50. if (!this.initialAccount) {
  51. this.account.fields = {};
  52. this.account.version = res.schema.version;
  53. this.fields.forEach(field => {
  54. let defaultObject = {};
  55. field.fieldTypes.forEach(fieldType => {
  56. if (fieldType.type === "text" || fieldType.type === "select") defaultObject[fieldType.fieldTypeId] = "";
  57. else if (fieldType.type === "checkbox") defaultObject[fieldType.fieldTypeId] = false;
  58. });
  59. this.account.fields[field.fieldId] = [];
  60. for(let i = 0; i < field.minEntries; i++) {
  61. this.account.fields[field.fieldId].push(defaultObject);
  62. }
  63. });
  64. } else {
  65. this.account = this.initialAccount;
  66. }
  67. });
  68. socket.emit("getAutosuggest", res => {
  69. this.autosuggest = res.autosuggest;
  70. });
  71. });
  72. }
  73. };
  74. </script>
  75. <style lang="scss" scoped>
  76. form {
  77. width: 400px;
  78. }
  79. </style>