Browse Source

Display card according to elections state

Arnaud Vergnet 4 years ago
parent
commit
db96700693
1 changed files with 136 additions and 10 deletions
  1. 136
    10
      src/screens/Amicale/VoteScreen.js

+ 136
- 10
src/screens/Amicale/VoteScreen.js View File

@@ -4,6 +4,7 @@ import * as React from 'react';
4 4
 import {ScrollView, StyleSheet} from "react-native";
5 5
 import {Avatar, Card, Paragraph, withTheme} from 'react-native-paper';
6 6
 import AuthenticatedScreen from "../../components/Amicale/AuthenticatedScreen";
7
+import {stringToDate} from "../../utils/Planning";
7 8
 
8 9
 const ICON_AMICALE = require('../../../assets/amicale.png');
9 10
 
@@ -12,6 +13,30 @@ type Props = {
12 13
     theme: Object,
13 14
 }
14 15
 
16
+const FAKE_DATE = {
17
+    "date_begin": "2020-04-06 13:00",
18
+    "date_end": "2020-04-06 20:00",
19
+    "date_result_begin": "2020-04-06 20:15",
20
+    "date_result_end": "2020-04-07 12:00",
21
+};
22
+
23
+const FAKE_DATE2 = {
24
+    "date_begin": null,
25
+    "date_end": null,
26
+    "date_result_begin": null,
27
+    "date_result_end": null,
28
+};
29
+
30
+const FAKE_TEAMS = {
31
+    has_voted: false,
32
+    teams: [
33
+        {
34
+            id: 1,
35
+            name: "TEST TEAM",
36
+        },
37
+    ],
38
+};
39
+
15 40
 type State = {}
16 41
 
