Skip to content

Commit

Permalink
[front] request matchボタンからmatch をrequest する (#286)
Browse files Browse the repository at this point in the history
* [frontend] request match ボタンから request 出来る

* [front] denyされた後に listener を解除した
  • Loading branch information
kotto5 authored Feb 26, 2024
1 parent 65f4404 commit 9a1d8c0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
5 changes: 5 additions & 0 deletions frontend/app/lib/hooks/useRequestMatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ export const useRequestMatch = (userId: number) => {
const [isRequestingMatch, setIsRequestingMatch] = useState(false);
const [sendRequestPending, setSendRequestPending] = useState(false);

const handleDenyMatchRequest = useCallback(() => {
setIsRequestingMatch(false);
socket.off("denied-match-request", handleDenyMatchRequest);
}, [userId]);
const requestMatch = useCallback(async () => {
setSendRequestPending(true);
socket.on("denied-match-request", handleDenyMatchRequest);
await socket.emit("request-match", { requestedUserId: userId });
setIsRequestingMatch(true);
setSendRequestPending(false);
Expand Down
21 changes: 16 additions & 5 deletions frontend/app/ui/user/match-request-button.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
"use client";
import { Button } from "@/components/ui/button";
import { useFormState } from "react-dom";
import { useRequestMatch } from "@/app/lib/hooks/useRequestMatch";

export default function MatchRequestButton({ id }: { id: number }) {
// TODO: Implement this
const [code, action] = useFormState(
() => console.log("Match Request to ", id),
undefined,
const {
sendRequestPending,
isRequestingMatch,
requestMatch,
cancelRequestMatch,
} = useRequestMatch(id);

return isRequestingMatch ? (
<Button onClick={cancelRequestMatch} disabled={sendRequestPending}>
Cancel Request
</Button>
) : (
<Button onClick={requestMatch} disabled={sendRequestPending}>
Request Match
</Button>
);
return <Button onClick={action}>Request Match</Button>;
}

0 comments on commit 9a1d8c0

Please sign in to comment.