App.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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
  7. class='button button--modal'
  8. @click='showAjaxErrorsModal = false'
  9. ref='ajaxErrorsModalButton'
  10. >
  11. OK
  12. </button>
  13. </div>
  14. </modal-window>
  15. <modal-window v-model='showAccountModal' @input='closeAccountModal'>
  16. <tab-view :tabs='["Sign up", "Login"]' v-model="showAccountTab" padding='true'>
  17. <template slot='Sign up'>
  18. <p style='margin-top: 0;' v-if='$store.state.token'>
  19. <strong>Providing the token is still valid, this will create an admin account</strong>
  20. </p>
  21. <p style='margin-top: 0;' v-else>
  22. Sign up to create and post in threads.
  23. <br/>It only takes a few seconds
  24. </p>
  25. <form @submit.prevent='createAccount'>
  26. <fancy-input
  27. v-model='signup.username'
  28. :error='signup.errors.username'
  29. placeholder='Username'
  30. width='100%'
  31. >
  32. </fancy-input>
  33. <fancy-input
  34. v-model='signup.password'
  35. :error='signup.errors.hash'
  36. placeholder='Password'
  37. type='password'
  38. width='100%'
  39. >
  40. </fancy-input>
  41. <fancy-input
  42. v-model='signup.confirmPassword'
  43. :error='signup.errors.confirmPassword'
  44. placeholder='Confirm password'
  45. type='password'
  46. width='100%'
  47. >
  48. </fancy-input>
  49. <div style='margin-top: 0.5rem;'>
  50. <loading-button
  51. class='button--green button--margin'
  52. :loading='signup.loading'
  53. @click='createAccount'
  54. >
  55. Sign up
  56. </loading-button>
  57. <div class='button button--borderless' @click='closeAccountModal'>
  58. Cancel
  59. </div>
  60. </div>
  61. </form>
  62. </template>
  63. <template slot='Login'>
  64. <p style='margin-top: 0;'>
  65. Login to create and post in threads.
  66. </p>
  67. <form @submit.prevent='doLogin'>
  68. <fancy-input
  69. v-model='login.username'
  70. :error='login.errors.username'
  71. placeholder='Username'
  72. width='100%'
  73. >
  74. </fancy-input>
  75. <fancy-input
  76. v-model='login.password'
  77. :error='login.errors.hash'
  78. placeholder='Password'
  79. type='password'
  80. width='100%'
  81. >
  82. </fancy-input>
  83. <div style='margin-top: 0.5rem;'>
  84. <loading-button
  85. class='button button--green button--margin'
  86. :loading='login.loading'
  87. @click='doLogin'
  88. >
  89. <span class='fa fa-unlock-alt' style='margin-right:0.25rem'></span> Log in
  90. </loading-button>
  91. <div class='button button--borderless' @click='closeAccountModal'>
  92. Cancel
  93. </div>
  94. </div>
  95. </form>
  96. </template>
  97. </tab-view>
  98. </modal-window>
  99. <header class='header'>
  100. <div class='header__group'>
  101. <router-link class='logo' to='/'>{{name}}</router-link>
  102. </div>
  103. <div class='header__group' :class='{ "header__group--show": showMenu }'>
  104. <template v-if='$store.state.username'>
  105. <notification-button></notification-button>
  106. <router-link
  107. to='/admin'
  108. class='button button--thin_text'
  109. v-if='$store.state.admin'
  110. >
  111. Admin settings
  112. </router-link>
  113. <router-link
  114. to='/settings'
  115. class='button button--thin_text'
  116. >
  117. Settings
  118. </router-link>
  119. <loading-button
  120. @click='logout'
  121. :loading='loadingLogout'
  122. class='button--thin_text'
  123. >
  124. Log out
  125. </loading-button>
  126. </template>
  127. <template v-else>
  128. <div class='button button--green button--thin_text' @click='showAccountModalTab(0)'>
  129. Sign up
  130. </div>
  131. <div class='button button--thin_text' @click='showAccountModalTab(1)'>
  132. Login
  133. </div>
  134. </template>
  135. <search-box header-bar='true'></search-box>
  136. </div>
  137. <div class='header__overlay' :class='{ "header__overlay--show": showMenu }' @click='toggleMenu'></div>
  138. <span class='fa fa-bars header__menu_button' @click='toggleMenu'></span>
  139. </header>
  140. <not-found v-show='$store.state.show404Page'></not-found>
  141. <router-view v-show='!$store.state.show404Page'></router-view>
  142. </div>
  143. </template>
  144. <script>
  145. import ModalWindow from './components/ModalWindow'
  146. import TabView from './components/TabView'
  147. import FancyInput from './components/FancyInput'
  148. import LoadingButton from './components/LoadingButton'
  149. import NotificationButton from './components/NotificationButton'
  150. import SearchBox from './components/SearchBox'
  151. import NotFound from './components/routes/NotFound'
  152. import AjaxErrorHandler from './assets/js/errorHandler'
  153. export default {
  154. name: 'app',
  155. components: {
  156. ModalWindow,
  157. TabView,
  158. FancyInput,
  159. LoadingButton,
  160. NotificationButton,
  161. SearchBox,
  162. NotFound
  163. },
  164. data () {
  165. return {
  166. signup: {
  167. username: '',
  168. password: '',
  169. confirmPassword: '',
  170. loading: false,
  171. errors: {
  172. username: '',
  173. hash: '',
  174. confirmPassword: ''
  175. }
  176. },
  177. login: {
  178. username: '',
  179. password: '',
  180. loading: false,
  181. errors: {
  182. username: '',
  183. hash: ''
  184. }
  185. },
  186. loadingLogout: false,
  187. showMenu: false,
  188. ajaxErrorHandler: AjaxErrorHandler(this.$store)
  189. }
  190. },
  191. computed: {
  192. name () {
  193. return this.$store.state.meta.name
  194. },
  195. showAccountModal: {
  196. get () { return this.$store.state.accountModal },
  197. set (val) {
  198. this.$store.commit('setAccountModalState', val);
  199. }
  200. },
  201. showAjaxErrorsModal: {
  202. get () { return this.$store.state.ajaxErrorsModal },
  203. set (val) { this.$store.commit('setAjaxErrorsModalState', val) }
  204. },
  205. showAccountTab : {
  206. get (val) { return this.$store.state.accountTabs },
  207. set (index) { this.$store.commit('setAccountTabs', index) }
  208. },
  209. categories() {
  210. return this.$store.state.meta.categories
  211. }
  212. },
  213. methods: {
  214. showAccountModalTab (index) {
  215. this.toggleMenu()
  216. this.showAccountModal = true
  217. this.showAccountTab = index
  218. },
  219. toggleMenu () {
  220. this.showMenu = !this.showMenu
  221. },
  222. logout () {
  223. this.toggleMenu()
  224. this.loadingLogout = true
  225. this.axios.post(
  226. '/api/v1/user/' +
  227. this.$store.state.username +
  228. '/logout'
  229. ).then(res => {
  230. this.loadingLogout = false
  231. this.$store.commit('setUsername', '')
  232. this.$store.commit('setAdmin', res.data.admin)
  233. socket.emit('accountEvent')
  234. this.$router.push('/')
  235. }).catch(err => {
  236. this.loadingLogout = false
  237. this.ajaxErrorHandler(err)
  238. })
  239. },
  240. clearSignup () {
  241. this.signup.username = ''
  242. this.signup.password = ''
  243. this.signup.confirmPassword = ''
  244. this.$store.commit('setToken', null)
  245. },
  246. clearSignupErrors () {
  247. this.signup.errors.username = ''
  248. this.signup.errors.hash = ''
  249. this.signup.errors.confirmPassword = ''
  250. },
  251. clearLogin () {
  252. this.login.username = ''
  253. this.login.password = ''
  254. },
  255. clearLoginErrors () {
  256. this.login.errors.username = ''
  257. this.login.errors.hash = ''
  258. },
  259. closeAccountModal () {
  260. this.showAccountModal = false
  261. this.clearLogin()
  262. this.clearSignup()
  263. this.clearLoginErrors()
  264. this.clearSignupErrors()
  265. },
  266. createAccount () {
  267. this.clearSignupErrors()
  268. let postParams = {
  269. username: this.signup.username,
  270. password: this.signup.password
  271. }
  272. if(this.$store.state.token) {
  273. postParams.admin = true
  274. postParams.token = this.$store.state.token
  275. }
  276. if(this.signup.password !== this.signup.confirmPassword) {
  277. this.signup.errors.confirmPassword = 'Passwords must match'
  278. } else {
  279. this.signup.loading = true
  280. this.axios.post('/api/v1/user', postParams).then(res => {
  281. this.signup.loading = false
  282. this.$store.commit('setUsername', res.data.username)
  283. this.$store.commit('setAdmin', res.data.admin)
  284. this.closeAccountModal()
  285. socket.emit('accountEvent')
  286. }).catch(e => {
  287. this.signup.loading = false
  288. this.ajaxErrorHandler(e, (error) => {
  289. let path = error.path
  290. if(this.signup.errors[path] !== undefined && this.signup.errors[path] !== undefined) {
  291. this.signup.errors[path] = error.message
  292. }
  293. })
  294. })
  295. }
  296. },
  297. doLogin () {
  298. this.clearSignupErrors()
  299. if(!this.login.username.trim().length) {
  300. this.login.errors.username = 'Username must not be blank'
  301. return
  302. }
  303. this.login.loading = true
  304. this.axios.post(`/api/v1/user/${this.login.username}/login`, {
  305. password: this.login.password
  306. }).then(res => {
  307. this.login.loading = false
  308. this.$store.commit('setUsername', res.data.username)
  309. this.$store.commit('setAdmin', res.data.admin)
  310. this.closeAccountModal()
  311. socket.emit('accountEvent')
  312. }).catch(e => {
  313. this.login.loading = false
  314. this.ajaxErrorHandler(e, (error) => {
  315. let path = error.path
  316. if(this.signup.errors[path] !== undefined && this.signup.errors[path] !== undefined) {
  317. this.signup.errors[path] = error.message
  318. }
  319. })
  320. })
  321. }
  322. },
  323. created () {
  324. this.axios.get('/api/v1/settings')
  325. .then(res => {
  326. this.$store.commit('setSettings', res.data)
  327. this.$store.dispatch('setTitle', this.$store.state.meta.title)
  328. }).catch(err => {
  329. if(err.response.data.errors[0].name === 'noSettings') {
  330. this.$router.push('/start')
  331. } else {
  332. this.ajaxErrorHandler(err)
  333. }
  334. })
  335. this.axios.get('/api/v1/category')
  336. .then(res => {
  337. this.$store.commit('addCategories', res.data)
  338. //Need categories to have loaded to set
  339. //the title of the index page
  340. //but if we're on another page (i.e. title is not set)
  341. //don't overwrite the title
  342. if(!this.$store.state.meta.title.length && this.$route.params.category) {
  343. let selectedCategory = this.$route.params.category.toUpperCase()
  344. let category = this.categories.find(c => c.value === selectedCategory)
  345. this.$store.dispatch('setTitle', category.name)
  346. }
  347. })
  348. .catch(this.ajaxErrorHandler)
  349. },
  350. watch: {
  351. $route () {
  352. this.showMenu = false
  353. },
  354. '$store.state.ajaxErrorsModal': function(val) {
  355. if(val) {
  356. this.$refs.ajaxErrorsModalButton.focus()
  357. }
  358. }
  359. }
  360. }
  361. </script>
  362. <style lang='scss'>
  363. @import url('https://fonts.googleapis.com/css?family=Lato:300,300i,400,400i,700');
  364. @import './assets/scss/variables.scss';
  365. @import './assets/scss/elementStyles.scss';
  366. html, body {
  367. width: 100%;
  368. height: 100%;
  369. margin: 0;
  370. padding: 0;
  371. color: $color__text--primary;
  372. @include text;
  373. }
  374. * {
  375. box-sizing: border-box;
  376. }
  377. .route_container {
  378. width: 80%;
  379. max-width: 1250px;
  380. margin: 0 auto;
  381. margin-top: 2rem;
  382. padding-bottom: 2rem;
  383. }
  384. #app {
  385. padding-top: 4.5rem;
  386. height: 100%;
  387. }
  388. .header {
  389. width: 100%;
  390. padding: 0.5rem 2rem;
  391. position: fixed;
  392. top: 0;
  393. z-index: 2;
  394. display: flex;
  395. align-items: center;
  396. justify-content: space-between;
  397. border-bottom: 0.125rem solid $color__gray--primary;
  398. background-color: #fff;
  399. @at-root #{&}__group {
  400. display: flex;
  401. align-items: center;
  402. > * { margin: 0 0.5rem; }
  403. > *:first-child { margin-left: 0; }
  404. > *:last-child { margin-right: 0; }
  405. }
  406. @at-root #{&}__menu_button {
  407. position: fixed;
  408. left: 1rem;
  409. z-index: 1;
  410. font-size: 1.5rem;
  411. top: 1rem;
  412. display: none;
  413. }
  414. @at-root #{&}__overlay {
  415. width: 100%;
  416. height: 100%;
  417. position: fixed;
  418. top: 0;
  419. left: 0;
  420. z-index: 1;
  421. pointer-events: none;
  422. opacity: 0;
  423. background-color: hsla(215, 13%, 25%, 0.5);
  424. transition: all 0.4s;
  425. }
  426. }
  427. .logo {
  428. @include text($font--role-emphasis, 2rem, normal);
  429. @include user-select(none);
  430. cursor: pointer;
  431. background: none;
  432. white-space: nowrap;
  433. overflow: hidden;
  434. text-overflow: ellipsis;
  435. max-width: 20rem;
  436. &:hover, &:visited, &:active {
  437. color: $color__text--primary;
  438. }
  439. }
  440. @media (max-width: 870px) {
  441. .route_container {
  442. width: calc(100% - 2rem);
  443. margin: 0 1rem;
  444. margin-top: 0rem;
  445. }
  446. .logo {
  447. position: relative;
  448. z-index: 2;
  449. max-width: calc(100vw - 7rem);
  450. }
  451. .header__menu_button {
  452. display: inline-block;
  453. cursor: pointer;
  454. }
  455. .header__overlay--show {
  456. pointer-events: all;
  457. opacity: 1;
  458. }
  459. .header__group:first-child {
  460. margin-left: 1rem;
  461. }
  462. .header__group:nth-child(2) {
  463. position: fixed;
  464. padding-top: 1.5rem;
  465. width: 17rem;
  466. display: flex;
  467. flex-direction: column;
  468. z-index: 2;
  469. background: #fff;
  470. top: 0;
  471. left: calc(-100% - 2rem);
  472. height: 100%;
  473. box-shadow: none;
  474. transition: left 0.4s cubic-bezier(0.4, 0, 0.2, 1), box-shadow 0.4s ease-in;
  475. > .button {
  476. width: 100%;
  477. border-radius: 0;
  478. margin: 0;
  479. margin-bottom: 1rem;
  480. }
  481. &::before {
  482. position: absolute;
  483. top: 0;
  484. left: 0;
  485. width: 100%;
  486. height: 0.3rem;
  487. content: '';
  488. background: linear-gradient(to right, hsl(200, 98%, 43%), hsla(193, 98%, 48%, 1));
  489. }
  490. }
  491. .header__group:nth-child(2).header__group--show {
  492. left: 0;
  493. box-shadow: 0 0 1rem rgba(0, 0, 0, 0.4);
  494. }
  495. .search_box {
  496. margin: 0;
  497. display: inline-block;
  498. }
  499. }
  500. </style>