admin-comments.vue 7.8 KB
Newer Older
NGPixel's avatar
NGPixel committed
1 2 3 4 5
<template lang='pug'>
  v-container(fluid, grid-list-lg)
    v-layout(row, wrap)
      v-flex(xs12)
        .admin-header
6
          img.animated.fadeInUp(src='/_assets/svg/icon-chat-bubble.svg', alt='Comments', style='width: 80px;')
NGPixel's avatar
NGPixel committed
7 8 9 10
          .admin-header-title
            .headline.primary--text.animated.fadeInLeft {{$t('admin:comments.title')}}
            .subtitle-1.grey--text.animated.fadeInLeft.wait-p2s {{$t('admin:comments.subtitle')}}
          v-spacer
NGPixel's avatar
NGPixel committed
11 12 13
          v-btn.animated.fadeInDown.wait-p3s(icon, outlined, color='grey', href='https://docs.requarks.io/comments', target='_blank')
            v-icon mdi-help-circle
          v-btn.mx-3.animated.fadeInDown.wait-p2s(icon, outlined, color='grey', @click='refresh')
NGPixel's avatar
NGPixel committed
14
            v-icon mdi-refresh
NGPixel's avatar
NGPixel committed
15
          v-btn.animated.fadeInDown(color='success', @click='save', depressed, large)
NGPixel's avatar
NGPixel committed
16 17 18 19 20 21
            v-icon(left) mdi-check
            span {{$t('common:actions.apply')}}

      v-flex(lg3, xs12)
        v-card.animated.fadeInUp
          v-toolbar(flat, color='primary', dark, dense)
22
            .subtitle-1 {{$t('admin:comments.provider')}}
NGPixel's avatar
NGPixel committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
          v-list.py-0(two-line, dense)
            template(v-for='(provider, idx) in providers')
              v-list-item(:key='provider.key', @click='selectedProvider = provider.key', :disabled='!provider.isAvailable')
                v-list-item-avatar(size='24')
                  v-icon(color='grey', v-if='!provider.isAvailable') mdi-minus-box-outline
                  v-icon(color='primary', v-else-if='provider.key === selectedProvider') mdi-checkbox-marked-circle-outline
                  v-icon(color='grey', v-else) mdi-checkbox-blank-circle-outline
                v-list-item-content
                  v-list-item-title.body-2(:class='!provider.isAvailable ? `grey--text` : (selectedProvider === provider.key ? `primary--text` : ``)') {{ provider.title }}
                  v-list-item-subtitle: .caption(:class='!provider.isAvailable ? `grey--text text--lighten-1` : (selectedProvider === provider.key ? `blue--text ` : ``)') {{ provider.description }}
                v-list-item-avatar(v-if='selectedProvider === provider.key', size='24')
                  v-icon.animated.fadeInLeft(color='primary', large) mdi-chevron-right
              v-divider(v-if='idx < providers.length - 1')

      v-flex(lg9, xs12)
        v-card.animated.fadeInUp.wait-p2s
          v-toolbar(color='primary', dense, flat, dark)
            .subtitle-1 {{provider.title}}
41 42 43 44 45 46
          v-card-info(color='blue')
            div
              div {{provider.description}}
              span.caption: a(:href='provider.website') {{provider.website}}
            v-spacer
            .admin-providerlogo
NGPixel's avatar
NGPixel committed
47
              img(:src='provider.logo', :alt='provider.title')
48
          v-card-text
NGPixel's avatar
NGPixel committed
49 50 51
            .overline.my-5 {{$t('admin:comments.providerConfig')}}
            .body-2.ml-3(v-if='!provider.config || provider.config.length < 1'): em {{$t('admin:comments.providerNoConfig')}}
            template(v-else, v-for='cfg in provider.config')
52
              v-select.mb-3(
NGPixel's avatar
NGPixel committed
53 54 55 56 57 58 59 60 61 62
                v-if='cfg.value.type === "string" && cfg.value.enum'
                outlined
                :items='cfg.value.enum'
                :key='cfg.key'
                :label='cfg.value.title'
                v-model='cfg.value.value'
                prepend-icon='mdi-cog-box'
                :hint='cfg.value.hint ? cfg.value.hint : ""'
                persistent-hint
                :class='cfg.value.hint ? "mb-2" : ""'
63
                :style='cfg.value.maxWidth > 0 ? `max-width:` + cfg.value.maxWidth + `px;` : ``'
NGPixel's avatar
NGPixel committed
64
              )
