AddGoodsClassification.js 10 KB

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