Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: prompt to upgrade theme when installing existing theme #3970

Merged
merged 2 commits into from May 25, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions console/src/locales/en.yaml
Expand Up @@ -604,6 +604,10 @@ core:
titles:
install: Install theme
upgrade: Upgrade theme ({display_name})
operations:
existed_during_installation:
title: The theme already exists.
description: The currently installed theme already exists, do you want to upgrade?
tabs:
local: Local
remote:
Expand Down
4 changes: 4 additions & 0 deletions console/src/locales/zh-CN.yaml
Expand Up @@ -604,6 +604,10 @@ core:
titles:
install: 安装主题
upgrade: 升级主题({display_name})
operations:
existed_during_installation:
title: 主题已存在
description: 当前安装的主题已存在,是否升级?
tabs:
local: 本地上传
remote:
Expand Down
4 changes: 4 additions & 0 deletions console/src/locales/zh-TW.yaml
Expand Up @@ -604,6 +604,10 @@ core:
titles:
install: 安裝主題
upgrade: 升級主題({display_name})
operations:
existed_during_installation:
title: 主題已存在
description: 當前安裝的主題已存在,是否升級?
tabs:
local: 本地上傳
remote:
Expand Down
Expand Up @@ -356,7 +356,6 @@ watch(
v-if="visible"
v-model:visible="themeUploadVisible"
:upgrade-theme="themeToUpgrade"
@close="refetch"
/>

<ThemePreviewModal
Expand Down
@@ -1,5 +1,12 @@
<script lang="ts" setup>
import { Toast, VButton, VModal, VTabItem, VTabs } from "@halo-dev/components";
import {
Dialog,
Toast,
VButton,
VModal,
VTabItem,
VTabs,
} from "@halo-dev/components";
import UppyUpload from "@/components/upload/UppyUpload.vue";
import { computed, ref, watch, nextTick } from "vue";
import type { Theme } from "@halo-dev/api-client";
Expand All @@ -9,6 +16,8 @@ import { useThemeStore } from "@/stores/theme";
import { apiClient } from "@/utils/api-client";
import { useRouteQuery } from "@vueuse/router";
import { submitForm } from "@formkit/core";
import type { ErrorResponse } from "@uppy/core";
import type { UppyFile } from "@uppy/utils";

const { t } = useI18n();
const queryClient = useQueryClient();
Expand Down Expand Up @@ -83,6 +92,71 @@ const onUploaded = () => {
handleVisibleChange(false);
};

interface ThemeInstallationErrorResponse {
detail: string;
instance: string;
themeName: string;
requestId: string;
status: number;
timestamp: string;
title: string;
type: string;
}

const THEME_ALREADY_EXISTS_TYPE = "https://halo.run/probs/theme-alreay-exists";

const onError = (file: UppyFile<unknown>, response: ErrorResponse) => {
const body = response.body as ThemeInstallationErrorResponse;

if (body.type === THEME_ALREADY_EXISTS_TYPE) {
handleCatchExistsException(body, file.data as File);
}
};

const handleCatchExistsException = async (
error: ThemeInstallationErrorResponse,
file?: File
) => {
Dialog.info({
title: t(
"core.theme.upload_modal.operations.existed_during_installation.title"
),
description: t(
"core.theme.upload_modal.operations.existed_during_installation.description"
),
confirmText: t("core.common.buttons.confirm"),
cancelText: t("core.common.buttons.cancel"),
onConfirm: async () => {
if (activeTabId.value === "local") {
if (!file) {
throw new Error("File is required");
}

await apiClient.theme.upgradeTheme({
name: error.themeName,
file: file,
});
} else if (activeTabId.value === "remote") {
await apiClient.theme.upgradeThemeFromUri({
name: error.themeName,
upgradeFromUriRequest: {
uri: remoteDownloadUrl.value,
},
});
} else {
throw new Error("Unknown tab id");
}

Toast.success(t("core.common.toast.upgrade_success"));

queryClient.invalidateQueries({ queryKey: ["themes"] });
themeStore.fetchActivatedTheme();

handleVisibleChange(false);
},
});
};

// remote download
const activeTabId = ref("local");
const remoteDownloadUrl = ref("");
Expand Down Expand Up @@ -120,10 +194,16 @@ const handleDownloadTheme = async () => {

handleVisibleChange(false);

routeRemoteDownloadUrl.value = null;
} catch (error) {
console.log("Failed to download theme", error);
// eslint-disable-next-line
} catch (error: any) {
const data = error?.response.data as ThemeInstallationErrorResponse;
if (data?.type === THEME_ALREADY_EXISTS_TYPE) {
handleCatchExistsException(data);
}

console.error("Failed to download theme", error);
} finally {
routeRemoteDownloadUrl.value = null;
downloading.value = false;
}
};
Expand Down Expand Up @@ -164,6 +244,7 @@ watch(
:endpoint="endpoint"
auto-proceed
@uploaded="onUploaded"
@error="onError"
/>
</VTabItem>
<VTabItem
Expand Down