refactor: UserEditingModal component

Signed-off-by: Ryan Wang <i@ryanc.cc>
pull/591/head
Ryan Wang 2022-07-22 21:37:27 +08:00
parent 0c73b03faf
commit 52bb68e565
1 changed files with 32 additions and 27 deletions

View File

@ -15,6 +15,7 @@ import { v4 as uuid } from "uuid";
import { roleLabels } from "@/constants/labels"; import { roleLabels } from "@/constants/labels";
import { rbacAnnotations } from "@/constants/annotations"; import { rbacAnnotations } from "@/constants/annotations";
import YAML from "yaml"; import YAML from "yaml";
import cloneDeep from "lodash.clonedeep";
const props = defineProps({ const props = defineProps({
visible: { visible: {
@ -29,15 +30,14 @@ const props = defineProps({
const emit = defineEmits(["update:visible", "close"]); const emit = defineEmits(["update:visible", "close"]);
interface EditingFormState { interface FormState {
user: User; user: User;
saving: boolean; saving: boolean;
rawMode: boolean; rawMode: boolean;
raw: string; raw: string;
} }
const roles = ref<Role[]>([]); const initialFormState: FormState = {
const editingFormState = ref<EditingFormState>({
user: { user: {
spec: { spec: {
displayName: "", displayName: "",
@ -58,11 +58,14 @@ const editingFormState = ref<EditingFormState>({
saving: false, saving: false,
rawMode: false, rawMode: false,
raw: "", raw: "",
}); };
const roles = ref<Role[]>([]);
const formState = ref<FormState>(cloneDeep(initialFormState));
const selectedRole = ref(""); const selectedRole = ref("");
const isUpdateMode = computed(() => { const isUpdateMode = computed(() => {
return !!editingFormState.value.user.metadata.creationTimestamp; return !!formState.value.user.metadata.creationTimestamp;
}); });
const creationModalTitle = computed(() => { const creationModalTitle = computed(() => {
@ -70,7 +73,7 @@ const creationModalTitle = computed(() => {
}); });
const modalWidth = computed(() => { const modalWidth = computed(() => {
return editingFormState.value.rawMode ? 800 : 700; return formState.value.rawMode ? 800 : 700;
}); });
const basicRoles = computed(() => { const basicRoles = computed(() => {
@ -81,8 +84,10 @@ const basicRoles = computed(() => {
watch(props, (newVal) => { watch(props, (newVal) => {
if (newVal.visible && props.user) { if (newVal.visible && props.user) {
editingFormState.value.user = props.user; formState.value.user = cloneDeep(props.user);
return;
} }
formState.value = cloneDeep(initialFormState);
}); });
const handleFetchRoles = async () => { const handleFetchRoles = async () => {
@ -103,18 +108,18 @@ const handleVisibleChange = (visible: boolean) => {
const handleCreateUser = async () => { const handleCreateUser = async () => {
try { try {
editingFormState.value.saving = true; formState.value.saving = true;
let user: User; let user: User;
if (isUpdateMode.value) { if (isUpdateMode.value) {
const response = await apiClient.extension.user.updatev1alpha1User( const response = await apiClient.extension.user.updatev1alpha1User(
editingFormState.value.user.metadata.name, formState.value.user.metadata.name,
editingFormState.value.user formState.value.user
); );
user = response.data; user = response.data;
} else { } else {
const response = await apiClient.extension.user.createv1alpha1User( const response = await apiClient.extension.user.createv1alpha1User(
editingFormState.value.user formState.value.user
); );
user = response.data; user = response.data;
} }
@ -130,17 +135,17 @@ const handleCreateUser = async () => {
} catch (e) { } catch (e) {
console.error(e); console.error(e);
} finally { } finally {
editingFormState.value.saving = false; formState.value.saving = false;
} }
}; };
const handleRawModeChange = () => { const handleRawModeChange = () => {
editingFormState.value.rawMode = !editingFormState.value.rawMode; formState.value.rawMode = !formState.value.rawMode;
if (editingFormState.value.rawMode) { if (formState.value.rawMode) {
editingFormState.value.raw = YAML.stringify(editingFormState.value.user); formState.value.raw = YAML.stringify(formState.value.user);
} else { } else {
editingFormState.value.user = YAML.parse(editingFormState.value.raw); formState.value.user = YAML.parse(formState.value.raw);
} }
}; };
onMounted(handleFetchRoles); onMounted(handleFetchRoles);
@ -154,35 +159,35 @@ onMounted(handleFetchRoles);
> >
<template #actions> <template #actions>
<div class="modal-header-action" @click="handleRawModeChange"> <div class="modal-header-action" @click="handleRawModeChange">
<IconCodeBoxLine v-if="!editingFormState.rawMode" /> <IconCodeBoxLine v-if="!formState.rawMode" />
<IconEye v-else /> <IconEye v-else />
</div> </div>
</template> </template>
<VCodemirror <VCodemirror
v-show="editingFormState.rawMode" v-show="formState.rawMode"
v-model="editingFormState.raw" v-model="formState.raw"
height="50vh" height="50vh"
language="yaml" language="yaml"
/> />
<div v-show="!editingFormState.rawMode"> <div v-show="!formState.rawMode">
<FormKit id="user-form" type="form" @submit="handleCreateUser"> <FormKit id="user-form" type="form" @submit="handleCreateUser">
<FormKit <FormKit
v-model="editingFormState.user.metadata.name" v-model="formState.user.metadata.name"
:disabled="true" :disabled="true"
label="用户名" label="用户名"
type="text" type="text"
validation="required" validation="required"
></FormKit> ></FormKit>
<FormKit <FormKit
v-model="editingFormState.user.spec.displayName" v-model="formState.user.spec.displayName"
label="显示名称" label="显示名称"
type="text" type="text"
validation="required" validation="required"
></FormKit> ></FormKit>
<FormKit <FormKit
v-model="editingFormState.user.spec.email" v-model="formState.user.spec.email"
label="电子邮箱" label="电子邮箱"
type="email" type="email"
validation="required" validation="required"
@ -202,17 +207,17 @@ onMounted(handleFetchRoles);
validation="required" validation="required"
></FormKit> ></FormKit>
<FormKit <FormKit
v-model="editingFormState.user.spec.phone" v-model="formState.user.spec.phone"
label="手机号" label="手机号"
type="text" type="text"
></FormKit> ></FormKit>
<FormKit <FormKit
v-model="editingFormState.user.spec.avatar" v-model="formState.user.spec.avatar"
label="头像" label="头像"
type="text" type="text"
></FormKit> ></FormKit>
<FormKit <FormKit
v-model="editingFormState.user.spec.bio" v-model="formState.user.spec.bio"
label="描述" label="描述"
type="textarea" type="textarea"
></FormKit> ></FormKit>
@ -220,7 +225,7 @@ onMounted(handleFetchRoles);
</div> </div>
<template #footer> <template #footer>
<VButton <VButton
:loading="editingFormState.saving" :loading="formState.saving"
type="secondary" type="secondary"
@click="$formkit.submit('user-form')" @click="$formkit.submit('user-form')"
> >