Skip to content

Commit

Permalink
Merge pull request #81 from CSCfi/feature/consistent-alerts
Browse files Browse the repository at this point in the history
Feature/consistent alerts
  • Loading branch information
saulipurhonen authored Oct 8, 2020
2 parents 74bd74d + cc12ca8 commit 8ce12d4
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 82 deletions.
3 changes: 2 additions & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"printWidth": 120,
"semi": false,
"arrowParens": "avoid"
"arrowParens": "avoid",
"endOfLine": "auto"
}
46 changes: 9 additions & 37 deletions src/components/NewDraftWizard/WizardComponents/ObjectIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import React, { useState } from "react"
import MuiAccordion from "@material-ui/core/Accordion"
import MuiAccordionDetails from "@material-ui/core/AccordionDetails"
import MuiAccordionSummary from "@material-ui/core/AccordionSummary"
import Button from "@material-ui/core/Button"
import Dialog from "@material-ui/core/Dialog"
import DialogActions from "@material-ui/core/DialogActions"
import DialogContent from "@material-ui/core/DialogContent"
import DialogContentText from "@material-ui/core/DialogContentText"
import DialogTitle from "@material-ui/core/DialogTitle"
import List from "@material-ui/core/List"
import ListItem from "@material-ui/core/ListItem"
import ListItemText from "@material-ui/core/ListItemText"
Expand All @@ -18,6 +12,8 @@ import Typography from "@material-ui/core/Typography"
import NoteAddIcon from "@material-ui/icons/NoteAdd"
import { useDispatch, useSelector } from "react-redux"

import WizardAlert from "./WizardAlert"

import { setObjectType } from "features/objectTypeSlice"
import { setSubmissionType } from "features/submissionTypeSlice"

Expand Down Expand Up @@ -90,36 +86,6 @@ const AccordionDetails = withStyles({
},
})(MuiAccordionDetails)

/*
* Render alert for form cancellation options
*/
const CancelFormDialog = ({ open, handleCancelling }: { open: boolean, handleCancelling: boolean => void }) => (
<Dialog
open={open}
onClose={() => handleCancelling(false)}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
<DialogTitle id="alert-dialog-title">{"Would you like to save draft version of this form?"}</DialogTitle>
<DialogContent>
<DialogContentText id="alert-dialog-description">
If you save form as a draft, you can continue filling it later.
</DialogContentText>
</DialogContent>
<DialogActions>
<Button variant="contained" onClick={() => handleCancelling(false)} color="secondary">
Cancel
</Button>
<Button variant="contained" onClick={() => handleCancelling(true)} color="primary">
Do not save
</Button>
<Button variant="contained" onClick={() => handleCancelling(true)} color="primary">
Save
</Button>
</DialogActions>
</Dialog>
)

/*
* Render list of submission types to be used in accordions
*/
Expand Down Expand Up @@ -224,7 +190,13 @@ const ObjectIndex = () => {
</Accordion>
)
})}
<CancelFormDialog open={cancelFormOpen} handleCancelling={handleCancelling} />
{cancelFormOpen && (
<WizardAlert
handleAlert={handleCancelling}
parentLocation="submission"
alertType={currentSubmissionType}
></WizardAlert>
)}
</div>
)
}
Expand Down
198 changes: 198 additions & 0 deletions src/components/NewDraftWizard/WizardComponents/WizardAlert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
//@flow
import React from "react"

import Button from "@material-ui/core/Button"
import Dialog from "@material-ui/core/Dialog"
import DialogActions from "@material-ui/core/DialogActions"
import DialogContent from "@material-ui/core/DialogContent"
import DialogContentText from "@material-ui/core/DialogContentText"
import DialogTitle from "@material-ui/core/DialogTitle"
import Link from "@material-ui/core/Link"
import { useSelector } from "react-redux"
import { Link as RouterLink } from "react-router-dom"

