ThreadPoll.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <template>
  2. <div class='poll'>
  3. <transition name='slide' mode='out-in'>
  4. <div class='poll__loading' key='loading' v-if='!poll'>
  5. <loading-icon :dark='true'></loading-icon>
  6. </div>
  7. <div key='poll' v-else>
  8. <div class='poll__question'>{{poll.question}}</div>
  9. <div class='poll__voting_view' v-if='view === "voting" && !poll.hasVoted'>
  10. <div class='poll__answers'>
  11. <div
  12. class='poll__answer'
  13. :class='{ "poll__answer--selected" : answer === selected }'
  14. v-for='answer in poll.PollAnswers'
  15. @click='selected = answer'
  16. >
  17. {{answer.answer}}
  18. </div>
  19. </div>
  20. <div class='poll__buttons'>
  21. <loading-button
  22. class='button--blue'
  23. :class='{ "button--disabled": !selected }'
  24. :loading='loading'
  25. @click='vote'
  26. >Vote now</loading-button>
  27. <button
  28. class='button button--borderless button--thin_text'
  29. @click='view = "results"'
  30. >View results</button>
  31. </div>
  32. </div>
  33. <div class='poll__results_view' v-else>
  34. <div class='poll__total_votes'>
  35. {{poll.totalVotes}}
  36. {{poll.totalVotes | pluralize('total vote')}}
  37. </div>
  38. <div class='poll__results'>
  39. <div class='poll__result' v-for='result in poll.PollAnswers'>
  40. <div>
  41. {{result.answer}}
  42. <span class='poll__result__info'>
  43. &middot;
  44. {{result.PollVotes.length}} {{result.PollVotes.length | pluralize('vote')}}
  45. ({{result.percent || 0}}%)
  46. </span>
  47. </div>
  48. <div class='poll__result__bar_outer'></div>
  49. <div class='poll__result__bar' :style='{ "width": (result.percent || 0) + "%" }'></div>
  50. </div>
  51. </div>
  52. <div class='poll__buttons' v-if='!poll.hasVoted'>
  53. <button class='button button--thin_text' @click='view = "voting"'>Back</button>
  54. </div>
  55. </div>
  56. </div>
  57. </transition>
  58. </div>
  59. </template>
  60. <script>
  61. import LoadingIcon from './LoadingIcon'
  62. import LoadingButton from './LoadingButton'
  63. import AjaxErrorHandler from '../assets/js/errorHandler'
  64. export default {
  65. name: 'ThreadPoll',
  66. props: ['id'],
  67. components: { LoadingIcon, LoadingButton },
  68. data () {
  69. return {
  70. poll: null,
  71. view: 'voting',
  72. selected: null,
  73. loading: false
  74. }
  75. },
  76. methods: {
  77. vote () {
  78. if(!this.$store.state.username) {
  79. this.$store.commit('setAccountTabs', 0)
  80. this.$store.commit('setAccountModalState', true)
  81. return
  82. }
  83. this.loading = true
  84. this.axios
  85. .post('/api/v1/poll/' + this.id, { answer: this.selected.answer })
  86. .then(res => {
  87. this.poll = res.data
  88. this.loading = false
  89. this.hasVoted = true
  90. this.view = 'results'
  91. })
  92. .catch(e => {
  93. this.loading = false
  94. AjaxErrorHandler(this.$store)(e)
  95. })
  96. }
  97. },
  98. mounted () {
  99. this.axios
  100. .get('/api/v1/poll/' + this.id)
  101. .then(res => this.poll = res.data)
  102. .catch(AjaxErrorHandler(this.$store))
  103. }
  104. }
  105. </script>
  106. <style lang='scss' scoped>
  107. @import '../assets/scss/variables.scss';
  108. .poll {
  109. padding: 1rem;
  110. width: 80%;
  111. background-color: #fff;
  112. position: relative;
  113. margin-bottom: 2rem;
  114. border-radius: 0.25rem;
  115. border: thin solid $color__gray--darker;
  116. @at-root #{&}__loading {
  117. @include loading-overlay();
  118. opacity: 1;
  119. }
  120. @at-root #{&}__question {
  121. font-weight: bold;
  122. font-size: 1.125rem;
  123. word-break: break-all;
  124. }
  125. @at-root #{&}__answers, #{&}__results {
  126. margin: 1rem 0;
  127. }
  128. @at-root #{&}__answer {
  129. padding: 0.5rem 0.625rem;
  130. margin: 0.5rem 0;
  131. border: thin solid $color__gray--primary;
  132. border-radius: 0.125rem;
  133. cursor: pointer;
  134. transition: all 0.2s;
  135. position: relative;
  136. overflow: hidden;
  137. text-overflow: ellipsis;
  138. &::after {
  139. content: '';
  140. position: absolute;
  141. left: 0;
  142. top: 0;
  143. height: 100%;
  144. background-color: $color__blue--primary;
  145. width: 0rem;
  146. opacity: 0;
  147. border-radius: 0.125rem 0 0 0.125rem;
  148. transition: all 0.2s;
  149. }
  150. &:hover {
  151. box-shadow: 0 0.1rem 0.25rem 0 rgba(214, 214, 214, 0.5);
  152. }
  153. @at-root #{&}--selected {
  154. font-weight: bold;
  155. &::after {
  156. opacity: 1;
  157. width: 0.3rem;
  158. }
  159. &:hover {
  160. box-shadow: none;
  161. }
  162. }
  163. }
  164. @at-root #{&}__buttons {
  165. > * {
  166. margin-right: 0.5rem;
  167. }
  168. }
  169. @at-root #{&}__total_votes {
  170. color: $color__text--secondary;
  171. font-style: italic;
  172. }
  173. @at-root #{&}__result {
  174. margin: 0.5rem 0;
  175. word-break: break-all;
  176. @at-root #{&}__info {
  177. color: $color__text--secondary;
  178. }
  179. @at-root #{&}__bar_outer {
  180. width: 100%;
  181. border-radius: 1rem;
  182. height: 1rem;
  183. margin-top: 0.25rem;
  184. border: thin solid $color__blue--primary;
  185. }
  186. @at-root #{&}__bar {
  187. background-color: lighten($color__blue--primary, 15%);
  188. height: 1rem;
  189. border-radius: 1rem;
  190. position: relative;
  191. top: -1rem;
  192. margin-bottom: -1rem;
  193. }
  194. }
  195. }
  196. @include thread_mobile_breakpoint ('.poll');
  197. </style>