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.

LoadingConfirmDialog.tsx 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. type PropsType = {
  29. visible: boolean;
  30. onDismiss?: () => void;
  31. onAccept?: () => Promise<void>; // async function to be executed
  32. title?: string;
  33. titleLoading?: string;
  34. message?: string;
  35. startLoading?: boolean;
  36. };
  37. type StateType = {
  38. loading: boolean;
  39. };
  40. export default class LoadingConfirmDialog extends React.PureComponent<
  41. PropsType,
  42. StateType
  43. > {
  44. static defaultProps = {
  45. onDismiss: () => {},
  46. onAccept: (): Promise<void> => {
  47. return Promise.resolve();
  48. },
  49. title: '',
  50. titleLoading: '',
  51. message: '',
  52. startLoading: false,
  53. };
  54. constructor(props: PropsType) {
  55. super(props);
  56. this.state = {
  57. loading:
  58. props.startLoading != null
  59. ? props.startLoading
  60. : LoadingConfirmDialog.defaultProps.startLoading,
  61. };
  62. }
  63. /**
  64. * Set the dialog into loading state and closes it when operation finishes
  65. */
  66. onClickAccept = () => {
  67. const {props} = this;
  68. this.setState({loading: true});
  69. if (props.onAccept != null) {
  70. props.onAccept().then(this.hideLoading);
  71. }
  72. };
  73. /**
  74. * Waits for fade out animations to finish before hiding loading
  75. * @returns {NodeJS.Timeout}
  76. */
  77. hideLoading = (): NodeJS.Timeout =>
  78. setTimeout(() => {
  79. this.setState({loading: false});
  80. }, 200);
  81. /**
  82. * Hide the dialog if it is not loading
  83. */
  84. onDismiss = () => {
  85. const {state, props} = this;
  86. if (!state.loading && props.onDismiss != null) {
  87. props.onDismiss();
  88. }
  89. };
  90. render() {
  91. const {state, props} = this;
  92. return (
  93. <Portal>
  94. <Dialog visible={props.visible} onDismiss={this.onDismiss}>
  95. <Dialog.Title>
  96. {state.loading ? props.titleLoading : props.title}
  97. </Dialog.Title>
  98. <Dialog.Content>
  99. {state.loading ? (
  100. <ActivityIndicator animating size="large" />
  101. ) : (
  102. <Paragraph>{props.message}</Paragraph>
  103. )}
  104. </Dialog.Content>
  105. {state.loading ? null : (
  106. <Dialog.Actions>
  107. <Button onPress={this.onDismiss} style={{marginRight: 10}}>
  108. {i18n.t('dialog.cancel')}
  109. </Button>
  110. <Button onPress={this.onClickAccept}>
  111. {i18n.t('dialog.yes')}
  112. </Button>
  113. </Dialog.Actions>
  114. )}
  115. </Dialog>
  116. </Portal>
  117. );
  118. }
  119. }