Skip to content

Commit

Permalink
feat: backup and restore support override (#73)
Browse files Browse the repository at this point in the history
* feat: backup and restore support override

* fix: deleted the non-override case accidentally

* fix: add types

* fix: codefactor issues

---------

Co-authored-by: Andy Hsu <[email protected]>
  • Loading branch information
itsHenry35 and xhofe authored Apr 2, 2023
1 parent 2bde1ae commit cc9661f
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 42 deletions.
3 changes: 2 additions & 1 deletion src/lang/en/br.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"start_restore": "Start restore",
"finish_restore": "Finish restore",
"success_restore_item": "[ {{item}} ] Restore was successful",
"failed_restore_item": "[ {{item}} ] Restore failed"
"failed_restore_item": "[ {{item}} ] Restore failed",
"override": "Override"
}
186 changes: 147 additions & 39 deletions src/pages/manage/backup-restore.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { HStack, Button, VStack, Text } from "@hope-ui/solid"
import {
HStack,
Button,
VStack,
Text,
Switch as HopeSwitch,
} from "@hope-ui/solid"
import { r, handleRespWithoutNotify, notify } from "~/utils"
import { useFetch, useManageTitle, useT } from "~/hooks"
import {
Expand Down Expand Up @@ -44,6 +50,7 @@ const Log = (props: { msg: string; type: LogType }) => {
}

const BackupRestore = () => {
const [override, setOverride] = createSignal(false)
const t = useT()
useManageTitle("manage.sidemenu.backup-restore")
let logRef: HTMLDivElement
Expand Down Expand Up @@ -136,12 +143,74 @@ const BackupRestore = () => {
const [addMetaLoading, addMeta] = useFetch((meta: Meta): PEmptyResp => {
return r.post(`/admin/meta/create`, meta)
})
const [updateUserLoading, updateUser] = useFetch((user: User): PEmptyResp => {
return r.post(`/admin/user/update`, user)
})
const [updateStorageLoading, updateStorage] = useFetch(
(storage: Storage): PEmptyResp => {
return r.post(`/admin/storage/update`, storage)
}
)
const [updateMetaLoading, updateMeta] = useFetch((meta: Meta): PEmptyResp => {
return r.post(`/admin/meta/update`, meta)
})
async function handleOvrData<T>(
dataArray: T[],
getDataFunc: { (): PResp<{ content: T[]; total: number }> },
addDataFunc: {
(t: T): PEmptyResp
},
updateDataFunc: {
(t: T): PEmptyResp
},
idFieldName: keyof T,
itemName: string
) {
const currentData = (await getDataFunc()).data.content
for (const i in dataArray) {
const currentItem = dataArray[i]
const currentIdValue = currentItem[idFieldName]
const currentDataItem = currentData.find(
(d) => d[idFieldName] === currentIdValue
)
const method = currentDataItem ? "update" : "add"
const handleDataFunc = method === "add" ? addDataFunc : updateDataFunc
await handleRespWithoutNotify(
await handleDataFunc(currentItem),
() => {
appendLog(
t("br.success_restore_item", {
item: t(itemName),
}) +
"-" +
`[${currentIdValue}]`,
"success"
)
},
(msg) => {
appendLog(
t("br.failed_restore_item", {
item: t(itemName),
}) +
"-" +
`[${currentIdValue}]` +
":" +
msg,
"error"
)
}
)
}
}
const restoreLoading = () => {
return (
addSettingsLoading() ||
addUserLoading() ||
addStorageLoading() ||
addMetaLoading()
addMetaLoading() ||
updateUserLoading() ||
updateStorageLoading() ||
updateMetaLoading()
)
}
const restore = async () => {
Expand All @@ -159,6 +228,9 @@ const BackupRestore = () => {
const reader = new FileReader()
reader.onload = async () => {
const data: Data = JSON.parse(reader.result as string)
if (override()) {
await backup()
}
data.settings &&
handleRespWithoutNotify(
await addSettings(
Expand All @@ -185,42 +257,69 @@ const BackupRestore = () => {
)
}
)
for (const item of [
{ name: "users", fn: addUser, data: data.users, key: "username" },
{
name: "storages",
fn: addStorage,
data: data.storages,
key: "mount_path",
},
{ name: "metas", fn: addMeta, data: data.metas, key: "path" },
] as const) {
for (const itemData of item.data || []) {
itemData.id = 0
handleRespWithoutNotify(
await item.fn(itemData),
() => {
appendLog(
t("br.success_restore_item", {
item: t(`manage.sidemenu.${item.name}`),
}) +
"-" +
`[${(itemData as any)[item.key]}]`,
"success"
)
},
(msg) => {
appendLog(
t("br.failed_restore_item", {
item: t(`manage.sidemenu.${item.name}`),
}) +
` [ ${(itemData as any)[item.key]} ] ` +
":" +
msg,
"error"
)
}
)
if (override()) {
await handleOvrData(
data.users,
getUsers,
addUser,
updateUser,
"username",
"manage.sidemenu.users"
)
await handleOvrData(
data.storages,
getStorages,
addStorage,
updateStorage,
"mount_path",
"manage.sidemenu.storages"
)
await handleOvrData(
data.metas,
getMetas,
addMeta,
updateMeta,
"path",
"manage.sidemenu.metas"
)
} else {
for (const item of [
{ name: "users", fn: addUser, data: data.users, key: "username" },
{
name: "storages",
fn: addStorage,
data: data.storages,
key: "mount_path",
},
{ name: "metas", fn: addMeta, data: data.metas, key: "path" },
] as const) {
for (const itemData of item.data || []) {
itemData.id = 0
handleRespWithoutNotify(
await item.fn(itemData),
() => {
appendLog(
t("br.success_restore_item", {
item: t(`manage.sidemenu.${item.name}`),
}) +
"-" +
`[${(itemData as any)[item.key]}]`,
"success"
)
},
(msg) => {
appendLog(
t("br.failed_restore_item", {
item: t(`manage.sidemenu.${item.name}`),
}) +
` [ ${(itemData as any)[item.key]} ] ` +
":" +
msg,
"error"
)
}
)
}
}
}
appendLog(t("br.finish_restore"), "info")
Expand All @@ -231,7 +330,7 @@ const BackupRestore = () => {
}
return (
<VStack spacing="$2" w="$full">
<HStack spacing="$2" alignItems="start" w="$full">
<HStack spacing="$2" w="$full">
<Button
loading={backupLoading()}
onClick={() => {
Expand All @@ -249,6 +348,15 @@ const BackupRestore = () => {
>
{t("br.restore")}
</Button>
<HopeSwitch
id="restore-override"
checked={override()}
onChange={(e: { currentTarget: HTMLInputElement }) =>
setOverride(e.currentTarget.checked)
}
>
{t("br.override")}
</HopeSwitch>
</HStack>
<VStack
p="$2"
Expand Down
4 changes: 2 additions & 2 deletions src/pages/manage/users/AddOrEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const AddOrEdit = () => {
role: 0,
permission: 0,
disabled: false,
github_id: 0,
sso_id: "",
})
const [userLoading, loadUser] = useFetch(
(): PResp<User> => r.get(`/admin/user/get?id=${id}`)
Expand Down Expand Up @@ -147,7 +147,7 @@ const AddOrEdit = () => {
loading={okLoading()}
onClick={async () => {
const resp = await ok()
// TODO maybe can use handleRrespWithNotifySuccess
// TODO maybe can use handleRespWithNotifySuccess
handleResp(resp, () => {
notify.success(t("global.save_success"))
back()
Expand Down

0 comments on commit cc9661f

Please sign in to comment.