Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useEffect, useCallback, useState, useRef, useMemo } from 'react';
  2. import { useDispatch, useSelector } from 'react-redux';
  3. import { getContentGroup } from '../../../actions/getContentGroup';
  4. import _debounce from 'lodash.debounce';
  5. import {
  6.   selectContentGroupData,
  7.   selectContentGroupStatuses
  8. } from '../../../selectors/getContentGroup';
  9. import { RenderController } from '../../../../Common/components/RenderController';
  10. import { css } from 'aphrodite';
  11. import * as styles from './styles';
  12. import { Breadcrumbs } from '../../../../Common/components/Breadcrumbs';
  13. import { Button } from '../../../../FormComponents/components/Button';
  14. import { Input } from '../../../../FormComponents/components/Input';
  15. import { Table } from '../../../../Common/components/Table';
  16. import { NavLink } from 'react-router-dom';
  17. import {
  18.   colorLightBlue,
  19.   colorLightGray,
  20.   colorRed,
  21.   commonStyles
  22. } from '../../../../../core/styles/styles';
  23. import { modalAdd, modalRemove } from 'Modal/actions';
  24. import { Modal } from 'Modal';
  25. import AddCircle from 'icons/addCircle.svg';
  26. import RemoveCircle from 'icons/removeCircle.svg';
  27. import moment, { duration } from 'moment';
  28. import { BrowseFlyerRuns } from '../../../../FlyerRuns/components/BrowseFlyerRuns';
  29. import { baseRules } from '../../../../../core/constants';
  30. import { useForm } from '../../../../Common/hooks';
  31. import { sendDeleteContentGroup } from '../../../actions/deleteContentGroup';
  32. import { sendUpdateContentGroupFormData } from '../../../actions/updateContentGroup';
  33.  
  34. // TODO  fix redux structure!!! separate content groups inside campaign groups
  35. export const ContentGroup = ({
  36.   match: {
  37.     params: { id, campaignGroupName, contentGroupId }
  38.   }
  39. }) => {
  40.   const dispatch = useDispatch();
  41.  
  42.   //copy
  43.   const [activeMerchant, setActiveMerchant] = useState(null);
  44.   const [activeRemoveFlyerRun, setActiveRemoveFlyerRun] = useState({
  45.     merchantId: null,
  46.     flyerRunId: null
  47.   });
  48.  
  49.   //copy
  50.   const closeModal = useCallback(() => dispatch(modalRemove('flyerRuns')), [dispatch]);
  51.   //copy
  52.   const addModal = useCallback(
  53.     id => {
  54.       setActiveMerchant(id);
  55.       dispatch(modalAdd('flyerRuns'));
  56.     },
  57.     [dispatch]
  58.   );
  59.  
  60.   useEffect(() => {
  61.     dispatch(getContentGroup(id, contentGroupId));
  62.   }, []);
  63.  
  64.   const { contentGroupStatus, contentGroupData } = useSelector(state => ({
  65.     contentGroupStatus: selectContentGroupStatuses(state).get,
  66.     contentGroupData: selectContentGroupData(state)
  67.   }));
  68.   const { name, elements, secondaryId } = contentGroupData;
  69.  
  70.   const links = [
  71.     { link: '/campaignGroups/home', name: 'Campaigns groups' },
  72.     { link: `/campaignGroups/${id}/`, name: `${campaignGroupName}` },
  73.     { link: `/campaignGroups/${id}/contentGroup/${contentGroupId}`, name: `${name}` }
  74.   ];
  75.  
  76.   const initialState = {
  77.     name: '',
  78.     elements: []
  79.   };
  80.  
  81.   const { values, errors, handleChange, handleSubmit, setValue } = useForm(initialState);
  82.  
  83.   // TODO create custom hook debounce
  84.   const refValues = useRef(values);
  85.   refValues.current = values;
  86.   const sendUpdate = data => {
  87.     dispatch(sendUpdateContentGroupFormData(id, contentGroupId, data ? data : refValues.current));
  88.   };
  89.   const delayedQuery = useMemo(() => _debounce(() => sendUpdate(), 1000), []);
  90.  
  91.   useEffect(() => {
  92.     setValue(contentGroupData);
  93.   }, [contentGroupData]);
  94.  
  95.   // copy
  96.   const renderFlyerRunName = (name, id, availableFrom, availableTo) =>
  97.     `${moment(availableFrom).format('ddd YYYY-MM-DD')} to ${moment(availableTo).format(
  98.       'ddd YYYY-MM-DD'
  99.     )} - ${name} (${id})`;
  100.  
  101.   const handleClick = (id, name, allFlyerRunData) => {
  102.     const data = {
  103.       ...values,
  104.       elements: elements.map(item =>
  105.         item.merchant_id === activeMerchant
  106.           ? { ...item, flyer_runs: [...item.flyer_runs, { ...allFlyerRunData }] }
  107.           : item
  108.       )
  109.     };
  110.     // TODO fix sendUpdate
  111.     // TODO fix sendUpdate
  112.     // TODO fix sendUpdate
  113.     setValue(data);
  114.     sendUpdate(data);
  115.   };
  116.  
  117.   const deleteFlyerRun = () => {
  118.     const data = {
  119.       ...values,
  120.       elements: values.elements.map(item => {
  121.         return item.merchant_id === activeRemoveFlyerRun.merchantId
  122.           ? {
  123.               ...item,
  124.               flyer_runs: item.flyer_runs.filter(
  125.                 item => +item.id !== +activeRemoveFlyerRun.flyerRunId
  126.               )
  127.             }
  128.           : item;
  129.       })
  130.     };
  131.     setValue(data);
  132.     sendUpdate(data);
  133.     dispatch(modalRemove('deleteFlyerRun'));
  134.   };
  135.  
  136.   // copy
  137.   const removeFlyerRun = (merchantId, flyerRunId) => {
  138.     setActiveRemoveFlyerRun({ merchantId, flyerRunId });
  139.     dispatch(modalAdd('deleteFlyerRun', `Remove flyer run`));
  140.   };
  141.  
  142.   // TODO add memo?
  143.   const addCircleIcon = (handler = () => null, isActive = true) => (
  144.     <AddCircle
  145.       width={18}
  146.       height={18}
  147.       fill={isActive ? colorLightBlue : colorLightGray}
  148.       onClick={handler}
  149.       className={css(styles.regular.icon)}
  150.     />
  151.   );
  152.  
  153.   const jsxColumns = id => [
  154.     {
  155.       id: 'name',
  156.       name: (
  157.         <span className={css(styles.regular.flyerRunTitle)}>
  158.           Flyer Runs {addCircleIcon(() => addModal(id))}
  159.         </span>
  160.       ),
  161.       width: '45%'
  162.     },
  163.     {
  164.       id: 'select_items',
  165.       name: 'Select Items',
  166.       width: '15%'
  167.     },
  168.     {
  169.       id: 'tag',
  170.       name: 'Tag',
  171.       width: '10%'
  172.     },
  173.     {
  174.       id: 'qc',
  175.       name: 'QC',
  176.       width: '10%'
  177.     },
  178.     {
  179.       id: 'synced',
  180.       name: 'Last Synced',
  181.       width: '20%'
  182.     }
  183.   ];
  184.  
  185.   const jsxRows = (merchant_id, elements) =>
  186.     elements.map(({ name, id, available_from, available_to, select_items, tag, qc, synced }) => ({
  187.       name: (
  188.         <span className={css(styles.regular.flyerRunName)}>
  189.           {renderFlyerRunName(name, id, available_from, available_to)}
  190.           <RemoveCircle
  191.             width={18}
  192.             height={18}
  193.             fill={colorRed}
  194.             onClick={() => removeFlyerRun(merchant_id, id)}
  195.             className={css(styles.regular.icon)}
  196.           />
  197.         </span>
  198.       ),
  199.       select_items: select_items
  200.         ? select_items
  201.         : addCircleIcon(() => console.log('api for selected items ')),
  202.       tag: tag
  203.         ? tag
  204.         : select_items
  205.         ? addCircleIcon(() => console.log('api for tag '))
  206.         : addCircleIcon(() => null, false),
  207.       qc: qc
  208.         ? qc
  209.         : select_items && tag
  210.         ? addCircleIcon(() => console.log('api for qc '))
  211.         : addCircleIcon(() => null, false),
  212.       synced
  213.     }));
  214.  
  215.   return (
  216.     <div>
  217.       <RenderController statuses={[contentGroupStatus]}>
  218.         <div className={css(styles.regular.root)}>
  219.           <Breadcrumbs links={links} />
  220.           <div className={css(styles.regular.titleLine)}>
  221.             <span className={css(styles.regular.contentGroupName)}>{name}</span>
  222.             <Button template="delete" onClick={() => dispatch(modalAdd('deleteContentGroup'))} />
  223.           </div>
  224.           <div className={css(styles.regular.content)}>
  225.             <div className={css(styles.regular.input)}>
  226.               <Input
  227.                 value={values.name}
  228.                 label="Name"
  229.                 name="name"
  230.                 onChange={e => {
  231.                   handleChange(e);
  232.                   delayedQuery();
  233.                 }}
  234.               />
  235.             </div>
  236.             <div className={css(styles.regular.merchants)}>
  237.               <span className={css(styles.regular.merchantsTitle)}>Merchants</span>
  238.               {!!values.elements.length &&
  239.                 values.elements.map(({ merchant_name, merchant_id, flyer_runs }) => (
  240.                   <div key={merchant_id} className={css(styles.regular.merchant)}>
  241.                     <span className={css(styles.regular.merchantName)}>{merchant_name}</span>
  242.                     {!!flyer_runs.length ? (
  243.                       <div className={css(styles.regular.tableWrap)}>
  244.                         <Table
  245.                           columns={jsxColumns(merchant_id)}
  246.                           rows={jsxRows(merchant_id, flyer_runs)}
  247.                           isStriped={true}
  248.                         />
  249.                       </div>
  250.                     ) : (
  251.                       <div className={css(styles.regular.emptyFlyerRuns)}>
  252.                         {/* copy */}
  253.                         <span className={css(styles.regular.emptyFlyerRunsText)}>Flyer Runs</span>
  254.                         <Button
  255.                           label="Add"
  256.                           onClick={() => addModal(merchant_id)}
  257.                           template="filled"
  258.                           additionalStyles={{
  259.                             minWidth: 'unset',
  260.                             minHeight: 'unset',
  261.                             padding: '5px 15px'
  262.                           }}
  263.                         />
  264.                       </div>
  265.                     )}
  266.                   </div>
  267.                 ))}
  268.             </div>
  269.           </div>
  270.         </div>
  271.       </RenderController>
  272.       {/* // copy */}
  273.       <Modal type="default" name="flyerRuns" width="1300px" showCloseButton={false}>
  274.         <BrowseFlyerRuns
  275.           closeModal={closeModal}
  276.           handleClick={handleClick}
  277.           selectedFlyerRuns={
  278.             values.elements.find(({ merchant_id }) => merchant_id === activeMerchant)?.flyer_runs
  279.           }
  280.           currentMerchantId={activeMerchant}
  281.         />
  282.       </Modal>
  283.       <Modal
  284.         type="delete"
  285.         name="deleteFlyerRun"
  286.         showCloseButton={false}
  287.         onConfirm={deleteFlyerRun}
  288.       />
  289.       <Modal
  290.         type="delete"
  291.         name="deleteContentGroup"
  292.         showCloseButton={false}
  293.         onConfirm={() => {
  294.           dispatch(sendDeleteContentGroup(contentGroupData.id, secondaryId, id, values.name));
  295.         }}
  296.       />
  297.     </div>
  298.   );
  299. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement