From b73fcc9bc2eb5e314793790a9ed9de441021fa4b Mon Sep 17 00:00:00 2001 From: undeadlol1 Date: Fri, 6 Nov 2020 16:16:30 +0500 Subject: [PATCH] feat: separate TasksList and TasksListItem --- src/components/tasks/TasksList/TasksList.tsx | 80 +++------------ .../tasks/TasksListItem.stories.tsx | 13 +++ src/components/tasks/TasksListItem.tsx | 97 +++++++++++++++++++ 3 files changed, 123 insertions(+), 67 deletions(-) create mode 100644 src/components/tasks/TasksListItem.stories.tsx create mode 100644 src/components/tasks/TasksListItem.tsx diff --git a/src/components/tasks/TasksList/TasksList.tsx b/src/components/tasks/TasksList/TasksList.tsx index 36e5848d..887b9c10 100644 --- a/src/components/tasks/TasksList/TasksList.tsx +++ b/src/components/tasks/TasksList/TasksList.tsx @@ -1,24 +1,19 @@ import { Theme } from '@material-ui/core'; import Box from '@material-ui/core/Box'; -import IconButton from '@material-ui/core/IconButton'; import List from '@material-ui/core/List'; -import ListItem from '@material-ui/core/ListItem'; -import ListItemSecondaryAction from '@material-ui/core/ListItemSecondaryAction'; -import ListItemText from '@material-ui/core/ListItemText'; import Paper from '@material-ui/core/Paper'; import { makeStyles } from '@material-ui/core/styles'; -import DeleteIcon from '@material-ui/icons/Delete'; import Pagination from '@material-ui/lab/Pagination'; import debug from 'debug'; import get from 'lodash/get'; import isEmpty from 'lodash/isEmpty'; import React, { useState } from 'react'; import { When } from 'react-if'; -import { Link } from 'react-router-dom'; -import { useTypedSelector } from '../../../store/index'; +import { tasksPerPage } from '../../../contants'; import { Task } from '../../../entities/Task'; +import { useTypedSelector } from '../../../store/index'; import { tasksSelector } from '../../../store/selectors'; -import { tasksPerPage } from '../../../contants'; +import { TasksListItem } from '../TasksListItem'; const log = debug('TasksList'); @@ -31,7 +26,7 @@ interface TasksListProps { export function TasksList({ loading, - tasks, + tasks = [], canDelete, deleteTask, }: TasksListProps) { @@ -47,38 +42,24 @@ export function TasksList({ log('page: ', page); log('numberOfPAges: ', numberOfPAges); - if (loading) return null; - if (isEmpty(tasks) || get(tasks, 'empty')) return null; + if (loading || isEmpty(tasks) || get(tasks, 'empty')) { + return null; + } return ( {tasks.slice(sliceTasksFrom, sliceTasksTo).map((task) => { - const text = get(task, 'subtasks[0].name', task.name); return ( - - - - deleteTask && deleteTask(task.id)} - /> - - + task={task} + canDelete={canDelete} + deleteTask={deleteTask} + /> ); })} - tasksPerPage}> + tasksPerPage}> void; - isVisible: TasksListProps['canDelete']; -}) { - return ( - - onClick()} - > - - - - ); -} - function useStyles() { return makeStyles((theme: Theme) => { return { list: { width: '100%', }, - link: { - textDecoration: 'none', - color: theme.palette.text.primary, - }, - textWrapper: { - overflow: 'hidden', - textOverflow: 'ellipsis', - [theme.breakpoints.down('sm')]: { - whiteSpace: 'nowrap', - maxWidth: '100%', - }, - }, - text: { - display: 'inline', - }, paper: { width: '10000px', maxWidth: '100%', diff --git a/src/components/tasks/TasksListItem.stories.tsx b/src/components/tasks/TasksListItem.stories.tsx new file mode 100644 index 00000000..af764332 --- /dev/null +++ b/src/components/tasks/TasksListItem.stories.tsx @@ -0,0 +1,13 @@ +import React from 'react'; +import { sections } from '../storybookContants'; +import { TasksListItem } from './TasksListItem'; + +export default { + component: TasksListItem, + title: sections.unsorted + 'TasksListItem', +}; + +const props = {} + +export const Demo = args => +Demo.args = props diff --git a/src/components/tasks/TasksListItem.tsx b/src/components/tasks/TasksListItem.tsx new file mode 100644 index 00000000..c13af790 --- /dev/null +++ b/src/components/tasks/TasksListItem.tsx @@ -0,0 +1,97 @@ +import React, { memo } from 'react'; +import { + Box, + IconButton, + ListItem, + ListItemSecondaryAction, + ListItemText, + Theme, +} from '@material-ui/core'; +import { makeStyles } from '@material-ui/styles'; +import { Task } from '../../entities/Task'; +import TaskService from '../../services/TaskService'; +import get from 'lodash/get'; +import { Link } from 'react-router-dom'; +import DeleteIcon from '@material-ui/icons/Delete'; +import { When } from 'react-if'; + +const useStyles = makeStyles((theme: Theme) => ({ + link: { + textDecoration: 'none', + color: theme.palette.text.primary, + }, + textWrapper: { + overflow: 'hidden', + textOverflow: 'ellipsis', + [theme.breakpoints.down('sm')]: { + whiteSpace: 'nowrap', + maxWidth: '100%', + }, + }, + text: { + display: 'inline', + }, +})); + +interface Props { + task: Task; + canDelete?: boolean; + deleteTask?: (id: string) => void; +} + +const TasksListItem = memo(function TasksListItem({ + task, + canDelete, + deleteTask, +}: Props) { + const classes = useStyles(); + + const isStale = TaskService.isStale(task); + console.log('isStale: ', isStale); + const text = get(task, 'subtasks[0].name', task.name); + return ( + + + + deleteTask && deleteTask(task.id)} + /> + + + ); +}); + +function DeleteButton({ + isVisible: canDelete, + onClick, +}: { + onClick: () => void; + isVisible: Props['canDelete']; +}) { + return ( + + onClick()} + > + + + + ); +} + +TasksListItem.displayName = 'TasksListItem'; + +export { TasksListItem };