App.vue 9.5 KB

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