-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: separate TasksList and TasksListItem
- Loading branch information
1 parent
3609ea4
commit b73fcc9
Showing
3 changed files
with
123 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 => <TasksListItem {...args} /> | ||
Demo.args = props |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<ListItem | ||
component={Link} | ||
className={classes.link} | ||
to={`/tasks/${task.id}`} | ||
> | ||
<ListItemText | ||
primary={text} | ||
classes={{ | ||
primary: classes.text, | ||
root: classes.textWrapper, | ||
}} | ||
/> | ||
<ListItemSecondaryAction> | ||
<DeleteButton | ||
isVisible={canDelete} | ||
onClick={() => deleteTask && deleteTask(task.id)} | ||
/> | ||
</ListItemSecondaryAction> | ||
</ListItem> | ||
); | ||
}); | ||
|
||
function DeleteButton({ | ||
isVisible: canDelete, | ||
onClick, | ||
}: { | ||
onClick: () => void; | ||
isVisible: Props['canDelete']; | ||
}) { | ||
return ( | ||
<When condition={!!canDelete}> | ||
<IconButton | ||
edge="end" | ||
aria-label="Delete" | ||
onClick={() => onClick()} | ||
> | ||
<DeleteIcon /> | ||
</IconButton> | ||
</When> | ||
); | ||
} | ||
|
||
TasksListItem.displayName = 'TasksListItem'; | ||
|
||
export { TasksListItem }; |