Skouter mortgage estimates. Web application with view written in PHP and Vue, but controller and models in Go.
 
 
 
 
 
 

181 lines
4.3 KiB

  1. <template>
  2. <div class="page settings">
  3. <h2>Settings</h2>
  4. <section class="form inputs">
  5. <h3>Avatar</h3>
  6. <canvas class="displayer" width="200" height="200" ref="avatar"></canvas>
  7. <input type="file"
  8. @change="e => changeAvatar(e.target.files[0])"
  9. />
  10. <button @click="uploadAvatar">Upload</button>
  11. <label class="error">{{avatarError}}</label>
  12. </section>
  13. <section class="form inputs letterhead">
  14. <h3>Letterhead</h3>
  15. <canvas class="displayer" height="200" ref="letterhead"></canvas>
  16. <input type="file"
  17. @change="e => {setLetterhead(e.target.files[0])}"
  18. />
  19. <button @click="uploadLetterhead">Upload</button>
  20. <label class="error">{{letterheadError}}</label>
  21. </section>
  22. <section class="form inputs special">
  23. <h3>Profile</h3>
  24. <label for="">First Name</label>
  25. <input type="text">
  26. <label for="">Last Name</label>
  27. <input type="text">
  28. <label for="">NMLS ID</label>
  29. <input type="text">
  30. <label for="">Branch ID</label>
  31. <input type="text">
  32. <select id="" name="" >
  33. <option value="usa">USA</option>
  34. <option value="canada">Canada</option>
  35. </select>
  36. <button @click="check">Save</button>
  37. </section>
  38. <section class="form inputs special">
  39. <h3>Login</h3>
  40. <label for="">Email</label>
  41. <input type="text">
  42. <label for="">New Password</label>
  43. <input type="text">
  44. <button @click="check">Save</button>
  45. </section>
  46. <Dialog v-if="ready" @close="() => ready = false">
  47. <h3>Confirm your current password to save changes.</h3>
  48. <input type="text">
  49. <button>Confirm</button>
  50. </Dialog>
  51. </div>
  52. </template>
  53. <script setup>
  54. import { ref, watch, onMounted } from "vue"
  55. import Dialog from "./dialog.vue"
  56. let avatar = ref(null) // the canvas element
  57. let letterhead = ref(null) // the canvas element
  58. let ready = ref(false)
  59. let avatarChanged = ref(false)
  60. let avatarError = ref('')
  61. let letterHeadError = ref('')
  62. let letterheadError = ref('')
  63. const props = defineProps(['user', 'token'])
  64. const emit = defineEmits(['updateAvatar', 'updateLetterhead'])
  65. function save() {
  66. }
  67. function check() {
  68. ready.value = true
  69. }
  70. function uploadAvatar() {
  71. avatar.value.toBlob(b => {
  72. fetch(`/api/user/avatar`,
  73. {method: 'POST',
  74. body: b,
  75. headers: {
  76. "Accept": "application/json",
  77. "Authorization": `Bearer ${props.token}`,
  78. },
  79. }).then(resp => {
  80. if (resp.ok) {emit('updateAvatar')}
  81. })
  82. })
  83. }
  84. function uploadLetterhead() {
  85. letterhead.value.toBlob(b => {
  86. fetch(`/api/user/letterhead`,
  87. {method: 'POST',
  88. body: b,
  89. headers: {
  90. "Accept": "application/json",
  91. "Authorization": `Bearer ${props.token}`,
  92. },
  93. }).then(resp => {
  94. if (resp.ok) {emit('updateLetterhead')}
  95. })
  96. })
  97. }
  98. function setLetterhead(f) {
  99. const validTypes = ['image/jpeg', 'image/png']
  100. if (!validTypes.includes(f.type)) {
  101. letterheadError.value = 'Image must be JPEG of PNG format'
  102. return
  103. }
  104. fetch(`/api/letterhead`,
  105. {method: 'POST',
  106. body: f,
  107. headers: {
  108. "Accept": "application/json",
  109. "Authorization": `Bearer ${props.token}`,
  110. },
  111. }).then(resp => {
  112. if (resp.ok) {
  113. resp.blob().then(b => changeLetterhead(b))
  114. } else {
  115. resp.text().then(e => letterheadError.value = e)
  116. }
  117. })
  118. }
  119. function changeAvatar(blob) {
  120. const validTypes = ['image/jpeg', 'image/png']
  121. if (!validTypes.includes(blob.type)) {
  122. avatarError.value = 'Image must be JPEG of PNG format'
  123. return
  124. }
  125. avatarError.value = ''
  126. createImageBitmap(blob,
  127. {resizeWidth: 200, resizeHeight: 200, resizeQuality: 'medium'}).
  128. then((img) => {
  129. avatar.value.getContext("2d").drawImage(img, 0, 0, 200, 200)
  130. })
  131. }
  132. function changeLetterhead(blob) {
  133. const validTypes = ['image/jpeg', 'image/png']
  134. if (!validTypes.includes(blob.type)) {
  135. letterheadError.value = 'Image must be JPEG of PNG format'
  136. return
  137. }
  138. createImageBitmap(blob).
  139. then((img) => {
  140. let ctx = letterhead.value.getContext("2d")
  141. ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height)
  142. ctx.drawImage(img, 0, 0)
  143. })
  144. }
  145. watch(props.user, (u) => {
  146. if (props.user.avatar) {
  147. changeAvatar(props.user.avatar)
  148. }
  149. if (props.user.letterhead) {
  150. changeLetterhead(props.user.letterhead)
  151. }
  152. }, {immediate: true})
  153. </script>