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.

DashboardEditScreen.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // @flow
  2. import * as React from 'react';
  3. import {StackNavigationProp} from '@react-navigation/stack';
  4. import {Button, Card, Paragraph, withTheme} from 'react-native-paper';
  5. import {FlatList} from 'react-native';
  6. import {View} from 'react-native-animatable';
  7. import i18n from 'i18n-js';
  8. import type {
  9. ServiceCategoryType,
  10. ServiceItemType,
  11. } from '../../../managers/ServicesManager';
  12. import DashboardManager from '../../../managers/DashboardManager';
  13. import DashboardItem from '../../../components/Home/EventDashboardItem';
  14. import DashboardEditAccordion from '../../../components/Lists/DashboardEdit/DashboardEditAccordion';
  15. import DashboardEditPreviewItem from '../../../components/Lists/DashboardEdit/DashboardEditPreviewItem';
  16. import AsyncStorageManager from '../../../managers/AsyncStorageManager';
  17. import CollapsibleFlatList from '../../../components/Collapsible/CollapsibleFlatList';
  18. type PropsType = {
  19. navigation: StackNavigationProp,
  20. };
  21. type StateType = {
  22. currentDashboard: Array<ServiceItemType | null>,
  23. currentDashboardIdList: Array<string>,
  24. activeItem: number,
  25. };
  26. /**
  27. * Class defining the Settings screen. This screen shows controls to modify app preferences.
  28. */
  29. class DashboardEditScreen extends React.Component<PropsType, StateType> {
  30. content: Array<ServiceCategoryType>;
  31. initialDashboard: Array<ServiceItemType | null>;
  32. initialDashboardIdList: Array<string>;
  33. constructor(props: PropsType) {
  34. super(props);
  35. const dashboardManager = new DashboardManager(props.navigation);
  36. this.initialDashboardIdList = AsyncStorageManager.getObject(
  37. AsyncStorageManager.PREFERENCES.dashboardItems.key,
  38. );
  39. this.initialDashboard = dashboardManager.getCurrentDashboard();
  40. this.state = {
  41. currentDashboard: [...this.initialDashboard],
  42. currentDashboardIdList: [...this.initialDashboardIdList],
  43. activeItem: 0,
  44. };
  45. this.content = dashboardManager.getCategories();
  46. }
  47. getDashboardRowRenderItem = ({
  48. item,
  49. index,
  50. }: {
  51. item: DashboardItem,
  52. index: number,
  53. }): React.Node => {
  54. const {activeItem} = this.state;
  55. return (
  56. <DashboardEditPreviewItem
  57. image={item.image}
  58. onPress={() => {
  59. this.setState({activeItem: index});
  60. }}
  61. isActive={activeItem === index}
  62. />
  63. );
  64. };
  65. getDashboard(content: Array<DashboardItem>): React.Node {
  66. return (
  67. <FlatList
  68. data={content}
  69. extraData={this.state}
  70. renderItem={this.getDashboardRowRenderItem}
  71. horizontal
  72. contentContainerStyle={{
  73. marginLeft: 'auto',
  74. marginRight: 'auto',
  75. marginTop: 5,
  76. }}
  77. />
  78. );
  79. }
  80. getRenderItem = ({item}: {item: ServiceCategoryType}): React.Node => {
  81. const {currentDashboardIdList} = this.state;
  82. return (
  83. <DashboardEditAccordion
  84. item={item}
  85. onPress={this.updateDashboard}
  86. activeDashboard={currentDashboardIdList}
  87. />
  88. );
  89. };
  90. getListHeader(): React.Node {
  91. const {currentDashboard} = this.state;
  92. return (
  93. <Card style={{margin: 5}}>
  94. <Card.Content>
  95. <View style={{padding: 5}}>
  96. <Button
  97. mode="contained"
  98. onPress={this.undoDashboard}
  99. style={{
  100. marginLeft: 'auto',
  101. marginRight: 'auto',
  102. marginBottom: 10,
  103. }}>
  104. {i18n.t('screens.settings.dashboardEdit.undo')}
  105. </Button>
  106. <View style={{height: 50}}>
  107. {this.getDashboard(currentDashboard)}
  108. </View>
  109. </View>
  110. <Paragraph style={{textAlign: 'center'}}>
  111. {i18n.t('screens.settings.dashboardEdit.message')}
  112. </Paragraph>
  113. </Card.Content>
  114. </Card>
  115. );
  116. }
  117. updateDashboard = (service: ServiceItemType) => {
  118. const {currentDashboard, currentDashboardIdList, activeItem} = this.state;
  119. currentDashboard[activeItem] = service;
  120. currentDashboardIdList[activeItem] = service.key;
  121. this.setState({
  122. currentDashboard,
  123. currentDashboardIdList,
  124. });
  125. AsyncStorageManager.set(
  126. AsyncStorageManager.PREFERENCES.dashboardItems.key,
  127. currentDashboardIdList,
  128. );
  129. };
  130. undoDashboard = () => {
  131. this.setState({
  132. currentDashboard: [...this.initialDashboard],
  133. currentDashboardIdList: [...this.initialDashboardIdList],
  134. });
  135. AsyncStorageManager.set(
  136. AsyncStorageManager.PREFERENCES.dashboardItems.key,
  137. this.initialDashboardIdList,
  138. );
  139. };
  140. render(): React.Node {
  141. return (
  142. <CollapsibleFlatList
  143. data={this.content}
  144. renderItem={this.getRenderItem}
  145. ListHeaderComponent={this.getListHeader()}
  146. style={{}}
  147. />
  148. );
  149. }
  150. }
  151. export default withTheme(DashboardEditScreen);