Reload page when vote fails and move user to wait when submit vote

This commit is contained in:
Arnaud Vergnet 2020-04-07 14:38:55 +02:00
parent c70bafe273
commit 34594f82d9
2 changed files with 79 additions and 48 deletions

View file

@ -105,6 +105,10 @@ class AuthenticatedScreen extends React.Component<Props, State> {
); );
} }
reload() {
this.fetchData();
}
render() { render() {
return ( return (
this.state.loading this.state.loading

View file

@ -72,6 +72,7 @@ type Props = {
type State = { type State = {
selectedTeam: string, selectedTeam: string,
hasVoted: boolean,
voteDialogVisible: boolean, voteDialogVisible: boolean,
errorDialogVisible: boolean, errorDialogVisible: boolean,
currentError: number, currentError: number,
@ -84,6 +85,7 @@ class VoteScreen extends React.Component<Props, State> {
voteDialogVisible: false, voteDialogVisible: false,
errorDialogVisible: false, errorDialogVisible: false,
currentError: 0, currentError: 0,
hasVoted: false,
}; };
colors: Object; colors: Object;
@ -98,18 +100,58 @@ class VoteScreen extends React.Component<Props, State> {
mainFlatListData: Array<Object>; mainFlatListData: Array<Object>;
totalVotes: number; totalVotes: number;
authRef: Object;
constructor(props) { constructor(props) {
super(props); super(props);
this.colors = props.theme.colors; this.colors = props.theme.colors;
this.hasVoted = false; this.hasVoted = false;
this.today = new Date(); this.today = new Date();
this.authRef = React.createRef();
this.mainFlatListData = [ this.mainFlatListData = [
{key: 'main'}, {key: 'main'},
{key: 'info'}, {key: 'info'},
] ]
} }
reloadData = () => this.authRef.current.reload();
generateDateObject() {
this.dates = {
date_begin: stringToDate(this.datesString.date_begin),
date_end: stringToDate(this.datesString.date_end),
date_result_begin: stringToDate(this.datesString.date_result_begin),
date_result_end: stringToDate(this.datesString.date_result_end),
};
}
getDateString(date: Date, dateString: string) {
if (this.today.getDate() === date.getDate())
return getTimeOnlyString(dateString);
else
return dateString;
}
isVoteAvailable() {
return this.dates.date_begin !== null;
}
isVoteRunning() {
return this.today > this.dates.date_begin && this.today < this.dates.date_end;
}
isVoteStarted() {
return this.today > this.dates.date_begin;
}
isResultRunning() {
return this.today > this.dates.date_result_begin && this.today < this.dates.date_result_end;
}
isResultStarted() {
return this.today > this.dates.date_result_begin;
}
mainRenderItem = ({item}: Object) => { mainRenderItem = ({item}: Object) => {
if (item.key === 'info') if (item.key === 'info')
return this.getTitleCard(); return this.getTitleCard();
@ -134,7 +176,7 @@ class VoteScreen extends React.Component<Props, State> {
{/*$FlowFixMe*/} {/*$FlowFixMe*/}
<FlatList <FlatList
data={this.mainFlatListData} data={this.mainFlatListData}
extraData={this.state.selectedTeam} extraData={this.state.selectedTeam + this.state.hasVoted.toString()}
renderItem={this.mainRenderItem} renderItem={this.mainRenderItem}
/> />
<LoadingConfirmDialog <LoadingConfirmDialog
@ -154,14 +196,15 @@ class VoteScreen extends React.Component<Props, State> {
); );
}; };
onVoteDialogDismiss = () => this.setState({voteDialogVisible: false});
onErrorDialogDismiss = () => this.setState({errorDialogVisible: false});
showVoteDialog = () => this.setState({voteDialogVisible: true}); showVoteDialog = () => this.setState({voteDialogVisible: true});
showErrorDialog = (error: number) => this.setState({
errorDialogVisible: true, onVoteDialogDismiss = (voteStatus: boolean) => {
currentError: error, voteStatus = voteStatus === undefined ? false : voteStatus;
}); this.setState({
voteDialogVisible: false,
hasVoted: voteStatus,
})
};
onVoteDialogAccept = async () => { onVoteDialogAccept = async () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -170,50 +213,31 @@ class VoteScreen extends React.Component<Props, State> {
["vote"], ["vote"],
[parseInt(this.state.selectedTeam)]) [parseInt(this.state.selectedTeam)])
.then(() => { .then(() => {
this.onVoteDialogDismiss(); this.onVoteDialogDismiss(true);
resolve(); resolve();
}) })
.catch((error: number) => { .catch((error: number) => {
this.onVoteDialogDismiss(); this.onVoteDialogDismiss(false);
this.showErrorDialog(error); this.showErrorDialog(error);
resolve(); resolve();
}); });
}); });
}; };
generateDateObject() { showErrorDialog = (error: number) => this.setState({
this.dates = { errorDialogVisible: true,
date_begin: stringToDate(this.datesString.date_begin), currentError: error,
date_end: stringToDate(this.datesString.date_end), });
date_result_begin: stringToDate(this.datesString.date_result_begin),
date_result_end: stringToDate(this.datesString.date_result_end),
};
}
isVoteAvailable() { onErrorDialogDismiss = () => {
return this.dates.date_begin !== null; this.setState({errorDialogVisible: false});
} this.reloadData();
};
isVoteRunning() {
return this.today > this.dates.date_begin && this.today < this.dates.date_end;
}
isVoteStarted() {
return this.today > this.dates.date_begin;
}
isResultRunning() {
return this.today > this.dates.date_result_begin && this.today < this.dates.date_result_end;
}
isResultStarted() {
return this.today > this.dates.date_result_begin;
}
getContent() { getContent() {
if (!this.isVoteStarted()) if (!this.isVoteStarted())
return this.getTeaseVoteCard(); return this.getTeaseVoteCard();
else if (this.isVoteRunning() && !this.hasVoted) else if (this.isVoteRunning() && (!this.hasVoted && !this.state.hasVoted))
return this.getVoteCard(); return this.getVoteCard();
else if (!this.isResultStarted()) else if (!this.isResultStarted())
return this.getWaitVoteCard(); return this.getWaitVoteCard();
@ -405,14 +429,23 @@ class VoteScreen extends React.Component<Props, State> {
return ( return (
<Card style={styles.card}> <Card style={styles.card}>
<Card.Title <Card.Title
title={"VOTES HAVE ENDED"} title={this.isVoteRunning() ? "VOTE SUBMITTED" : "VOTES HAVE ENDED"}
subtitle={"WAITING FOR RESULTS"} subtitle={"WAITING FOR RESULTS"}
left={(props) => <ActivityIndicator {...props}/>} left={(props) => <ActivityIndicator {...props}/>}
/> />
<Card.Content> <Card.Content>
{
this.state.hasVoted
? <Paragraph style={{color: this.colors.success}}>
VOTE SUBMITTED. THX FOR YOUR PARTICIPATION
</Paragraph>
: null
}
{ {
this.hasVoted this.hasVoted
? <Paragraph>THX FOR THE VOTE</Paragraph> ? <Paragraph style={{color: this.colors.success}}>
THX FOR THE VOTE
</Paragraph>
: null : null
} }
{ {
@ -428,17 +461,11 @@ class VoteScreen extends React.Component<Props, State> {
); );
} }
getDateString(date: Date, dateString: string) {
if (this.today.getDate() === date.getDate())
return getTimeOnlyString(dateString);
else
return dateString;
}
render() { render() {
return ( return (
<AuthenticatedScreen <AuthenticatedScreen
{...this.props} {...this.props}
ref={this.authRef}
links={[ links={[
{ {
link: 'elections/teams', link: 'elections/teams',