tiny-rdm/frontend/src/components/dialogs/AddFieldsDialog.vue

220 lines
7.1 KiB
Vue
Raw Normal View History

2023-06-27 15:53:29 +08:00
<script setup>
import { computed, reactive, watch } from 'vue'
2023-08-02 17:57:39 +08:00
import { types } from '@/consts/support_redis_type.js'
import useDialog from 'stores/dialog'
import NewStringValue from '@/components/new_value/NewStringValue.vue'
import NewSetValue from '@/components/new_value/NewSetValue.vue'
2023-06-27 15:53:29 +08:00
import { useI18n } from 'vue-i18n'
2023-08-02 17:57:39 +08:00
import AddListValue from '@/components/new_value/AddListValue.vue'
import AddHashValue from '@/components/new_value/AddHashValue.vue'
import AddZSetValue from '@/components/new_value/AddZSetValue.vue'
import useConnectionStore from 'stores/connections.js'
import NewStreamValue from '@/components/new_value/NewStreamValue.vue'
2023-07-25 19:12:50 +08:00
import { size, slice } from 'lodash'
2023-06-27 15:53:29 +08:00
const i18n = useI18n()
const newForm = reactive({
server: '',
db: 0,
key: '',
type: '',
opType: 0,
value: null,
reload: true,
})
const addValueComponent = {
[types.STRING]: NewStringValue,
[types.HASH]: AddHashValue,
[types.LIST]: AddListValue,
[types.SET]: NewSetValue,
[types.ZSET]: AddZSetValue,
2023-07-25 19:12:50 +08:00
[types.STREAM]: NewStreamValue,
2023-06-27 15:53:29 +08:00
}
const defaultValue = {
[types.STRING]: '',
[types.HASH]: [],
[types.LIST]: [],
[types.SET]: [],
[types.ZSET]: [],
2023-07-25 19:12:50 +08:00
[types.STREAM]: ['*'],
2023-06-27 15:53:29 +08:00
}
/**
* dialog title
* @type {ComputedRef<string>}
*/
const title = computed(() => {
switch (newForm.type) {
case types.LIST:
return i18n.t('new_item')
case types.HASH:
return i18n.t('new_field')
case types.SET:
return i18n.t('new_field')
case types.ZSET:
return i18n.t('new_field')
2023-07-25 19:12:50 +08:00
case types.STREAM:
return i18n.t('new_field')
2023-06-27 15:53:29 +08:00
}
return ''
})
const dialogStore = useDialog()
watch(
() => dialogStore.addFieldsDialogVisible,
(visible) => {
if (visible) {
const { server, db, key, type } = dialogStore.addFieldParam
newForm.server = server
newForm.db = db
newForm.key = key
newForm.type = type
newForm.opType = 0
newForm.value = null
}
2023-07-25 19:12:50 +08:00
},
2023-06-27 15:53:29 +08:00
)
const connectionStore = useConnectionStore()
const onAdd = async () => {
try {
const { server, db, key, type } = newForm
let { value } = newForm
if (value == null) {
value = defaultValue[type]
}
switch (type) {
case types.LIST:
{
let data
if (newForm.opType === 1) {
data = await connectionStore.prependListItem(server, db, key, value)
} else {
data = await connectionStore.appendListItem(server, db, key, value)
}
const { success, msg } = data
if (success) {
if (newForm.reload) {
connectionStore.loadKeyValue(server, db, key).then(() => {})
}
$message.success(i18n.t('handle_succ'))
2023-06-27 15:53:29 +08:00
} else {
$message.error(msg)
2023-06-27 15:53:29 +08:00
}
}
break
case types.HASH:
{
const { success, msg } = await connectionStore.addHashField(server, db, key, newForm.opType, value)
if (success) {
if (newForm.reload) {
connectionStore.loadKeyValue(server, db, key).then(() => {})
}
$message.success(i18n.t('handle_succ'))
2023-06-27 15:53:29 +08:00
} else {
$message.error(msg)
2023-06-27 15:53:29 +08:00
}
}
break
case types.SET:
{
const { success, msg } = await connectionStore.addSetItem(server, db, key, value)
if (success) {
if (newForm.reload) {
connectionStore.loadKeyValue(server, db, key).then(() => {})
}
$message.success(i18n.t('handle_succ'))
2023-06-27 15:53:29 +08:00
} else {
$message.error(msg)
2023-06-27 15:53:29 +08:00
}
}
break
case types.ZSET:
{
const { success, msg } = await connectionStore.addZSetItem(server, db, key, newForm.opType, value)
if (success) {
if (newForm.reload) {
connectionStore.loadKeyValue(server, db, key).then(() => {})
}
$message.success(i18n.t('handle_succ'))
2023-06-27 15:53:29 +08:00
} else {
$message.error(msg)
2023-06-27 15:53:29 +08:00
}
}
break
2023-07-25 19:12:50 +08:00
case types.STREAM:
{
if (size(value) > 2) {
const { success, msg } = await connectionStore.addStreamValue(
server,
db,
key,
value[0],
slice(value, 1),
)
if (success) {
if (newForm.reload) {
connectionStore.loadKeyValue(server, db, key).then(() => {})
}
$message.success(i18n.t('handle_succ'))
2023-07-25 19:12:50 +08:00
} else {
$message.error(msg)
2023-07-25 19:12:50 +08:00
}
}
}
break
2023-06-27 15:53:29 +08:00
}
dialogStore.closeAddFieldsDialog()
} catch (e) {
$message.error(e.message)
2023-06-27 15:53:29 +08:00
}
}
const onClose = () => {
dialogStore.closeAddFieldsDialog()
}
</script>
<template>
<n-modal
v-model:show="dialogStore.addFieldsDialogVisible"
:closable="false"
:close-on-esc="false"
:mask-closable="false"
:negative-button-props="{ size: 'medium' }"
:negative-text="$t('cancel')"
:positive-button-props="{ size: 'medium' }"
:positive-text="$t('confirm')"
:show-icon="false"
:title="title"
preset="dialog"
style="width: 600px"
transform-origin="center"
@positive-click="onAdd"
2023-08-24 15:23:25 +08:00
@negative-click="onClose">
2023-06-27 15:53:29 +08:00
<n-scrollbar style="max-height: 500px">
<n-form :model="newForm" :show-require-mark="false" label-placement="top" style="padding-right: 15px">
2023-06-27 15:53:29 +08:00
<n-form-item :label="$t('key')" path="key" required>
<n-input v-model:value="newForm.key" placeholder="" readonly />
</n-form-item>
<component
:is="addValueComponent[newForm.type]"
v-model:type="newForm.opType"
2023-08-24 15:23:25 +08:00
v-model:value="newForm.value" />
2023-06-27 15:53:29 +08:00
<n-form-item label=" " path="key" required>
<n-checkbox v-model:checked="newForm.reload">
{{ $t('reload_when_succ') }}
</n-checkbox>
</n-form-item>
</n-form>
</n-scrollbar>
</n-modal>
</template>
<style lang="scss" scoped></style>