AccountForm.vue 2.0 KB

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