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

319 lines
8.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" v-model="user.firstName">
  26. <label for="">Last Name</label>
  27. <input type="text" :value="user.lastName">
  28. <label for="">NMLS ID</label>
  29. <input type="text">
  30. <label for="">Branch ID</label>
  31. <input type="text" :value="user.branchId" disabled>
  32. <select id="" name="" :value="user.country">
  33. <option value="USA">USA</option>
  34. <option value="Canada">Canada</option>
  35. </select>
  36. <div class="address-entry">
  37. <label for="">Address</label>
  38. <input type="text" @input="searchLocation" :value="address.full">
  39. <dropdown v-if="addresses && addresses.length"
  40. :entries="addresses.map(a => ({text: a.full_address, value: a}))"
  41. @select="setAddress"
  42. >
  43. </dropdown>
  44. </div>
  45. <button @click="saveProfile">Save</button>
  46. </section>
  47. <section class="form inputs special">
  48. <h3>Subscriptions</h3>
  49. <label for="">Standard Plan ($49/month)</label>
  50. <button @click="() => unsubing = true">Unsubscribe</button>
  51. <label for="">Newsletter</label>
  52. <button @click="changeNewsSub">
  53. {{props.user.newsletter ? "Unsubscribe" : "Subscribe"}}
  54. </button>
  55. </section>
  56. <section class="form inputs special">
  57. <h3>Reset Password</h3>
  58. <label for="">Old Password</label>
  59. <input type="password" v-model="oldPass">
  60. <label for="">New Password</label>
  61. <input type="password" v-model="newPass">
  62. <label for="">Confirm Password</label>
  63. <input type="password" v-model="confirmPass">
  64. <button
  65. :disabled="!(oldPass && newPass && confirmPass)"
  66. @click="changePassword">Save</button>
  67. <label class="error">{{passError}}</label>
  68. </section>
  69. <Dialog v-if="ready" @close="() => ready = false">
  70. <h3>Confirm your current password to save changes.</h3>
  71. <input type="text">
  72. <button>Confirm</button>
  73. </Dialog>
  74. <UnsubPrompt v-if="unsubing" :token="token" @close="() => unsubing = false"/>
  75. </div>
  76. </template>
  77. <script setup>
  78. import { ref, watch, onMounted } from "vue"
  79. import Dialog from "./dialog.vue"
  80. import Dropdown from "./dropdown.vue"
  81. import UnsubPrompt from "./unsubscribe.vue"
  82. let avatar = ref(null) // the canvas element
  83. let letterhead = ref(null) // the canvas element
  84. let ready = ref(false)
  85. let unsubing = ref(false)
  86. let avatarChanged = ref(false)
  87. let avatarError = ref('')
  88. let letterheadError = ref('')
  89. let passError = ref('')
  90. let oldPass = ref('')
  91. let newPass = ref('')
  92. let confirmPass = ref('')
  93. const locationsId = ref(null)
  94. const addresses = ref([])
  95. const address = ref({})
  96. const props = defineProps(['user', 'token'])
  97. const emit = defineEmits(['updateAvatar', 'updateLetterhead', 'updateUser'])
  98. const user = Object.assign({}, props.user)
  99. function save() {
  100. }
  101. function check() {
  102. ready.value = true
  103. }
  104. function uploadAvatar() {
  105. avatar.value.toBlob(b => {
  106. fetch(`/api/user/avatar`,
  107. {method: 'POST',
  108. body: b,
  109. headers: {
  110. "Accept": "application/json",
  111. "Authorization": `Bearer ${props.token}`,
  112. },
  113. }).then(resp => {
  114. if (resp.ok) {emit('updateAvatar')}
  115. })
  116. })
  117. }
  118. function uploadLetterhead() {
  119. letterhead.value.toBlob(b => {
  120. fetch(`/api/user/letterhead`,
  121. {method: 'POST',
  122. body: b,
  123. headers: {
  124. "Accept": "application/json",
  125. "Authorization": `Bearer ${props.token}`,
  126. },
  127. }).then(resp => {
  128. if (resp.ok) {emit('updateLetterhead')}
  129. })
  130. })
  131. }
  132. function setLetterhead(f) {
  133. letterheadError.value = ""
  134. const validTypes = ['image/jpeg', 'image/png']
  135. if (!validTypes.includes(f.type)) {
  136. letterheadError.value = 'Image must be JPEG of PNG format'
  137. return
  138. }
  139. fetch(`/api/letterhead`,
  140. {method: 'POST',
  141. body: f,
  142. headers: {
  143. "Accept": "application/json",
  144. "Authorization": `Bearer ${props.token}`,
  145. },
  146. }).then(resp => {
  147. if (resp.ok) {
  148. resp.blob().then(b => changeLetterhead(b))
  149. } else {
  150. resp.text().then(e => letterheadError.value = e)
  151. }
  152. })
  153. }
  154. function changeAvatar(blob) {
  155. const validTypes = ['image/jpeg', 'image/png']
  156. if (!validTypes.includes(blob?.type)) {
  157. avatarError.value = 'Image must be JPEG of PNG format'
  158. return
  159. }
  160. avatarError.value = ''
  161. createImageBitmap(blob,
  162. {resizeWidth: 200, resizeHeight: 200, resizeQuality: 'medium'}).
  163. then((img) => {
  164. avatar.value.getContext("2d").drawImage(img, 0, 0, 200, 200)
  165. })
  166. }
  167. function changeLetterhead(blob) {
  168. const validTypes = ['image/jpeg', 'image/png']
  169. if (!validTypes.includes(blob.type)) {
  170. letterheadError.value = 'Image must be JPEG of PNG format'
  171. return
  172. }
  173. createImageBitmap(blob).
  174. then((img) => {
  175. let ctx = letterhead.value.getContext("2d")
  176. ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height)
  177. ctx.drawImage(img, 0, 0)
  178. })
  179. }
  180. function setAddress(a) {
  181. address.value = {
  182. id: address.value.id,
  183. full: a.full_address,
  184. street: a.address,
  185. city: a.context?.place.name ?? '',
  186. region: a.context?.region?.name ?? '',
  187. country: a.context?.country?.name ?? '',
  188. zip: a.context?.postcode?.name ?? '',
  189. }
  190. addresses.value = null
  191. }
  192. function saveProfile() {
  193. user.value.address = address.value
  194. fetch(`/api/user`,
  195. {method: 'PATCH',
  196. body: JSON.stringify(user.value),
  197. headers: {
  198. "Accept": "application/json",
  199. "Authorization": `Bearer ${props.token}`,
  200. },
  201. }).then(resp => {
  202. if (resp.ok) {}
  203. })
  204. }
  205. function changePassword(f) {
  206. fetch(`/api/user/password`,
  207. {method: 'POST',
  208. body: JSON.stringify({
  209. old: oldPass.value,
  210. new: newPass.value,
  211. confirm: confirmPass.value,
  212. }),
  213. headers: {
  214. "Accept": "application/json",
  215. "Authorization": `Bearer ${props.token}`,
  216. },
  217. }).then(resp => {
  218. if (resp.ok) {
  219. resp.blob().then(b => {
  220. oldPass.value = ""
  221. newPass.value = ""
  222. confirmPass.value = ""
  223. passError.value = ""
  224. })
  225. } else {
  226. resp.text().then(e => passError.value = e)
  227. }
  228. })
  229. }
  230. function searchLocation(e) {
  231. address.value.full = e.target.value
  232. clearTimeout(locationsId.value)
  233. locationsId.value = setTimeout(getLocations, 1000, e)
  234. }
  235. function getLocations(e) {
  236. let prefix = "https://api.mapbox.com/search/searchbox/v1/suggest?"
  237. let search = e.target.value
  238. let key = encodeURIComponent(process.env.MAPBOX_API_KEY)
  239. if (!search) return
  240. fetch(`${prefix}q=${search}&proximity=ip&access_token=${key}&session_token=1`
  241. ).then(resp => resp.json()).then(entries => {
  242. if (!entries?.suggestions) {
  243. addresses.value = null
  244. return
  245. }
  246. addresses.value = entries.suggestions.filter(e => e.full_address)
  247. })
  248. }
  249. function changeNewsSub() {
  250. fetch(`/api/user/newsletter`,
  251. {method: 'GET',
  252. headers: {
  253. "Accept": "application/json",
  254. "Authorization": `Bearer ${props.token}`,
  255. },
  256. }).then(resp => {
  257. if (resp.ok) emit('updateUser')
  258. })
  259. }
  260. watch(props.user, (u) => {
  261. if (props.user.avatar) {
  262. changeAvatar(props.user.avatar)
  263. }
  264. if (props.user.letterhead) {
  265. changeLetterhead(props.user.letterhead)
  266. }
  267. address.value = Object.assign({}, props.user.address)
  268. user.value = Object.assign({}, props.user)
  269. user.value.address = address.value
  270. }, {immediate: true})
  271. </script>
  272. <style scoped>
  273. div.address-entry {
  274. display: flex;
  275. flex-flow: column;
  276. position: relative;
  277. }
  278. </style>