forked from veraPDF/verapdf-webapp-gui
-
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.
veraPDF#25: Implemented option for conditional settings
Igor Poplavsky
committed
Jun 10, 2020
1 parent
44eac05
commit 89f8c81
Showing
13 changed files
with
264 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,67 @@ | ||
import React, { useMemo } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { hasFilesAttached } from '../../../../store/pdfFiles/selectors'; | ||
import { connect } from 'react-redux'; | ||
import { Redirect } from 'react-router-dom'; | ||
|
||
import AppPages from '../../../AppPages'; | ||
import { validate } from '../../../../store/job/actions'; | ||
import { hasFilesAttached } from '../../../../store/pdfFiles/selectors'; | ||
import { getUseSettings } from '../../../../store/application/selectors'; | ||
import { getJobId } from '../../../../store/job/selectors'; | ||
import Dropzone from './dropzone/Dropzone'; | ||
import WizardStep from '../../wizardStep/WizardStep'; | ||
import PageNavigation from '../../../shared/pageNavigation/PageNavigation'; | ||
import SettingsCheckbox from './settingsCheckbox/SettingsCheckbox'; | ||
|
||
function Upload(props) { | ||
const { filesAttached } = props; | ||
const forwardButton = useMemo( | ||
() => ({ | ||
label: 'Configure job', | ||
to: AppPages.SETTINGS, | ||
disabled: !filesAttached, | ||
}), | ||
[filesAttached] | ||
); | ||
function Upload({ filesAttached, isUseSettings, jobId, onValidateClick }) { | ||
const forwardButton = useMemo(() => { | ||
const button = { disabled: !filesAttached }; | ||
if (isUseSettings) { | ||
return { | ||
...button, | ||
label: 'Configure job', | ||
to: AppPages.SETTINGS, | ||
}; | ||
} | ||
return { | ||
...button, | ||
label: 'Validate', | ||
onClick: onValidateClick, | ||
}; | ||
}, [filesAttached, onValidateClick, isUseSettings]); | ||
|
||
if (!isUseSettings && jobId) { | ||
// Once job is initialized and we know its ID redirect to status page to track its progress | ||
return <Redirect push to={AppPages.STATUS.url(jobId)} />; | ||
} | ||
|
||
return ( | ||
<WizardStep stepIndex={AppPages.UPLOAD}> | ||
<Dropzone /> | ||
<PageNavigation forward={forwardButton} /> | ||
<PageNavigation back={<SettingsCheckbox />} forward={forwardButton} /> | ||
</WizardStep> | ||
); | ||
} | ||
|
||
Upload.propTypes = { | ||
jobId: PropTypes.string, | ||
isUseSettings: PropTypes.bool.isRequired, | ||
filesAttached: PropTypes.bool.isRequired, | ||
onValidateClick: PropTypes.func.isRequired, | ||
}; | ||
|
||
const mapStateToProps = state => { | ||
return { | ||
filesAttached: hasFilesAttached(state), | ||
isUseSettings: getUseSettings(state), | ||
jobId: getJobId(state), | ||
}; | ||
}; | ||
|
||
const mapDispatchToProps = dispatch => { | ||
return { | ||
onValidateClick: () => dispatch(validate()), | ||
}; | ||
}; | ||
|
||
export default connect(mapStateToProps)(Upload); | ||
export default connect(mapStateToProps, mapDispatchToProps)(Upload); |
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 |
---|---|---|
|
@@ -11,7 +11,6 @@ | |
|
||
&__container { | ||
width: 70%; | ||
max-width: 700px; | ||
height: 100px; | ||
margin: auto; | ||
padding: 20px; | ||
|
93 changes: 93 additions & 0 deletions
93
src/components/layouts/pages/upload/settingsCheckbox/SettingsCheckbox.js
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,93 @@ | ||
import React, { useCallback, useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { connect } from 'react-redux'; | ||
|
||
import { getUseSettings } from '../../../../../store/application/selectors'; | ||
import { getProfile } from '../../../../../store/job/settings/selectors'; | ||
import { getDefaultProfileLabel, getDefaultProfileName } from '../../../../../store/validationProfiles/selectors'; | ||
import { toggleUseSettings } from '../../../../../store/application/actions'; | ||
import { resetProfile } from '../../../../../store/job/settings/actions'; | ||
import Checkbox from '../../../../shared/checkbox/Checkbox'; | ||
import Dialog from '../../../../shared/dialog/Dialog'; | ||
|
||
const CHECK_SETTINGS = 'Use custom validation settings'; | ||
|
||
function SettingsCheckbox({ | ||
isUseSettings, | ||
toggleSettings, | ||
profile, | ||
defaultProfile, | ||
defaultProfileLabel, | ||
resetProfile, | ||
}) { | ||
const [resetSettingsDialogOpened, setResetSettingsDialogOpened] = useState(false); | ||
const onSettingsToggle = useCallback(() => { | ||
if (profile !== defaultProfile && isUseSettings) { | ||
return setResetSettingsDialogOpened(true); | ||
} | ||
return toggleSettings(); | ||
}, [defaultProfile, profile, toggleSettings, isUseSettings]); | ||
|
||
const onResetSettingsClose = useCallback(() => { | ||
setResetSettingsDialogOpened(false); | ||
}, []); | ||
const dialogActions = [ | ||
{ | ||
label: 'Cancel', | ||
color: 'primary', | ||
align: 'start', | ||
onClick: onResetSettingsClose, | ||
}, | ||
{ | ||
label: 'Reset settings', | ||
color: 'primary', | ||
variant: 'contained', | ||
onClick: () => { | ||
resetProfile(); | ||
toggleSettings(); | ||
onResetSettingsClose(); | ||
}, | ||
}, | ||
]; | ||
|
||
return ( | ||
<section className="settings-checkbox"> | ||
<Checkbox checked={isUseSettings} label={CHECK_SETTINGS} onChange={onSettingsToggle} /> | ||
<Dialog | ||
onClose={onResetSettingsClose} | ||
open={resetSettingsDialogOpened} | ||
actions={dialogActions} | ||
title={`You are about to reset profile to default ${defaultProfileLabel}.`} | ||
> | ||
Proceed? | ||
</Dialog> | ||
</section> | ||
); | ||
} | ||
|
||
SettingsCheckbox.propTypes = { | ||
isUseSettings: PropTypes.bool.isRequired, | ||
profile: PropTypes.string, | ||
defaultProfile: PropTypes.string, | ||
defaultProfileLabel: PropTypes.string, | ||
toggleSettings: PropTypes.func.isRequired, | ||
resetProfile: PropTypes.func.isRequired, | ||
}; | ||
|
||
const mapStateToProps = state => { | ||
return { | ||
isUseSettings: getUseSettings(state), | ||
profile: getProfile(state), | ||
defaultProfile: getDefaultProfileName(state), | ||
defaultProfileLabel: getDefaultProfileLabel(state), | ||
}; | ||
}; | ||
|
||
const mapDispatchToProps = dispatch => { | ||
return { | ||
toggleSettings: () => dispatch(toggleUseSettings()), | ||
resetProfile: () => dispatch(resetProfile()), | ||
}; | ||
}; | ||
|
||
export default connect(mapStateToProps, mapDispatchToProps)(SettingsCheckbox); |
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,32 @@ | ||
import React, { useCallback } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import MaterialCheckbox from '@material-ui/core/Checkbox'; | ||
import FormControlLabel from '@material-ui/core/FormControlLabel'; | ||
|
||
function Checkbox({ checked, label, disabled, onChange }) { | ||
const handleChange = useCallback(e => onChange(e.target.checked), [onChange]); | ||
|
||
return ( | ||
<FormControlLabel | ||
className="checkbox-container" | ||
disabled={disabled} | ||
label={label} | ||
control={<MaterialCheckbox checked={checked} color="primary" onChange={handleChange} />} | ||
/> | ||
); | ||
} | ||
|
||
Checkbox.propTypes = { | ||
checked: PropTypes.bool.isRequired, | ||
label: PropTypes.string, | ||
disabled: PropTypes.bool, | ||
onChange: PropTypes.func.isRequired, | ||
}; | ||
|
||
Checkbox.defaultProps = { | ||
label: '', | ||
disabled: false, | ||
}; | ||
|
||
export default Checkbox; |
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 |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
flex-grow: 1; | ||
flex-basis: 0; | ||
justify-content: center; | ||
align-items: start; | ||
|
||
.app-link { | ||
color: inherit; | ||
|
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
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 |
---|---|---|
@@ -1,15 +1,36 @@ | ||
import { handleActions } from 'redux-actions'; | ||
import { USE_SETTINGS_FLAG } from '../constants'; | ||
|
||
const DEFAULT_STATE = { | ||
initialized: false, | ||
locked: false, | ||
useSettings: false, | ||
}; | ||
|
||
export const getDefaultState = () => { | ||
let useSettings = sessionStorage.getItem(USE_SETTINGS_FLAG); | ||
|
||
if (!useSettings) { | ||
return DEFAULT_STATE; | ||
} | ||
|
||
useSettings = JSON.parse(useSettings); | ||
return { | ||
...DEFAULT_STATE, | ||
useSettings, | ||
}; | ||
}; | ||
|
||
export default handleActions( | ||
{ | ||
APP_RESET: () => DEFAULT_STATE, | ||
APP_STARTUP_FINISH: state => ({ ...state, initialized: true }), | ||
APP_LOCK_SET: (state, { payload: locked }) => ({ ...state, locked }), | ||
USE_SETTINGS_TOGGLE: state => { | ||
const useSettings = !state.useSettings; | ||
sessionStorage.setItem(USE_SETTINGS_FLAG, useSettings); | ||
return { ...state, useSettings }; | ||
}, | ||
}, | ||
DEFAULT_STATE | ||
getDefaultState() | ||
); |
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
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 |
---|---|---|
@@ -1,9 +1,15 @@ | ||
import { createAction } from 'redux-actions'; | ||
import { JOB_SETTINGS } from '../../constants'; | ||
import { getDefaultProfileName } from '../../validationProfiles/selectors'; | ||
|
||
const updateSettings = createAction('SETTINGS_UPDATE'); | ||
|
||
export const setSetting = (setting, value) => async (dispatch, getState) => { | ||
await dispatch(updateSettings({ [setting]: value })); | ||
sessionStorage.setItem(JOB_SETTINGS, JSON.stringify(getState().jobSettings)); | ||
}; | ||
|
||
export const resetProfile = () => async (dispatch, getState) => { | ||
const profile = getDefaultProfileName(getState()); | ||
dispatch(updateSettings({ profile })); | ||
}; |
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