Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | 2x 2x 62x 62x 62x 62x 62x 62x 62x 1x 62x 62x 1x 62x 62x 62x 1x 62x 62x 4x 62x 386x 386x 386x 1x 386x 62x 22x 62x | import React from "react";
import OurTable, { DateColumn } from "main/components/OurTable";
import Plaintext from "main/components/Utils/Plaintext";
import { Button } from "react-bootstrap";
import { Link } from "react-router";
import { toast } from "react-toastify";
import { useBackend, useBackendMutation } from "main/utils/useBackend";
// lib-jobs job cancellation (POST /api/jobs/{id}/cancel): a queued job is
// killed immediately; a running job is marked "cancelling" and stops at its
// next ctx.log() checkpoint. Any other status is terminal.
const CANCELLABLE_STATUSES = ["queued", "running"];
const PagedJobsTable = () => {
const testId = "PagedJobsTable";
const refreshJobsIntervalMilliseconds = 5000;
const [selectedPage, setSelectedPage] = React.useState(0);
const pageSize = 10;
// Stryker disable all
const { data: page } = useBackend(
["/api/jobs/all"],
{
method: "GET",
url: "/api/jobs/paginated",
params: {
page: selectedPage,
pageSize: pageSize,
sortField: "id",
sortDirection: "DESC",
},
},
{ content: [], totalPages: 0 },
{ refetchInterval: refreshJobsIntervalMilliseconds },
);
// Stryker restore all
const cellToAxiosParamsCancel = (cell) => ({
url: `/api/jobs/${cell.row.original.id}/cancel`,
method: "POST",
});
const cancelSuccess = () => {
toast("Cancellation requested.");
};
// Stryker disable all : hard to test for query caching
const cancelMutation = useBackendMutation(
cellToAxiosParamsCancel,
{ onSuccess: cancelSuccess },
["/api/jobs/all"],
);
// Stryker restore all
const cancelCallback = (cell) => {
cancelMutation.mutate(cell);
};
const testid = "PagedJobsTable";
const previousPageCallback = () => {
return () => {
setSelectedPage(selectedPage - 1);
};
};
const nextPageCallback = () => {
return () => {
setSelectedPage(selectedPage + 1);
};
};
const columns = [
{
Header: "id",
accessor: "id", // accessor is the "key" in the data
},
DateColumn("Created", (cell) => cell.row.original.createdAt),
DateColumn("Updated", (cell) => cell.row.original.updatedAt),
{
Header: "Status",
accessor: "status",
},
{
Header: "Cancel",
// Stryker disable next-line StringLiteral : react-table falls back to the Header when id is empty, so the mutant is equivalent
id: "Cancel",
Cell: ({ cell }) =>
CANCELLABLE_STATUSES.includes(cell.row.original.status) ? (
<Button
variant="danger"
size="sm"
onClick={() => cancelCallback(cell)}
data-testid={`${testId}-cell-row-${cell.row.index}-col-Cancel-button`}
>
Cancel
</Button>
) : null,
},
{
// Since lib-jobs v0.2.0 the log in a list/paginated response is only a
// preview (the most recent lines); the full log lives in job_logs and
// is served by GET /api/jobs/logs/{id}.
Header: "Log",
// Stryker disable next-line StringLiteral : react-table falls back to the Header when id is empty, so the mutant is equivalent
id: "Log",
Cell: ({ cell }) => (
<>
<Plaintext text={cell.row.original.log} />
<Link
to={`/admin/jobs/logs/${cell.row.original.id}`}
data-testid={`${testId}-cell-row-${cell.row.index}-col-Log-link`}
>
See entire log
</Link>
</>
),
},
];
const sortees = React.useMemo(
() => [
{
id: "id",
desc: true,
},
],
// Stryker disable next-line all
[],
);
return (
<>
<p>Page: {selectedPage + 1}</p>
<Button
data-testid={`${testId}-previous-button`}
onClick={previousPageCallback()}
disabled={selectedPage === 0}
>
Previous
</Button>
<Button
data-testid={`${testId}-next-button`}
onClick={nextPageCallback()}
disabled={page.totalPages === 0 || selectedPage === page.totalPages - 1}
>
Next
</Button>
<OurTable
data={page.content}
columns={columns}
testid={testid}
initialState={{ sortBy: sortees }}
/>
</>
);
};
export default PagedJobsTable;
|