import React, { useState, useEffect } from "react"
import { usersForVoting } from "../../graphql/queries/usersForVoting"
import { useQuery, useMutation } from "@apollo/react-hooks"
import { Grid, Message, Button, Progress } from "semantic-ui-react"
import { Prompt } from "react-router-dom"
import Loading from "../common/Loading"
import Error from "../common/Error"
import VotingGridRow from "./VotingGridRow"
import { addVotes } from "../../graphql/mutations/addVotes"
const Voting = ({
user,
handleSuccess,
handleError,
votingEnds,
votedSoFar,
eligibleVoters,
}) => {
const { data, loading, error } = useQuery(usersForVoting)
const [votes, setVotes] = useState({})
const [voteCount, setVoteCount] = useState(0)
const [suggestedArrays, setSuggestedArrays] = useState(null)
const [addVotesMutation] = useMutation(addVotes, {
onError: handleError,
onCompleted: () => handleSuccess("Votes successfully recorded."),
refetchQueries: [
{
query: usersForVoting,
},
],
})
const handleSubmit = async () => {
await addVotesMutation({
variables: {
votes: Object.keys(votes).map(key => ({
discord_id: key,
score: votes[key],
})),
},
})
}
useEffect(() => {
if (loading || error) return
const sameRegionSuggested = []
const otherRegionSuggested = []
data.usersForVoting.suggested.forEach(suggested => {
if (suggested.plus_region === user.plus.plus_region) {
sameRegionSuggested.push(suggested)
} else {
otherRegionSuggested.push(suggested)
}
})
setSuggestedArrays({
sameRegion: sameRegionSuggested,
otherRegion: otherRegionSuggested,
})
if (data.usersForVoting.votes) {
const voteObj = {}
data.usersForVoting.votes.forEach(
vote => (voteObj[vote.discord_id] = vote.score)
)
setVotes(voteObj)
setVoteCount(data.usersForVoting.votes.length)
}
}, [loading, error, data, user])
if (error) return
if (loading || !suggestedArrays) return
const date = new Date()
if (votingEnds < date.getTime())
return (
Voting for the month is over
Results will be posted later
)
const hoursLeft = Math.ceil((votingEnds - date.getTime()) / (1000 * 60 * 60))
const alreadyVoted = data.usersForVoting.votes.length > 0
return (
<>
0 &&
voteCount <
data.usersForVoting.users.length +
data.usersForVoting.suggested.length
}
message="Are you sure you want to leave? Vote form won't be saved."
/>
{user.plus.plus_region === "EU" ? "European" : "American"} players
{data.usersForVoting.users.map(userForVoting => {
if (userForVoting.plus.plus_region !== user.plus.plus_region)
return null
return (
setVoteCount(voteCount + 1)}
/>
)
})}
{suggestedArrays.sameRegion.length > 0 && (
<>
{user.plus.plus_region === "EU" ? "European" : "American"} players
(suggested)
{suggestedArrays.sameRegion.map(suggestion => {
return (
setVoteCount(voteCount + 1)}
/>
)
})}
>
)}
{user.plus.plus_region === "NA" ? "European" : "American"} players
{data.usersForVoting.users.map(userForVoting => {
if (userForVoting.plus.plus_region === user.plus.plus_region)
return null
return (
setVoteCount(voteCount + 1)}
/>
)
})}
{suggestedArrays.otherRegion.length > 0 && (
<>
{user.plus.plus_region === "NA" ? "European" : "American"} players
(suggested)
{suggestedArrays.otherRegion.map(suggestion => {
return (
setVoteCount(voteCount + 1)}
/>
)
})}
>
)}
>
)
}
export default Voting