/*
* Dialog contents are rendered based on parent component location and alert type
*/
const CancelFormDialog = ({
handleDialog,
alertType,
parentLocation,
currentSubmissionType,
}: {
handleDialog: boolean => void,
alertType: string,
parentLocation: string,
currentSubmissionType: string,
}) => {
let [dialogTitle, dialogContent] = ["", ""]
let dialogActions
const formContent = "If you save form as a draft, you can continue filling it later."
const xmlContent = "If you save xml as a draft, you can upload it later."
const objectContent = "If you save object as a draft, you can upload it later."
switch (parentLocation) {
case "submission": {
switch (alertType) {
case "form": {
dialogTitle = "Would you like to save draft version of this form"
dialogContent = formContent
break
}
case "xml": {
dialogTitle = "Would you like to save draft version of this xml upload"
dialogContent = xmlContent
break
}
case "existing": {
dialogTitle = "Would you like to save draft version of this existing object upload"
dialogContent = objectContent
break
}
default: {
dialogTitle = "default"
dialogContent = "default content"
}
}
dialogActions = (
<DialogActions>
<Button variant="contained" onClick={() => handleDialog(false)} color="secondary">
Cancel
</Button>
<Button variant="contained" onClick={() => handleDialog(true)} color="primary">
Do not save
</Button>
<Button variant="contained" onClick={() => handleDialog(true)} color="primary">
Save
</Button>
</DialogActions>
)
break
}
case "footer": {
switch (alertType) {
case "cancel": {
dialogTitle = "Cancel creating a submission folder?"
dialogContent =
"If you cancel creating submission folder, the folder and its content will not be saved anywhere."
dialogActions = (
<DialogActions>
<Button variant="outlined" onClick={() => handleDialog(false)} color="primary" autoFocus>
No, continue creating the folder
</Button>
<Link component={RouterLink} aria-label="Cancel a new folder and move to frontpage" to="/home">
<Button variant="contained" onClick={() => handleDialog(true)} color="primary">
Yes, cancel creating folder
</Button>
</Link>
</DialogActions>
)
break
}
case "save": {
dialogTitle = "Folder saved"
dialogContent = "Folder has been saved"
dialogActions = (
<DialogActions style={{ justifyContent: "center" }}>
<Link component={RouterLink} aria-label="Cancel a new folder and move to frontpage" to="/">
<Button variant="contained" onClick={() => handleDialog(true)} color="primary">
Return to homepage
</Button>
</Link>
</DialogActions>
)
break
}
default: {
dialogTitle = "default"
dialogContent = "default content"
}
}
break
}
case "stepper": {
dialogTitle = "Move to " + alertType + " step?"
dialogContent = "You have unsaved data. You can save current form as draft"
switch (currentSubmissionType) {
case "form": {
dialogContent = formContent
break
}
case "xml": {
dialogContent = xmlContent
break
}
case "existing": {
dialogContent = objectContent
break
}
default: {
dialogContent = "default content"
}
}
dialogActions = (
<DialogActions>
<Button variant="contained" onClick={() => handleDialog(false)} color="secondary">
Cancel
</Button>
<Button variant="contained" onClick={() => handleDialog(true)} color="primary">
Navigate without saving
</Button>
<Button variant="contained" onClick={() => handleDialog(true)} color="primary">
Save and navigate
</Button>
</DialogActions>
)
break
}
default: {
dialogTitle = "default"
dialogContent = "default content"
}
}

return (
<Dialog
open={true}
onClose={() => handleDialog(false)}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
<DialogTitle id="alert-dialog-title">{dialogTitle}</DialogTitle>
<DialogContent>
<DialogContentText id="alert-dialog-description">{dialogContent}</DialogContentText>
</DialogContent>
{dialogActions}
</Dialog>
)
}

/*
* Render alert form based on location and type
*/
const WizardAlert = ({
handleAlert,
parentLocation,
alertType,
}: {
handleAlert: boolean => void,
parentLocation: string,
alertType: string,
}) => {
const currentSubmissionType = useSelector(state => state.submissionType)
const handleDialog = (action: boolean) => {
handleAlert(action)
}

return (
<div>
<CancelFormDialog
handleDialog={handleDialog}
alertType={alertType}
parentLocation={parentLocation}
currentSubmissionType={currentSubmissionType}
/>
</div>
)
}

