Skip to content

Commit b841fc3

Browse files
aster-voidnakaterm
authored andcommitted
add spinner while processing (and fix async bug) (#615)
1 parent 2ad09a5 commit b841fc3

File tree

3 files changed

+51
-31
lines changed

3 files changed

+51
-31
lines changed

web/components/human/humanListItem.tsx

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useState } from "react";
12
import Dots from "../common/Dots";
23
import UserAvatar from "./avatar";
34

@@ -9,12 +10,12 @@ type HumanListItemProps = {
910
rollUpName?: boolean; // is currently only intended to be used in Chat
1011
unreadCount?: number; // only intended to be used in chat
1112
statusMessage?: string;
12-
onDelete?: (id: number) => void;
13+
onDelete?: (id: number) => Promise<void>;
1314
onOpen?: (user: { id: number; name: string; pictureUrl: string }) => void;
14-
onAccept?: (id: number) => void;
15-
onReject?: (id: number) => void;
16-
onCancel?: (id: number) => void;
17-
onRequest?: (id: number) => void;
15+
onAccept?: (id: number) => Promise<void>;
16+
onReject?: (id: number) => Promise<void>;
17+
onCancel?: (id: number) => Promise<void>;
18+
onRequest?: (id: number) => Promise<void>;
1819
hasDots?: boolean;
1920
dotsActions?: object;
2021
};
@@ -73,46 +74,36 @@ export function HumanListItem(props: HumanListItemProps) {
7374
<span className="badge badge-primary">{unreadCount}</span>
7475
) : undefined}
7576
{onAccept && (
76-
<button
77-
type="button"
77+
<ActionButton
7878
className="btn btn-primary btn-sm m-1"
79-
onClick={(e) => {
80-
e.stopPropagation();
81-
onAccept(id);
82-
}}
79+
onClick={async () => await onAccept(id)}
8380
>
8481
承認
85-
</button>
82+
</ActionButton>
8683
)}
8784
{onReject && (
88-
<button
89-
type="button"
85+
<ActionButton
9086
className="btn btn-sm m-1"
91-
onClick={(e) => {
92-
e.stopPropagation();
93-
onReject(id);
94-
}}
87+
onClick={async () => await onReject(id)}
9588
>
9689
拒否
97-
</button>
90+
</ActionButton>
9891
)}
9992
{onCancel && (
100-
<button
101-
type="button"
93+
<ActionButton
10294
className="btn btn-sm m-1"
103-
onClick={() => onCancel(id)}
95+
onClick={async () => await onCancel(id)}
10496
>
10597
キャンセル
106-
</button>
98+
</ActionButton>
10799
)}
108100
{onRequest && (
109-
<button
110-
type="button"
101+
<ActionButton
111102
className="btn btn-sm m-1 bg-primary text-white"
112-
onClick={() => onRequest(id)}
103+
onClick={async () => await onRequest(id)}
113104
>
114105
リクエスト
115-
</button>
106+
</ActionButton>
116107
)}
117108
{hasDots && (
118109
<Dots
@@ -141,3 +132,32 @@ export function HumanListItem(props: HumanListItemProps) {
141132
</div>
142133
);
143134
}
135+
136+
export function ActionButton({
137+
onClick,
138+
className,
139+
children,
140+
}: {
141+
onClick: () => Promise<void>;
142+
className: string;
143+
children: string; // change this to React.Element or something if you want to put something other than text in here
144+
}) {
145+
const [inProgress, setInProgress] = useState<boolean>(false);
146+
return inProgress ? (
147+
<button type="button" className={className} disabled>
148+
<span className="loading loading-spinner" />
149+
</button>
150+
) : (
151+
<button
152+
type="button"
153+
className={className}
154+
onClick={async (e) => {
155+
e.stopPropagation();
156+
setInProgress(true);
157+
await onClick();
158+
}}
159+
>
160+
{children}
161+
</button>
162+
);
163+
}

web/components/match/myRequests.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export default function MyReq() {
2929
name={receivingUser.name}
3030
pictureUrl={receivingUser.pictureUrl}
3131
onOpen={() => openModal(receivingUser)}
32-
onCancel={(id) => {
33-
request.cancel(id).then(reload);
32+
onCancel={async (id) => {
33+
await request.cancel(id).then(reload);
3434
}}
3535
/>
3636
))}

web/components/search/table.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ export default function UserTable({
2424
onOpen={() => openModal(user)}
2525
onRequest={
2626
canRequest(user.id)
27-
? () => {
28-
request.send(user.id);
27+
? async () => {
28+
await request.send(user.id);
2929
location.reload();
3030
}
3131
: undefined

0 commit comments

Comments
 (0)