App.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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;'>
  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. <notification-button></notification-button>
  81. <button @click='$router.push("/settings")' class='button' >
  82. Settings
  83. </button>
  84. <loading-button @click='logout' :loading='loadingLogout'>
  85. Log out
  86. </loading-button>
  87. </template>
  88. <template v-else>
  89. <div class='button button--green' @click='showAccountModalTab(0)'>
  90. Sign up
  91. </div>
  92. <div class='button' @click='showAccountModalTab(1)'>
  93. Login
  94. </div>
  95. </template>
  96. <div class='search' tabindex='0'>
  97. <input class='search__field' placeholder='Search this forum'>
  98. <button class='button button--borderless search__button'><span class='fa fa-search'></span></button>
  99. </div>
  100. </div>
  101. </header>
  102. <router-view></router-view>
  103. </div>
  104. </template>
  105. <script>
  106. import ModalWindow from './components/ModalWindow'
  107. import TabView from './components/TabView'
  108. import FancyInput from './components/FancyInput'
  109. import LoadingButton from './components/LoadingButton'
  110. import NotificationButton from './components/NotificationButton'
  111. import mapGetters from 'vuex'
  112. import AjaxErrorHandler from './assets/js/errorHandler'
  113. let { addFlexBoxChildren } = require('./assets/js/flexBoxGridCorrect')
  114. export default {
  115. name: 'app',
  116. components: {
  117. ModalWindow,
  118. TabView,
  119. FancyInput,
  120. LoadingButton,
  121. NotificationButton
  122. },
  123. data () {
  124. return {
  125. signup: {
  126. username: '',
  127. password: '',
  128. confirmPassword: '',
  129. loading: false,
  130. errors: {
  131. username: '',
  132. password: '',
  133. confirmPassword: ''
  134. }
  135. },
  136. login: {
  137. username: '',
  138. password: '',
  139. loading: false,
  140. errors: {
  141. username: '',
  142. password: ''
  143. }
  144. },
  145. loadingLogout: false,
  146. ajaxErrorHandler: AjaxErrorHandler(this.$store)
  147. }
  148. },
  149. computed: {
  150. name () {
  151. return this.$store.state.meta.name
  152. },
  153. showAccountModal: {
  154. get () { return this.$store.state.accountModal },
  155. set (val) {
  156. this.$store.commit('setAccountModalState', val);
  157. }
  158. },
  159. showAjaxErrorsModal: {
  160. get () { return this.$store.state.ajaxErrorsModal },
  161. set (val) { this.$store.commit('setAjaxErrorsModalState', val) }
  162. },
  163. showAccountTab : {
  164. get (val) { return this.$store.state.accountTabs },
  165. set (index) { this.$store.commit('setAccountTabs', index) }
  166. },
  167. categories() {
  168. return this.$store.state.meta.categories
  169. }
  170. },
  171. watch: {
  172. $route (to) {
  173. if(to.path === '/') {
  174. setTimeout(() => {
  175. addFlexBoxChildren('.index_categories', 'index_category');
  176. }, 50);
  177. }
  178. },
  179. categories () {
  180. setTimeout(() => {
  181. addFlexBoxChildren('.index_categories', 'index_category');
  182. }, 50);
  183. }
  184. },
  185. methods: {
  186. showAccountModalTab (index) {
  187. this.showAccountModal = true
  188. this.showAccountTab = index
  189. },
  190. logout () {
  191. this.loadingLogout = true
  192. this.axios.post(
  193. '/api/v1/user/' +
  194. this.$store.state.username +
  195. '/logout'
  196. ).then(res => {
  197. this.loadingLogout = false
  198. this.$store.commit('setUsername', '')
  199. this.$router.push('/')
  200. }).catch(err => {
  201. this.loadingLogout = false
  202. this.ajaxErrorHandler(err)
  203. })
  204. },
  205. clearSignup () {
  206. this.signup.username = ''
  207. this.signup.password = ''
  208. this.signup.confirmPassword = ''
  209. },
  210. clearSignupErrors () {
  211. this.signup.errors.username = ''
  212. this.signup.errors.password = ''
  213. this.signup.errors.confirmPassword = ''
  214. },
  215. clearLogin () {
  216. this.login.username = ''
  217. this.login.password = ''
  218. },
  219. clearLoginErrors () {
  220. this.login.errors.username = ''
  221. this.login.errors.password = ''
  222. },
  223. closeAccountModal () {
  224. this.showAccountModal = false
  225. this.clearLogin()
  226. this.clearSignup()
  227. this.clearLoginErrors()
  228. this.clearSignupErrors()
  229. },
  230. createAccount () {
  231. this.clearSignupErrors()
  232. if(this.signup.password !== this.signup.confirmPassword) {
  233. this.signup.errors.confirmPassword = 'Passwords must match'
  234. } else {
  235. this.signup.loading = true
  236. this.axios.post('/api/v1/user', {
  237. username: this.signup.username,
  238. password: this.signup.password
  239. }).then(res => {
  240. this.signup.loading = false
  241. this.$store.commit('setUsername', res.data.username)
  242. this.closeAccountModal()
  243. }).catch(e => {
  244. this.signup.loading = false
  245. this.ajaxErrorHandler(e, (error) => {
  246. let param = error.parameter
  247. if(this.signup.errors[param] !== undefined) {
  248. this.signup.errors[param] = error.message
  249. }
  250. })
  251. })
  252. }
  253. },
  254. doLogin () {
  255. this.clearSignupErrors()
  256. if(!this.login.username.trim().length) {
  257. this.login.errors.username = 'Username must not be blank'
  258. return
  259. }
  260. this.login.loading = true
  261. this.axios.post(`/api/v1/user/${this.login.username}/login`, {
  262. password: this.login.password
  263. }).then(res => {
  264. this.login.loading = false
  265. this.$store.commit('setUsername', res.data.username)
  266. this.closeAccountModal()
  267. }).catch(e => {
  268. this.login.loading = false
  269. this.ajaxErrorHandler(e, (error) => {
  270. let param = error.parameter
  271. if(this.login.errors[param] !== undefined) {
  272. this.login.errors[param] = error.message
  273. }
  274. })
  275. })
  276. }
  277. },
  278. created () {
  279. this.axios.get('/api/v1/settings')
  280. .then(res => {
  281. this.$store.commit('setForumName', res.data.forumName)
  282. }).catch(err => {
  283. if(err.response.data.errors[0].name === 'noSettings') {
  284. this.$router.push('/start')
  285. } else {
  286. this.ajaxErrorHandler(err)
  287. }
  288. })
  289. this.axios.get('/api/v1/category')
  290. .then(res => this.$store.commit('addCategories', res.data))
  291. .catch(this.ajaxErrorHandler)
  292. }
  293. }
  294. </script>
  295. <style lang='scss'>
  296. @import url('https://fonts.googleapis.com/css?family=Lato:300,300i,400,400i,700,700i|Montserrat');
  297. @import './assets/scss/variables.scss';
  298. @import './assets/scss/elementStyles.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: 0.5rem 2rem;
  323. position: fixed;
  324. top: 0;
  325. z-index: 2;
  326. display: flex;
  327. align-items: center;
  328. justify-content: space-between;
  329. border-bottom: 0.125rem solid $color__gray--primary;
  330. background-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. border: 0.125rem solid $color__gray--primary;
  345. border-radius: 0.25rem;
  346. @at-root #{&}__field {
  347. outline: none;
  348. height: 100%;
  349. padding: 0 0.5rem;
  350. border: 0;
  351. @include text;
  352. color: $color__text--primary;
  353. @include placeholder {
  354. @include text;
  355. color: $color__lightgray--darkest;
  356. }
  357. }
  358. @at-root #{&}__button {
  359. border-radius: 0;
  360. }
  361. }
  362. </style>