App.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <template>
  2. <div id='app'>
  3. <modal-window v-model='showAjaxErrorsModal' style='z-index: 100' width='auto'>
  4. <div style='padding: 0rem 1rem 1rem 1rem;'>
  5. <p v-for='error in this.$store.state.ajaxErrors'>{{error}}</p>
  6. <button class='button' @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. export default {
  109. name: 'app',
  110. components: {
  111. ModalWindow,
  112. TabView,
  113. FancyInput,
  114. LoadingButton
  115. },
  116. data () {
  117. return {
  118. signup: {
  119. username: '',
  120. password: '',
  121. confirmPassword: '',
  122. loading: false,
  123. errors: {
  124. username: '',
  125. password: '',
  126. confirmPassword: ''
  127. }
  128. },
  129. login: {
  130. username: '',
  131. password: '',
  132. loading: false,
  133. errors: {
  134. username: '',
  135. password: ''
  136. }
  137. },
  138. loadingLogout: false,
  139. ajaxErrorHandler: AjaxErrorHandler(this.$store)
  140. }
  141. },
  142. computed: {
  143. name () {
  144. return this.$store.state.meta.name
  145. },
  146. showAccountModal: {
  147. get () { return this.$store.state.accountModal },
  148. set (val) {
  149. this.$store.commit('setAccountModalState', val);
  150. }
  151. },
  152. showAjaxErrorsModal: {
  153. get () { return this.$store.state.ajaxErrorsModal },
  154. set (val) { this.$store.commit('setAjaxErrorsModalState', val) }
  155. },
  156. showAccountTab : {
  157. get (val) { return this.$store.state.accountTabs },
  158. set (index) { this.$store.commit('setAccountTabs', index) }
  159. }
  160. },
  161. methods: {
  162. showAccountModalTab (index) {
  163. this.showAccountModal = true
  164. this.showAccountTab = index
  165. },
  166. logout () {
  167. this.loadingLogout = true
  168. this.axios.post(
  169. '/api/v1/user/' +
  170. this.$store.state.username +
  171. '/logout'
  172. ).then(res => {
  173. this.loadingLogout = false
  174. this.$store.commit('setUsername', '')
  175. }).catch(err => {
  176. this.loadingLogout = false
  177. this.ajaxErrorHandler(err)
  178. })
  179. },
  180. clearSignup () {
  181. this.signup.username = ''
  182. this.signup.password = ''
  183. this.signup.confirmPassword = ''
  184. },
  185. clearSignupErrors () {
  186. this.signup.errors.username = ''
  187. this.signup.errors.password = ''
  188. this.signup.errors.confirmPassword = ''
  189. },
  190. clearLogin () {
  191. this.login.username = ''
  192. this.login.password = ''
  193. },
  194. clearLoginErrors () {
  195. this.login.errors.username = ''
  196. this.login.errors.password = ''
  197. },
  198. closeAccountModal () {
  199. this.showAccountModal = false
  200. this.clearLogin()
  201. this.clearSignup()
  202. this.clearLoginErrors()
  203. this.clearSignupErrors()
  204. },
  205. createAccount () {
  206. this.clearSignupErrors()
  207. if(this.signup.password !== this.signup.confirmPassword) {
  208. this.signup.errors.confirmPassword = 'Passwords must match'
  209. } else {
  210. this.signup.loading = true
  211. this.axios.post('/api/v1/user', {
  212. username: this.signup.username,
  213. password: this.signup.password
  214. }).then(res => {
  215. this.signup.loading = false
  216. this.$store.commit('setUsername', res.data.username)
  217. this.closeAccountModal()
  218. }).catch(e => {
  219. this.signup.loading = false
  220. this.ajaxErrorHandler(e, (error) => {
  221. let param = error.parameter
  222. if(this.signup.errors[param] !== undefined) {
  223. this.signup.errors[param] = error.message
  224. }
  225. })
  226. })
  227. }
  228. },
  229. doLogin () {
  230. this.clearSignupErrors()
  231. if(!this.login.username.trim().length) {
  232. this.login.errors.username = 'Username must not be blank'
  233. return
  234. }
  235. this.login.loading = true
  236. this.axios.post(`/api/v1/user/${this.login.username}/login`, {
  237. password: this.login.password
  238. }).then(res => {
  239. this.login.loading = false
  240. this.$store.commit('setUsername', res.data.username)
  241. this.closeAccountModal()
  242. }).catch(e => {
  243. this.login.loading = false
  244. this.ajaxErrorHandler(e, (error) => {
  245. let param = error.parameter
  246. if(this.login.errors[param] !== undefined) {
  247. this.login.errors[param] = error.message
  248. }
  249. })
  250. })
  251. }
  252. },
  253. created () {
  254. this.axios.get('/api/v1/settings')
  255. .then(res => {
  256. this.$store.commit('setForumName', res.data.forumName)
  257. }).catch(err => {
  258. if(err.response.data.errors[0].name === 'noSettings') {
  259. this.$router.push('/start')
  260. } else {
  261. this.ajaxErrorHandler(err)
  262. }
  263. })
  264. this.axios.get('/api/v1/category')
  265. .then(res => {
  266. this.$store.commit('addCategories', res.data)
  267. }).catch(this.ajaxErrorHandler)
  268. }
  269. }
  270. </script>
  271. <style lang='scss'>
  272. @import url('https://fonts.googleapis.com/css?family=Lato:300,300i,400|Montserrat');
  273. @import './assets/scss/variables.scss';
  274. html, body {
  275. width: 100%;
  276. height: 100%;
  277. margin: 0;
  278. padding: 0;
  279. color: $color__text--primary;
  280. @include text;
  281. }
  282. * {
  283. box-sizing: border-box;
  284. }
  285. .route_container {
  286. width: 80%;
  287. margin: 0 auto;
  288. margin-top: 2rem;
  289. padding-bottom: 2rem;
  290. }
  291. #app {
  292. padding-top: 4.5rem;
  293. height: 100%;
  294. }
  295. .header {
  296. width: 100%;
  297. padding: 1rem 2rem;
  298. position: fixed;
  299. top: 0;
  300. z-index: 2;
  301. background-color: #fff;
  302. display: flex;
  303. align-items: center;
  304. justify-content: space-between;
  305. border-bottom: 0.125rem solid $color__gray--primary;
  306. @at-root #{&}__group {
  307. display: flex;
  308. > * { margin: 0 0.5rem; }
  309. > *:first-child { margin-left: 0; }
  310. > *:last-child { margin-right: 0; }
  311. }
  312. }
  313. .logo {
  314. @include text($font--role-emphasis, 2rem, normal);
  315. @include user-select(none);
  316. cursor: pointer;
  317. }
  318. .button {
  319. border: 0.125rem solid $color__gray--primary;
  320. display: inline-block;
  321. text-align: center;
  322. @include text($font--role-default, 1rem, 400);
  323. padding: 0.5rem;
  324. cursor: pointer;
  325. background-color: #fff;
  326. transition: background-color 0.2s, border-color 0.2s;
  327. outline: none;
  328. &:hover {
  329. background-color: $color__lightgray--primary;
  330. border-color: $color__gray--darker;
  331. }
  332. &:active {
  333. background-color: $color__lightgray--darker;
  334. border-color: $color__gray--darkest;
  335. }
  336. @at-root #{&}__icon {
  337. }
  338. @at-root #{&}--borderless {
  339. border: 0;
  340. }
  341. @at-root #{&}--orange {
  342. border-color: $color__orange--primary;
  343. &:hover { border-color: $color__orange--darker; }
  344. &:active { border-color: $color__orange--darkest; }
  345. }
  346. @at-root #{&}--green {
  347. background-color: $color__green--primary;
  348. color: #fff;
  349. border-color: $color__green--darker;
  350. &:hover {
  351. border-color: $color__green--darker;
  352. background-color: rgba(75, 171, 79, 0.86);
  353. }
  354. &:active {
  355. border-color: $color__green--darker;
  356. background-color: $color__green--darkester;
  357. }
  358. }
  359. }
  360. .input {
  361. border: 0.125rem solid $color__gray--primary;
  362. @include text;
  363. padding: 0.25rem;
  364. outline: none;
  365. &:hover {
  366. border-color: $color__gray--darker;
  367. }
  368. &:focus {
  369. border-color: $color__gray--darkest;
  370. }
  371. }
  372. .search {
  373. border: 0.125rem solid $color__gray--primary;
  374. &:hover {
  375. border-color: $color__gray--darker;
  376. }
  377. &:focus {
  378. border-color: $color__gray--darkest;
  379. }
  380. @at-root #{&}__field {
  381. outline: none;
  382. height: 100%;
  383. padding: 0 0.5rem;
  384. border: 0;
  385. @include text;
  386. @include placeholder {
  387. @include text;
  388. }
  389. }
  390. }
  391. </style>