Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProfileScreen.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // @flow
  2. import * as React from 'react';
  3. import {FlatList, StyleSheet, View} from "react-native";
  4. import {Avatar, Button, Card, Divider, List, withTheme} from 'react-native-paper';
  5. import AuthenticatedScreen from "../../components/Amicale/AuthenticatedScreen";
  6. import {openBrowser} from "../../utils/WebBrowser";
  7. import HeaderButton from "../../components/Custom/HeaderButton";
  8. import i18n from 'i18n-js';
  9. import LogoutDialog from "../../components/Amicale/LogoutDialog";
  10. type Props = {
  11. navigation: Object,
  12. theme: Object,
  13. }
  14. type State = {
  15. dialogVisible: boolean,
  16. }
  17. class ProfileScreen extends React.Component<Props, State> {
  18. state = {
  19. dialogVisible: false,
  20. };
  21. colors: Object;
  22. data: Object;
  23. flatListData: Array<Object>;
  24. constructor(props) {
  25. super(props);
  26. this.colors = props.theme.colors;
  27. this.flatListData = [
  28. {id: '0'},
  29. {id: '1'},
  30. {id: '2'},
  31. ]
  32. }
  33. componentDidMount() {
  34. const rightButton = this.getHeaderButtons.bind(this);
  35. this.props.navigation.setOptions({
  36. headerRight: rightButton,
  37. });
  38. }
  39. showDisconnectDialog = () => this.setState({ dialogVisible: true });
  40. hideDisconnectDialog = () => this.setState({ dialogVisible: false });
  41. getHeaderButtons() {
  42. return <HeaderButton icon={'logout'} onPress={this.showDisconnectDialog}/>;
  43. }
  44. getScreen(data: Object) {
  45. this.data = data;
  46. return (
  47. <View>
  48. <FlatList
  49. renderItem={item => this.getRenderItem(item)}
  50. keyExtractor={item => item.id}
  51. data={this.flatListData}
  52. />
  53. <LogoutDialog
  54. {...this.props}
  55. visible={this.state.dialogVisible}
  56. onDismiss={this.hideDisconnectDialog}
  57. />
  58. </View>
  59. )
  60. }
  61. getRenderItem({item}: Object): any {
  62. switch (item.id) {
  63. case '0':
  64. return this.getPersonalCard();
  65. case '1':
  66. return this.getClubCard();
  67. case '2':
  68. return this.getMembershipCar();
  69. }
  70. }
  71. getPersonalCard() {
  72. return (
  73. <Card style={styles.card}>
  74. <Card.Title
  75. title={this.data.first_name + ' ' + this.data.last_name}
  76. subtitle={this.data.email}
  77. left={(props) => <Avatar.Icon
  78. {...props}
  79. icon="account"
  80. color={this.colors.primary}
  81. style={styles.icon}
  82. />}
  83. />
  84. <Card.Content>
  85. <Divider/>
  86. <List.Section>
  87. <List.Subheader>{i18n.t("profileScreen.personalInformation")}</List.Subheader>
  88. {this.getPersonalListItem(this.data.birthday, "cake-variant")}
  89. {this.getPersonalListItem(this.data.phone, "phone")}
  90. {this.getPersonalListItem(this.data.email, "email")}
  91. {this.getPersonalListItem(this.data.branch, "school")}
  92. </List.Section>
  93. <Divider/>
  94. <Card.Actions>
  95. <Button
  96. icon="account-edit"
  97. mode="contained"
  98. onPress={() => openBrowser(this.data.link, this.colors.primary)}
  99. style={styles.editButton}>
  100. {i18n.t("profileScreen.editInformation")}
  101. </Button>
  102. </Card.Actions>
  103. </Card.Content>
  104. </Card>
  105. );
  106. }
  107. getClubCard() {
  108. return (
  109. <Card style={styles.card}>
  110. <Card.Title
  111. title={i18n.t("profileScreen.clubs")}
  112. subtitle={i18n.t("profileScreen.clubsSubtitle")}
  113. left={(props) => <Avatar.Icon
  114. {...props}
  115. icon="account-group"
  116. color={this.colors.primary}
  117. style={styles.icon}
  118. />}
  119. />
  120. <Card.Content>
  121. <Divider/>
  122. {this.getClubList(this.data.clubs)}
  123. </Card.Content>
  124. </Card>
  125. );
  126. }
  127. getMembershipCar() {
  128. return (
  129. <Card style={styles.card}>
  130. <Card.Title
  131. title={i18n.t("profileScreen.membership")}
  132. subtitle={i18n.t("profileScreen.membershipSubtitle")}
  133. left={(props) => <Avatar.Icon
  134. {...props}
  135. icon="credit-card"
  136. color={this.colors.primary}
  137. style={styles.icon}
  138. />}
  139. />
  140. <Card.Content>
  141. <List.Section>
  142. {this.getMembershipItem(this.data.validity)}
  143. </List.Section>
  144. </Card.Content>
  145. </Card>
  146. );
  147. }
  148. getClubList(list: Array<string>) {
  149. let dataset = [];
  150. for (let i = 0; i < list.length; i++) {
  151. dataset.push({name: list[i]});
  152. }
  153. return (
  154. <FlatList
  155. renderItem={({item}) =>
  156. <List.Item
  157. title={item.name}
  158. left={props => <List.Icon {...props} icon="chevron-right"/>}
  159. />
  160. }
  161. keyExtractor={item => item.name}
  162. data={dataset}
  163. />
  164. );
  165. }
  166. getMembershipItem(state: boolean) {
  167. return (
  168. <List.Item
  169. title={state ? i18n.t("profileScreen.membershipPayed") : i18n.t("profileScreen.membershipNotPayed")}
  170. left={props => <List.Icon
  171. {...props}
  172. color={state ? this.colors.success : this.colors.danger}
  173. icon={state ? 'check' : 'close'}
  174. />}
  175. />
  176. );
  177. }
  178. isFieldAvailable(field: ?string) {
  179. return field !== null;
  180. }
  181. getFieldValue(field: ?string) {
  182. return this.isFieldAvailable(field)
  183. ? field
  184. : i18n.t("profileScreen.noData");
  185. }
  186. getFieldColor(field: ?string) {
  187. return this.isFieldAvailable(field)
  188. ? this.colors.text
  189. : this.colors.textDisabled;
  190. }
  191. getPersonalListItem(field: ?string, icon: string) {
  192. return (
  193. <List.Item
  194. title={this.getFieldValue(field)}
  195. left={props => <List.Icon
  196. {...props}
  197. icon={icon}
  198. color={this.getFieldColor(field)}
  199. />}
  200. titleStyle={{color: this.getFieldColor(field)}}
  201. />
  202. );
  203. }
  204. render() {
  205. return (
  206. <AuthenticatedScreen
  207. {...this.props}
  208. link={'https://www.amicale-insat.fr/api/user/profile'}
  209. renderFunction={(data) => this.getScreen(data)}
  210. />
  211. );
  212. }
  213. }
  214. const styles = StyleSheet.create({
  215. card: {
  216. margin: 10,
  217. },
  218. icon: {
  219. backgroundColor: 'transparent'
  220. },
  221. editButton: {
  222. marginLeft: 'auto'
  223. }
  224. });
  225. export default withTheme(ProfileScreen);