Advertisement
Maikl5700

React Mobx

Nov 26th, 2020 (edited)
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. import React, { useState, useEffect } from 'react';
  2. import { observer } from 'mobx-react';
  3. import useStores from 'src/hooks/useStores';
  4. import { Select, Button } from 'antd';
  5. import getRequestColumns from './columns';
  6. import getCompanyColumns from '../CompaniesTable/columns';
  7.  
  8. const { Option } = Select;
  9. const getColumns = {
  10. company: getCompanyColumns,
  11. request: getRequestColumns,
  12. };
  13. export default observer(({ tableName }) => {
  14. const [showSelect, setShowSelect] = useState(false);
  15.  
  16. const {
  17. selectedColumnsStore,
  18. selectedColumnsStore: { applySelectedColumns, resetSelectedColumns, selectedColumns, setState },
  19. authStore: { moderatorLevel },
  20. } = useStores();
  21.  
  22. useEffect(() => {
  23. setState({ selectedColumns: selectedColumnsStore[`${tableName}AppliedColumns`] });
  24. }, [selectedColumnsStore, setState, tableName]);
  25.  
  26. const handleChange = value => {
  27. setState({ selectedColumns: value });
  28. };
  29.  
  30. const handleSave = () => {
  31. applySelectedColumns(tableName);
  32. setShowSelect(false);
  33. };
  34.  
  35. const handleReset = () => {
  36. resetSelectedColumns(tableName);
  37. };
  38.  
  39. const columnsFilteredByRole = getColumns[tableName](moderatorLevel);
  40. const appliedColumns = selectedColumnsStore[`${tableName}AppliedColumns`];
  41.  
  42. return (
  43. <div style={{ display: 'flex', justifyContent: 'flex-end', width: '100%', marginBottom: 10 }}>
  44. {showSelect ? (
  45. <>
  46. <Select
  47. mode="multiple"
  48. value={selectedColumns}
  49. style={{ width: '100%' }}
  50. placeholder="Выберите столбцы таблицы"
  51. defaultValue={appliedColumns.length ? appliedColumns : []}
  52. onChange={handleChange}
  53. >
  54. {columnsFilteredByRole.map((col, i) => (
  55. <Option key={i}>{col.title}</Option>
  56. ))}
  57. </Select>
  58. <Button style={{ margin: '0 10px' }} type="primary" onClick={handleSave}>
  59. Сохранить
  60. </Button>
  61. <Button type="primary" onClick={handleReset}>
  62. Сбросить
  63. </Button>
  64. </>
  65. ) : (
  66. <Button onClick={() => setShowSelect(true)} type="primary">
  67. Выбрать столбцы ({appliedColumns.length || columnsFilteredByRole.length}/
  68. {columnsFilteredByRole.length})
  69. </Button>
  70. )}
  71. </div>
  72. );
  73. });
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement