admin-comments.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template lang='pug'>
  2. v-container(fluid, grid-list-lg)
  3. v-layout(row, wrap)
  4. v-flex(xs12)
  5. .admin-header
  6. img.animated.fadeInUp(src='/_assets/svg/icon-chat-bubble.svg', alt='Comments', style='width: 80px;')
  7. .admin-header-title
  8. .headline.primary--text.animated.fadeInLeft {{$t('admin:comments.title')}}
  9. .subtitle-1.grey--text.animated.fadeInLeft.wait-p2s {{$t('admin:comments.subtitle')}}
  10. v-spacer
  11. v-btn.mx-1.animated.fadeInDown.wait-p2s(outlined, color='grey', @click='refresh', large)
  12. v-icon mdi-refresh
  13. v-btn.ml-1.animated.fadeInDown(color='success', @click='save', depressed, large)
  14. v-icon(left) mdi-check
  15. span {{$t('common:actions.apply')}}
  16. v-flex(lg3, xs12)
  17. v-card.animated.fadeInUp
  18. v-toolbar(flat, color='primary', dark, dense)
  19. .subtitle-1 {{$t('admin:comments.provider')}}
  20. v-list.py-0(two-line, dense)
  21. template(v-for='(provider, idx) in providers')
  22. v-list-item(:key='provider.key', @click='selectedProvider = provider.key', :disabled='!provider.isAvailable')
  23. v-list-item-avatar(size='24')
  24. v-icon(color='grey', v-if='!provider.isAvailable') mdi-minus-box-outline
  25. v-icon(color='primary', v-else-if='provider.key === selectedProvider') mdi-checkbox-marked-circle-outline
  26. v-icon(color='grey', v-else) mdi-checkbox-blank-circle-outline
  27. v-list-item-content
  28. v-list-item-title.body-2(:class='!provider.isAvailable ? `grey--text` : (selectedProvider === provider.key ? `primary--text` : ``)') {{ provider.title }}
  29. v-list-item-subtitle: .caption(:class='!provider.isAvailable ? `grey--text text--lighten-1` : (selectedProvider === provider.key ? `blue--text ` : ``)') {{ provider.description }}
  30. v-list-item-avatar(v-if='selectedProvider === provider.key', size='24')
  31. v-icon.animated.fadeInLeft(color='primary', large) mdi-chevron-right
  32. v-divider(v-if='idx < providers.length - 1')
  33. v-flex(lg9, xs12)
  34. v-card.animated.fadeInUp.wait-p2s
  35. v-toolbar(color='primary', dense, flat, dark)
  36. .subtitle-1 {{provider.title}}
  37. v-card-text
  38. .providerlogo
  39. img(:src='provider.logo', :alt='provider.title')
  40. .caption.pt-3 {{provider.description}}
  41. .caption.pb-3: a(:href='provider.website') {{provider.website}}
  42. v-divider.mt-3
  43. .overline.my-5 {{$t('admin:comments.providerConfig')}}
  44. .body-2.ml-3(v-if='!provider.config || provider.config.length < 1'): em {{$t('admin:comments.providerNoConfig')}}
  45. template(v-else, v-for='cfg in provider.config')
  46. v-select.mb-3(
  47. v-if='cfg.value.type === "string" && cfg.value.enum'
  48. outlined
  49. :items='cfg.value.enum'
  50. :key='cfg.key'
  51. :label='cfg.value.title'
  52. v-model='cfg.value.value'
  53. prepend-icon='mdi-cog-box'
  54. :hint='cfg.value.hint ? cfg.value.hint : ""'
  55. persistent-hint
  56. :class='cfg.value.hint ? "mb-2" : ""'
  57. :style='cfg.value.maxWidth > 0 ? `max-width:` + cfg.value.maxWidth + `px;` : ``'
  58. )
  59. v-switch.mb-6(
  60. v-else-if='cfg.value.type === "boolean"'
  61. :key='cfg.key'
  62. :label='cfg.value.title'
  63. v-model='cfg.value.value'
  64. color='primary'
  65. prepend-icon='mdi-cog-box'
  66. :hint='cfg.value.hint ? cfg.value.hint : ""'
  67. persistent-hint
  68. inset
  69. )
  70. v-textarea.mb-3(
  71. v-else-if='cfg.value.type === "string" && cfg.value.multiline'
  72. outlined
  73. :key='cfg.key'
  74. :label='cfg.value.title'
  75. v-model='cfg.value.value'
  76. prepend-icon='mdi-cog-box'
  77. :hint='cfg.value.hint ? cfg.value.hint : ""'
  78. persistent-hint
  79. :class='cfg.value.hint ? "mb-2" : ""'
  80. )
  81. v-text-field.mb-3(
  82. v-else
  83. outlined
  84. :key='cfg.key'
  85. :label='cfg.value.title'
  86. v-model='cfg.value.value'
  87. prepend-icon='mdi-cog-box'
  88. :hint='cfg.value.hint ? cfg.value.hint : ""'
  89. persistent-hint
  90. :class='cfg.value.hint ? "mb-2" : ""'
  91. :style='cfg.value.maxWidth > 0 ? `max-width:` + cfg.value.maxWidth + `px;` : ``'
  92. )
  93. </template>
  94. <script>
  95. import _ from 'lodash'
  96. import gql from 'graphql-tag'
  97. export default {
  98. data() {
  99. return {
  100. providers: [],
  101. selectedProvider: '',
  102. provider: {}
  103. }
  104. },
  105. watch: {
  106. selectedProvider(newValue, oldValue) {
  107. this.provider = _.find(this.providers, ['key', newValue]) || {}
  108. },
  109. providers(newValue, oldValue) {
  110. this.selectedProvider = _.get(_.find(this.providers, 'isEnabled'), 'key', 'db')
  111. }
  112. },
  113. methods: {
  114. async refresh() {
  115. await this.$apollo.queries.providers.refetch()
  116. this.$store.commit('showNotification', {
  117. message: this.$t('admin:comments.listRefreshSuccess'),
  118. style: 'success',
  119. icon: 'cached'
  120. })
  121. },
  122. async save() {
  123. this.$store.commit(`loadingStart`, 'admin-comments-saveproviders')
  124. try {
  125. const resp = await this.$apollo.mutate({
  126. mutation: gql`
  127. mutation($providers: [CommentProviderInput]!) {
  128. comments {
  129. updateProviders(providers: $providers) {
  130. responseResult {
  131. succeeded
  132. errorCode
  133. slug
  134. message
  135. }
  136. }
  137. }
  138. }
  139. `,
  140. variables: {
  141. providers: this.providers.map(tgt => ({
  142. isEnabled: tgt.key === this.selectedProvider,
  143. key: tgt.key,
  144. config: tgt.config.map(cfg => ({...cfg, value: JSON.stringify({ v: cfg.value.value })}))
  145. }))
  146. }
  147. })
  148. if (_.get(resp, 'data.comments.updateProviders.responseResult.succeeded', false)) {
  149. this.$store.commit('showNotification', {
  150. message: this.$t('admin:comments.configSaveSuccess'),
  151. style: 'success',
  152. icon: 'check'
  153. })
  154. } else {
  155. throw new Error(_.get(resp, 'data.comments.updateProviders.responseResult.message', this.$t('common:error.unexpected')))
  156. }
  157. } catch (err) {
  158. this.$store.commit('pushGraphError', err)
  159. }
  160. this.$store.commit(`loadingStop`, 'admin-comments-saveproviders')
  161. }
  162. },
  163. apollo: {
  164. providers: {
  165. query: gql`
  166. query {
  167. comments {
  168. providers {
  169. isEnabled
  170. key
  171. title
  172. description
  173. logo
  174. website
  175. isAvailable
  176. config {
  177. key
  178. value
  179. }
  180. }
  181. }
  182. }
  183. `,
  184. fetchPolicy: 'network-only',
  185. update: (data) => _.cloneDeep(data.comments.providers).map(str => ({
  186. ...str,
  187. config: _.sortBy(str.config.map(cfg => ({
  188. ...cfg,
  189. value: JSON.parse(cfg.value)
  190. })), [t => t.value.order])
  191. })),
  192. watchLoading (isLoading) {
  193. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-comments-refresh')
  194. }
  195. }
  196. }
  197. }
  198. </script>
  199. <style lang='scss' scoped>
  200. .providerlogo {
  201. width: 250px;
  202. height: 85px;
  203. float:right;
  204. display: flex;
  205. justify-content: flex-end;
  206. align-items: center;
  207. img {
  208. max-width: 100%;
  209. max-height: 50px;
  210. }
  211. }
  212. </style>