App.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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' :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' @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'><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 => {
  290. res.data.push({ name: 'All', value: 'ALL' })
  291. this.$store.commit('addCategories', res.data)
  292. }).catch(this.ajaxErrorHandler)
  293. }
  294. }
  295. </script>
  296. <style lang='scss'>
  297. @import url('https://fonts.googleapis.com/css?family=Lato:300,300i,400|Montserrat');
  298. @import './assets/scss/variables.scss';
  299. html, body {
  300. width: 100%;
  301. height: 100%;
  302. margin: 0;
  303. padding: 0;
  304. color: $color__text--primary;
  305. @include text;
  306. }
  307. * {
  308. box-sizing: border-box;
  309. }
  310. .route_container {
  311. width: 80%;
  312. margin: 0 auto;
  313. margin-top: 2rem;
  314. padding-bottom: 2rem;
  315. }
  316. #app {
  317. padding-top: 4.5rem;
  318. height: 100%;
  319. }
  320. .header {
  321. width: 100%;
  322. padding: 1rem 2rem;
  323. position: fixed;
  324. top: 0;
  325. z-index: 2;
  326. background-color: #fff;
  327. display: flex;
  328. align-items: center;
  329. justify-content: space-between;
  330. border-bottom: 0.125rem solid $color__gray--primary;
  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. .button {
  344. border: 0.125rem solid $color__gray--primary;
  345. display: inline-block;
  346. text-align: center;
  347. @include text($font--role-default, 1rem, 400);
  348. padding: 0.5rem;
  349. cursor: pointer;
  350. background-color: #fff;
  351. transition: background-color 0.2s, border-color 0.2s;
  352. outline: none;
  353. &:hover {
  354. background-color: $color__lightgray--primary;
  355. border-color: $color__gray--darker;
  356. }
  357. &:active {
  358. background-color: $color__lightgray--darker;
  359. border-color: $color__gray--darkest;
  360. }
  361. @at-root #{&}__icon {
  362. }
  363. @at-root #{&}--borderless {
  364. border: 0;
  365. }
  366. @at-root #{&}--modal {
  367. padding: 0.25rem 0.5rem;
  368. font-size: 0.8rem;
  369. float: right;
  370. margin-bottom: 1rem;
  371. &:last-child {
  372. margin-right: 0.5rem;
  373. }
  374. }
  375. @at-root #{&}--orange {
  376. border-color: $color__orange--primary;
  377. &:hover { border-color: $color__orange--darker; }
  378. &:active { border-color: $color__orange--darkest; }
  379. }
  380. @at-root #{&}--green {
  381. background-color: $color__green--primary;
  382. color: #fff;
  383. border-color: $color__green--darker;
  384. &:hover {
  385. border-color: $color__green--darker;
  386. background-color: rgba(75, 171, 79, 0.86);
  387. }
  388. &:active {
  389. border-color: $color__green--darker;
  390. background-color: $color__green--darkester;
  391. }
  392. }
  393. }
  394. .input {
  395. border: 0.125rem solid $color__gray--primary;
  396. @include text;
  397. padding: 0.25rem;
  398. outline: none;
  399. &:hover {
  400. border-color: $color__gray--darker;
  401. }
  402. &:focus {
  403. border-color: $color__gray--darkest;
  404. }
  405. }
  406. .search {
  407. border: 0.125rem solid $color__gray--primary;
  408. &:hover {
  409. border-color: $color__gray--darker;
  410. }
  411. &:focus {
  412. border-color: $color__gray--darkest;
  413. }
  414. @at-root #{&}__field {
  415. outline: none;
  416. height: 100%;
  417. padding: 0 0.5rem;
  418. border: 0;
  419. @include text;
  420. @include placeholder {
  421. @include text;
  422. }
  423. }
  424. }
  425. </style>