-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathScopeEdit.tsx
132 lines (130 loc) · 4.39 KB
/
ScopeEdit.tsx
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
// Copyright 2024 the JSR authors. All rights reserved. MIT license.
import type { FullScope } from "../../utils/api_types.ts";
import { useState } from "preact/hooks";
import { timeAgo } from "../../utils/timeAgo.ts";
import { api, path } from "../../utils/api.ts";
import { TableData, TableRow } from "../../components/Table.tsx";
export default function AdminScopeEdit({ scope }: { scope: FullScope }) {
const [edit, setEdit] = useState(false);
const [processing, setProcessing] = useState(false);
const [packageLimit, setPackageLimit] = useState(
String(scope.quotas.packageLimit),
);
const [newPackagePerWeekLimit, setNewPackagePerWeekLimit] = useState(
String(scope.quotas.newPackagePerWeekLimit),
);
const [publishAttemptsPerWeekLimit, setPublishAttemptsPerWeekLimit] =
useState(
String(scope.quotas.publishAttemptsPerWeekLimit),
);
return (
<TableRow key={scope.scope}>
<TableData>
<a href={`/@${scope.scope}`}>{scope.scope}</a>
</TableData>
<TableData title={scope.creator.id}>
<a href={`/user/${scope.creator.id}`}>{scope.creator.name}</a>
</TableData>
<TableData>
{edit
? (
<input
type="number"
class="block w-28 p-1.5 text-right input-container input"
disabled={processing}
value={packageLimit}
onChange={(e) => setPackageLimit(e.currentTarget.value)}
required
/>
)
: packageLimit}
</TableData>
<TableData>
{edit
? (
<input
type="number"
class="block w-28 p-1.5 text-right input-container input"
disabled={processing}
value={newPackagePerWeekLimit}
onChange={(e) => setNewPackagePerWeekLimit(e.currentTarget.value)}
required
/>
)
: newPackagePerWeekLimit}
</TableData>
<TableData>
{edit
? (
<input
type="number"
class="block w-28 p-1.5 text-right input-container input"
disabled={processing}
value={publishAttemptsPerWeekLimit}
onChange={(e) =>
setPublishAttemptsPerWeekLimit(e.currentTarget.value)}
required
/>
)
: publishAttemptsPerWeekLimit}
</TableData>
<TableData title={new Date(scope.createdAt).toISOString().slice(0, 10)}>
{timeAgo(new Date(scope.createdAt))}
</TableData>
<TableData align="right">
{edit
? (
<>
<button
type="button"
disabled={processing}
onClick={() => {
setProcessing(true);
api.patch(path`/admin/scopes/${scope.scope}`, {
packageLimit: +packageLimit,
newPackagePerWeekLimit: +newPackagePerWeekLimit,
versionPublishPerWeekLimit: +publishAttemptsPerWeekLimit,
}).then((res) => {
setProcessing(false);
if (res.ok) {
setEdit(false);
} else {
console.error(res);
}
});
}}
class="link disabled:text-jsr-gray-500 disabled:hover:cursor-wait"
>
Save<span class="sr-only">, {scope.scope}</span>
</button>
<button
type="button"
onClick={() => {
setEdit(false);
setPackageLimit(String(scope.quotas.packageLimit));
setNewPackagePerWeekLimit(
String(scope.quotas.newPackagePerWeekLimit),
);
setPublishAttemptsPerWeekLimit(
String(scope.quotas.publishAttemptsPerWeekLimit),
);
}}
class="link ml-2"
>
Cancel<span class="sr-only">, {scope.scope}</span>
</button>
</>
)
: (
<button
type="button"
onClick={() => setEdit(true)}
class="link"
>
Edit<span class="sr-only">, {scope.scope}</span>
</button>
)}
</TableData>
</TableRow>
);
}