Fetch local pack's files only after opening collapse item to manage emojis

This commit is contained in:
Angelina Filippova 2020-06-19 02:09:10 +03:00
parent d8299972b5
commit c2fc99bce0
4 changed files with 41 additions and 20 deletions

View file

@ -475,6 +475,7 @@ export default {
specifyShortcode: 'Specify a custom shortcode', specifyShortcode: 'Specify a custom shortcode',
specifyFilename: 'Specify a custom filename', specifyFilename: 'Specify a custom filename',
copy: 'Copy', copy: 'Copy',
copyToLocalPack: 'Copy to local pack' copyToLocalPack: 'Copy to local pack',
emptyPack: 'This emoji pack is empty'
} }
} }

View file

@ -4,6 +4,7 @@ import {
deleteEmojiFile, deleteEmojiFile,
deletePack, deletePack,
downloadFrom, downloadFrom,
fetchPack,
importFromFS, importFromFS,
listPacks, listPacks,
listRemotePacks, listRemotePacks,
@ -30,6 +31,9 @@ const packs = {
SET_LOCAL_PACKS: (state, packs) => { SET_LOCAL_PACKS: (state, packs) => {
state.localPacks = packs state.localPacks = packs
}, },
SET_PACK_FILES: (state, { name, files }) => {
state.localPacks = { ...state.localPacks, [name]: { ...state.localPacks[name], files }}
},
SET_REMOTE_INSTANCE: (state, name) => { SET_REMOTE_INSTANCE: (state, name) => {
state.remoteInstance = name state.remoteInstance = name
}, },
@ -99,6 +103,10 @@ const packs = {
}) })
} }
}, },
async FetchPack({ getters, commit }, name) {
const { data } = await fetchPack(name, getters.authHost, getters.token)
commit('SET_PACK_FILES', { name, files: data.files })
},
async ImportFromFS({ getters }) { async ImportFromFS({ getters }) {
const result = await importFromFS(getters.authHost, getters.token) const result = await importFromFS(getters.authHost, getters.token)
@ -136,12 +144,17 @@ const packs = {
commit('UPDATE_LOCAL_PACK_PACK', { name: packName, pack: result.data }) commit('UPDATE_LOCAL_PACK_PACK', { name: packName, pack: result.data })
} }
}, },
SetActiveCollapseItems({ commit, state }, activeItems) { SetActiveCollapseItems({ commit }, activeItems) {
commit('SET_ACTIVE_COLLAPSE_ITEMS', activeItems) commit('SET_ACTIVE_COLLAPSE_ITEMS', activeItems)
}, },
async SetLocalEmojiPacks({ commit, getters }) { async SetLocalEmojiPacks({ commit, getters }) {
const { data } = await listPacks(getters.authHost) const { data } = await listPacks(getters.authHost)
commit('SET_LOCAL_PACKS', data) const packs = Object.keys(data).reduce((acc, packName) => {
const { files, ...pack } = data[packName]
acc[packName] = pack
return acc
}, {})
commit('SET_LOCAL_PACKS', packs)
}, },
async SetRemoteEmojiPacks({ commit, getters }, { remoteInstance }) { async SetRemoteEmojiPacks({ commit, getters }, { remoteInstance }) {
const { data } = await listRemotePacks(getters.authHost, getters.token, remoteInstance) const { data } = await listRemotePacks(getters.authHost, getters.token, remoteInstance)

View file

@ -38,19 +38,22 @@
</el-link> </el-link>
</div> </div>
</div> </div>
<el-collapse v-model="showPackContent" class="contents-collapse"> <el-collapse v-model="showPackContent" class="contents-collapse" @change="handleChange($event, name)">
<el-collapse-item v-if="isLocal" :title=" $t('emoji.addNewEmoji')" name="addEmoji" class="no-background"> <el-collapse-item v-if="isLocal" :title=" $t('emoji.addNewEmoji')" name="addEmoji" class="no-background">
<new-emoji-uploader :pack-name="name"/> <new-emoji-uploader :pack-name="name"/>
</el-collapse-item> </el-collapse-item>
<el-collapse-item v-if="pack.files.length > 0" :title=" $t('emoji.manageEmoji')" name="manageEmoji" class="no-background"> <el-collapse-item :title=" $t('emoji.manageEmoji')" name="manageEmoji" class="no-background">
<single-emoji-editor <div v-if="pack.files && sortedFiles.length > 0">
v-for="[shortcode, file] in pack.files" <single-emoji-editor
:key="shortcode" v-for="[shortcode, file] in sortedFiles"
:host="host" :key="shortcode"
:pack-name="name" :host="host"
:shortcode="shortcode" :pack-name="name"
:file="file" :shortcode="shortcode"
:is-local="isLocal" /> :file="file"
:is-local="isLocal" />
</div>
<span v-else class="expl">{{ $t('emoji.emptyPack') }}</span>
</el-collapse-item> </el-collapse-item>
</el-collapse> </el-collapse>
</el-collapse-item> </el-collapse-item>
@ -101,6 +104,10 @@ export default {
return '155px' return '155px'
} }
}, },
sortedFiles() {
return Object.keys(this.pack.files).sort((a, b) => a.localeCompare(b))
.map(key => [key, this.pack.files[key]])
},
share: { share: {
get() { return this.pack.pack['share-files'] }, get() { return this.pack.pack['share-files'] },
set(value) { set(value) {
@ -170,6 +177,11 @@ export default {
.then(() => this.$store.dispatch('SetLocalEmojiPacks')) .then(() => this.$store.dispatch('SetLocalEmojiPacks'))
}).catch(() => {}) }).catch(() => {})
}, },
handleChange(openTabs, name) {
if (openTabs.includes('manageEmoji')) {
this.$store.dispatch('FetchPack', name)
}
},
savePackMetadata() { savePackMetadata() {
this.$store.dispatch('SavePackMetadata', { packName: this.name }) this.$store.dispatch('SavePackMetadata', { packName: this.name })
} }

View file

@ -31,8 +31,8 @@
</div> </div>
</el-form-item> </el-form-item>
<el-form-item v-if="Object.keys(localPacks).length > 0" :label="$t('emoji.packs')"> <el-form-item v-if="Object.keys(localPacks).length > 0" :label="$t('emoji.packs')">
<el-collapse v-for="(pack, name) in localPacks" :key="name" v-model="activeLocalPack"> <el-collapse v-for="(pack, name) in localPacks" :key="name" v-model="activeLocalPack" @change="setActiveCollapseItems">
<local-emoji-pack :name="name" :pack="sortPack(pack)" :host="$store.getters.authHost" :is-local="true" /> <local-emoji-pack :name="name" :pack="pack" :host="$store.getters.authHost" :is-local="true" />
</el-collapse> </el-collapse>
</el-form-item> </el-form-item>
<el-divider class="divider"/> <el-divider class="divider"/>
@ -128,11 +128,6 @@ export default {
this.$store.dispatch('ReloadEmoji') this.$store.dispatch('ReloadEmoji')
}) })
}, },
sortPack(pack) {
const orderedFiles = Object.keys(pack.files).sort((a, b) => a.localeCompare(b))
.map(key => [key, pack.files[key]])
return { ...pack, files: orderedFiles }
},
refreshLocalPacks() { refreshLocalPacks() {
try { try {
this.$store.dispatch('SetLocalEmojiPacks') this.$store.dispatch('SetLocalEmojiPacks')