Skouter mortgage estimates. Web application with view written in PHP and Vue, but controller and models in Go.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

281 satır
6.0 KiB

  1. <template>
  2. <div class="panel">
  3. <template v-if="user">
  4. <side-bar v-if="user"
  5. :role="user && user.status"
  6. :avatar="user.avatar"
  7. :active="active">
  8. </side-bar>
  9. <div v-if="loading" class="page loading">
  10. <span class="error" >{{loadingError}}</span>
  11. <spinner></spinner>
  12. </div>
  13. <home :user="user" v-else-if="active == 1" />
  14. <new-estimate
  15. :user="user"
  16. :fees="fees"
  17. :token="token"
  18. v-else-if="active == 2" />
  19. <estimates
  20. :user="user"
  21. :fees="fees"
  22. v-else-if="active == 3"
  23. :token="token"
  24. @addFeeTemp="(f) => fees.push(f)"
  25. @removeFeeTemp="(fee) => fees = fees.filter(f => f.id != fee.id)"
  26. @preview="previewEstimate"
  27. />
  28. <settings
  29. :user="user"
  30. :token="token"
  31. @updateAvatar="updateAvatar"
  32. @updateLetterhead="updateLetterhead"
  33. v-else-if="active == 4" />
  34. <sign-out :user="user" v-else-if="active == 5" />
  35. </template>
  36. <template v-if="!user && active == 6">
  37. <login @login="start" />
  38. </template>
  39. </div>
  40. </template>
  41. <script>
  42. import SideBar from "./sidebar.vue"
  43. import Spinner from "./spinner.vue"
  44. import Home from "./home.vue"
  45. import NewEstimate from "./new/new.vue"
  46. import Estimates from "./estimates.vue"
  47. import Settings from "./settings.vue"
  48. import SignOut from "./sign-out.vue"
  49. import Login from "./login.vue"
  50. function getCookie(name) {
  51. var re = new RegExp(name + "=([^;]+)")
  52. var value = re.exec(document.cookie)
  53. return (value != null) ? unescape(value[1]) : null
  54. }
  55. function refreshToken() {
  56. const token = getCookie("skouter")
  57. const timeout = setTimeout(this.refreshToken, 1000*60*25)
  58. fetch(`/api/token`,
  59. {method: 'GET',
  60. headers: {
  61. "Accept": "application/json",
  62. "Authorization": `Bearer ${token}`,
  63. },
  64. }).then(response => {
  65. if (!response.ok) {
  66. console.log("Error refreshing token.")
  67. clearTimeout(timeout)
  68. window.location.hash = '#login'
  69. } else {
  70. this.token = getCookie("skouter")
  71. }
  72. })
  73. }
  74. function getUser() {
  75. const token = getCookie("skouter")
  76. this.token = token
  77. return fetch(`/api/user`,
  78. {method: 'GET',
  79. headers: {
  80. "Accept": "application/json",
  81. "Authorization": `Bearer ${token}`,
  82. },
  83. }).then(response => {
  84. if (response.ok) {
  85. return response.json()
  86. } else {
  87. // Redirect to login if starting token is invalid
  88. window.location.hash = '#login'
  89. }
  90. }).then (result => {
  91. if (!result || !result.length) return // Exit if token is invalid
  92. this.user = result[0]
  93. if (this.user.avatar) return
  94. return getAvatar(token)
  95. }).then(b => {
  96. const validTypes = ['image/jpeg', 'image/png']
  97. if (!b || !validTypes.includes(b.type) || b.size <= 1) {
  98. return fetch("/assets/image/empty-avatar.jpg").
  99. then(r => r.blob()).then( a => this.user.avatar = a )
  100. }
  101. this.user.avatar = b
  102. return getLetterhead(token)
  103. }).then(b => {
  104. const validTypes = ['image/jpeg', 'image/png']
  105. if (!validTypes.includes(b.type) || b.size <= 1) {
  106. return fetch("/assets/image/empty-letterhead.jpg").
  107. then(r => r.blob()).then( a => this.user.letterhead = a )
  108. }
  109. this.user.letterhead = b
  110. })
  111. }
  112. function getAvatar(t) {
  113. return fetch("/api/user/avatar",
  114. {method: 'GET',
  115. headers: {
  116. "Accept": "application/json",
  117. "Authorization": `Bearer ${t || this.token}`,
  118. }
  119. }).then(r => r.blob())
  120. }
  121. function getLetterhead(t) {
  122. return fetch("/api/user/letterhead",
  123. {method: 'GET',
  124. headers: {
  125. "Accept": "application/json",
  126. "Authorization": `Bearer ${t || this.token}`,
  127. }
  128. }).then(r => r.blob())
  129. }
  130. function updateAvatar() {
  131. const token = getCookie("skouter")
  132. getAvatar(token).then(b => this.user.avatar = b)
  133. }
  134. function updateLetterhead() {
  135. const token = getCookie("skouter")
  136. getLetterhead(token).then(b => this.user.letterhead = b)
  137. }
  138. function getFees() {
  139. const token = getCookie("skouter")
  140. return fetch(`/api/fees`,
  141. {method: 'GET',
  142. headers: {
  143. "Accept": "application/json",
  144. "Authorization": `Bearer ${token}`,
  145. },
  146. }).then(response => {
  147. if (response.ok) { return response.json() }
  148. }).then (result => {
  149. if (!result || !result.length) return // Exit if token is invalid or no fees are saved
  150. this.fees = result
  151. })
  152. }
  153. // Used to check the current section of the app generally without a regex match
  154. // each time.
  155. function active() {
  156. if (this.hash == '' || this.hash == '#') {
  157. return 1
  158. } else if (/^#new\/?/.exec(this.hash)) {
  159. return 2
  160. } else if (/^#estimates\/?/.exec(this.hash)) {
  161. return 3
  162. } else if (/^#settings\/?/.exec(this.hash)) {
  163. return 4
  164. } else if (/^#sign-out\/?/.exec(this.hash)) {
  165. return 5
  166. } else if (/^#login\/?/.exec(this.hash)) {
  167. return 6
  168. } else if (/^#estimate\/?/.exec(this.hash)) {
  169. return 7
  170. } else {
  171. return 0
  172. }
  173. }
  174. // Fetch data before showing UI. If requests fail, assume token is expired.
  175. function start() {
  176. this.loading = true
  177. let loaders = []
  178. loaders.push(this.getUser())
  179. loaders.push(this.getFees())
  180. Promise.all(loaders).then((a, b) => {
  181. this.loading = false
  182. if (!b) {
  183. // Time untill token expiration may have elapsed before the page
  184. // reloaded
  185. this.refreshToken()
  186. }
  187. }).catch(error => {
  188. console.log("An error occured %O", error)
  189. this.loadingError = "Could not initialize app."
  190. window.location.hash = 'login'
  191. })
  192. }
  193. function previewEstimate(estimate) {
  194. this.preview = estimate
  195. window.location.hash = 'estimate'
  196. }
  197. export default {
  198. components: {
  199. SideBar,
  200. Spinner,
  201. Home,
  202. NewEstimate,
  203. EstimateTest,
  204. Estimates,
  205. Settings,
  206. SignOut,
  207. Login
  208. },
  209. computed: { active },
  210. methods: {
  211. getCookie,
  212. start,
  213. getUser,
  214. getFees,
  215. refreshToken,
  216. updateAvatar,
  217. getAvatar,
  218. updateLetterhead,
  219. getLetterhead,
  220. previewEstimate,
  221. },
  222. data() {
  223. return {
  224. loading: true,
  225. user: null,
  226. hash: window.location.hash,
  227. fees: [],
  228. loadingError: "",
  229. token: '',
  230. preview: null,
  231. }
  232. },
  233. created() {
  234. window.onhashchange = () => this.hash = window.location.hash
  235. this.token = this.getCookie("skouter")
  236. if (!this.token) {
  237. window.location.hash = 'login'
  238. this.loading = false
  239. return
  240. }
  241. this.start()
  242. }
  243. }
  244. </script>