Browse Source

Improved login flow

Arnaud Vergnet 4 years ago
parent
commit
fc7754588f
2 changed files with 93 additions and 29 deletions
  1. 61
    12
      src/screens/Services/ServicesScreen.js
  2. 32
    17
      src/screens/Services/ServicesSectionScreen.js

+ 61
- 12
src/screens/Services/ServicesScreen.js View File

@@ -8,8 +8,9 @@ import {withCollapsible} from "../../utils/withCollapsible";
8 8
 import {Collapsible} from "react-navigation-collapsible";
9 9
 import {CommonActions} from "@react-navigation/native";
10 10
 import {Animated, View} from "react-native";
11
-import {Avatar, Card, Divider, List, TouchableRipple, withTheme} from "react-native-paper";
11
+import {Avatar, Button, Card, Divider, List, Title, TouchableRipple, withTheme} from "react-native-paper";
12 12
 import type {CustomTheme} from "../../managers/ThemeManager";
13
+import ConnectionManager from "../../managers/ConnectionManager";
13 14
 
14 15
 type Props = {
15 16
     navigation: Object,
@@ -29,14 +30,19 @@ const AMICALE_IMAGE = require("../../../assets/amicale.png");
29 30
 const EE_IMAGE = "https://etud.insa-toulouse.fr/~eeinsat/wp-content/uploads/2019/09/logo-blanc.png";
30 31
 const TUTORINSA_IMAGE = "https://www.etud.insa-toulouse.fr/~tutorinsa/public/images/logo-gray.png";
31 32
 
32
-type listItem = {
33
+export type listItem = {
33 34
     title: string,
34 35
     description: string,
35 36
     image: string | number,
37
+    shouldLogin: boolean,
36 38
     content: cardList,
37 39
 }
38 40
 
39
-class ServicesScreen extends React.Component<Props> {
41
+type State = {
42
+    isLoggedIn: boolean,
43
+}
44
+
45
+class ServicesScreen extends React.Component<Props, State> {
40 46
 
41 47
     amicaleDataset: cardList;
42 48
     studentsDataset: cardList;
@@ -148,29 +154,40 @@ class ServicesScreen extends React.Component<Props> {
148 154
                 title: "AMICALE",
149 155
                 description: "LOGIN",
150 156
                 image: AMICALE_IMAGE,
157
+                shouldLogin: true,
151 158
                 content: this.amicaleDataset
152 159
             },
153 160
             {
154 161
                 title: "STUDENTS",
155 162
                 description: "SERVICES OFFERED BY STUDENTS",
156 163
                 image: 'account-group',
164
+                shouldLogin: false,
157 165
                 content: this.studentsDataset
158 166
             },
159 167
             {
160 168
                 title: "INSA",
161 169
                 description: "SERVICES OFFERED BY INSA",
162 170
                 image: 'school',
171
+                shouldLogin: false,
163 172
                 content: this.insaDataset
164 173
             },
165
-        ]
174
+        ];
175
+        this.state = {
176
+            isLoggedIn: ConnectionManager.getInstance().isLoggedIn()
177
+        }
166 178
     }
167 179
 
168 180
     componentDidMount() {
169
-        this.props.navigation.addListener('focus', this.handleNavigationParams);
181
+        this.props.navigation.addListener('focus', this.onFocus);
182
+
183
+    }
170 184
 
185
+    onFocus = () => {
186
+        this.handleNavigationParams();
187
+        this.setState({isLoggedIn: ConnectionManager.getInstance().isLoggedIn()})
171 188
     }
172 189
 
173
-    handleNavigationParams = () => {
190
+    handleNavigationParams() {
174 191
         if (this.props.route.params != null) {
175 192
             if (this.props.route.params.nextScreen != null) {
176 193
                 this.props.navigation.navigate(this.props.route.params.nextScreen);
@@ -198,25 +215,57 @@ class ServicesScreen extends React.Component<Props> {
198 215
             />
199 216
     }
200 217
 
218
+    getLoginMessage() {
219
+        return (
220
+            <View>
221
+                <Title style={{
222
+                    marginLeft: 'auto',
223
+                    marginRight: 'auto',
224
+                }}>
225
+                    NOT LOGGED IN
226
+                </Title>
227
+                <Button
228
+                    icon="login"
229
+                    mode="contained"
230
+                    onPress={() => this.props.navigation.navigate("login")}
231
+                    style={{
232
+                        marginLeft: 'auto',
233
+                        marginRight: 'auto',
234
+                    }}>
235
+                    LOGIN
236
+                </Button>
237
+            </View>
238
+        )
239
+    }
240
+
201 241
     renderItem = ({item}: { item: listItem }) => {
242
+        const shouldShowLogin = !this.state.isLoggedIn && item.shouldLogin;
202 243
         return (
203 244
             <TouchableRipple
204 245
                 style={{
205 246
                     margin: 5,
206 247
                     marginBottom: 20,
207 248
                 }}
208
-                onPress={() => this.props.navigation.navigate("services-section", {data: item})}
249
+                onPress={shouldShowLogin
250
+                    ? undefined
251
+                    : () => this.props.navigation.navigate("services-section", {data: item})}
209 252
             >
210 253
                 <View>
211 254
                     <Card.Title
212 255
                         title={item.title}
213 256
                         left={(props) => this.getAvatar(props, item.image)}
214
-                        right={(props) => <List.Icon {...props} icon="chevron-right"/>}
215
-                    />
216
-                    <CardList
217
-                        dataset={item.content}
218
-                        isHorizontal={true}
257
+                        right={shouldShowLogin
258
+                            ? undefined
259
+                            : (props) => <List.Icon {...props} icon="chevron-right"/>}
219 260
                     />
261
+                    {
262
+                        shouldShowLogin
263
+                            ? this.getLoginMessage()
264
+                            : <CardList
265
+                                dataset={item.content}
266
+                                isHorizontal={true}
267
+                            />
268
+                    }
220 269
                 </View>
221 270
             </TouchableRipple>
222 271
 

+ 32
- 17
src/screens/Services/ServicesSectionScreen.js View File

@@ -1,12 +1,15 @@
1 1
 // @flow
2 2
 
3 3
 import * as React from 'react';
4
-import type {cardList} from "../../components/Lists/CardList/CardList";
5 4
 import CardList from "../../components/Lists/CardList/CardList";
6 5
 import CustomTabBar from "../../components/Tabbar/CustomTabBar";
7 6
 import {withCollapsible} from "../../utils/withCollapsible";
8 7
 import {Collapsible} from "react-navigation-collapsible";
9 8
 import {CommonActions} from "@react-navigation/native";
9
+import ConnectionManager from "../../managers/ConnectionManager";
10
+import type {listItem} from "./ServicesScreen";
11
+import ErrorView from "../../components/Screens/ErrorView";
12
+import {ERROR_TYPE} from "../../utils/WebData";
10 13
 
11 14
 type Props = {
12 15
     navigation: Object,
@@ -14,20 +17,29 @@ type Props = {
14 17
     collapsibleStack: Collapsible,
15 18
 }
16 19
 
17
-type listItem = {
18
-    title: string,
19
-    description: string,
20
-    image: string | number,
21
-    content: cardList,
20
+type State = {
21
+    isLoggedIn: boolean,
22 22
 }
23 23
 
24
-class ServicesSectionScreen extends React.Component<Props> {
24
+class ServicesSectionScreen extends React.Component<Props, State> {
25 25
 
26 26
     finalDataset: listItem;
27 27
 
28 28
     constructor(props) {
29 29
         super(props);
30 30
         this.handleNavigationParams();
31
+        this.state = {
32
+            isLoggedIn: ConnectionManager.getInstance().isLoggedIn(),
33
+        }
34
+    }
35
+
36
+    componentDidMount() {
37
+        this.props.navigation.addListener('focus', this.onFocus);
38
+
39
+    }
40
+
41
+    onFocus = () => {
42
+        this.setState({isLoggedIn: ConnectionManager.getInstance().isLoggedIn()})
31 43
     }
32 44
 
33 45
     handleNavigationParams() {
@@ -45,16 +57,19 @@ class ServicesSectionScreen extends React.Component<Props> {
45 57
 
46 58
     render() {
47 59
         const {containerPaddingTop, scrollIndicatorInsetTop, onScroll} = this.props.collapsibleStack;
48
-        return <CardList
49
-            dataset={this.finalDataset.content}
50
-            isHorizontal={false}
51
-            onScroll={onScroll}
52
-            contentContainerStyle={{
53
-                paddingTop: containerPaddingTop,
54
-                paddingBottom: CustomTabBar.TAB_BAR_HEIGHT + 20
55
-            }}
56
-            scrollIndicatorInsets={{top: scrollIndicatorInsetTop}}
57
-        />
60
+        if (!this.state.isLoggedIn && this.finalDataset.shouldLogin)
61
+            return <ErrorView {...this.props} errorCode={ERROR_TYPE.BAD_TOKEN}/>;
62
+        else
63
+            return <CardList
64
+                dataset={this.finalDataset.content}
65
+                isHorizontal={false}
66
+                onScroll={onScroll}
67
+                contentContainerStyle={{
68
+                    paddingTop: containerPaddingTop,
69
+                    paddingBottom: CustomTabBar.TAB_BAR_HEIGHT + 20
70
+                }}
71
+                scrollIndicatorInsets={{top: scrollIndicatorInsetTop}}
72
+            />
58 73
     }
59 74
 }
60 75
 

Loading…
Cancel
Save