All files / web/modules/admin/category/list List.tsx

0% Statements 0/43
0% Branches 0/2
0% Functions 0/8
0% Lines 0/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130                                                                                                                                                                                                                                                                   
import Button from 'antd/lib/button';
import Icon from 'antd/lib/icon';
import AntdList from 'antd/lib/list';
import * as React from 'react';
import { Mutation } from 'react-apollo';
import {
  SortableContainer as SortableContainerHoc,
  SortableElement,
  SortableHandle,
} from 'react-sortable-hoc';
import styled from 'styled-components';
import { ICategory } from '../../../../../shared';
import ErrorMessage from '../../../components/ErrorMessage';
import Loader from '../../../components/Loader';
import { SETTINGS_CATEGORIES } from '../../queries';
import { DELETE_CATEGORY, SORT_CATEGORIES } from '../mutations';
 
const Handle = styled.div`
  display: flex;
  align-items: center;
  margin-right: 10px;
`;
 
interface IProps {
  categories: ICategory[];
}
 
const DragHandle = SortableHandle(() => (
  <Handle>
    <Icon type="drag" />
  </Handle>
));
 
const SortableItem = SortableElement(
  ({ category }: { category: ICategory }) => {
    const { _id } = category;
 
    const handleClick = (deleteCategory: any) => async () =>
      deleteCategory({ variables: { _id } });
 
    return (
      <Mutation
        mutation={DELETE_CATEGORY}
        refetchQueries={[{ query: SETTINGS_CATEGORIES }]}
      >
        {(deleteCategory, { loading, error }) => {
          if (loading) {
            return <Loader />;
          }
 
          if (error) {
            return <ErrorMessage message={error.message} />;
          }
 
          return (
            <AntdList.Item
              actions={[
                <Button
                  key="delete"
                  type="danger"
                  shape="circle"
                  icon="delete"
                  onClick={handleClick(deleteCategory)}
                />,
              ]}
              style={{ zIndex: 9999 }}
            >
              <DragHandle />
              {category.title}
            </AntdList.Item>
          );
        }}
      </Mutation>
    );
  },
);
 
const SortableContainer = SortableContainerHoc(({ items }: any) => {
  const renderListItem = (category: ICategory, index: number) => (
    <SortableItem category={category} index={index} />
  );
 
  return (
    <AntdList
      size="small"
      bordered={true}
      renderItem={renderListItem}
      dataSource={items}
    />
  );
});
 
const List = ({ categories }: IProps) => (
  <Mutation
    mutation={SORT_CATEGORIES}
    refetchQueries={[{ query: SETTINGS_CATEGORIES }]}
  >
    {(sortCategories, { loading, error }) => {
      if (loading) {
        return <Loader />;
      }
 
      if (error) {
        return <ErrorMessage message={error.message} />;
      }
 
      const onSortEnd = async ({
        oldIndex,
        newIndex,
      }: {
        oldIndex: number;
        newIndex: number;
      }) => {
        sortCategories({ variables: { oldIndex, newIndex } });
      };
 
      return (
        <SortableContainer
          lockAxis="y"
          onSortEnd={onSortEnd}
          useDragHandle={true}
          items={categories}
        />
      );
    }}
  </Mutation>
);
 
export default List;