FullReduction.js 6.8 KB

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