webConfigForm.js 3.02 KB
Newer Older
张烨's avatar
张烨 committed
1
import BaseForm from '@/components/BaseForm';
张烨's avatar
张烨 committed
2
import React, { useEffect, useState } from 'react';
张烨's avatar
张烨 committed
3 4 5 6
import {
  getLoginPage,
  getMapCofigs,
  getWebThemes,
Maofei94's avatar
Maofei94 committed
7
  getProductList,
张烨's avatar
张烨 committed
8 9
} from '@/services/webConfig/api';
import { Row, Col } from 'antd';
张烨's avatar
张烨 committed
10
import { getDefaultGetWebconfig, singleStyleData, webMode } from '../utils';
张烨's avatar
张烨 committed
11 12

const WebConfigForm = props => {
张烨's avatar
张烨 committed
13 14 15 16 17 18 19 20 21
  const {
    hasIntegerate,
    config,
    onCancel,
    onSubmit,
    onOk,
    isEdit,
    submitting,
  } = props;
张烨's avatar
张烨 committed
22 23 24
  const [webThemes, setWebThemes] = useState([]);
  const [mapConfigs, setMapConfigs] = useState([]);
  const [loginPages, setLoginPages] = useState([]);
Maofei94's avatar
Maofei94 committed
25
  const [products, setProducts] = useState([]);
张烨's avatar
张烨 committed
26 27 28 29 30 31
  const [form, setForm] = useState(null);

  const getForm = f => {
    setForm(f);
  };

张烨's avatar
张烨 committed
32 33 34 35 36 37
  useEffect(() => {
    if (form) {
      form.setFieldsValue(config);
    }
  }, [config]);

张烨's avatar
张烨 committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  const onGetThemes = () => {
    if (webThemes.length === 0) {
      getWebThemes().then(res => setWebThemes(res));
    }
  };

  const onGetLoginPages = () => {
    if (loginPages.length === 0) {
      getLoginPage().then(res => setLoginPages(res));
    }
  };

  const onGetMapConfig = () => {
    if (mapConfigs.length === 0) {
      getMapCofigs().then(res => setMapConfigs(res));
    }
  };
Maofei94's avatar
Maofei94 committed
55 56 57 58 59 60 61 62 63
  const onGetProduct = () => {
    if (products.length === 0) {
      getProductList().then(res => {
        if (res.code === 0) {
          setProducts(res.data);
        }
      });
    }
  };
张烨's avatar
张烨 committed
64 65 66

  const itemsMap = getDefaultGetWebconfig({
    initialValues: config,
张烨's avatar
张烨 committed
67
    submitting,
张烨's avatar
张烨 committed
68 69 70 71 72 73 74
    hasIntegerate,
    webThemes,
    onGetThemes,
    loginPages,
    mapConfigs,
    onGetLoginPages,
    onGetMapConfig,
Maofei94's avatar
Maofei94 committed
75 76
    products,
    onGetProduct,
张烨's avatar
张烨 committed
77
    isEdit,
张烨's avatar
张烨 committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  });

  const formConfig = {
    getForm,
    items: Object.keys(itemsMap).map(k => ({
      ...itemsMap[k],
      dataIndex: k,
    })),
    buttons: [
      {
        text: '取消',
        type: 'cancel',
        size: 'middle',
        onClick: () => {
          // eslint-disable-next-line no-unused-expressions
          onCancel && onCancel();
        },
      },
      {
        text: '确定',
        htmlType: 'submit',
        type: 'primary',
        size: 'middle',
张烨's avatar
张烨 committed
101
        loading: submitting,
张烨's avatar
张烨 committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        style: {
          marginLeft: '10px',
        },
      },
    ],
    buttonsWraper: bts => (
      <Row>
        <Col span={24} style={{ textAlign: 'right' }}>
          {bts}
        </Col>
      </Row>
    ),
  };

  return (
    <BaseForm
      {...formConfig}
      labelCol={{ span: 6 }}
      wrapperCol={{ span: 14 }}
      onValuesChange={v => {
张烨's avatar
张烨 committed
122
        if (form && v.mode === webMode.single) {
张烨's avatar
张烨 committed
123 124 125
          const sty = form.getFieldValue('style');
          if (!singleStyleData[sty]) {
            form.setFieldsValue([{ name: 'style', value: 'platform' }]);
张烨's avatar
张烨 committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139
          }
        }
      }}
      onFinish={values => {
        // eslint-disable-next-line no-unused-expressions
        onSubmit && onSubmit(values);
        // eslint-disable-next-line no-unused-expressions
        onOk && onOk(values);
      }}
    />
  );
};

export default WebConfigForm;