mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-04-21 14:42:03 -05:00
86 lines
1.8 KiB
TypeScript
86 lines
1.8 KiB
TypeScript
import { Button, IconButton } from "@chakra-ui/button";
|
|
import { Flex } from "@chakra-ui/layout";
|
|
import { useState } from "react";
|
|
import { FiEdit } from "react-icons/fi";
|
|
import { PlusVotingButton } from "./PlusVotingButton";
|
|
|
|
export function ChangeVoteButtons({
|
|
score,
|
|
isSameRegion,
|
|
editVote,
|
|
isLoadingMutation,
|
|
}: {
|
|
score: number;
|
|
isSameRegion: boolean;
|
|
editVote: (score: number) => void;
|
|
isLoadingMutation: boolean;
|
|
}) {
|
|
const [editing, setEditing] = useState(false);
|
|
const [currentScore, setCurrentScore] = useState(score);
|
|
|
|
const getNextScore = () => {
|
|
if (!isSameRegion) {
|
|
if (currentScore === 1) return -1;
|
|
return 1;
|
|
}
|
|
|
|
if (currentScore === -2) return -1;
|
|
if (currentScore === -1) return 1;
|
|
if (currentScore === 1) return 2;
|
|
|
|
return -2;
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<Flex align="center">
|
|
<PlusVotingButton
|
|
number={currentScore}
|
|
onClick={() => setCurrentScore(getNextScore())}
|
|
disabled={!editing}
|
|
isSmall
|
|
/>
|
|
</Flex>
|
|
{editing ? (
|
|
<Button
|
|
size="sm"
|
|
mr={2}
|
|
my="auto"
|
|
onClick={() => {
|
|
editVote(currentScore);
|
|
setEditing(false);
|
|
}}
|
|
disabled={isLoadingMutation}
|
|
>
|
|
Save
|
|
</Button>
|
|
) : (
|
|
<IconButton
|
|
aria-label="Edit vote"
|
|
icon={<FiEdit />}
|
|
colorScheme="gray"
|
|
variant="ghost"
|
|
onClick={() => setEditing(!editing)}
|
|
my="auto"
|
|
/>
|
|
)}
|
|
{editing ? (
|
|
<Button
|
|
colorScheme="red"
|
|
onClick={() => {
|
|
setEditing(false);
|
|
setCurrentScore(score);
|
|
}}
|
|
size="sm"
|
|
ml={2}
|
|
my="auto"
|
|
>
|
|
Cancel
|
|
</Button>
|
|
) : (
|
|
<div />
|
|
)}
|
|
</>
|
|
);
|
|
}
|