index.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import React from 'react';
  2. import { Text, View, TouchableOpacity } from 'react-native';
  3. import Modal from 'react-native-modal';
  4. import { styles } from './styles';
  5. import CloseIcon from '../../../assets/icons/close.svg';
  6. import { Colors } from 'src/theme';
  7. import { useNavigation } from '@react-navigation/native';
  8. import { NAVIGATION_PAGES } from 'src/types';
  9. import { ButtonVariants } from 'src/types/components';
  10. import { Button } from '../Button';
  11. export const WarningModal = ({
  12. isVisible,
  13. onClose,
  14. type,
  15. message,
  16. title,
  17. action,
  18. onModalHide
  19. }: {
  20. isVisible: boolean;
  21. onClose: () => void;
  22. type: string;
  23. message?: string;
  24. title?: string;
  25. action?: () => void;
  26. onModalHide?: () => void;
  27. }) => {
  28. const navigation = useNavigation();
  29. const content = {
  30. offline: {
  31. message: 'Please check your Internet connection and try again.',
  32. buttons: [
  33. {
  34. text: 'OK',
  35. textColor: Colors.WHITE,
  36. color: Colors.DARK_BLUE,
  37. action: onClose,
  38. borderColor: Colors.DARK_BLUE
  39. }
  40. ]
  41. },
  42. unauthorized: {
  43. message: 'To use this feature you need to have an account with NomadMania.',
  44. buttons: [
  45. {
  46. text: 'Login',
  47. textColor: Colors.DARK_BLUE,
  48. color: Colors.WHITE,
  49. action: () => {
  50. action && action();
  51. onClose();
  52. navigation.navigate(NAVIGATION_PAGES.LOGIN as never);
  53. },
  54. borderColor: Colors.DARK_BLUE
  55. },
  56. {
  57. text: 'Register',
  58. textColor: Colors.WHITE,
  59. color: Colors.DARK_BLUE,
  60. action: () => {
  61. action && action();
  62. onClose();
  63. navigation.navigate(NAVIGATION_PAGES.REGISTER as never);
  64. },
  65. borderColor: Colors.DARK_BLUE
  66. }
  67. ]
  68. },
  69. delete: {
  70. message,
  71. buttons: [
  72. {
  73. text: 'No',
  74. textColor: Colors.DARK_BLUE,
  75. color: Colors.WHITE,
  76. action: () => {
  77. onClose();
  78. },
  79. borderColor: Colors.DARK_BLUE
  80. },
  81. {
  82. text: 'Delete',
  83. textColor: Colors.WHITE,
  84. color: Colors.RED,
  85. action: () => {
  86. onClose();
  87. action && action();
  88. },
  89. borderColor: Colors.RED
  90. }
  91. ]
  92. },
  93. success: {
  94. message,
  95. buttons: [
  96. {
  97. text: 'OK',
  98. textColor: Colors.WHITE,
  99. color: Colors.ORANGE,
  100. action: () => {
  101. onClose;
  102. action && action();
  103. },
  104. borderColor: Colors.ORANGE
  105. }
  106. ]
  107. },
  108. confirm: {
  109. message,
  110. buttons: [
  111. {
  112. text: 'No',
  113. textColor: Colors.DARK_BLUE,
  114. color: Colors.WHITE,
  115. action: () => {
  116. onClose();
  117. },
  118. borderColor: Colors.DARK_BLUE
  119. },
  120. {
  121. text: 'Yes',
  122. textColor: Colors.WHITE,
  123. color: Colors.DARK_BLUE,
  124. action: () => {
  125. onClose();
  126. action && action();
  127. },
  128. borderColor: Colors.DARK_BLUE
  129. }
  130. ]
  131. }
  132. };
  133. const modalContent = content[type as keyof typeof content] || {};
  134. return (
  135. <Modal isVisible={isVisible} onModalHide={onModalHide}>
  136. <View style={styles.centeredView}>
  137. <View style={styles.modalView}>
  138. <View style={{ alignSelf: 'flex-end' }}>
  139. <TouchableOpacity onPress={onClose}>
  140. <CloseIcon fill={Colors.LIGHT_GRAY} />
  141. </TouchableOpacity>
  142. </View>
  143. <View style={styles.modalContent}>
  144. <Text style={styles.modalTitle}>{title ?? 'Oops!'}</Text>
  145. <Text style={styles.modalText}>{modalContent.message}</Text>
  146. <View style={styles.buttonContainer}>
  147. {modalContent.buttons.map(
  148. (
  149. button: {
  150. text: string;
  151. textColor: string;
  152. color: string;
  153. action: () => void;
  154. borderColor: string;
  155. },
  156. idx: number
  157. ) => (
  158. <Button
  159. key={idx}
  160. variant={ButtonVariants.OPACITY}
  161. containerStyles={{
  162. borderColor: button.borderColor,
  163. backgroundColor: button.color,
  164. width: type === 'offline' ? '60%' : '45%'
  165. }}
  166. textStyles={{
  167. color: button.textColor
  168. }}
  169. onPress={button.action}
  170. children={button.text}
  171. />
  172. )
  173. )}
  174. </View>
  175. </View>
  176. </View>
  177. </View>
  178. </Modal>
  179. );
  180. };