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.

LoadingConfirmDialog.tsx 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import * as React from 'react';
  20. import {
  21. ActivityIndicator,
  22. Button,
  23. Dialog,
  24. Paragraph,
  25. Portal,
  26. } from 'react-native-paper';
  27. import i18n from 'i18n-js';
  28. import { StyleSheet } from 'react-native';
  29. type PropsType = {
  30. visible: boolean;
  31. onDismiss?: () => void;
  32. onAccept?: () => Promise<void>; // async function to be executed
  33. title?: string;
  34. titleLoading?: string;
  35. message?: string;
  36. startLoading?: boolean;
  37. };
  38. type StateType = {
  39. loading: boolean;
  40. };
  41. const styles = StyleSheet.create({
  42. button: {
  43. marginRight: 10,
  44. },
  45. });
  46. export default class LoadingConfirmDialog extends React.PureComponent<
  47. PropsType,
  48. StateType
  49. > {
  50. static defaultProps = {
  51. onDismiss: () => {},
  52. onAccept: (): Promise<void> => {
  53. return Promise.resolve();
  54. },
  55. title: '',
  56. titleLoading: '',
  57. message: '',
  58. startLoading: false,
  59. };
  60. constructor(props: PropsType) {
  61. super(props);
  62. this.state = {
  63. loading:
  64. props.startLoading != null
  65. ? props.startLoading
  66. : LoadingConfirmDialog.defaultProps.startLoading,
  67. };
  68. }
  69. /**
  70. * Set the dialog into loading state and closes it when operation finishes
  71. */
  72. onClickAccept = () => {
  73. const { props } = this;
  74. this.setState({ loading: true });
  75. if (props.onAccept != null) {
  76. props.onAccept().then(this.hideLoading);
  77. }
  78. };
  79. /**
  80. * Waits for fade out animations to finish before hiding loading
  81. * @returns {NodeJS.Timeout}
  82. */
  83. hideLoading = (): NodeJS.Timeout =>
  84. setTimeout(() => {
  85. this.setState({ loading: false });
  86. }, 200);
  87. /**
  88. * Hide the dialog if it is not loading
  89. */
  90. onDismiss = () => {
  91. const { state, props } = this;
  92. if (!state.loading && props.onDismiss != null) {
  93. props.onDismiss();
  94. }
  95. };
  96. render() {
  97. const { state, props } = this;
  98. return (
  99. <Portal>
  100. <Dialog visible={props.visible} onDismiss={this.onDismiss}>
  101. <Dialog.Title>
  102. {state.loading ? props.titleLoading : props.title}
  103. </Dialog.Title>
  104. <Dialog.Content>
  105. {state.loading ? (
  106. <ActivityIndicator animating size="large" />
  107. ) : (
  108. <Paragraph>{props.message}</Paragraph>
  109. )}
  110. </Dialog.Content>
  111. {state.loading ? null : (
  112. <Dialog.Actions>
  113. <Button onPress={this.onDismiss} style={styles.button}>
  114. {i18n.t('dialog.cancel')}
  115. </Button>
  116. <Button onPress={this.onClickAccept}>
  117. {i18n.t('dialog.yes')}
  118. </Button>
  119. </Dialog.Actions>
  120. )}
  121. </Dialog>
  122. </Portal>
  123. );
  124. }
  125. }