| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- /* eslint-disable no-else-return */
- /* eslint-disable prefer-const */
- /* eslint-disable react/jsx-one-expression-per-line */
- /* eslint-disable no-underscore-dangle */
- /* eslint-disable no-shadow */
- import * as WebBrowser from "expo-web-browser";
- import * as React from "react";
- import { StyleSheet, View } from "react-native";
- import { useModel } from "flooks";
- import {
- Layout,
- Text,
- useTheme,
- Button,
- Input,
- Toggle,
- } from "@ui-kitten/components";
- import { useFocusEffect, useRoute } from "@react-navigation/native";
- import { useEventEmitter, useCreation, useMap, useUnmount } from "@umijs/hooks";
- import ActionButton from "react-native-action-button";
- import ListComponent from "../../components/ListComponentParent";
- import NavHeaderBar from "../../components/NavHeaderBar";
- const styles = StyleSheet.create({
- lay: {
- backgroundColor: "#fff",
- flex: 1,
- },
- padBot: {
- paddingBottom: 100,
- },
- list: {
- paddingVertical: 10,
- paddingHorizontal: 15,
- backgroundColor: "transparent",
- flex: 0,
- },
- item: {
- flexDirection: "row",
- alignItems: "center",
- paddingVertical: 10,
- },
- input: {
- marginHorizontal: 5,
- minWidth: 49,
- // width: 49,
- },
- text: {
- flex: 1,
- },
- flexRow: {
- flexDirection: "row",
- alignItems: "center",
- },
- buttonlast: {
- marginLeft: 10,
- },
- button: {
- alignSelf: "flex-start",
- },
- money: {
- marginLeft: 10,
- },
- addNew2: {
- width: 80,
- alignSelf: "center",
- },
- separatorStyle: {
- height: 0,
- },
- });
- export default function GoodsSpecificationScreen() {
- const theme = useTheme();
- const { changeBackground } = useModel("barModel", true);
- // const { mid } = useModel("userModel");
- const { httpPost } = useModel("httpModel", true);
- const { showDialog } = useModel("dialogModel");
- const [goodsId, setGoodsId] = React.useState(0);
- const list$ = useEventEmitter();
- const [dataMap, dataMapAction] = useMap();
- const [editMap, editMapAction] = useMap();
- const parentIds = useCreation(() => {
- return Array.from(dataMap.keys());
- }, [dataMap]);
- const { delText, editText, YMZEFF, confirm, cancel, removeTips } = useModel(
- "wordsModel"
- );
- const {
- addClassGoods,
- getGoodsClassParent,
- setGoodsClassMap,
- editGoodsMap,
- } = useModel("goodsModel");
- useFocusEffect(
- React.useCallback(() => {
- changeBackground(theme["color-primary-500"]);
- }, [])
- );
- useUnmount(() => {
- setGoodsClassMap(goodsId, dataMap);
- dataMapAction.reset();
- editMapAction.reset();
- console.log(editGoodsMap);
- });
- const route = useRoute();
- function getList() {
- const { params } = route;
- const { goodsId } = params || {};
- setGoodsId(goodsId || "NEW");
- return getGoodsClassParent(goodsId || "NEW").then(res => {
- dataMapAction.setAll(res);
- return Promise.resolve();
- });
- }
- const editInfo = info => {
- if (!info.parent) {
- dataMapAction.set(info.id, {
- ...info,
- edit: true,
- });
- } else {
- let parentinfo = dataMapAction.get(info.parent);
- parentinfo.children[info.childIndex].edit = true;
- dataMapAction.set(info.parent, parentinfo);
- }
- if (info.id) {
- editMapAction.set(info.id, info);
- } else {
- editMapAction.set(`${info.parent}_${info.childIndex}`, info);
- }
- };
- const delInfo = info => {
- showDialog({
- bodyText: removeTips,
- status: "danger",
- cancelable: true,
- confirmCallback: () => {
- if (!info.parent) {
- let parent = dataMapAction.get(info.id);
- if (parent.children.length > 0) {
- showDialog({
- bodyText: YMZEFF,
- status: "danger",
- });
- return;
- }
- dataMapAction.remove(info.id);
- } else {
- let parent = dataMapAction.get(info.parent);
- parent.children.splice(info.childIndex, 1);
- dataMapAction.set(info.parent, parent);
- }
- if (info.id && info.id > 0) {
- httpPost(`/goodsSpecification/del/${info.id}`, {}, {}, true);
- }
- },
- });
- };
- // 取消
- const cancelInfo = info => {
- // 处理dataMap
- if (info.parent) {
- let parent = dataMapAction.get(info.parent);
- if (info.isNew) {
- parent.children = parent.children.filter((item, index) => {
- return index !== info.childIndex;
- });
- } else {
- parent.children = parent.children.map((item, index) => {
- if (index === info.childIndex) {
- return {
- ...item,
- edit: false,
- };
- } else {
- return item;
- }
- });
- }
- dataMapAction.set(info.parent, parent);
- } else if (info.isNew) {
- dataMapAction.remove(info.id);
- } else {
- dataMapAction.set(info.id, {
- ...info,
- edit: true,
- });
- }
- // 处理editMap
- if (info.id) {
- editMapAction.remove(info.id);
- } else {
- editMapAction.remove(`${info.parent}_${info.childIndex}`);
- }
- };
- // 保存
- const saveInfo = info => {
- let saveData = {};
- if (info.id) {
- saveData = editMapAction.get(info.id);
- } else {
- saveData = editMapAction.get(`${info.parent}_${info.childIndex}`);
- }
- delete saveData.isNew;
- if (saveData.parent) {
- let parent = dataMapAction.get(saveData.parent);
- parent.children[saveData.childIndex] = {
- ...saveData,
- edit: false,
- };
- dataMapAction.set(saveData.parent, parent);
- } else {
- dataMapAction.set(info.id, {
- ...saveData,
- edit: false,
- });
- }
- // 处理editMap
- if (info.id) {
- editMapAction.remove(info.id);
- } else {
- editMapAction.remove(`${info.parent}_${info.childIndex}`);
- }
- if (saveData.id && saveData.id > 0) {
- const data = {
- ...saveData,
- goodsId,
- };
- delete data.addIndex;
- addClassGoods(data);
- }
- };
- function getRandomId() {
- const id = 0 - Math.floor(Math.random() * 100 + 1);
- if (parentIds.includes(id)) {
- return getRandomId();
- } else {
- return id;
- }
- }
- // 添加新类
- const addFullReduction = info => {
- if (info) {
- // 二级分类
- let parentInfo = dataMapAction.get(info.parent);
- let hasNew = parentInfo.children.some(item => {
- return item.isNew;
- });
- if (!hasNew) {
- info = {
- parent: info.parent,
- name: "",
- amount: "",
- edit: true,
- isNew: true,
- childIndex: parentInfo.children.length,
- };
- parentInfo.children.push(info);
- dataMapAction.set(info.parent, parentInfo);
- editMapAction.set(`${info.parent}_${info.childIndex}`, info);
- }
- } else {
- // 一级分类
- let hasNew = Array.from(dataMap.values()).some(item => {
- return item.isNew;
- });
- if (!hasNew) {
- let newKey = getRandomId();
- let newInfo = {
- name: "",
- multiple: false,
- edit: true,
- isNew: true,
- children: [],
- id: newKey,
- };
- dataMapAction.set(newKey, newInfo);
- editMapAction.set(newKey, newInfo);
- }
- }
- };
- const saveItem = (info, index) => {
- return (
- <View Key={index}>
- <Layout style={styles.item}>
- <Layout style={[styles.text, styles.flexRow]}>
- {info.parent ? (
- <Text category="c1">添加{info.name}</Text>
- ) : (
- <Text category="s1">分类名:{info.name}</Text>
- )}
- {!!info.parent && (
- <Layout style={styles.money}>
- <Text category="c1">¥{info.amount}</Text>
- </Layout>
- )}
- {!info.parent && (
- <Layout style={styles.money}>
- <Text category="s1">{info.multiple ? "多选" : "单选"}</Text>
- </Layout>
- )}
- </Layout>
- <Button
- size="small"
- appearance="outline"
- onPress={() => editInfo(info)}
- >
- {editText}
- </Button>
- <Button
- size="small"
- status="danger"
- style={styles.buttonlast}
- onPress={() => delInfo(info)}
- >
- {delText}
- </Button>
- </Layout>
- {!info.parent && (
- <Button
- style={styles.addNew2}
- onPress={() =>
- addFullReduction({ addType: "2", parent: info.id, index })
- }
- >
- 添加规格
- </Button>
- )}
- </View>
- );
- };
- const editItem = info => {
- let editData = {};
- if (info.id) {
- editData = editMapAction.get(info.id);
- } else {
- editData = editMapAction.get(`${info.parent}_${info.childIndex}`);
- }
- return (
- <Layout style={styles.item}>
- <Layout style={[styles.text, styles.flexRow]}>
- <Text>{info.parent ? "规格" : "类名"}</Text>
- <Input
- size="small"
- defaultValue={editData.name}
- style={styles.input}
- key={`${info.id ? info.id : `${info.parent}_${info.childIndex}`}_0`}
- onChangeText={text => {
- if (info.id) {
- editMapAction.set(info.id, {
- ...editData,
- name: text,
- });
- } else {
- editMapAction.set(`${info.parent}_${info.childIndex}`, {
- ...editData,
- name: text,
- });
- }
- }}
- />
- {!!info.parent && (
- <>
- <Text>价钱</Text>
- <Input
- size="small"
- defaultValue={editData.amount}
- style={styles.input}
- key={`${
- info.id ? info.id : `${info.parent}_${info.childIndex}`
- }_1`}
- keyboardType="numeric"
- onChangeText={text => {
- if (info.id) {
- editMapAction.set(info.id, {
- ...editData,
- amount: text.toString(),
- });
- } else {
- editMapAction.set(`${info.parent}_${info.childIndex}`, {
- ...editData,
- amount: text.toString(),
- });
- }
- }}
- />
- </>
- )}
- {!info.parent && (
- <>
- <Text>是否多选</Text>
- <Toggle
- key="Toggle"
- checked={editData.multiple}
- onChange={checked => {
- editMapAction.set(info.id, {
- ...editData,
- multiple: checked,
- });
- }}
- />
- </>
- )}
- </Layout>
- <Button
- disabled={!editData.name || (!editData.amount && !!editData.parent)}
- size="small"
- onPress={() => saveInfo(info)}
- >
- {confirm}
- </Button>
- <Button
- size="small"
- appearance="outline"
- style={styles.buttonlast}
- onPress={() => cancelInfo(info)}
- >
- {cancel}
- </Button>
- </Layout>
- );
- };
- const renderItem = ({ item, index }) => {
- if (item.edit) {
- return editItem(item, index);
- }
- return saveItem(item, index);
- };
- return (
- <>
- <NavHeaderBar title="商品规格编辑" />
- <Layout style={[styles.lay]}>
- <ListComponent
- getInfo={getList}
- dataList={dataMap}
- renderItem={renderItem}
- style={styles.list}
- separatorStyle={styles.separatorStyle}
- showEmpty
- list$={list$}
- extraData={{ dataMap, editMap }}
- />
- <ActionButton
- buttonColor={theme["color-primary-500"]}
- onPress={addFullReduction}
- position="left"
- />
- </Layout>
- </>
- );
- }
|