AddGoodsClassification.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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, Card, List } from "@ui-kitten/components";
  6. import { useFocusEffect, useRoute } from "@react-navigation/native";
  7. // import ActionButton from "react-native-action-button";
  8. import { useModel } from "flooks";
  9. import ScrollPage from "../components/ScrollPage";
  10. import NavHeaderBar from "../components/NavHeaderBar";
  11. import GoodsCard from "../components/GoodsCard";
  12. import EmptyComponent from "../components/EmptyComponent";
  13. const Header = (props, title) => (
  14. <Text {...props} category="s1">
  15. {title}
  16. </Text>
  17. );
  18. const styles = StyleSheet.create({
  19. flex1: {
  20. backgroundColor: "rgba(0,0,0,0)",
  21. paddingHorizontal: 15,
  22. paddingBottom: 20,
  23. },
  24. list: {
  25. backgroundColor: "rgba(0,0,0,0)",
  26. },
  27. container: {
  28. backgroundColor: "#EEEEEE",
  29. paddingTop: 20,
  30. },
  31. tabContent: {
  32. backgroundColor: "#fff",
  33. marginTop: 20,
  34. },
  35. img: {
  36. width: 100,
  37. height: 100,
  38. alignSelf: "center",
  39. },
  40. img2: {
  41. width: 97,
  42. height: 21,
  43. alignSelf: "center",
  44. marginTop: 2,
  45. },
  46. text: {
  47. marginTop: 16,
  48. },
  49. layoutLeft: {
  50. // flexDirection: "row",
  51. paddingVertical: 10,
  52. justifyContent: "center",
  53. alignItems: "center",
  54. },
  55. form: {
  56. paddingHorizontal: 26,
  57. paddingVertical: 20,
  58. },
  59. textareaContainer: {
  60. backgroundColor: "#F0F0F0",
  61. height: 100,
  62. borderRadius: 4,
  63. },
  64. textarea: {
  65. textAlignVertical: "top", // hack android
  66. fontSize: 13,
  67. color: "#333",
  68. paddingHorizontal: 14,
  69. paddingVertical: 10,
  70. height: 100,
  71. },
  72. });
  73. export default function AddGoodsClassification({}) {
  74. const theme = useTheme();
  75. const { changeBackground } = useModel("barModel");
  76. const { httpGet, httpPost } = useModel("httpModel", true);
  77. const { success } = useModel("loadingModel", true);
  78. const {
  79. goodsClassificationTitle1,
  80. goodsClassificationTitle2,
  81. goodsClassificationTitle3,
  82. add,
  83. remove,
  84. successText,
  85. removeTips,
  86. addClassTips,
  87. } = useModel("wordsModel");
  88. const { showDialog } = useModel("dialogModel", true);
  89. const { selectInfos, changeSelect } = useModel("goodsModel");
  90. const [goods, changeGoods] = React.useState([]);
  91. const [selectId, changeSelectId] = React.useState(0);
  92. const [goodsClass, setGoodsClass] = React.useState([]);
  93. const [pageType, changeType] = React.useState();
  94. const topGoods = React.useMemo(() => {
  95. if (pageType === "signboard") {
  96. return goods.filter(item => {
  97. return item.signboard;
  98. });
  99. }
  100. if (pageType === "recommend") {
  101. return goods.filter(item => {
  102. return item.recommend;
  103. });
  104. }
  105. return [...goodsClass];
  106. }, [goods, goodsClass]);
  107. const elseGoods = React.useMemo(() => {
  108. if (pageType === "signboard") {
  109. return goods.filter(item => {
  110. return !item.signboard;
  111. });
  112. }
  113. if (pageType === "recommend") {
  114. return goods.filter(item => {
  115. return !item.recommend;
  116. });
  117. }
  118. const _topIds = topGoods.map(item => {
  119. return item.id;
  120. });
  121. return goods.filter(item => {
  122. return _topIds.indexOf(item.id) === -1;
  123. });
  124. }, [goods, goodsClass]);
  125. function getSelectGoods() {
  126. httpGet("/classification/allGoods", {
  127. classificationId: selectId,
  128. }).then(res => {
  129. let list = res || [];
  130. list = list.filter(item => {
  131. return item != null;
  132. });
  133. setGoodsClass(list);
  134. });
  135. }
  136. function getAllGoods() {
  137. httpGet("/goods/my").then(res => {
  138. changeGoods(res);
  139. });
  140. }
  141. function changeSignboard(id, signboard, tips, info) {
  142. if (pageType === "signboard" || pageType === "recommend") {
  143. httpPost(
  144. "/goods/save",
  145. {
  146. id,
  147. [pageType]: signboard,
  148. },
  149. { body: "json" }
  150. ).then(res => {
  151. success(tips + successText);
  152. const _goods = goods.map(item => {
  153. if (item.id !== id) {
  154. return item;
  155. }
  156. return res;
  157. });
  158. changeGoods(_goods);
  159. });
  160. } else if (pageType === "classification" && selectId === "new") {
  161. const _goodsClass = [...goodsClass];
  162. if (signboard) {
  163. _goodsClass.push(info);
  164. } else {
  165. _goodsClass.splice(
  166. _goodsClass.findIndex(item => {
  167. return item.id === id;
  168. }),
  169. 1
  170. );
  171. }
  172. success(tips + successText);
  173. setGoodsClass(_goodsClass);
  174. changeSelect(_goodsClass);
  175. } else if (signboard) {
  176. const ids = topGoods.map(item => {
  177. return item.id;
  178. });
  179. ids.push(id);
  180. httpGet(
  181. "/classification/saveGoods",
  182. {
  183. classificationId: selectId,
  184. string: ids.join(","),
  185. },
  186. { body: "json" }
  187. ).then(() => {
  188. success(tips + successText);
  189. getSelectGoods();
  190. });
  191. } else {
  192. httpGet(
  193. "/classification/delGoods",
  194. {
  195. classificationId: selectId,
  196. goodId: id,
  197. },
  198. { body: "json" }
  199. ).then(() => {
  200. success(tips + successText);
  201. getSelectGoods();
  202. });
  203. }
  204. }
  205. const route = useRoute();
  206. useFocusEffect(
  207. React.useCallback(() => {
  208. changeBackground(theme["color-primary-500"]);
  209. const { params } = route;
  210. const { type, classificationId } = params;
  211. if (type === "classification") {
  212. changeSelectId(classificationId);
  213. }
  214. if (type) {
  215. changeType(type);
  216. } else {
  217. changeType("signboard");
  218. }
  219. getAllGoods();
  220. }, [])
  221. );
  222. React.useEffect(() => {
  223. if (selectId && selectId !== "new") {
  224. getSelectGoods();
  225. } else if (selectId === "new") {
  226. setGoodsClass(selectInfos);
  227. }
  228. }, [selectId]);
  229. const chooseGoodsItem = ({ item }) => (
  230. <GoodsCard
  231. info={item}
  232. key={item.id}
  233. isAdd
  234. removeEvent={() => {
  235. showDialog({
  236. bodyText: removeTips,
  237. status: "danger",
  238. cancelable: true,
  239. confirmCallback: () => {
  240. changeSignboard(item.id, false, remove, item);
  241. },
  242. });
  243. }}
  244. />
  245. );
  246. const goodsItem = ({ item }) => (
  247. <GoodsCard
  248. info={item}
  249. key={item.id}
  250. isAdd={false}
  251. addEvent={() => {
  252. if (topGoods.length < 2 || pageType !== "recommend") {
  253. changeSignboard(item.id, true, add, item);
  254. } else {
  255. showDialog({
  256. bodyText: addClassTips,
  257. });
  258. }
  259. }}
  260. />
  261. );
  262. return (
  263. <>
  264. <NavHeaderBar title={goodsClassificationTitle1} />
  265. <ScrollPage>
  266. <Layout style={styles.container}>
  267. <Card
  268. appearance="headerList"
  269. header={props => {
  270. return Header(props, goodsClassificationTitle2);
  271. }}
  272. disabled
  273. >
  274. <List
  275. data={topGoods}
  276. style={styles.list}
  277. renderItem={chooseGoodsItem}
  278. ListEmptyComponent={EmptyComponent}
  279. />
  280. </Card>
  281. <Card
  282. appearance="headerList"
  283. header={props => {
  284. return Header(props, goodsClassificationTitle3);
  285. }}
  286. disabled
  287. >
  288. <List
  289. data={elseGoods}
  290. style={styles.list}
  291. renderItem={goodsItem}
  292. ListEmptyComponent={EmptyComponent}
  293. />
  294. </Card>
  295. </Layout>
  296. {/* <ActionButton
  297. style={{ zIndex: 2 }}
  298. buttonColor={theme["color-primary-500"]}
  299. onPress={() => {
  300. let ids = topGoods.map((item) => {
  301. return item.id;
  302. });
  303. ids = ids.join(",");
  304. console.log(ids);
  305. navigation.navigate(route.params.preKey, {
  306. choosIds: ids,
  307. });
  308. }}
  309. position='center'
  310. /> */}
  311. </ScrollPage>
  312. </>
  313. );
  314. }