ViewRedeem.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import React, { useState } from 'react';
  2. import { StyleSheet, Text, TextInput, View } from 'react-native';
  3. import Button from '../../components/Button';
  4. import TextView from '../../components/TextView';
  5. import Modal from "react-native-modal";
  6. import { ModalProps } from "../../components/BottomModal";
  7. import Dialog from '../../components/Dialog';
  8. import apiVoucher from '../../api/apiVoucher';
  9. export default ViewRedeem = ({
  10. userInfo={},
  11. onChange
  12. }) => {
  13. const [visible, showDialog] = useState(false);
  14. const [promoCode, setPromoCode] = useState("");
  15. const redeemVoucher = () => {
  16. showDialog(false);
  17. if (promoCode) {
  18. setTimeout(() => {
  19. Dialog.showProgressDialog();
  20. apiVoucher.redeemVoucher({
  21. redemptionCode: promoCode
  22. }).then(res => {
  23. Dialog.dismissLoading();
  24. if (onChange) {
  25. onChange();
  26. }
  27. if (res.msg) {
  28. setTimeout(() => {
  29. Dialog.showDialog({
  30. title: $t("voucher.vouchers"),
  31. message: res.msg,
  32. showCancel: false
  33. })
  34. }, 500);
  35. }
  36. }).catch(err => {
  37. Dialog.dismissLoading();
  38. if (err.err) {
  39. setTimeout(() => {
  40. Dialog.showDialog({
  41. title: $t("common.error"),
  42. message: err.err,
  43. showCancel: false
  44. })
  45. }, 500);
  46. }
  47. })
  48. }, 300);
  49. } else {
  50. toastLong($t("voucher.plsInputPromoCode"))
  51. }
  52. }
  53. return (
  54. <View>
  55. <View style={styles.redeemLayout}>
  56. <Button
  57. style={styles.buttonLeft}
  58. viewStyle={styles.buttonView}
  59. onClick={() => showDialog(true)}>
  60. <MaterialCommunityIcons
  61. name="ticket-percent-outline"
  62. size={24}
  63. color={textPrimary}/>
  64. <TextView
  65. style={styles.buttonText}>
  66. {$t("voucher.inputPromoCode")}
  67. </TextView>
  68. </Button>
  69. <Text style={styles.btnDivide}></Text>
  70. <View
  71. style={styles.buttonRight}>
  72. <TextView
  73. style={styles.buttonText}>
  74. {$t("voucher.availablePoints")}
  75. </TextView>
  76. <TextView
  77. style={styles.valueText}>
  78. {userInfo?.points || '0'}
  79. </TextView>
  80. </View>
  81. </View>
  82. <Modal
  83. isVisible={visible}
  84. onBackButtonPress={() => showDialog(false)}
  85. onBackdropPress={() => showDialog(false)}
  86. {...ModalProps}>
  87. <View style={Dialog.styles.modalDialog}>
  88. <TextView style={Dialog.styles.title}>{$t('voucher.inputPromoCode')}</TextView>
  89. <View style={Dialog.styles.message}>
  90. <TextInput
  91. style={styles.inputView}
  92. allowFontScaling={false}
  93. maxLength={6}
  94. placeholderTextColor={textPlacehoder}
  95. onChangeText={text => setPromoCode(text)}
  96. onSubmitEditing={() => redeemVoucher()}
  97. />
  98. </View>
  99. { isIOS
  100. ? <View style={Dialog.styles.modalFooter}>
  101. <Button
  102. style={Dialog.styles.btnGroup}
  103. viewStyle={Dialog.styles.btnView}
  104. textStyle={Dialog.styles.btnConfirm}
  105. text={$t('nav.confirm')}
  106. onClick={() => redeemVoucher()}/>
  107. <Button
  108. style={[Dialog.styles.btnGroup, Dialog.styles.btnRight]}
  109. viewStyle={Dialog.styles.btnView}
  110. textStyle={Dialog.styles.btnConfirm}
  111. text={$t('nav.cancel')}
  112. onClick={() => showDialog(false)}/>
  113. </View>
  114. : <View style={Dialog.styles.modalFooter}>
  115. <Dialog.AndroidButton
  116. title={$t('nav.cancel')}
  117. onPress={() => showDialog(false)}/>
  118. <Dialog.AndroidButton
  119. title={$t('nav.confirm')}
  120. onPress={() => redeemVoucher()}/>
  121. </View>
  122. }
  123. </View>
  124. </Modal>
  125. </View>
  126. );
  127. }
  128. const styles = StyleSheet.create({
  129. redeemLayout: {
  130. borderWidth: 1,
  131. borderColor: '#DADADA',
  132. borderRadius: 4,
  133. alignItems: 'center',
  134. flexDirection: 'row'
  135. },
  136. btnDivide: {
  137. width: 1,
  138. height: 26,
  139. backgroundColor: '#DADADA'
  140. },
  141. buttonLeft: {
  142. flex: 1,
  143. borderTopLeftRadius: 3,
  144. borderTopRightRadius: 0,
  145. borderBottomLeftRadius: 3,
  146. borderBottomRightRadius: 0,
  147. backgroundColor: 'transparent'
  148. },
  149. buttonRight: {
  150. flex: 1,
  151. alignItems: 'center',
  152. flexDirection: 'row',
  153. justifyContent: 'center',
  154. backgroundColor: 'transparent'
  155. },
  156. buttonView: {
  157. flex: 1,
  158. height: 42,
  159. alignItems: 'center',
  160. flexDirection: 'row',
  161. justifyContent: 'center'
  162. },
  163. buttonText: {
  164. color: textPrimary,
  165. fontSize: 13,
  166. paddingLeft: 8,
  167. paddingRight: 8
  168. },
  169. valueText: {
  170. color: colorAccent,
  171. fontSize: 14
  172. },
  173. inputView: {
  174. color: textPrimary,
  175. fontSize: 14,
  176. height: 42,
  177. marginTop: 8,
  178. textAlign: 'center',
  179. paddingLeft: 12,
  180. paddingRight: 12,
  181. borderWidth: 1,
  182. borderColor: '#DADADA',
  183. borderRadius: 4,
  184. alignItems: 'center',
  185. flexDirection: 'row',
  186. textTransform: 'uppercase',
  187. backgroundColor: colorLight
  188. }
  189. })