HomeScreenPage1.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /* eslint-disable no-underscore-dangle */
  2. import * as React from "react";
  3. import {
  4. Icon,
  5. Text,
  6. Card,
  7. Layout,
  8. ListItem,
  9. Button,
  10. } from "@ui-kitten/components";
  11. import { StyleSheet, Dimensions } from "react-native";
  12. import { useFocusEffect } from "@react-navigation/native";
  13. import { useModel } from "flooks";
  14. import GoodsCard from "../components/GoodsCardVertical";
  15. import GoodsCardLarge from "../components/GoodsCard";
  16. import * as RootNavigation from "../navigation/RootNavigation";
  17. import EmptyComponent from "../components/EmptyComponent";
  18. import {
  19. ClassificationUtil,
  20. getClassificationByName,
  21. } from "../Utils/SystemRuleUtil";
  22. const styles = StyleSheet.create({
  23. tabContainer: {
  24. backgroundColor: "transparent",
  25. flex: 1,
  26. },
  27. goodsList: {
  28. flexDirection: "row",
  29. justifyContent: "space-between",
  30. paddingVertical: 10,
  31. width: Dimensions.get("window").width - 30,
  32. backgroundColor: "#F0F0F0",
  33. },
  34. title: {
  35. alignSelf: "stretch",
  36. },
  37. card: {
  38. alignItems: "center",
  39. backgroundColor: "#F0F0F0",
  40. },
  41. container: {
  42. backgroundColor: "#F0F0F0",
  43. maxWidth: 93,
  44. paddingBottom: 100,
  45. width: 93,
  46. flexShrink: 0,
  47. },
  48. content: {
  49. flexDirection: "row",
  50. minHeight: 200,
  51. flex: 1,
  52. },
  53. goodsContainer: {
  54. flex: 1,
  55. backgroundColor: "#FFFFFF",
  56. paddingBottom: 100,
  57. paddingTop: 10,
  58. },
  59. classificat: {
  60. backgroundColor: "#F0F0F0",
  61. },
  62. addClassificat: {
  63. width: 36,
  64. alignSelf: "center",
  65. marginTop: 20,
  66. },
  67. addGoods: {
  68. width: 40,
  69. height: 40,
  70. alignSelf: "flex-end",
  71. marginTop: 20,
  72. marginRight: 15,
  73. },
  74. add: {
  75. flex: 1,
  76. backgroundColor: "#FFF5D8",
  77. borderColor: "#FFF5D8",
  78. minHeight: 232,
  79. marginHorizontal: 2,
  80. },
  81. selectItem: {
  82. color: "#FFC21C",
  83. fontWeight: "bold",
  84. },
  85. hid: {
  86. width: 10,
  87. height: 10,
  88. position: "absolute",
  89. right: -10,
  90. },
  91. });
  92. const StarIcon = props => <Icon {...props} name="plus" />;
  93. export default function HomePage1() {
  94. const { httpPost, httpGet } = useModel("httpModel", true);
  95. const [classifications, setClassifications] = React.useState([]);
  96. const [selectId, changeSelectId] = React.useState(0);
  97. const [goods, setGoods] = React.useState([]);
  98. const [goodsClass, setGoodsClass] = React.useState([]);
  99. const { showDialog } = useModel("dialogModel", true);
  100. const { success } = useModel("loadingModel", true);
  101. const { removeTips, remove, successText, NWHGKX, TEWQHV } = useModel(
  102. "wordsModel"
  103. );
  104. const { changeBackground } = useModel("barModel");
  105. const {
  106. classificationId,
  107. setClassificationId,
  108. getMyClassification,
  109. Classifications,
  110. } = useModel("goodsModel");
  111. function getGoods() {
  112. httpGet("/goods/my").then(res => {
  113. setGoods(res);
  114. });
  115. }
  116. useFocusEffect(
  117. React.useCallback(() => {
  118. changeBackground("#fff");
  119. getMyClassification().then(res => {
  120. if (!classificationId) {
  121. const _classifications = [...res];
  122. const { id } = _classifications.shift() || {};
  123. changeSelectId(id);
  124. } else {
  125. changeSelectId(classificationId);
  126. }
  127. setClassificationId(0);
  128. });
  129. getGoods();
  130. }, [])
  131. );
  132. React.useEffect(() => {
  133. setClassifications(Classifications);
  134. }, [Classifications]);
  135. const topGoods = React.useMemo(() => {
  136. return goods.filter(item => {
  137. return item.recommend;
  138. });
  139. }, [goods]);
  140. React.useEffect(() => {
  141. function getAllGoods() {
  142. httpGet("/classification/allGoods", {
  143. classificationId: selectId,
  144. }).then(res => {
  145. setGoodsClass(
  146. res.filter(item => {
  147. return item != null;
  148. })
  149. );
  150. });
  151. }
  152. if (selectId) {
  153. getAllGoods();
  154. }
  155. }, [selectId]);
  156. const selectClassInfo = React.useMemo(() => {
  157. return (
  158. classifications.find(item => {
  159. return item.id === selectId;
  160. }) || {}
  161. );
  162. }, [selectId, classifications]);
  163. const classificationList = () => {
  164. return classifications.map((item, index) => {
  165. return (
  166. <ListItem
  167. appearance="classification"
  168. key={index}
  169. title={textprops => (
  170. <Text
  171. {...textprops}
  172. style={[
  173. textprops.style,
  174. { textAlign: "center" },
  175. item.id === selectId && styles.selectItem,
  176. ]}
  177. >
  178. {item.name}
  179. </Text>
  180. )}
  181. accessoryRight={props => {
  182. if (!item.isOpen) {
  183. return (
  184. <Icon
  185. fill="rgb(241, 90, 60)"
  186. {...props}
  187. style={[props.style, styles.hid]}
  188. name="power"
  189. />
  190. );
  191. }
  192. return null;
  193. }}
  194. onPress={() => {
  195. changeSelectId(item.id);
  196. }}
  197. />
  198. );
  199. });
  200. };
  201. const classGoodsList = () => {
  202. return goodsClass.map((item, index) => {
  203. return (
  204. <GoodsCardLarge key={index} appearance="classification" info={item} />
  205. );
  206. });
  207. };
  208. const PulsIcon = props => (
  209. <Icon
  210. {...props}
  211. // eslint-disable-next-line react/destructuring-assignment
  212. style={[props.style, { width: 50, height: 50 }]}
  213. name="plus"
  214. />
  215. );
  216. function changeSignboard(id, signboard) {
  217. httpPost(
  218. "/goods/save",
  219. {
  220. id,
  221. recommend: signboard,
  222. },
  223. { body: "json" }
  224. ).then(() => {
  225. success(remove + successText);
  226. getGoods();
  227. });
  228. }
  229. const topGoodsMap = list => {
  230. const _list = list.map((item, index) => {
  231. return (
  232. <GoodsCard
  233. info={item}
  234. key={index}
  235. removeEvent={() => {
  236. showDialog({
  237. bodyText: removeTips,
  238. status: "danger",
  239. cancelable: true,
  240. confirmCallback: () => {
  241. changeSignboard(item.id, false, remove);
  242. },
  243. });
  244. }}
  245. />
  246. );
  247. });
  248. if (_list.length === 0) {
  249. _list.push(
  250. <Button
  251. style={styles.add}
  252. accessoryLeft={PulsIcon}
  253. key={1}
  254. onPress={() => {
  255. RootNavigation.navigate("AddClassification", {
  256. type: "recommend",
  257. });
  258. }}
  259. />
  260. );
  261. }
  262. if (_list.length === 1) {
  263. _list.push(
  264. <Button
  265. style={styles.add}
  266. accessoryLeft={PulsIcon}
  267. key={2}
  268. onPress={() => {
  269. RootNavigation.navigate("AddClassification", {
  270. type: "recommend",
  271. });
  272. }}
  273. />
  274. );
  275. }
  276. return _list;
  277. };
  278. return (
  279. <>
  280. <Layout style={styles.tabContainer}>
  281. <Card
  282. appearance="homePage1Card"
  283. disabled
  284. style={styles.card}
  285. backgroundColor="#F0F0F0"
  286. header={props => (
  287. <Text {...props} style={[styles.title, props.style]}>
  288. {NWHGKX}
  289. </Text>
  290. )}
  291. >
  292. {/* <List
  293. style={styles.goodsList}
  294. data={topGoods}
  295. renderItem={topGoodsItem}
  296. /> */}
  297. <Layout style={styles.goodsList}>{topGoodsMap(topGoods)}</Layout>
  298. </Card>
  299. <Layout style={styles.content}>
  300. <Layout style={styles.container}>
  301. {classificationList()}
  302. <Button
  303. style={styles.addClassificat}
  304. appearance="classification"
  305. onPress={() => {
  306. RootNavigation.navigate("ClassificationManage");
  307. }}
  308. >
  309. {TEWQHV}
  310. </Button>
  311. </Layout>
  312. {/* <List
  313. style={styles.container}
  314. data={classifications}
  315. renderItem={classificationItem}
  316. ListFooterComponent={() => (
  317. )}
  318. /> */}
  319. <Layout style={styles.goodsContainer}>
  320. {classGoodsList()}
  321. {goodsClass.length === 0 && <EmptyComponent />}
  322. <Button
  323. style={styles.addGoods}
  324. appearance="classification"
  325. status="danger"
  326. accessoryLeft={StarIcon}
  327. onPress={() => {
  328. const _classificationUtil = new ClassificationUtil(
  329. selectClassInfo
  330. );
  331. if (
  332. _classificationUtil.getType() ===
  333. getClassificationByName("好评") ||
  334. _classificationUtil.getType() ===
  335. getClassificationByName("折扣")
  336. ) {
  337. setClassificationId(selectId);
  338. RootNavigation.navigate("SystemClassificationEdit", {
  339. classifyId: selectId,
  340. });
  341. } else {
  342. setClassificationId(selectId);
  343. RootNavigation.navigate("AddGoods", {
  344. classifyId: selectId,
  345. });
  346. }
  347. }}
  348. />
  349. </Layout>
  350. </Layout>
  351. </Layout>
  352. </>
  353. );
  354. }
  355. // const goodsItem = ({ item, index }) => (
  356. // <GoodsCardLarge key={index} appearance='classification' info={item} />
  357. // );