17 42
 class VoteScreen extends React.Component<Props, State> {
@@ -20,21 +45,88 @@ class VoteScreen extends React.Component<Props, State> {
20 45
 
21 46
     colors: Object;
22 47
 
48
+    teams: Array<Object> | null;
49
+    hasVoted: boolean;
50
+    datesString: Object;
51
+    dates: Object;
52
+
53
+    today: Date;
54
+
23 55
     constructor(props) {
24 56
         super(props);
25 57
         this.colors = props.theme.colors;
58
+        this.hasVoted = false;
59
+        this.teams = null;
60
+        this.today = new Date();
26 61
     }
27 62
 
28
-    getScreen = (data: Object) => {
29
-        console.log(data);
63
+    getScreen = (data: Array<Object>) => {
64
+        data[0] = FAKE_TEAMS;
65
+        data[1] = FAKE_DATE;
66
+
67
+        if (data[0] !== null) {
68
+            this.teams = data[0].teams;
69
+            this.hasVoted = data[0].has_voted;
70
+        }
71
+        this.datesString = data[1];
72
+        this.generateDateObject();
73
+        console.log(this.teams);
74
+        console.log(this.datesString);
75
+        console.log(this.dates);
30 76
         return (
31 77
             <ScrollView>
78
+                {
79
+                    this.isVoteAvailable()
80
+                        ? this.getContent()
81
+                        : null
82
+                }
32 83
                 {this.getTitleCard()}
33
-                {this.getVoteCard()}
34 84
             </ScrollView>
35 85
         );
36 86
     };
37 87
 
88
+    generateDateObject() {
89
+        this.dates = {
90
+            date_begin: stringToDate(this.datesString.date_begin),
91
+            date_end: stringToDate(this.datesString.date_end),
92
+            date_result_begin: stringToDate(this.datesString.date_result_begin),
93
+            date_result_end: stringToDate(this.datesString.date_result_end),
94
+        };
95
+    }
96
+
97
+    isVoteAvailable() {
98
+        return this.dates.date_begin !== null;
99
+    }
100
+
101
+    isVoteRunning() {
102
+        return this.today > this.dates.date_begin && this.today < this.dates.date_end;
103
+    }
104
+
105
+    isVoteStarted() {
106
+        return this.today > this.dates.date_begin;
107
+    }
108
+
109
+    isResultRunning() {
110
+        return this.today > this.dates.date_result_begin && this.today < this.dates.date_result_end;
111
+    }
112
+
113
+    isResultStarted() {
114
+        return this.today > this.dates.date_result_begin;
115
+    }
116
+
117
+    getContent() {
118
+        if (!this.isVoteStarted())
119
+            return this.getTeaseVoteCard();
120
+        else if (this.isVoteRunning() && !this.hasVoted)
121
+            return this.getVoteCard();
122
+        else if (!this.isResultStarted())
123
+            return this.getWaitVoteCard();
124
+        else if (this.isResultRunning())
125
+            return this.getVoteResultCard();
126
+        else
127
+            return null;
128
+    }
129
+
38 130
     getTitleCard() {
39 131
         return (
40 132
             <Card style={styles.card}>
@@ -64,12 +156,15 @@ class VoteScreen extends React.Component<Props, State> {
64 156
         );
65 157
     }
66 158
 
159
+    /**
160
+     * The user has not voted yet, and the votes are open
161
+     */
67 162
     getVoteCard() {
68 163
         return (
69 164
             <Card style={styles.card}>
70 165
                 <Card.Title
71
-                    title={"VOTE OPEN"}
72
-                    subtitle={"VALID UNTIL DATE END"}
166
+                    title={"getVoteCard"}
167
+                    subtitle={"getVoteCard"}
73 168
                 />
74 169
                 <Card.Content>
75 170
                     <Paragraph>TEAM1</Paragraph>
@@ -79,12 +174,15 @@ class VoteScreen extends React.Component<Props, State> {
79 174
         );
80 175
     }
81 176
 
177
+    /**
178
+     * Votes have ended, results can be displayed
179
+     */
82 180
     getVoteResultCard() {
83 181
         return (
84 182
             <Card style={styles.card}>
85 183
                 <Card.Title
86
-                    title={"VOTE RESULTS"}
87
-                    subtitle={"DATE END RESULTS"}
184
+                    title={"getVoteResultCard"}
185
+                    subtitle={"getVoteResultCard"}
88 186
                 />
89 187
                 <Card.Content>
90 188
                     <Paragraph>TEAM1</Paragraph>
@@ -94,8 +192,36 @@ class VoteScreen extends React.Component<Props, State> {
94 192
         );
95 193
     }
96 194
 
97
-    getEmptyVoteCard() {
195
+    /**
196
+     * Vote will open shortly
197
+     */
198
+    getTeaseVoteCard() {
199
+        return (
200
+            <Card style={styles.card}>
201
+                <Card.Title
202
+                    title={"getTeaseVoteCard"}
203
+                    subtitle={"getTeaseVoteCard"}
204
+                />
205
+                <Card.Content>
206
+                </Card.Content>
207
+            </Card>
208
+        );
209
+    }
98 210
 
211
+    /**
212
+     * User has voted, waiting for results
213
+     */
214
+    getWaitVoteCard() {
215
+        return (
216
+            <Card style={styles.card}>
217
+                <Card.Title
218
+                    title={"getWaitVoteCard"}
219
+                    subtitle={"getWaitVoteCard"}
220
+                />
221
+                <Card.Content>
222
+                </Card.Content>
223
+            </Card>
224
+        );
99 225
     }
100 226
 
101 227
     render() {
@@ -108,8 +234,8 @@ class VoteScreen extends React.Component<Props, State> {
108 234
                         mandatory: false,
109 235
                     },
110 236
                     {
111
-                        link: 'elections/dates',
112
-                        mandatory: true,
237
+                        link: 'elections/datesString',
238
+                        mandatory: false,
113 239
                     },
114 240
                 ]}
115 241
                 renderFunction={this.getScreen}

Loading…
Cancel
Save