AddScreen.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* eslint-disable no-shadow */
  2. /* eslint-disable no-unused-vars */
  3. import * as WebBrowser from "expo-web-browser";
  4. import * as React from "react";
  5. import { StyleSheet } from "react-native";
  6. import moment from "moment";
  7. import { useModel } from "flooks";
  8. import { useFocusEffect } from "@react-navigation/native";
  9. import { Layout, Button, useTheme, Divider } from "@ui-kitten/components";
  10. import NavHeaderBar from "../../components/NavHeaderBar";
  11. import FormInput from "../../components/FormInput";
  12. const styles = StyleSheet.create({
  13. form: {
  14. paddingHorizontal: 10,
  15. flex: 1,
  16. marginTop: 10,
  17. },
  18. radio: {
  19. paddingVertical: 15,
  20. paddingHorizontal: 15,
  21. },
  22. btn: {
  23. marginHorizontal: 15,
  24. marginVertical: 10,
  25. },
  26. btnList: {
  27. alignItems: "center",
  28. },
  29. label: {
  30. width: 105,
  31. },
  32. });
  33. export default function CouponAddScreen({ navigation, route }) {
  34. const theme = useTheme();
  35. const { changeBackground } = useModel("barModel");
  36. const { KEPOWT, PXTFMU, delText, TMDCMR, VCGOOE, ZTIPSF, confirm } = useModel(
  37. "wordsModel"
  38. );
  39. const [id, setId] = React.useState();
  40. const {
  41. defaultStartTime,
  42. defaultEndTime,
  43. saveInfo,
  44. removeInfo,
  45. getInfo,
  46. } = useModel("couponModel");
  47. const [name, changeName] = React.useState("");
  48. const [amount, changeAmount] = React.useState("");
  49. const [fullAmount, changeFullAmount] = React.useState("");
  50. const [startDate, changeStartDate] = React.useState("");
  51. const [endDate, changeEndDate] = React.useState("");
  52. useFocusEffect(
  53. React.useCallback(() => {
  54. changeBackground(theme["color-primary-500"]);
  55. const { params } = route;
  56. const { id, pageName } = params || {};
  57. if (id) {
  58. setId(id);
  59. } else {
  60. changeEndDate(defaultEndTime);
  61. changeStartDate(defaultStartTime);
  62. }
  63. }, [])
  64. );
  65. React.useEffect(() => {
  66. if (id) {
  67. getInfo(id).then(res => {
  68. changeName(res.name);
  69. changeAmount(res.amount.toString());
  70. changeFullAmount(res.fullAmount.toString());
  71. changeStartDate(res.startDate);
  72. changeEndDate(res.endDate);
  73. });
  74. }
  75. }, [id]);
  76. const canNext = React.useMemo(() => {
  77. if (
  78. name &&
  79. amount &&
  80. startDate &&
  81. endDate &&
  82. moment(startDate, "YYYY-MM-DD").format("X") <
  83. moment(endDate, "YYYY-MM-DD").format("X")
  84. ) {
  85. return true;
  86. }
  87. return false;
  88. }, [name, amount, fullAmount, id, startDate, endDate]);
  89. const submit = () => {
  90. saveInfo({ id, name, amount, fullAmount, startDate, endDate }).then(res => {
  91. navigation.goBack();
  92. });
  93. };
  94. const remove = () => {
  95. removeInfo(id).then(res => {
  96. navigation.goBack();
  97. });
  98. };
  99. return (
  100. <>
  101. <NavHeaderBar title={KEPOWT} />
  102. <Layout style={styles.form}>
  103. {/* 优惠券名称 */}
  104. <FormInput
  105. label={PXTFMU}
  106. labelStyle={styles.label}
  107. key={1}
  108. placeholder={TMDCMR}
  109. value={name}
  110. onChange={changeName}
  111. />
  112. {/* 优惠券金额 */}
  113. <FormInput
  114. label="优惠券金额"
  115. labelStyle={styles.label}
  116. key={2}
  117. placeholder="输入金额"
  118. type="money"
  119. value={amount}
  120. onChange={changeAmount}
  121. />
  122. <Divider />
  123. {/* 优惠券门槛金额 */}
  124. <FormInput
  125. label="优惠券门槛金额"
  126. labelStyle={styles.label}
  127. key={3}
  128. placeholder="输入门槛金额"
  129. type="money"
  130. value={fullAmount}
  131. onChange={changeFullAmount}
  132. />
  133. {/* 优惠券开始时间 */}
  134. <FormInput
  135. label={VCGOOE}
  136. labelStyle={styles.label}
  137. key={4}
  138. placeholder=""
  139. type="date"
  140. value={startDate}
  141. onChange={changeStartDate}
  142. />
  143. {/* 优惠券结束时间 */}
  144. <FormInput
  145. label={ZTIPSF}
  146. labelStyle={styles.label}
  147. key={5}
  148. placeholder=""
  149. type="date"
  150. value={endDate}
  151. onChange={changeEndDate}
  152. />
  153. <Layout style={styles.btnList}>
  154. <Button style={styles.btn} disabled={!canNext} onPress={submit}>
  155. {confirm}
  156. </Button>
  157. {id != null && (
  158. <Button appearance="ghost" status="info" onPress={remove}>
  159. {delText}
  160. </Button>
  161. )}
  162. </Layout>
  163. </Layout>
  164. </>
  165. );
  166. }