Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

refactor: logic of saving in the post settings modal #476

Merged
merged 1 commit into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 101 additions & 35 deletions src/components/Post/PostSettingModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -122,27 +122,37 @@
</a-tab-pane>
</a-tabs>
</div>
<template slot="footer">
<template #footer>
<slot name="extraFooter" />
<ReactiveButton
v-if="!form.model.id"
v-if="draftSaveVisible"
:errored="form.draftSaveErrored"
:loading="form.draftSaving"
:text="`${hasId ? '转为' : '保存'}草稿`"
erroredText="保存失败"
loadedText="保存成功"
text="保存草稿"
type="danger"
@callback="handleSavedCallback"
@click="handleCreateOrUpdate('DRAFT')"
@callback="handleSavedCallback()"
@click="handleSaveDraft()"
></ReactiveButton>
<ReactiveButton
v-if="publishVisible"
:errored="form.publishErrored"
:loading="form.publishing"
erroredText="发布失败"
loadedText="发布成功"
text="转为发布"
@callback="handleSavedCallback()"
@click="handlePublish()"
></ReactiveButton>
<ReactiveButton
:errored="form.saveErrored"
:erroredText="`${form.model.id ? '保存' : '发布'}失败`"
:loadedText="`${form.model.id ? '保存' : '发布'}成功`"
:erroredText="`${hasId ? '保存' : '发布'}失败`"
:loadedText="`${hasId ? '保存' : '发布'}成功`"
:loading="form.saving"
:text="`${form.model.id ? '保存' : '发布'}`"
@callback="handleSavedCallback"
@click="handleCreateOrUpdate()"
:text="`${hasId ? '保存' : '发布'}`"
@callback="handleSavedCallback()"
@click="handleSave()"
></ReactiveButton>
<a-button :disabled="loading" @click="modalVisible = false">关闭</a-button>
</template>
Expand All @@ -164,6 +174,7 @@ import { mixin, mixinDevice } from '@/mixins/mixin.js'
import { datetimeFormat } from '@/utils/datetime'
import pinyin from 'tiny-pinyin'
import { mapGetters } from 'vuex'
import { postStatuses } from '@/core/constant'

