Add: clear

This commit is contained in:
2022-04-02 15:42:45 +08:00
parent 33eaa9ee00
commit 880c26b8d3
2 changed files with 50 additions and 11 deletions

View File

@@ -2,10 +2,11 @@ import {
deleteTimeRange,
getLimit,
authenticate,
updateUsername,
updateUsernameWithLimit,
} from "../../../../libs/db";
import { addAPIQPS } from '../../../../libs/stats';
import { addAPIQPS } from "../../../../libs/stats";
export default function handler(req, res) {
addAPIQPS();
@@ -43,6 +44,16 @@ export default function handler(req, res) {
return;
}
// admin update username
const { token } = req.body;
if (authenticate(token)) {
updateUsername.run(username, id);
res.status(200).json({
success: true,
});
return;
}
try {
const limit = getLimit();
updateUsernameWithLimit(username, id, limit);

View File

@@ -34,6 +34,29 @@ export default function Time(props) {
const router = useRouter();
const updateUsernameByAdmin = (id, username) => {
fetch(`${prefix}/api/time/ranges/${id}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
username,
token,
}),
})
.then((res) => res.json())
.then((res) => {
if (res.error) {
setSnackbarError(true);
setSnackbarErrorMessage(res.error);
} else {
setSnackbarSuccess(true);
refreshRanges();
}
});
};
const getStats = () => {
fetch(`${prefix}/api/stats`, {
method: "GET",
@@ -330,16 +353,21 @@ export default function Time(props) {
{range.username ? range.username : "抢!"}
</Button>
{isAdmin() && (
<Button
sx={{
userSelect: "none",
}}
variant="contained"
color="secondary"
onClick={() => deleteRange(range.id)}
>
删除
</Button>
<>
<Button
variant="contained"
onClick={() => updateUsernameByAdmin(range.id, "")}
>
清空
</Button>
<Button
variant="contained"
color="secondary"
onClick={() => deleteRange(range.id)}
>
删除
</Button>
</>
)}
</TableCell>
</TableRow>