|
@@ -15,7 +15,6 @@ const LIST_ITEM_HEIGHT = 70;
|
15
|
15
|
export type PlanexGroupType = {
|
16
|
16
|
name: string,
|
17
|
17
|
id: number,
|
18
|
|
- isFav: boolean,
|
19
|
18
|
};
|
20
|
19
|
|
21
|
20
|
export type PlanexGroupCategoryType = {
|
|
@@ -43,46 +42,11 @@ function sortName(
|
43
|
42
|
}
|
44
|
43
|
|
45
|
44
|
const GROUPS_URL = 'http://planex.insa-toulouse.fr/wsAdeGrp.php?projectId=1';
|
46
|
|
-const REPLACE_REGEX = /_/g;
|
47
|
45
|
|
48
|
46
|
/**
|
49
|
47
|
* Class defining planex group selection screen.
|
50
|
48
|
*/
|
51
|
49
|
class GroupSelectionScreen extends React.Component<PropsType, StateType> {
|
52
|
|
- /**
|
53
|
|
- * Removes the given group from the given array
|
54
|
|
- *
|
55
|
|
- * @param favorites The array containing favorites groups
|
56
|
|
- * @param group The group to remove from the array
|
57
|
|
- */
|
58
|
|
- static removeGroupFromFavorites(
|
59
|
|
- favorites: Array<PlanexGroupType>,
|
60
|
|
- group: PlanexGroupType,
|
61
|
|
- ) {
|
62
|
|
- for (let i = 0; i < favorites.length; i += 1) {
|
63
|
|
- if (group.id === favorites[i].id) {
|
64
|
|
- favorites.splice(i, 1);
|
65
|
|
- break;
|
66
|
|
- }
|
67
|
|
- }
|
68
|
|
- }
|
69
|
|
-
|
70
|
|
- /**
|
71
|
|
- * Adds the given group to the given array
|
72
|
|
- *
|
73
|
|
- * @param favorites The array containing favorites groups
|
74
|
|
- * @param group The group to add to the array
|
75
|
|
- */
|
76
|
|
- static addGroupToFavorites(
|
77
|
|
- favorites: Array<PlanexGroupType>,
|
78
|
|
- group: PlanexGroupType,
|
79
|
|
- ) {
|
80
|
|
- const favGroup = {...group};
|
81
|
|
- favGroup.isFav = true;
|
82
|
|
- favorites.push(favGroup);
|
83
|
|
- favorites.sort(sortName);
|
84
|
|
- }
|
85
|
|
-
|
86
|
50
|
constructor(props: PropsType) {
|
87
|
51
|
super(props);
|
88
|
52
|
this.state = {
|
|
@@ -130,14 +94,17 @@ class GroupSelectionScreen extends React.Component<PropsType, StateType> {
|
130
|
94
|
*/
|
131
|
95
|
getRenderItem = ({item}: {item: PlanexGroupCategoryType}): React.Node => {
|
132
|
96
|
const {currentSearchString, favoriteGroups} = this.state;
|
133
|
|
- if (this.shouldDisplayAccordion(item)) {
|
|
97
|
+ if (
|
|
98
|
+ this.shouldDisplayAccordion(item) ||
|
|
99
|
+ (item.id === 0 && item.content.length === 0)
|
|
100
|
+ ) {
|
134
|
101
|
return (
|
135
|
102
|
<GroupListAccordion
|
136
|
103
|
item={item}
|
|
104
|
+ favorites={[...favoriteGroups]}
|
137
|
105
|
onGroupPress={this.onListItemPress}
|
138
|
106
|
onFavoritePress={this.onListFavoritePress}
|
139
|
107
|
currentSearchString={currentSearchString}
|
140
|
|
- favoriteNumber={favoriteGroups.length}
|
141
|
108
|
height={LIST_ITEM_HEIGHT}
|
142
|
109
|
/>
|
143
|
110
|
);
|
|
@@ -146,21 +113,6 @@ class GroupSelectionScreen extends React.Component<PropsType, StateType> {
|
146
|
113
|
};
|
147
|
114
|
|
148
|
115
|
/**
|
149
|
|
- * Replaces underscore by spaces and sets the favorite state of every group in the given category
|
150
|
|
- *
|
151
|
|
- * @param groups The groups to format
|
152
|
|
- * @return {Array<PlanexGroupType>}
|
153
|
|
- */
|
154
|
|
- getFormattedGroups(groups: Array<PlanexGroupType>): Array<PlanexGroupType> {
|
155
|
|
- return groups.map((group: PlanexGroupType): PlanexGroupType => {
|
156
|
|
- const newGroup = {...group};
|
157
|
|
- newGroup.name = group.name.replace(REPLACE_REGEX, ' ');
|
158
|
|
- newGroup.isFav = this.isGroupInFavorites(group);
|
159
|
|
- return newGroup;
|
160
|
|
- });
|
161
|
|
- }
|
162
|
|
-
|
163
|
|
- /**
|
164
|
116
|
* Creates the dataset to be used in the FlatList
|
165
|
117
|
*
|
166
|
118
|
* @param fetchedData
|
|
@@ -231,16 +183,8 @@ class GroupSelectionScreen extends React.Component<PropsType, StateType> {
|
231
|
183
|
* @param group The group to add/remove to favorites
|
232
|
184
|
*/
|
233
|
185
|
updateGroupFavorites(group: PlanexGroupType) {
|
234
|
|
- const {favoriteGroups} = this.state;
|
235
|
|
- const newFavorites = [...favoriteGroups];
|
236
|
|
- if (this.isGroupInFavorites(group))
|
237
|
|
- GroupSelectionScreen.removeGroupFromFavorites(newFavorites, group);
|
238
|
|
- else GroupSelectionScreen.addGroupToFavorites(newFavorites, group);
|
239
|
|
- this.setState({favoriteGroups: newFavorites});
|
240
|
|
- AsyncStorageManager.set(
|
241
|
|
- AsyncStorageManager.PREFERENCES.planexFavoriteGroups.key,
|
242
|
|
- newFavorites,
|
243
|
|
- );
|
|
186
|
+ if (this.isGroupInFavorites(group)) this.removeGroupFromFavorites(group);
|
|
187
|
+ else this.addGroupToFavorites(group);
|
244
|
188
|
}
|
245
|
189
|
|
246
|
190
|
/**
|
|
@@ -276,9 +220,7 @@ class GroupSelectionScreen extends React.Component<PropsType, StateType> {
|
276
|
220
|
// eslint-disable-next-line flowtype/no-weak-types
|
277
|
221
|
(Object.values(fetchedData): Array<any>).forEach(
|
278
|
222
|
(category: PlanexGroupCategoryType) => {
|
279
|
|
- const newCat = {...category};
|
280
|
|
- newCat.content = this.getFormattedGroups(category.content);
|
281
|
|
- data.push(newCat);
|
|
223
|
+ data.push(category);
|
282
|
224
|
},
|
283
|
225
|
);
|
284
|
226
|
data.sort(sortName);
|
|
@@ -290,6 +232,50 @@ class GroupSelectionScreen extends React.Component<PropsType, StateType> {
|
290
|
232
|
return data;
|
291
|
233
|
}
|
292
|
234
|
|
|
235
|
+ /**
|
|
236
|
+ * Removes the given group from the favorites
|
|
237
|
+ *
|
|
238
|
+ * @param group The group to remove from the array
|
|
239
|
+ */
|
|
240
|
+ removeGroupFromFavorites(group: PlanexGroupType) {
|
|
241
|
+ this.setState((prevState: StateType): {
|
|
242
|
+ favoriteGroups: Array<PlanexGroupType>,
|
|
243
|
+ } => {
|
|
244
|
+ const {favoriteGroups} = prevState;
|
|
245
|
+ for (let i = 0; i < favoriteGroups.length; i += 1) {
|
|
246
|
+ if (group.id === favoriteGroups[i].id) {
|
|
247
|
+ favoriteGroups.splice(i, 1);
|
|
248
|
+ break;
|
|
249
|
+ }
|
|
250
|
+ }
|
|
251
|
+ AsyncStorageManager.set(
|
|
252
|
+ AsyncStorageManager.PREFERENCES.planexFavoriteGroups.key,
|
|
253
|
+ favoriteGroups,
|
|
254
|
+ );
|
|
255
|
+ return {favoriteGroups};
|
|
256
|
+ });
|
|
257
|
+ }
|
|
258
|
+
|
|
259
|
+ /**
|
|
260
|
+ * Adds the given group to favorites
|
|
261
|
+ *
|
|
262
|
+ * @param group The group to add to the array
|
|
263
|
+ */
|
|
264
|
+ addGroupToFavorites(group: PlanexGroupType) {
|
|
265
|
+ this.setState((prevState: StateType): {
|
|
266
|
+ favoriteGroups: Array<PlanexGroupType>,
|
|
267
|
+ } => {
|
|
268
|
+ const {favoriteGroups} = prevState;
|
|
269
|
+ favoriteGroups.push(group);
|
|
270
|
+ favoriteGroups.sort(sortName);
|
|
271
|
+ AsyncStorageManager.set(
|
|
272
|
+ AsyncStorageManager.PREFERENCES.planexFavoriteGroups.key,
|
|
273
|
+ favoriteGroups,
|
|
274
|
+ );
|
|
275
|
+ return {favoriteGroups};
|
|
276
|
+ });
|
|
277
|
+ }
|
|
278
|
+
|
293
|
279
|
render(): React.Node {
|
294
|
280
|
const {props, state} = this;
|
295
|
281
|
return (
|