Skip to content

Commit

Permalink
Refactor booth
Browse files Browse the repository at this point in the history
  • Loading branch information
mjal committed Jan 15, 2024
1 parent 48b3673 commit f52af28
Showing 1 changed file with 63 additions and 50 deletions.
113 changes: 63 additions & 50 deletions src/components/ElectionBooth.res
Original file line number Diff line number Diff line change
Expand Up @@ -12,69 +12,82 @@ module Choice = {
}
}

module BoothAfterVote = {
@react.component
let make = (~electionId) => {
let (state, dispatch) = StateContext.use()
<>
<Text style={S.flatten([S.title, Style.viewStyle(~margin=30.0->Style.dp, ())])}>
{"Merci pour votre vote"->React.string}
</Text>
<S.Button
title="Retour à l'élection"
onPress={_ => { dispatch(Navigate(list{"elections", electionId})) }}
/>
</>
}
}

module Booth = {
@react.component
let make = (~election, ~electionId, ~account) => {
let (state, dispatch) = StateContext.use()
let {t} = ReactI18next.useTranslation()
let (choice, setChoice) = React.useState(_ => None)
let question = switch Election.description(election) {
| "" => t(. "election.new.question")
| question => question
}
<>
<View style=S.questionBox>
<S.Section title=question />
{Array.mapWithIndex(Election.choices(election), (i, choiceName) => {
let selected = choice == Some(i)

<Choice
name=choiceName selected key={Int.toString(i)} onSelect={_ => setChoice(_ => Some(i))}
/>
})->React.array}
</View>
<S.Button
title="Voter"
onPress={_ => {
let nbChoices = Array.length(Election.choices(election))
Core.Ballot.vote(~electionId, ~voter=account, ~choice, ~nbChoices)(state, dispatch)
}}
/>
</>
}
}

@react.component
let make = (~election: Election.t, ~electionId) => {
let (state, dispatch) = StateContext.use()
let {t} = ReactI18next.useTranslation()
let (choice, setChoice) = React.useState(_ => None)
let (_voteAgain, setVoteAgain) = React.useState(_ => false)
let (state, _dispatch) = StateContext.use()

let oAccount = Array.getBy(state.accounts, (account) => {
Array.getBy(election.voterIds, userId => userId == account.userId)
->Option.isSome
})

let question = switch Election.description(election) {
| "" => t(. "election.new.question")
| question => question
let oBallot = switch oAccount {
| None => None
| Some(account) => Array.getBy(state.ballots, (ballot) => {
ballot.electionId == electionId && ballot.voterId == account.userId
})
}

<>
<ElectionHeader election />

{ switch Array.getBy(state.accounts, (account) => {
Array.getBy(election.voterIds, userId => userId == account.userId)->Option.isSome
}) {
{ switch oAccount {
| None =>
<Text style={S.flatten([S.title, Style.viewStyle(~margin=30.0->Style.dp, ())])}>
{"Vous n'avez pas de clés de vote"->React.string}
</Text>
| Some(account) =>
switch Array.getBy(state.ballots, (ballot) => {
ballot.electionId == electionId && ballot.voterId == account.userId
}) {
| None =>
<>
<View style=S.questionBox>
<S.Section title=question />
{Array.mapWithIndex(Election.choices(election), (i, choiceName) => {
let selected = choice == Some(i)

<Choice
name=choiceName selected key={Int.toString(i)} onSelect={_ => setChoice(_ => Some(i))}
/>
})->React.array}
</View>
<S.Button
title="Voter"
onPress={_ => {
let nbChoices = Array.length(Election.choices(election))
Core.Ballot.vote(~electionId, ~voter=account, ~choice, ~nbChoices)(state, dispatch)
setVoteAgain(_ => false)
}}
/>
</>
| Some(_ballot) =>
<>
<Text style={S.flatten([S.title, Style.viewStyle(~margin=30.0->Style.dp, ())])}>
{"Merci pour votre vote"->React.string}
</Text>
<S.Button
title="Retour à l'élection"
onPress={_ => { dispatch(Navigate(list{"elections", electionId})) }}
/>
<S.Button
title="Changer mon vote"
onPress={_ => {
setVoteAgain(_ => true)
}}
/>
</>
switch oBallot {
| None => <Booth election electionId account />
| Some(_ballot) => <BoothAfterVote electionId />
}
} }
</>
Expand Down

0 comments on commit f52af28

Please sign in to comment.