FullReduction.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* eslint-disable no-underscore-dangle */
  2. import * as WebBrowser from "expo-web-browser";
  3. import * as React from "react";
  4. import { StyleSheet } from "react-native";
  5. import { Layout, Text, useTheme, Button, List } from "@ui-kitten/components";
  6. import { useModel } from "flooks";
  7. import { Input, Div } from "react-native-magnus";
  8. import { useFocusEffect } from "@react-navigation/native";
  9. import ActionButton from "react-native-action-button";
  10. import EmptyComponent from "../components/EmptyComponent";
  11. import NavHeaderBar from "../components/NavHeaderBar";
  12. const styles = StyleSheet.create({
  13. all: {
  14. backgroundColor: "#fff",
  15. flex: 1,
  16. },
  17. lay: {
  18. backgroundColor: "#fff",
  19. },
  20. padBot: {
  21. paddingBottom: 100,
  22. },
  23. list: {
  24. paddingVertical: 10,
  25. paddingHorizontal: 15,
  26. backgroundColor: "transparent",
  27. flex: 1,
  28. },
  29. item: {
  30. flexDirection: "row",
  31. alignItems: "center",
  32. paddingVertical: 10,
  33. },
  34. input: {
  35. marginHorizontal: 5,
  36. minWidth: 49,
  37. },
  38. text: {
  39. flex: 1,
  40. },
  41. flexRow: {
  42. flexDirection: "row",
  43. alignItems: "center",
  44. },
  45. buttonlast: {
  46. marginLeft: 10,
  47. },
  48. button: {
  49. alignSelf: "flex-start",
  50. },
  51. });
  52. export default function FullReduction() {
  53. const theme = useTheme();
  54. const { changeBackground } = useModel("barModel", true);
  55. const { mid } = useModel("userModel");
  56. const { httpGet, httpPost } = useModel("httpModel", true);
  57. const { success, warnning } = useModel("loadingModel", true);
  58. const { showDialog } = useModel("dialogModel");
  59. const {
  60. userTitle21,
  61. fullReduction2,
  62. fullReduction1,
  63. delText,
  64. editText,
  65. confirm,
  66. cancel,
  67. successText,
  68. removeTips,
  69. } = useModel("wordsModel");
  70. const [fullReductions, changeFllReduction] = React.useState([
  71. { fullAmount: "", minusAmount: "" },
  72. ]);
  73. useFocusEffect(
  74. React.useCallback(() => {
  75. changeBackground(theme["color-primary-500"]);
  76. httpGet("/fullReduction/my").then(res => {
  77. if (res.length > 0) {
  78. changeFllReduction(
  79. res.map(item => {
  80. return { ...item, edit: false };
  81. })
  82. );
  83. }
  84. });
  85. }, [])
  86. );
  87. const editInfo = (info, index) => {
  88. const _fullReductions = [...fullReductions];
  89. info.edit = true;
  90. _fullReductions[index] = info;
  91. changeFllReduction(_fullReductions);
  92. };
  93. const delInfo = (info, index) => {
  94. showDialog({
  95. bodyText: removeTips,
  96. status: "danger",
  97. cancelable: true,
  98. confirmCallback: () => {
  99. httpPost(`/fullReduction/del/${info.id}`)
  100. .then(() => {
  101. success(successText);
  102. const _fullReductions = [...fullReductions];
  103. _fullReductions.splice(index, 1);
  104. changeFllReduction(_fullReductions);
  105. })
  106. .catch(e => {
  107. warnning(e.error);
  108. });
  109. },
  110. });
  111. };
  112. const cancelInfo = (info, index) => {
  113. const _fullReductions = [...fullReductions];
  114. if (info.id) {
  115. info.edit = false;
  116. _fullReductions[index] = info;
  117. } else {
  118. _fullReductions.pop();
  119. }
  120. changeFllReduction(_fullReductions);
  121. };
  122. const saveInfo = (info, index) => {
  123. httpPost(
  124. "/fullReduction/save",
  125. {
  126. ...info,
  127. merchantId: mid,
  128. },
  129. {
  130. body: "json",
  131. }
  132. )
  133. .then(res => {
  134. success(successText);
  135. const _fullReductions = [...fullReductions];
  136. _fullReductions.splice(index, 1, {
  137. ...res,
  138. edit: false,
  139. });
  140. changeFllReduction(_fullReductions);
  141. })
  142. .catch(e => {
  143. warnning(e.error);
  144. });
  145. };
  146. function changeText(value, index, type) {
  147. const _fullReductions = [...fullReductions];
  148. const info = _fullReductions[index];
  149. if (type === "full") {
  150. info.fullAmount = value;
  151. } else {
  152. info.minusAmount = value;
  153. }
  154. _fullReductions.splice(index, 1, info);
  155. changeFllReduction(_fullReductions);
  156. }
  157. const addFullReduction = () => {
  158. const _fullReductions = [...fullReductions];
  159. const last = _fullReductions[_fullReductions.length - 1] || {};
  160. if (_fullReductions.length === 0 || last.id) {
  161. _fullReductions.push({
  162. fullAmount: "",
  163. minusAmount: "",
  164. });
  165. changeFllReduction(_fullReductions);
  166. }
  167. };
  168. const saveItem = (info, index) => (
  169. <Layout style={styles.item}>
  170. <Text style={styles.text}>
  171. {fullReduction1}
  172. {info.fullAmount}
  173. {fullReduction2}
  174. {info.minusAmount}
  175. </Text>
  176. <Button
  177. size="small"
  178. appearance="outline"
  179. onPress={() => editInfo(info, index)}
  180. >
  181. {editText}
  182. </Button>
  183. <Button
  184. size="small"
  185. status="danger"
  186. style={styles.buttonlast}
  187. onPress={() => delInfo(info, index)}
  188. >
  189. {delText}
  190. </Button>
  191. </Layout>
  192. );
  193. const editItem = (info, index) => (
  194. <Layout style={styles.item}>
  195. <Layout style={[styles.text, styles.flexRow]}>
  196. <Text>{fullReduction1}</Text>
  197. <Input
  198. keyboardType="numeric"
  199. flex={1}
  200. py={3}
  201. mx={5}
  202. value={info.fullAmount.toString()}
  203. onChangeText={value => changeText(value, index, "full")}
  204. />
  205. <Text>{fullReduction2}</Text>
  206. <Input
  207. py={3}
  208. keyboardType="numeric"
  209. flex={1}
  210. mx={5}
  211. value={info.minusAmount.toString()}
  212. onChangeText={value => changeText(value, index, "minus")}
  213. />
  214. </Layout>
  215. <Button
  216. size="small"
  217. disabled={!info.minusAmount || !info.fullAmount}
  218. onPress={() => saveInfo(info, index)}
  219. >
  220. {confirm}
  221. </Button>
  222. <Button
  223. size="small"
  224. appearance="outline"
  225. style={styles.buttonlast}
  226. onPress={() => cancelInfo(info, index)}
  227. >
  228. {cancel}
  229. </Button>
  230. </Layout>
  231. );
  232. const renderItem = ({ item, index }) => {
  233. if (!item.id || item.edit) {
  234. return editItem(item, index);
  235. }
  236. return saveItem(item, index);
  237. };
  238. return (
  239. <>
  240. <Div bg="white" flex={1}>
  241. <NavHeaderBar title={userTitle21} />
  242. <List
  243. ListHeaderComponent={() => {
  244. return <Div h={50} />;
  245. }}
  246. style={styles.list}
  247. data={fullReductions}
  248. renderItem={renderItem}
  249. ListEmptyComponent={EmptyComponent}
  250. />
  251. <ActionButton
  252. buttonColor={theme["color-primary-500"]}
  253. onPress={addFullReduction}
  254. position="left"
  255. />
  256. </Div>
  257. </>
  258. );
  259. }