65
              v-switch.mb-6(
NGPixel's avatar
NGPixel committed
66 67 68 69 70 71 72 73 74 75
                v-else-if='cfg.value.type === "boolean"'
                :key='cfg.key'
                :label='cfg.value.title'
                v-model='cfg.value.value'
                color='primary'
                prepend-icon='mdi-cog-box'
                :hint='cfg.value.hint ? cfg.value.hint : ""'
                persistent-hint
                inset
                )
76
              v-textarea.mb-3(
NGPixel's avatar
NGPixel committed
77 78 79 80 81 82 83 84 85 86
                v-else-if='cfg.value.type === "string" && cfg.value.multiline'
                outlined
                :key='cfg.key'
                :label='cfg.value.title'
                v-model='cfg.value.value'
                prepend-icon='mdi-cog-box'
                :hint='cfg.value.hint ? cfg.value.hint : ""'
                persistent-hint
                :class='cfg.value.hint ? "mb-2" : ""'
                )
87
              v-text-field.mb-3(
NGPixel's avatar
NGPixel committed
88 89 90 91 92 93 94 95 96
                v-else
                outlined
                :key='cfg.key'
                :label='cfg.value.title'
                v-model='cfg.value.value'
                prepend-icon='mdi-cog-box'
                :hint='cfg.value.hint ? cfg.value.hint : ""'
                persistent-hint
                :class='cfg.value.hint ? "mb-2" : ""'
97
                :style='cfg.value.maxWidth > 0 ? `max-width:` + cfg.value.maxWidth + `px;` : ``'
NGPixel's avatar
NGPixel committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
                )
</template>

<script>
import _ from 'lodash'
import gql from 'graphql-tag'

export default {
  data() {
    return {
      providers: [],
      selectedProvider: '',
      provider: {}
    }
  },
  watch: {
    selectedProvider(newValue, oldValue) {
      this.provider = _.find(this.providers, ['key', newValue]) || {}
    },
    providers(newValue, oldValue) {
      this.selectedProvider = _.get(_.find(this.providers, 'isEnabled'), 'key', 'db')
    }
  },
  methods: {
    async refresh() {
      await this.$apollo.queries.providers.refetch()
      this.$store.commit('showNotification', {
        message: this.$t('admin:comments.listRefreshSuccess'),
        style: 'success',
        icon: 'cached'
      })
    },
    async save() {
      this.$store.commit(`loadingStart`, 'admin-comments-saveproviders')
      try {
        const resp = await this.$apollo.mutate({
134 135 136 137 138 139 140 141 142 143 144 145 146 147
          mutation: gql`
            mutation($providers: [CommentProviderInput]!) {
              comments {
                updateProviders(providers: $providers) {
                  responseResult {
                    succeeded
                    errorCode
                    slug
                    message
                  }
                }
              }
            }
          `,
NGPixel's avatar
NGPixel committed
148 149 150 151 152 153 154 155
          variables: {
            providers: this.providers.map(tgt => ({
              isEnabled: tgt.key === this.selectedProvider,
              key: tgt.key,
              config: tgt.config.map(cfg => ({...cfg, value: JSON.stringify({ v: cfg.value.value })}))
            }))
          }
        })
156
        if (_.get(resp, 'data.comments.updateProviders.responseResult.succeeded', false)) {
NGPixel's avatar
NGPixel committed
157 158 159 160 161 162
          this.$store.commit('showNotification', {
            message: this.$t('admin:comments.configSaveSuccess'),
            style: 'success',
            icon: 'check'
          })
        } else {
163
          throw new Error(_.get(resp, 'data.comments.updateProviders.responseResult.message', this.$t('common:error.unexpected')))
NGPixel's avatar
NGPixel committed
164 165 166 167
        }
      } catch (err) {
        this.$store.commit('pushGraphError', err)
      }
168
      this.$store.commit(`loadingStop`, 'admin-comments-saveproviders')
NGPixel's avatar
NGPixel committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    }
  },
  apollo: {
    providers: {
      query: gql`
        query {
          comments {
            providers {
              isEnabled
              key
              title
              description
              logo
              website
              isAvailable
              config {
                key
                value
              }
            }
          }
        }
      `,
      fetchPolicy: 'network-only',
      update: (data) => _.cloneDeep(data.comments.providers).map(str => ({
        ...str,
        config: _.sortBy(str.config.map(cfg => ({
          ...cfg,
          value: JSON.parse(cfg.value)
        })), [t => t.value.order])
      })),
      watchLoading (isLoading) {
        this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-comments-refresh')
      }
    }
  }
}
</script>