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

190 рядки
4.0 KiB

  1. <template>
  2. <div class="panel">
  3. <template v-if="user">
  4. <side-bar v-if="user" :role="user && user.status" :active="active">
  5. </side-bar>
  6. <div v-if="loading" class="page loading">
  7. <span class="error" >{{loadingError}}</span>
  8. <spinner></spinner>
  9. </div>
  10. <home :user="user" v-else-if="active == 1" />
  11. <new-estimate :user="user" :fees="fees" :token="token" v-else-if="active == 2" />
  12. <estimates :user="user" :fees="fees" v-else-if="active == 3" />
  13. <settings :user="user" v-else-if="active == 4" />
  14. <sign-out :user="user" v-else-if="active == 5" />
  15. </template>
  16. <template v-if="!user && active == 6">
  17. <login @login="start" />
  18. </template>
  19. </div>
  20. </template>
  21. <script>
  22. import SideBar from "./sidebar.vue"
  23. import Spinner from "./spinner.vue"
  24. import Home from "./home.vue"
  25. import NewEstimate from "./new/new.vue"
  26. import Estimates from "./estimates.vue"
  27. import Settings from "./settings.vue"
  28. import SignOut from "./sign-out.vue"
  29. import Login from "./login.vue"
  30. function getCookie(name) {
  31. var re = new RegExp(name + "=([^;]+)")
  32. var value = re.exec(document.cookie)
  33. return (value != null) ? unescape(value[1]) : null
  34. }
  35. function refreshToken() {
  36. const token = getCookie("skouter")
  37. this.token = token
  38. fetch(`/api/token`,
  39. {method: 'GET',
  40. headers: {
  41. "Accept": "application/json",
  42. "Authorization": `Bearer ${token}`,
  43. },
  44. }).then(response => {
  45. if (!response.ok) {
  46. console.log("Error refreshing token.")
  47. }
  48. })
  49. // Recursive refresh
  50. setTimeout(this.refreshToken, 1000*60*25)
  51. }
  52. function getUser() {
  53. const token = getCookie("skouter")
  54. this.token = token
  55. return fetch(`/api/user`,
  56. {method: 'GET',
  57. headers: {
  58. "Accept": "application/json",
  59. "Authorization": `Bearer ${token}`,
  60. },
  61. }).then(response => {
  62. if (response.ok) {
  63. return response.json()
  64. } else {
  65. // Redirect to login if starting token is invalid
  66. window.location.hash = '#login'
  67. }
  68. }).then (result => {
  69. if (!result || !result.length) return // Exit if token is invalid
  70. this.user = result[0]
  71. })
  72. }
  73. function getFees() {
  74. const token = getCookie("skouter")
  75. return fetch(`/api/fees`,
  76. {method: 'GET',
  77. headers: {
  78. "Accept": "application/json",
  79. "Authorization": `Bearer ${token}`,
  80. },
  81. }).then(response => {
  82. if (response.ok) { return response.json() }
  83. }).then (result => {
  84. if (!result || !result.length) return // Exit if token is invalid or no fees are saved
  85. this.fees = result
  86. })
  87. }
  88. // Used to check the current section of the app generally without a regex match
  89. // each time.
  90. function active() {
  91. if (this.hash == '' || this.hash == '#') {
  92. return 1
  93. } else if (/^#new\/?/.exec(this.hash)) {
  94. return 2
  95. } else if (/^#estimates\/?/.exec(this.hash)) {
  96. return 3
  97. } else if (/^#settings\/?/.exec(this.hash)) {
  98. return 4
  99. } else if (/^#sign-out\/?/.exec(this.hash)) {
  100. return 5
  101. } else if (/^#login\/?/.exec(this.hash)) {
  102. return 6
  103. } else {
  104. return 0
  105. }
  106. }
  107. // Fetch data before showing UI. If requests fail, assume token is expired.
  108. function start() {
  109. this.loading = true
  110. let loaders = []
  111. loaders.push(this.getUser())
  112. loaders.push(this.getFees())
  113. Promise.all(loaders).then((a, b) => {
  114. this.loading = false
  115. if (!b) {
  116. // Time untill token expiration may have elapsed before the page
  117. // reloaded
  118. this.refreshToken()
  119. }
  120. }).catch(error => {
  121. console.log("An error occured %O", error)
  122. this.loadingError = "Could not initialize app."
  123. window.location.hash = 'login'
  124. })
  125. }
  126. export default {
  127. components: {
  128. SideBar,
  129. Spinner,
  130. Home,
  131. NewEstimate,
  132. Estimates,
  133. Settings,
  134. SignOut,
  135. Login
  136. },
  137. computed: { active },
  138. methods: {
  139. getCookie,
  140. start,
  141. getUser,
  142. getFees,
  143. refreshToken,
  144. },
  145. data() {
  146. return {
  147. loading: true,
  148. user: null,
  149. hash: window.location.hash,
  150. fees: [],
  151. loadingError: "",
  152. token: '',
  153. }
  154. },
  155. created() {
  156. window.onhashchange = () => this.hash = window.location.hash
  157. this.token = this.getCookie("skouter")
  158. if (!this.token) {
  159. window.location.hash = 'login'
  160. this.loading = false
  161. return
  162. }
  163. this.start()
  164. }
  165. }
  166. </script>