export default WizardAlert
54 changes: 13 additions & 41 deletions src/components/NewDraftWizard/WizardComponents/WizardFooter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@
import React, { useState } from "react"

import Button from "@material-ui/core/Button"
import Dialog from "@material-ui/core/Dialog"
import DialogActions from "@material-ui/core/DialogActions"
import DialogContent from "@material-ui/core/DialogContent"
import DialogContentText from "@material-ui/core/DialogContentText"
import DialogTitle from "@material-ui/core/DialogTitle"
import Link from "@material-ui/core/Link"
import { makeStyles } from "@material-ui/core/styles"
import { useDispatch, useSelector } from "react-redux"
import { Link as RouterLink } from "react-router-dom"

import FormAlert from "./WizardAlert"

import { resetObjectType } from "features/objectTypeSlice"
import { deleteFolderAndContent } from "features/submissionFolderSlice"
Expand Down Expand Up @@ -42,35 +37,6 @@ const useStyles = makeStyles(theme => ({
},
}))

/*
* Render alert for wizard cancellation
*/
const CancelDialog = ({ open, handleCancel }: { open: boolean, handleCancel: boolean => void }) => (
<Dialog
open={open}
onClose={() => handleCancel(false)}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
<DialogTitle id="alert-dialog-title">{"Cancel creating a submission folder?"}</DialogTitle>
<DialogContent>
<DialogContentText id="alert-dialog-description">
If you cancel creating submission folder, the folder and its content will not be saved anywhere.
</DialogContentText>
</DialogContent>
<DialogActions>
<Button variant="outlined" onClick={() => handleCancel(false)} color="primary" autoFocus>
No, continue creating the folder
</Button>
<Link component={RouterLink} aria-label="Cancel a new folder and move to frontpage" to="/home">
<Button variant="contained" onClick={() => handleCancel(true)} color="primary">
Yes, cancel creating folder
</Button>
</Link>
</DialogActions>
</Dialog>
)

/**
* Define wizard footer with changing button actions.
*/
Expand All @@ -79,15 +45,16 @@ const WizardFooter = () => {
const dispatch = useDispatch()
const wizardStep = useSelector(state => state.wizardStep)
const folder = useSelector(state => state.submissionFolder)
const [cancelDialogOpen, setCancelDialogOpen] = useState(false)
const [dialogOpen, setDialogOpen] = useState(false)
const [alertType, setAlertType] = useState("")

const handleCancel = cancelWizard => {
if (cancelWizard) {
dispatch(resetWizard())
dispatch(resetObjectType())
dispatch(deleteFolderAndContent(folder))
} else {
setCancelDialogOpen(false)
setDialogOpen(false)
}
}

Expand All @@ -99,7 +66,10 @@ const WizardFooter = () => {
<Button
variant="contained"
color="secondary"
onClick={() => setCancelDialogOpen(true)}
onClick={() => {
setDialogOpen(true)
setAlertType("cancel")
}}
className={classes.footerButton}
>
Cancel
Expand All @@ -112,8 +82,10 @@ const WizardFooter = () => {
color="primary"
disabled={wizardStep < 1}
className={classes.footerButton}
// TODO: Implement save functionality
onClick={() => {
console.log("This should save and exit!")
setDialogOpen(true)
setAlertType("save")
}}
>
Save and Exit
Expand All @@ -130,7 +102,7 @@ const WizardFooter = () => {
</div>
)}
</div>
<CancelDialog open={cancelDialogOpen} handleCancel={handleCancel} />
{dialogOpen && <FormAlert handleAlert={handleCancel} parentLocation="footer" alertType={alertType}></FormAlert>}
</div>
)
}
Expand Down
Loading

0 comments on commit 8ce12d4

Please sign in to comment.