ViewRedeem.js 5.5 KB

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