Skouter mortgage estimates. Web application with view written in PHP and Vue, but controller and models in Go.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

290 rindas
6.3 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. @download="downloadEstimate"
  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) return // Exit if token is invalid
  92. this.user = result
  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 downloadEstimate(estimate) {
  194. fetch(`/api/pdf`,
  195. {method: 'POST',
  196. body: JSON.stringify(estimate),
  197. headers: {
  198. "Accept": "application/json",
  199. "Authorization": `Bearer ${this.token}`,
  200. },
  201. }).then(response => {
  202. if (response.ok) { return response.blob() }
  203. }).then (result => {
  204. console.log(result)
  205. if (!result) return // Exit if token is invalid or blank file returned
  206. })
  207. }
  208. export default {
  209. components: {
  210. SideBar,
  211. Spinner,
  212. Home,
  213. NewEstimate,
  214. Estimates,
  215. Settings,
  216. SignOut,
  217. Login
  218. },
  219. computed: { active },
  220. methods: {
  221. getCookie,
  222. start,
  223. getUser,
  224. getFees,
  225. refreshToken,
  226. updateAvatar,
  227. getAvatar,
  228. updateLetterhead,
  229. getLetterhead,
  230. downloadEstimate,
  231. },
  232. data() {
  233. return {
  234. loading: true,
  235. user: null,
  236. hash: window.location.hash,
  237. fees: [],
  238. loadingError: "",
  239. token: '',
  240. }
  241. },
  242. created() {
  243. window.onhashchange = () => this.hash = window.location.hash
  244. this.token = this.getCookie("skouter")
  245. if (!this.token) {
  246. window.location.hash = 'login'
  247. this.loading = false
  248. return
  249. }
  250. this.start()
  251. }
  252. }
  253. </script>