// apis
import apiClient from '@/utils/api-client'
Expand Down Expand Up @@ -196,12 +207,15 @@ export default {
},
data() {
return {
postStatuses,
form: {
model: {},
saving: false,
saveErrored: false,
draftSaving: false,
draftSaveErrored: false
draftSaveErrored: false,
publishing: false,
publishErrored: false
},

templates: [],
Expand Down Expand Up @@ -238,29 +252,36 @@ export default {
}
},
fullPath() {
const permalinkType = this.options.post_permalink_type
const blogUrl = this.options.blog_url
const archivesPrefix = this.options.archives_prefix
const pathSuffix = this.options.path_suffix || ''
const slug = this.form.model.slug || '{slug}'
const createTime = this.form.model.createTime || new Date()
const id = this.form.model.id || '{id}'
switch (permalinkType) {
const { post_permalink_type, archives_prefix, blog_url, path_suffix: path_suffix = '' } = this.options
const { slug: slug = '{slug}', createTime: createTime = new Date(), id: id = '{id}' } = this.form.model

switch (post_permalink_type) {
case 'DEFAULT':
return `${blogUrl}/${archivesPrefix}/${slug}${pathSuffix}`
return `${blog_url}/${archives_prefix}/${slug}${path_suffix}`
case 'YEAR':
return `${blogUrl}${datetimeFormat(createTime, '/YYYY/')}${slug}${pathSuffix}`
return `${blog_url}${datetimeFormat(createTime, '/YYYY/')}${slug}${path_suffix}`
case 'DATE':
return `${blogUrl}${datetimeFormat(createTime, '/YYYY/MM/')}${slug}${pathSuffix}`
return `${blog_url}${datetimeFormat(createTime, '/YYYY/MM/')}${slug}${path_suffix}`
case 'DAY':
return `${blogUrl}${datetimeFormat(createTime, '/YYYY/MM/DD/')}${slug}${pathSuffix}`
return `${blog_url}${datetimeFormat(createTime, '/YYYY/MM/DD/')}${slug}${path_suffix}`
case 'ID':
return `${blogUrl}/?p=${id}`
return `${blog_url}/?p=${id}`
case 'ID_SLUG':
return `${blogUrl}/${archivesPrefix}/${id}${pathSuffix}`
return `${blog_url}/${archives_prefix}/${id}${path_suffix}`
default:
return ''
}
},
hasId() {
return !!this.form.model.id
},
draftSaveVisible() {
const { draftSaving, publishing } = this.form
return (this.form.model.status !== postStatuses.DRAFT.value || !this.hasId || draftSaving) && !publishing
},
publishVisible() {
const { draftSaving, publishing } = this.form
return ((this.form.model.status === postStatuses.DRAFT.value && this.hasId) || publishing) && !draftSaving
}
},
watch: {
Expand All @@ -287,32 +308,76 @@ export default {
/**
* Creates or updates a post
*/
async handleCreateOrUpdate(preStatus = 'PUBLISHED') {
async handleCreateOrUpdate() {
if (!this.form.model.title) {
this.$notification['error']({
message: '提示',
description: '文章标题不能为空!'
})
return
throw new Error('文章标题不能为空!')
}

this.form.model.status = preStatus
this.form.model.keepRaw = true
const { id, status } = this.form.model
try {
this.form[status === 'PUBLISHED' ? 'saving' : 'draftSaving'] = true

if (id) {
await apiClient.post.update(id, this.form.model)
if (this.hasId) {
await apiClient.post.update(this.form.model.id, this.form.model)
} else {
await apiClient.post.create(this.form.model)
}
} catch (error) {
this.form[status === 'PUBLISHED' ? 'saveErrored' : 'draftSaveErrored'] = true
this.$log.error(error)
throw new Error(error)
}
},

async handleSave() {
try {
this.form.saving = true

const { status } = this.form.model

if (!status) {
this.form.model.status = this.postStatuses.PUBLISHED.value
}

await this.handleCreateOrUpdate()
} catch (e) {
this.form.saveErrored = true
this.$log.error('Failed to save post', e)
} finally {
setTimeout(() => {
this.form.saving = false
}, 400)
}
},

async handlePublish() {
try {
this.form.publishing = true
this.form.model.status = this.postStatuses.PUBLISHED.value

await this.handleCreateOrUpdate()
} catch (e) {
this.form.publishErrored = true
this.$log.error('Failed to publish post', e)
} finally {
setTimeout(() => {
this.form.publishing = false
}, 400)
}
},

async handleSaveDraft() {
try {
this.form.draftSaving = true
this.form.model.status = this.postStatuses.DRAFT.value

await this.handleCreateOrUpdate()
} catch (e) {
this.form.draftSaveErrored = true
this.$log.error('Failed to save draft post', e)
} finally {
setTimeout(() => {
this.form.draftSaving = false
}, 400)
}
Expand All @@ -322,9 +387,10 @@ export default {
* Handle saved callback event
*/
handleSavedCallback() {
if (this.form.saveErrored || this.form.draftSaveErrored) {
if (this.form.saveErrored || this.form.draftSaveErrored || this.form.publishErrored) {
this.form.saveErrored = false
this.form.draftSaveErrored = false
this.form.publishErrored = false
} else {
this.savedCallback && this.savedCallback()
}
Expand Down
9 changes: 6 additions & 3 deletions src/core/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,20 @@ export const sheetStatuses = {
PUBLISHED: {
color: 'green',
status: 'success',
text: '已发布'
text: '已发布',
value: 'PUBLISHED'
},
DRAFT: {
color: 'yellow',
status: 'warning',
text: '草稿'
text: '草稿',
value: 'DRAFT'
},
RECYCLE: {
color: 'red',
status: 'error',
text: '回收站'
text: '回收站',
value: 'RECYCLE'
}
}

Expand Down
Loading