Skip to content

Commit

Permalink
Optimize code based on review
Browse files Browse the repository at this point in the history
  • Loading branch information
LEIYOUSU committed Oct 30, 2024
1 parent d747913 commit 34f10c5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import { useI18n } from "vue-i18n";
const props = withDefaults(
defineProps<{
group?: Group;
isNew?: boolean;
}>(),
{
group: undefined,
isNew: false,
}
);
Expand Down Expand Up @@ -46,7 +44,12 @@ const modalTitle = props.group
const handleSave = async () => {
try {
isSubmitting.value = true;
if (props.isNew) {
if (props.group) {
await coreApiClient.storage.group.updateGroup({
name: formState.value.metadata.name,
group: formState.value,
});
} else {
const { data: groups } = await coreApiClient.storage.group.listGroup();
const hasDisplayNameDuplicate = groups.items.some(
(group) => group.spec.displayName === formState.value.spec.displayName
Expand All @@ -57,14 +60,6 @@ const handleSave = async () => {
);
return;
}
}
if (props.group) {
await coreApiClient.storage.group.updateGroup({
name: formState.value.metadata.name,
group: formState.value,
});
} else {
await coreApiClient.storage.group.createGroup({
group: formState.value,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const emit = defineEmits<{
}>();
const queryClient = useQueryClient();
const isNewGroup = ref(false);
const defaultGroups: Group[] = [
{
spec: {
Expand Down Expand Up @@ -68,16 +67,10 @@ const onCreationModalClose = () => {
queryClient.invalidateQueries({ queryKey: ["attachment-groups"] });
creationModalVisible.value = false;
};
const handleBadgeClick = () => {
creationModalVisible.value = true;
isNewGroup.value = true;
};
</script>
<template>
<AttachmentGroupEditingModal
v-if="!readonly && creationModalVisible"
:is-new="isNewGroup"
@close="onCreationModalClose"
/>
<div
Expand Down Expand Up @@ -107,7 +100,7 @@ const handleBadgeClick = () => {
>
<AttachmentGroupBadge
:features="{ actions: false }"
@click="handleBadgeClick"
@click="creationModalVisible = true"
>
<template #text>
<span>{{ $t("core.common.buttons.new") }}</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,14 @@ const selectedPolicy = ref<Policy>();
const selectedTemplateName = ref();
const policyEditingModal = ref(false);
const isNewPolicy = ref(false);
const handleOpenEditingModal = (policy: Policy) => {
selectedPolicy.value = policy;
isNewPolicy.value = false;
policyEditingModal.value = true;
};
const handleOpenCreateNewPolicyModal = (policyTemplate: PolicyTemplate) => {
selectedTemplateName.value = policyTemplate.metadata.name;
isNewPolicy.value = true;
policyEditingModal.value = true;
};
Expand Down Expand Up @@ -234,7 +231,6 @@ function getPolicyTemplateDisplayName(templateName: string) {
v-if="policyEditingModal"
:policy="selectedPolicy"
:template-name="selectedTemplateName"
:is-new="isNewPolicy"
@close="onEditingModalClose"
/>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ const props = withDefaults(
defineProps<{
policy?: Policy;
templateName?: string;
isNew?: boolean;
}>(),
{
policy: undefined,
templateName: undefined,
isNew: false,
}
);
Expand Down Expand Up @@ -137,22 +135,7 @@ const submitting = ref(false);
const handleSave = async () => {
try {
submitting.value = true;
if (props.isNew) {
const { data: policies } =
await coreApiClient.storage.policy.listPolicy();
const hasDisplayNameDuplicate = policies.items.some(
(policy) => policy.spec.displayName === formState.value.spec.displayName
);
if (hasDisplayNameDuplicate) {
Toast.error(
t("core.attachment.policy_editing_modal.toast.policy_name_exists")
);
return;
}
}
const configMapToUpdate = convertToSave();
if (isUpdateMode) {
await coreApiClient.configMap.updateConfigMap({
name: configMap.value.metadata.name,
Expand All @@ -164,6 +147,18 @@ const handleSave = async () => {
policy: formState.value,
});
} else {
const { data: policies } =
await coreApiClient.storage.policy.listPolicy();
const hasDisplayNameDuplicate = policies.items.some(
(policy) => policy.spec.displayName === formState.value.spec.displayName
);
if (hasDisplayNameDuplicate) {
Toast.error(
t("core.attachment.policy_editing_modal.toast.policy_name_exists")
);
return;
}
const { data: newConfigMap } =
await coreApiClient.configMap.createConfigMap({
configMap: configMapToUpdate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ const selectedGroupName = useLocalStorage("attachment-upload-group", "");
const selectedPolicyName = useLocalStorage("attachment-upload-policy", "");
const policyEditingModal = ref(false);
const groupEditingModal = ref(false);
const isNewPolicy = ref(false);
const isNewGroup = ref(false);
const policyTemplateNameToCreate = ref();
onMounted(() => {
Expand All @@ -47,12 +45,10 @@ const handleOpenCreateNewPolicyModal = async (
policyTemplate: PolicyTemplate
) => {
policyTemplateNameToCreate.value = policyTemplate.metadata.name;
isNewPolicy.value = true;
policyEditingModal.value = true;
};
const handleOpenCreateNewGroupModal = () => {
isNewGroup.value = true;
groupEditingModal.value = true;
};
Expand Down Expand Up @@ -183,13 +179,11 @@ const onGroupEditingModalClose = async () => {
<AttachmentPolicyEditingModal
v-if="policyEditingModal"
:template-name="policyTemplateNameToCreate"
:is-new="isNewPolicy"
@close="onEditingModalClose"
/>

<AttachmentGroupEditingModal
v-if="groupEditingModal"
:is-new="isNewGroup"
@close="onGroupEditingModalClose"
/>
</template>

0 comments on commit 34f10c5

Please sign in to comment.