1
0

AccountForm.vue 1.8 KB

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