Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
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 5.4KB

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