App.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <template>
  2. <div id='app'>
  3. <modal-window v-model='showAjaxErrorsModal' style='z-index: 100' width='25rem'>
  4. <div style='padding: 0rem 1rem 1rem 1rem; border-radius: 0.25rem;'>
  5. <p v-for='error in this.$store.state.ajaxErrors'>{{error}}</p>
  6. <button class='button button--modal' @click='showAjaxErrorsModal = false'>OK</button>
  7. </div>
  8. </modal-window>
  9. <modal-window v-model='showAccountModal'>
  10. <tab-view :tabs='["Sign up", "Login"]' v-model="showAccountTab" padding='true'>
  11. <template slot='Sign up'>
  12. <p style='margin-top: 0;' v-if='$store.state.token'>
  13. <strong>Providing the token is still valid, this will create an admin account</strong>
  14. </p>
  15. <p style='margin-top: 0;' v-else>
  16. Sign up to create and post in threads.
  17. <br/>It only takes a few seconds
  18. </p>
  19. <fancy-input
  20. v-model='signup.username'
  21. :error='signup.errors.username'
  22. placeholder='Username'
  23. width='100%'
  24. >
  25. </fancy-input>
  26. <fancy-input
  27. v-model='signup.password'
  28. :error='signup.errors.hash'
  29. placeholder='Password'
  30. type='password'
  31. width='100%'
  32. >
  33. </fancy-input>
  34. <fancy-input
  35. v-model='signup.confirmPassword'
  36. :error='signup.errors.confirmPassword'
  37. placeholder='Confirm password'
  38. type='password'
  39. width='100%'
  40. >
  41. </fancy-input>
  42. <div style='margin-top: 0.5rem;'>
  43. <loading-button class='button--green button--margin' :loading='signup.loading' @click='createAccount'>
  44. Sign up
  45. </loading-button>
  46. <button class='button' @click='closeAccountModal'>
  47. Cancel
  48. </button>
  49. </div>
  50. </template>
  51. <template slot='Login'>
  52. <p style='margin-top: 0;'>
  53. Login to create and post in threads.
  54. </p>
  55. <fancy-input
  56. v-model='login.username'
  57. :error='login.errors.username'
  58. placeholder='Username'
  59. width='100%'
  60. >
  61. </fancy-input>
  62. <fancy-input
  63. v-model='login.password'
  64. :error='login.errors.hash'
  65. placeholder='Password'
  66. type='password'
  67. width='100%'
  68. >
  69. </fancy-input>
  70. <div style='margin-top: 0.5rem;'>
  71. <loading-button class='button button--green button--margin' :loading='login.loading' @click='doLogin'>
  72. <span class='fa fa-unlock-alt' style='margin-right:0.25rem'></span> Log in
  73. </loading-button>
  74. <button class='button' @click='closeAccountModal'>
  75. Cancel
  76. </button>
  77. </div>
  78. </template>
  79. </tab-view>
  80. </modal-window>
  81. <header class='header'>
  82. <div class='header__group'>
  83. <div class='logo' @click='$router.push("/")'>{{name}}</div>
  84. </div>
  85. <div class='header__group'>
  86. <template v-if='$store.state.username'>
  87. <notification-button></notification-button>
  88. <button @click='$router.push("/admin")' class='button button--thin_text' v-if='$store.state.admin'>
  89. Admin settings
  90. </button>
  91. <button @click='$router.push("/settings")' class='button button--thin_text' >
  92. Settings
  93. </button>
  94. <loading-button @click='logout' :loading='loadingLogout' class='button--thin_text'>
  95. Log out
  96. </loading-button>
  97. </template>
  98. <template v-else>
  99. <div class='button button--green button--thin_text' @click='showAccountModalTab(0)'>
  100. Sign up
  101. </div>
  102. <div class='button button--thin_text' @click='showAccountModalTab(1)'>
  103. Login
  104. </div>
  105. </template>
  106. <div
  107. class='search'
  108. tabindex='0'
  109. @keydown.enter='goToSearch'
  110. >
  111. <input
  112. class='search__field'
  113. placeholder='Search this forum'
  114. v-model='searchField'
  115. >
  116. <button
  117. class='search__button'
  118. @click='goToSearch'
  119. >
  120. <span class='fa fa-search'></span>
  121. </button>
  122. </div>
  123. </div>
  124. </header>
  125. <router-view></router-view>
  126. </div>
  127. </template>
  128. <script>
  129. import ModalWindow from './components/ModalWindow'
  130. import TabView from './components/TabView'
  131. import FancyInput from './components/FancyInput'
  132. import LoadingButton from './components/LoadingButton'
  133. import NotificationButton from './components/NotificationButton'
  134. import AjaxErrorHandler from './assets/js/errorHandler'
  135. export default {
  136. name: 'app',
  137. components: {
  138. ModalWindow,
  139. TabView,
  140. FancyInput,
  141. LoadingButton,
  142. NotificationButton
  143. },
  144. data () {
  145. return {
  146. signup: {
  147. username: '',
  148. password: '',
  149. confirmPassword: '',
  150. loading: false,
  151. errors: {
  152. username: '',
  153. hash: '',
  154. confirmPassword: ''
  155. }
  156. },
  157. login: {
  158. username: '',
  159. password: '',
  160. loading: false,
  161. errors: {
  162. username: '',
  163. hash: ''
  164. }
  165. },
  166. loadingLogout: false,
  167. ajaxErrorHandler: AjaxErrorHandler(this.$store),
  168. searchField: ''
  169. }
  170. },
  171. computed: {
  172. name () {
  173. return this.$store.state.meta.name
  174. },
  175. showAccountModal: {
  176. get () { return this.$store.state.accountModal },
  177. set (val) {
  178. this.$store.commit('setAccountModalState', val);
  179. }
  180. },
  181. showAjaxErrorsModal: {
  182. get () { return this.$store.state.ajaxErrorsModal },
  183. set (val) { this.$store.commit('setAjaxErrorsModalState', val) }
  184. },
  185. showAccountTab : {
  186. get (val) { return this.$store.state.accountTabs },
  187. set (index) { this.$store.commit('setAccountTabs', index) }
  188. },
  189. categories() {
  190. return this.$store.state.meta.categories
  191. }
  192. },
  193. methods: {
  194. goToSearch () {
  195. if(this.searchField.trim().length) {
  196. this.$router.push("/search/" + this.searchField)
  197. }
  198. },
  199. showAccountModalTab (index) {
  200. this.showAccountModal = true
  201. this.showAccountTab = index
  202. },
  203. logout () {
  204. this.loadingLogout = true
  205. this.axios.post(
  206. '/api/v1/user/' +
  207. this.$store.state.username +
  208. '/logout'
  209. ).then(res => {
  210. this.loadingLogout = false
  211. this.$store.commit('setUsername', '')
  212. this.$store.commit('setAdmin', res.data.admin)
  213. this.$router.push('/')
  214. }).catch(err => {
  215. this.loadingLogout = false
  216. this.ajaxErrorHandler(err)
  217. })
  218. },
  219. clearSignup () {
  220. this.signup.username = ''
  221. this.signup.password = ''
  222. this.signup.confirmPassword = ''
  223. this.$store.commit('setToken', null)
  224. },
  225. clearSignupErrors () {
  226. this.signup.errors.username = ''
  227. this.signup.errors.hash = ''
  228. this.signup.errors.confirmPassword = ''
  229. },
  230. clearLogin () {
  231. this.login.username = ''
  232. this.login.password = ''
  233. },
  234. clearLoginErrors () {
  235. this.login.errors.username = ''
  236. this.login.errors.hash = ''
  237. },
  238. closeAccountModal () {
  239. this.showAccountModal = false
  240. this.clearLogin()
  241. this.clearSignup()
  242. this.clearLoginErrors()
  243. this.clearSignupErrors()
  244. },
  245. createAccount () {
  246. this.clearSignupErrors()
  247. let postParams = {
  248. username: this.signup.username,
  249. password: this.signup.password
  250. }
  251. if(this.$store.state.token) {
  252. postParams.admin = true
  253. postParams.token = this.$store.state.token
  254. }
  255. if(this.signup.password !== this.signup.confirmPassword) {
  256. this.signup.errors.confirmPassword = 'Passwords must match'
  257. } else {
  258. this.signup.loading = true
  259. this.axios.post('/api/v1/user', postParams).then(res => {
  260. this.signup.loading = false
  261. this.$store.commit('setUsername', res.data.username)
  262. this.$store.commit('setAdmin', res.data.admin)
  263. this.closeAccountModal()
  264. socket.emit('login')
  265. }).catch(e => {
  266. this.signup.loading = false
  267. this.ajaxErrorHandler(e, (error) => {
  268. let path = error.path
  269. if(this.signup.errors[path] !== undefined && this.signup.errors[path] !== undefined) {
  270. this.signup.errors[path] = error.message
  271. }
  272. })
  273. })
  274. }
  275. },
  276. doLogin () {
  277. this.clearSignupErrors()
  278. if(!this.login.username.trim().length) {
  279. this.login.errors.username = 'Username must not be blank'
  280. return
  281. }
  282. this.login.loading = true
  283. this.axios.post(`/api/v1/user/${this.login.username}/login`, {
  284. password: this.login.password
  285. }).then(res => {
  286. this.login.loading = false
  287. this.$store.commit('setUsername', res.data.username)
  288. this.$store.commit('setAdmin', res.data.admin)
  289. this.closeAccountModal()
  290. socket.emit('login')
  291. }).catch(e => {
  292. this.login.loading = false
  293. this.ajaxErrorHandler(e, (error) => {
  294. let path = error.path
  295. if(this.signup.errors[path] !== undefined && this.signup.errors[path] !== undefined) {
  296. this.signup.errors[path] = error.message
  297. }
  298. })
  299. })
  300. }
  301. },
  302. created () {
  303. this.axios.get('/api/v1/settings')
  304. .then(res => {
  305. this.$store.commit('setForumName', res.data.forumName)
  306. this.$store.commit('setForumDescription', res.data.forumDescription)
  307. this.$store.dispatch('setTitle', this.$store.state.meta.title)
  308. }).catch(err => {
  309. if(err.response.data.errors[0].name === 'noSettings') {
  310. this.$router.push('/start')
  311. } else {
  312. this.ajaxErrorHandler(err)
  313. }
  314. })
  315. this.axios.get('/api/v1/category')
  316. .then(res => {
  317. this.$store.commit('addCategories', res.data)
  318. //Need categories to have loaded to set
  319. //the title of the index page
  320. //but if we're on another page (i.e. title is not set)
  321. //don't overwrite the title
  322. if(!this.$store.state.meta.title.length && this.$route.params.category) {
  323. let selectedCategory = this.$route.params.category.toUpperCase()
  324. let category = this.categories.find(c => c.value === selectedCategory)
  325. this.$store.dispatch('setTitle', category.name)
  326. }
  327. })
  328. .catch(this.ajaxErrorHandler)
  329. }
  330. }
  331. </script>
  332. <style lang='scss'>
  333. @import url('https://fonts.googleapis.com/css?family=Lato:300,300i,400,400i,700,700i|Montserrat');
  334. @import './assets/scss/variables.scss';
  335. @import './assets/scss/elementStyles.scss';
  336. html, body {
  337. width: 100%;
  338. height: 100%;
  339. margin: 0;
  340. padding: 0;
  341. color: $color__text--primary;
  342. @include text;
  343. }
  344. * {
  345. box-sizing: border-box;
  346. }
  347. .route_container {
  348. width: 80%;
  349. margin: 0 auto;
  350. margin-top: 2rem;
  351. padding-bottom: 2rem;
  352. }
  353. #app {
  354. padding-top: 4.5rem;
  355. height: 100%;
  356. }
  357. .header {
  358. width: 100%;
  359. padding: 0.5rem 2rem;
  360. position: fixed;
  361. top: 0;
  362. z-index: 2;
  363. display: flex;
  364. align-items: center;
  365. justify-content: space-between;
  366. border-bottom: 0.125rem solid $color__gray--primary;
  367. background-color: #fff;
  368. @at-root #{&}__group {
  369. display: flex;
  370. align-items: center;
  371. > * { margin: 0 0.5rem; }
  372. > *:first-child { margin-left: 0; }
  373. > *:last-child { margin-right: 0; }
  374. }
  375. }
  376. .logo {
  377. @include text($font--role-emphasis, 2rem, normal);
  378. @include user-select(none);
  379. cursor: pointer;
  380. }
  381. .search {
  382. border: 1px solid $color__gray--darker;
  383. border-radius: 0.25rem;
  384. outline: none;
  385. overflow: hidden;
  386. @at-root #{&}__field {
  387. outline: none;
  388. height: 100%;
  389. padding: 0 0.5rem;
  390. border: 0;
  391. @include text;
  392. color: $color__text--primary;
  393. @include placeholder {
  394. @include text;
  395. color: $color__lightgray--darkest;
  396. }
  397. }
  398. @at-root #{&}__button {
  399. @extend .button;
  400. border: 0;
  401. border-radius: 0 0.125rem 0.125rem 0;
  402. }
  403. }
  404. </style>