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.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // @flow
  20. import * as React from 'react';
  21. import {
  22. ActivityIndicator,
  23. Button,
  24. Dialog,
  25. Paragraph,
  26. Portal,
  27. } from 'react-native-paper';
  28. import i18n from 'i18n-js';
  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. class LoadingConfirmDialog extends React.PureComponent<PropsType, StateType> {
  42. static defaultProps = {
  43. onDismiss: () => {},
  44. onAccept: (): Promise<void> => {
  45. return Promise.resolve();
  46. },
  47. title: '',
  48. titleLoading: '',
  49. message: '',
  50. startLoading: false,
  51. };
  52. constructor(props: PropsType) {
  53. super(props);
  54. this.state = {
  55. loading:
  56. props.startLoading != null
  57. ? props.startLoading
  58. : LoadingConfirmDialog.defaultProps.startLoading,
  59. };
  60. }
  61. /**
  62. * Set the dialog into loading state and closes it when operation finishes
  63. */
  64. onClickAccept = () => {
  65. const {props} = this;
  66. this.setState({loading: true});
  67. if (props.onAccept != null) props.onAccept().then(this.hideLoading);
  68. };
  69. /**
  70. * Waits for fade out animations to finish before hiding loading
  71. * @returns {TimeoutID}
  72. */
  73. hideLoading = (): TimeoutID =>
  74. setTimeout(() => {
  75. this.setState({loading: false});
  76. }, 200);
  77. /**
  78. * Hide the dialog if it is not loading
  79. */
  80. onDismiss = () => {
  81. const {state, props} = this;
  82. if (!state.loading && props.onDismiss != null) props.onDismiss();
  83. };
  84. render(): React.Node {
  85. const {state, props} = this;
  86. return (
  87. <Portal>
  88. <Dialog visible={props.visible} onDismiss={this.onDismiss}>
  89. <Dialog.Title>
  90. {state.loading ? props.titleLoading : props.title}
  91. </Dialog.Title>
  92. <Dialog.Content>
  93. {state.loading ? (
  94. <ActivityIndicator animating size="large" />
  95. ) : (
  96. <Paragraph>{props.message}</Paragraph>
  97. )}
  98. </Dialog.Content>
  99. {state.loading ? null : (
  100. <Dialog.Actions>
  101. <Button onPress={this.onDismiss} style={{marginRight: 10}}>
  102. {i18n.t('dialog.cancel')}
  103. </Button>
  104. <Button onPress={this.onClickAccept}>
  105. {i18n.t('dialog.yes')}
  106. </Button>
  107. </Dialog.Actions>
  108. )}
  109. </Dialog>
  110. </Portal>
  111. );
  112. }
  113. }
  114. export default LoadingConfirmDialog;