Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby committed May 23, 2024
2 parents 1e25d30 + c51f2f4 commit 2002c43
Show file tree
Hide file tree
Showing 28 changed files with 520 additions and 936 deletions.
176 changes: 1 addition & 175 deletions ui/console-src/composables/use-setting-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,185 +2,11 @@
// types
import { computed, watch, type ComputedRef, type Ref } from "vue";
import { ref } from "vue";
import { apiClient } from "@/utils/api-client";

// libs
import { cloneDeep, merge } from "lodash-es";
import { cloneDeep } from "lodash-es";
import type { ConfigMap, Setting, SettingForm } from "@halo-dev/api-client";
import type { FormKitSchemaCondition, FormKitSchemaNode } from "@formkit/core";
import { Toast } from "@halo-dev/components";
import { useI18n } from "vue-i18n";

const initialConfigMap: ConfigMap = {
apiVersion: "v1alpha1",
kind: "ConfigMap",
metadata: {
name: "",
},
data: {},
};

interface useSettingFormReturn {
setting: Ref<Setting | undefined>;
configMap: Ref<ConfigMap>;
configMapFormData: Ref<Record<string, Record<string, string>> | undefined>;
saving: Ref<boolean>;
handleFetchSettings: () => void;
handleFetchConfigMap: () => void;
handleSaveConfigMap: () => void;
handleReset: () => void;
}

export function useSettingForm(
settingName: Ref<string | undefined>,
configMapName: Ref<string | undefined>
): useSettingFormReturn {
const { t } = useI18n();

const setting = ref<Setting>();
const configMap = ref<ConfigMap>(cloneDeep(initialConfigMap));
const configMapFormData = ref<
Record<string, Record<string, string>> | undefined
>();

const saving = ref(false);

const handleFetchSettings = async () => {
if (!settingName.value) {
setting.value = undefined;
return;
}
try {
const { data } = await apiClient.extension.setting.getv1alpha1Setting({
name: settingName.value,
});
setting.value = data;

// init configMapFormData
if (!configMapFormData.value) {
const { forms } = setting.value.spec;
const initialConfigMapFormData: Record<
string,
Record<string, string>
> = {};
forms.forEach((form) => {
initialConfigMapFormData[form.group] = {};
const formSchema = form.formSchema as (
| FormKitSchemaCondition
| FormKitSchemaNode
)[];
formSchema.forEach((schema) => {
// @ts-ignore
if ("name" in schema && "$formkit" in schema) {
initialConfigMapFormData[form.group][schema.name] =
schema.value || undefined;
}
});
});
configMapFormData.value = cloneDeep(initialConfigMapFormData);
}
} catch (e) {
console.error("Failed to fetch setting", e);
}
};

const handleFetchConfigMap = async () => {
if (!configMapName.value) {
configMap.value = cloneDeep(initialConfigMap);
configMapFormData.value = undefined;
return;
}
try {
const response = await apiClient.extension.configMap.getv1alpha1ConfigMap(
{
name: configMapName.value,
},
{
mute: true,
}
);

configMap.value = response.data;

const { data } = configMap.value;

if (data) {
// merge objects value
const { forms } = setting.value?.spec || {};

forms?.forEach((form) => {
if (!configMapFormData.value) {
return;
}
configMapFormData.value[form.group] = merge(
configMapFormData.value[form.group] || {},
JSON.parse(data[form.group] || "{}")
);
});
}
} catch (e) {
console.error("Failed to fetch configMap", e);
}
};

const handleSaveConfigMap = async () => {
try {
saving.value = true;

if (!configMap.value.metadata.name && configMapName.value) {
configMap.value.metadata.name = configMapName.value;
}

setting.value?.spec.forms.forEach((item: SettingForm) => {
// @ts-ignore
configMap.value.data[item.group] = JSON.stringify(
configMapFormData?.value?.[item.group]
);
});

if (!configMap.value.metadata.creationTimestamp) {
const { data } =
await apiClient.extension.configMap.createv1alpha1ConfigMap({
configMap: configMap.value,
});

configMapName.value = data.metadata.name;
} else {
const { data } =
await apiClient.extension.configMap.updatev1alpha1ConfigMap({
configMap: configMap.value,
name: configMap.value.metadata.name,
});
configMapName.value = data.metadata.name;
}

Toast.success(t("core.common.toast.save_success"));
} catch (e) {
console.error("Failed to save configMap", e);
} finally {
await handleFetchSettings();
await handleFetchConfigMap();
saving.value = false;
}
};

const handleReset = () => {
setting.value = undefined;
configMap.value = cloneDeep(initialConfigMap);
configMapFormData.value = undefined;
};

return {
setting,
configMap,
configMapFormData,
saving,
handleFetchSettings,
handleFetchConfigMap,
handleSaveConfigMap,
handleReset,
};
}

