Skip to content

Commit

Permalink
perf(task): merge requests of operating selected (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
KirCute authored Dec 10, 2024
1 parent e3eb093 commit 24a8f43
Showing 1 changed file with 28 additions and 47 deletions.
75 changes: 28 additions & 47 deletions src/pages/manage/tasks/Tasks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
For,
JSX,
onCleanup,
Setter,
Show,
} from "solid-js"
import { Paginator } from "~/components"
Expand Down Expand Up @@ -180,52 +179,22 @@ export const Tasks = (props: TasksProps) => {
}),
)
}
const doWithSelected = (
loadingSetter: Setter<boolean>,
fetchFunc: (task: TaskInfo) => PEmptyResp,
successCallback?: () => void,
) => {
return async () => {
loadingSetter(true)
const promises = filteredTask()
.filter((task) => task.selected)
.map(fetchFunc)
let success = true
for (const p of promises) {
const resp = await p
if (resp.code !== 200) {
success = false
handleResp(resp)
if (resp.code === 401) return
}
}
loadingSetter(false)
if (success) successCallback?.()
}
}
const [retrySelectedLoading, setRetrySelectedLoading] = createSignal(false)
const retrySelected = doWithSelected(
setRetrySelectedLoading,
(task) => {
return r.post(`/task/${props.type}/retry?tid=${task.id}`)
},
() => {
notify.info(t("tasks.retry"))
refresh()
},
const getSelectedId = () =>
filteredTask()
.filter((task) => task.selected)
.map((task) => task.id)
const [retrySelectedLoading, retrySelected] = useFetch(
(): PEmptyResp => r.post(`/task/${props.type}/retry_some`, getSelectedId()),
)
const [operateSelectedLoading, setOperateSelectedLoading] =
createSignal(false)
const operateSelected = doWithSelected(
setOperateSelectedLoading,
(task) => {
return r.post(`/task/${props.type}/${operateName}?tid=${task.id}`)
},
() => {
notify.success(t("global.delete_success"))
refresh()
},
const [operateSelectedLoading, operateSelected] = useFetch(
(): PEmptyResp =>
r.post(`/task/${props.type}/${operateName}_some`, getSelectedId()),
)
const notifyIndividualError = (msg: Record<string, string>) => {
Object.entries(msg).forEach(([key, value]) => {
notify.error(`${key}: ${value}`)
})
}
const [page, setPage] = createSignal(1)
const pageSize = 20
const operateName = props.done === "undone" ? "cancel" : "delete"
Expand Down Expand Up @@ -300,15 +269,27 @@ export const Tasks = (props: TasksProps) => {
<Button
colorScheme="primary"
loading={retrySelectedLoading()}
onClick={retrySelected}
onClick={async () => {
const resp = await retrySelected()
handleResp(resp, (data) => {
notifyIndividualError(data)
refresh()
})
}}
>
{t(`tasks.retry_selected`)}
</Button>
</Show>
<Button
colorScheme="warning"
loading={operateSelectedLoading()}
onClick={operateSelected}
onClick={async () => {
const resp = await operateSelected()
handleResp(resp, (data) => {
notifyIndividualError(data)
refresh()
})
}}
>
{t(`tasks.${operateName}_selected`)}
</Button>
Expand Down

0 comments on commit 24a8f43

Please sign in to comment.