111

306 阅读1分钟

import React, { useCallback, useEffect, useState } from 'react'; import { Box, Button, Checkbox, createStyles, makeStyles, Theme, Tooltip, } from '@material-ui/core'; import { useProduct } from '../../store'; import { getFindServiceSaveFlag } from '../../services/product'; import { ProjectService } from '../../services';

const useStyles = makeStyles((theme: Theme) => createStyles({ content: { width: '1200px', margin: '0 auto', height: 'auto', fontFamily: 'ArialMT', paddingBottom: '20px', }, flex: { display: 'flex', width: '1200px', flexWrap: 'wrap', }, buttonFlex: { display: 'flex', width: '1200px', flexWrap: 'wrap', justifyContent: 'space-between', alignItems: 'center', }, header: { margin: '24px 0 0px 24px', }, h4: { fontFamily: 'Arial-BoldMT', fontSize: '16px', color: '#2C2C2C', }, span: { fontFamily: 'ArialMT', fontSize: '14px', color: '#000000', lineHeight: '16px', }, red: { color: '#F55753', }, title: { margin: '0 0 0px 24px', width: '30%', }, h5: { lineHeight: '40px', paddingLeft: '24px', height: '40px', fontFamily: 'Arial-BoldMT', fontSize: '14px', color: '#626262', margin: 0, }, rightSave: { fontSize: '12px', color: '#676C73', '& .MuiButtonBase-root': { width: '64px', height: '25px', marginLeft: '8px', fontSize: '12px', lineHeight: '12px', border: '1px #d9d9d9 solid', background: '#fff', }, }, buttonDefault: { '& .MuiButtonBase-root': { width: '64px', height: '25px', marginLeft: '8px', fontSize: '12px', background: '#1c60ff', borderColor: '#d9d9d9', color: '#fff', }, }, means: { margin: ${theme.spacing(2)}px 0, }, padding: { paddingLeft: '24px', backgroundColor: '#fff', }, paddingButton: { padding: '0 0 24px 6px', }, default: { marginTop: '16px', '& .MuiButtonBase-root': { background: '#edeef0', borderRadius: '20px', padding: '3px 9px !important', }, }, active: { marginTop: '16px', '& .MuiButtonBase-root': { background: '#1c60ff', color: '#fff', borderRadius: '20px', padding: '3px 9px !important', }, }, MuiTouchRippleRoot: { padding: '0 !importal', }, checkbox: { color: '#2BA68C', }, center: { margin: '0 auto', display: 'block', marginTop: '30px', }, button1: { textTransform: 'capitalize', backgroundColor: '#296DDA', width: '108px', height: '40px', }, group: { margin: '24px 0 4px 24px', background: '#ebedf0', lineHeight: '12px', paddingLeft: '16px', }, marginL70: { marginLeft: '70px', }, show: { opacity: 1, }, hide: { opacity: 0, }, }), ); let oldServiceModuleData = '[]'; const SelectFunction = () => { const [productInfo, { fetchData }] = useProduct(); const classes = useStyles(); const productId = '1214889460873940993'; // const deviceTypeId = '1199938732359204866'; useEffect(() => { if (!productInfo.id || productInfo.id !== productId) { fetchData(productId); } }, [productInfo, fetchData]);

const [saveFlag, setSaveFlag] = useState(false); const [serviceModuleData, setServiceModuleData] = useState([]); const [isChange, setIsChange] = useState(false); const [toggle, setToggle] = useState(true); useEffect(() => { const getData = async () => { const result = await getFindServiceSaveFlag(productInfo.id, productInfo.deviceTypeId); oldServiceModuleData = JSON.stringify(result.serviceModuleList); setServiceModuleData(result.serviceModuleList); setSaveFlag(result.saveFlag); }; if (productInfo.id) { getData(); } }, [productInfo]);

const handleFeatureChange = useCallback((type: string, groupIndex: number, featureIndex: number) => { const feature = serviceModuleData[groupIndex][type][featureIndex]; let info = feature; // 属性的详情 if (type === 'actionList') { info = feature.actionInfo || {}; // 方法的详情 } else if (type === 'eventList') { info = feature.eventInfo || {}; // 事件的详情 } if (info.propertyStatus !== 1) { feature.whetherCheck = !feature.whetherCheck; } setServiceModuleData(JSON.parse(JSON.stringify(serviceModuleData))); setIsChange(() => oldServiceModuleData !== serviceModuleData); // setSaveFlag(true); }, [serviceModuleData]);

const handleGroupChange = useCallback((group: any) => { const item = serviceModuleData.find((i: any) => i.id === group.id); if (item.status !== 1) { // 功能组的status为1表示必选 item.whetherCheck = !item.whetherCheck; } setServiceModuleData(JSON.parse(JSON.stringify(serviceModuleData))); // setServiceModuleData(JSON.parse(oldServiceModuleData)); setToggle(() => oldServiceModuleData !== serviceModuleData); setIsChange(!isChange); }, [serviceModuleData, isChange]);

const updataModul = useCallback(() => { if (toggle) { const serviceModuleListParams = []; let actionList: any[]; let propertyList: any[]; let eventList: any[];

  serviceModuleData.forEach((item: any) => {
    if (item.whetherCheck || item.status === 1) {
      actionList = [];
      propertyList = [];
      eventList = [];
      item.actionList.forEach((v: any) => {
        if (v.whetherCheck || v.actionInfo.propertyStatus === 1) {
          actionList.push(v);
        }
      });
      item.propertyList.forEach((v: any) => {
        if (v.whetherCheck || v.propertyStatus === 1) {
          propertyList.push(v);
        }
      });
      item.eventList.forEach((v: any) => {
        if (v.whetherCheck || v.eventInfo.propertyStatus === 1) {
          eventList.push(v);
        }
      });
      if (eventList.length > 0 || propertyList.length > 0 || actionList.length > 0 || item.status === 1) {
        const parames = {
          serviceModuleId: item.id,
          parentServiceModuleId: item.parentId,
          actionList,
          propertyList,
          eventList,
        };
        serviceModuleListParams.push(parames);
      }
    }
  });
} else {
  return false;
}
console.log(serviceModuleData, 'serviceModuleData');

}, [serviceModuleData, toggle]);

const onCancel = useCallback(() => { // debugger; setServiceModuleData(JSON.parse(oldServiceModuleData)); setIsChange(false); if (!toggle) { setToggle(!toggle); } }, [toggle]);

const onClass = (whetherCheck: boolean) => whetherCheck ? ${classes.active} : ${classes.default}; const onDisplay = (whetherCheck: boolean) => whetherCheck ? ${classes.flex} ${classes.marginL70} ${classes.show} : ${classes.flex} ${classes.marginL70} ${classes.hide};

// TODO // const proDetail = storage.get('productDetail') || {}; const proStatus = 1;

const getFeature = (group: any, type: string, pIndex: number) => { const list = group[type]; return ( { list.length > 0 ? list.map((feature: any, index: any) => { let info = feature; // 属性的详情 if (type === 'actionList') { info = feature.actionInfo || {}; // 方法的详情 } else if (type === 'eventList') { info = feature.eventInfo || {}; // 事件的详情 } const disabled = (info.propertyStatus === 1); return ( <Button variant="outlined" size="small" color="primary" disabled={disabled} onClick={() => handleFeatureChange(type, pIndex, index)} > {disabled && * } {info.name} ); }) :

-
} ); };

return ( <Box className={${classes.h4} ${classes.buttonFlex}}>

Select common function
{isChange && saveFlag && <Box className={(proStatus === 1 || proStatus === 2) ? ${classes.rightSave} : ${classes.buttonDefault}}> Content changed. It will be effective after saved. <Button onClick={updataModul} // disabled={proStatus === 1 || proStatus === 2} // 审核通过或者审核中,无法保存 > Save Cancel }
means default,cannot be unselected.
{serviceModuleData.map((group: any, index: any) => (
<Checkbox className={classes.checkbox} value="secondary" color="primary" disabled={group.status === 1} checked={group.whetherCheck} onChange={() => handleGroupChange(group)} /> <Button key={group.id} disabled={group.status === 1}> {group.status === 1 && } {group.name}
Attribution
{ getFeature(group, 'propertyList', index) }
Method
{ getFeature(group, 'actionList', index) }
Event
{ getFeature(group, 'eventList', index) }

      ))}
    </div>
  </div>
  {!saveFlag &&
    <Button
      variant="contained"
      color="primary"
      className={`${classes.center} ${classes.button1}`}
      onClick={updataModul}
    >
      Confirm
    </Button>
  }
</Box>

); };

export default SelectFunction;