interface useSettingFormConvertReturn {
formSchema: ComputedRef<
Expand Down
39 changes: 17 additions & 22 deletions ui/console-src/modules/contents/attachments/AttachmentList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@ import {
IconArrowRight,
IconCheckboxFill,
IconDatabase2Line,
IconFolder,
IconGrid,
IconList,
IconUpload,
IconRefreshLine,
IconUpload,
Toast,
VButton,
VCard,
VDropdown,
VDropdownItem,
VEmpty,
VLoading,
VPageHeader,
VPagination,
VSpace,
VEmpty,
IconFolder,
VLoading,
Toast,
VDropdown,
VDropdownItem,
} from "@halo-dev/components";
import LazyImage from "@/components/image/LazyImage.vue";
import AttachmentDetailModal from "./components/AttachmentDetailModal.vue";
import AttachmentUploadModal from "./components/AttachmentUploadModal.vue";
import AttachmentPoliciesModal from "./components/AttachmentPoliciesModal.vue";
import AttachmentGroupList from "./components/AttachmentGroupList.vue";
import { computed, onMounted, ref, watch } from "vue";
import type { Ref } from "vue";
import { computed, onMounted, provide, ref, watch } from "vue";
import type { Attachment, Group } from "@halo-dev/api-client";
import { useFetchAttachmentPolicy } from "./composables/use-attachment-policy";
import { useAttachmentControl } from "./composables/use-attachment";
Expand All @@ -37,8 +38,6 @@ import { useFetchAttachmentGroup } from "./composables/use-attachment-group";
import { useI18n } from "vue-i18n";
import { useLocalStorage } from "@vueuse/core";
import UserFilterDropdown from "@/components/filter/UserFilterDropdown.vue";
import { provide } from "vue";
import type { Ref } from "vue";
import AttachmentListItem from "./components/AttachmentListItem.vue";
const { t } = useI18n();
Expand All @@ -50,7 +49,7 @@ const detailVisible = ref(false);
const { policies } = useFetchAttachmentPolicy();
const { groups, handleFetchGroups } = useFetchAttachmentGroup();
const selectedGroup = ref<Group>();
const selectedGroup = useRouteQuery<string | undefined>("group");
// Filter
const keyword = useRouteQuery<string>("keyword", "");
Expand Down Expand Up @@ -111,12 +110,8 @@ const {
isChecked,
handleReset,
} = useAttachmentControl({
group: selectedGroup,
policy: computed(() => {
return policies.value?.find(
(policy) => policy.metadata.name === selectedPolicy.value
);
}),
groupName: selectedGroup,
policyName: selectedPolicy,
user: selectedUser,
accepts: computed(() => {
if (!selectedAccepts.value) {
Expand Down Expand Up @@ -187,6 +182,7 @@ const onDetailModalClose = () => {
const onUploadModalClose = () => {
routeQueryAction.value = undefined;
handleFetchAttachments();
uploadVisible.value = false;
};
// View type
Expand Down Expand Up @@ -258,11 +254,11 @@ onMounted(() => {
</span>
</template>
</AttachmentDetailModal>
<AttachmentUploadModal
v-model:visible="uploadVisible"
@close="onUploadModalClose"
<AttachmentUploadModal v-if="uploadVisible" @close="onUploadModalClose" />
<AttachmentPoliciesModal
v-if="policyVisible"
@close="policyVisible = false"
/>
<AttachmentPoliciesModal v-model:visible="policyVisible" />
<VPageHeader :title="$t('core.attachment.title')">
<template #icon>
<IconFolder class="mr-2 self-center" />
Expand Down Expand Up @@ -470,7 +466,6 @@ onMounted(() => {

<div :style="`${viewType === 'list' ? 'padding:12px 16px 0' : ''}`">
<AttachmentGroupList
v-model:selected-group="selectedGroup"
@select="handleReset"
@update="handleFetchGroups"
@reload-attachments="handleFetchAttachments"
Expand Down
Loading

0 comments on commit 2002c43

Please sign in to comment.