|
| 1 | +import React, { useState, useContext, useEffect } from 'react'; |
| 2 | +import { makeStyles } from '@material-ui/core/styles'; |
| 3 | +import Button from '@material-ui/core/Button'; |
| 4 | +import Avatar from '@material-ui/core/Avatar'; |
| 5 | +import List from '@material-ui/core/List'; |
| 6 | +import ListItem from '@material-ui/core/ListItem'; |
| 7 | +import ListItemAvatar from '@material-ui/core/ListItemAvatar'; |
| 8 | +import ListItemText from '@material-ui/core/ListItemText'; |
| 9 | +import DialogTitle from '@material-ui/core/DialogTitle'; |
| 10 | +import Dialog from '@material-ui/core/Dialog'; |
| 11 | +import DeleteRoundedIcon from '@material-ui/icons/DeleteRounded'; |
| 12 | +import { blue } from '@material-ui/core/colors'; |
| 13 | + |
| 14 | +import { |
| 15 | + getProjects, |
| 16 | + deleteProject |
| 17 | +} from '../../helperFunctions/projectGetSaveDel'; |
| 18 | +import localforage from 'localforage'; |
| 19 | +import { stateContext } from '../../context/context'; |
| 20 | +import initialState from '../../context/initialState'; |
| 21 | + |
| 22 | +export interface ProjectDialogProps { |
| 23 | + open: boolean; |
| 24 | + projects: Array<Object>; |
| 25 | + onClose: () => void; |
| 26 | +} |
| 27 | + |
| 28 | +// The options to be rendered when dialog is open |
| 29 | +function ProjectsDialog(props: ProjectDialogProps) { |
| 30 | + const classes = useStyles(); |
| 31 | + const { onClose, open, projects } = props; |
| 32 | + const [state, dispatch] = useContext(stateContext); |
| 33 | + |
| 34 | + // If no projects selected, keep the name of the current displayed |
| 35 | + const handleClose = () => { |
| 36 | + // onClose(selectedValue); |
| 37 | + onClose(); |
| 38 | + }; |
| 39 | + |
| 40 | + // If new project selected, close and set value to new project name |
| 41 | + const handleDelete = (value: string) => { |
| 42 | + const selectedProject = projects.filter( |
| 43 | + (project: any) => project.name === value |
| 44 | + )[0]; |
| 45 | + console.log('selectedProject is', selectedProject); |
| 46 | + deleteProject(selectedProject); |
| 47 | + localforage.clear(); |
| 48 | + dispatch({ type: 'SET INITIAL STATE', payload: initialState }); |
| 49 | + onClose(); |
| 50 | + }; |
| 51 | + |
| 52 | + return ( |
| 53 | + <Dialog |
| 54 | + onClose={handleClose} |
| 55 | + aria-labelledby="project-dialog-title" |
| 56 | + open={open} |
| 57 | + > |
| 58 | + <DialogTitle id="project-dialog-title">Delete Project</DialogTitle> |
| 59 | + <List> |
| 60 | + {projects.map((project: any, index: number) => ( |
| 61 | + <ListItem |
| 62 | + button |
| 63 | + onClick={() => handleDelete(project.name)} |
| 64 | + key={index} |
| 65 | + > |
| 66 | + <ListItemAvatar> |
| 67 | + <Avatar className={classes.avatar}> |
| 68 | + <DeleteRoundedIcon /> |
| 69 | + </Avatar> |
| 70 | + </ListItemAvatar> |
| 71 | + <ListItemText primary={project.name} /> |
| 72 | + </ListItem> |
| 73 | + ))} |
| 74 | + </List> |
| 75 | + </Dialog> |
| 76 | + ); |
| 77 | +} |
| 78 | + |
| 79 | +export default function ProjectsFolder() { |
| 80 | + const [open, setOpen] = useState(false); |
| 81 | + const [projects, setProjects] = useState([{ hello: 'cat' }]); |
| 82 | + |
| 83 | + const classes = useStyles(); |
| 84 | + |
| 85 | + const handleClickOpen = () => { |
| 86 | + getProjects().then(data => { |
| 87 | + if (data) { |
| 88 | + setProjects(data); |
| 89 | + setOpen(true); |
| 90 | + } |
| 91 | + }); |
| 92 | + }; |
| 93 | + |
| 94 | + const handleClose = () => { |
| 95 | + setOpen(false); |
| 96 | + }; |
| 97 | + |
| 98 | + return ( |
| 99 | + <div> |
| 100 | + <Button |
| 101 | + className={classes.button} |
| 102 | + variant="outlined" |
| 103 | + color="primary" |
| 104 | + onClick={handleClickOpen} |
| 105 | + endIcon={<DeleteRoundedIcon />} |
| 106 | + > |
| 107 | + Delete Project |
| 108 | + </Button> |
| 109 | + <ProjectsDialog open={open} onClose={handleClose} projects={projects} /> |
| 110 | + </div> |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +const useStyles = makeStyles({ |
| 115 | + button: { |
| 116 | + width: '55%', |
| 117 | + backgroundColor: 'rgba(1,212,109,0.1)', |
| 118 | + fontSize: '1em', |
| 119 | + minWidth: '300px', |
| 120 | + marginTop: '10px', |
| 121 | + marginBotton: '10px' |
| 122 | + }, |
| 123 | + avatar: { |
| 124 | + backgroundColor: blue[100], |
| 125 | + color: blue[600] |
| 126 | + } |
| 127 | +}); |
0 commit comments