Skip to content

Commit

Permalink
feat: adapt state of task api
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Nov 21, 2023
1 parent ee1737a commit 2dde057
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
20 changes: 13 additions & 7 deletions src/lang/en/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
"copy": "Copy file from a storage to another storage",
"done": "Completed",
"undone": "Running",
"pending": "Pending",
"running": "Running",
"canceling": "Canceling",
"succeeded": "Succeeded",
"canceled": "Canceled",
"errored": "Errored",
"clear_succeeded": "Clear Succeeded",
"retry": "Retry"
"retry": "Retry",
"state": {
"0": "Pending",
"1": "Running",
"2": "Succeeded",
"3": "Canceling",
"4": "Canceled",
"5": "Errored",
"6": "Failing",
"7": "Failed",
"8": "WaitingRetry",
"9": "BeforeRetry"
}
}
28 changes: 19 additions & 9 deletions src/pages/manage/tasks/Task.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ import { PEmptyResp, TaskInfo } from "~/types"
import { handleResp, notify, r } from "~/utils"
import { TasksProps } from "./Tasks"

enum TaskStateEnum {
Pending,
Running,
Succeeded,
Canceling,
Canceled,
Errored,
Failing,
Failed,
WaitingRetry,
BeforeRetry,
}

const StateMap: Record<
string,
| "primary"
Expand All @@ -26,26 +39,23 @@ const StateMap: Record<
| "danger"
| undefined
> = {
errored: "danger",
succeeded: "success",
canceled: "neutral",
[TaskStateEnum.Failed]: "danger",
[TaskStateEnum.Succeeded]: "success",
[TaskStateEnum.Canceled]: "neutral",
}
export const TaskState = (props: { state: string }) => {
export const TaskState = (props: { state: number }) => {
const t = useT()
return (
<Badge colorScheme={StateMap[props.state] ?? "info"}>
{t(`tasks.${props.state}`)}
{t(`tasks.state.${props.state}`)}
</Badge>
)
}

const DONE = ["succeeded", "canceled", "errored"]
const UNDONE = ["pending", "running", "canceling"]

export const Task = (props: TaskInfo & TasksProps) => {
const t = useT()
const operateName = props.done === "undone" ? "cancel" : "delete"
const canRetry = props.done === "done" && props.state === "errored"
const canRetry = props.done === "done" && props.state === TaskStateEnum.Failed
const [operateLoading, operate] = useFetch(
(): PEmptyResp =>
r.post(`/admin/task/${props.type}/${operateName}?tid=${props.id}`),
Expand Down
2 changes: 1 addition & 1 deletion src/types/task.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface TaskInfo {
id: string
name: string
state: string
state: number
status: string
progress: number
error: string
Expand Down

0 comments on commit 2dde057

Please sign in to comment.