-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #769 from open-formulieren/issue/4929-react-router…
…-upgrade Convert cosign v5 react-router routes to data router format
- Loading branch information
Showing
12 changed files
with
386 additions
and
224 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
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,141 @@ | ||
import PropTypes from 'prop-types'; | ||
import {useContext} from 'react'; | ||
import {FormattedMessage} from 'react-intl'; | ||
import {useAsync} from 'react-use'; | ||
|
||
import {ConfigContext, SubmissionContext} from 'Context'; | ||
import {get} from 'api'; | ||
import Body from 'components/Body'; | ||
import ErrorMessage from 'components/Errors/ErrorMessage'; | ||
import Loader from 'components/Loader'; | ||
import LoginOptionsDisplay from 'components/LoginOptions/LoginOptionsDisplay'; | ||
import {getLoginUrl} from 'components/utils'; | ||
import Types from 'types'; | ||
import {getBEMClassName} from 'utils'; | ||
|
||
const getCosignStatus = async (baseUrl, submissionUuid) => { | ||
const endpoint = `${baseUrl}submissions/${submissionUuid}/co-sign`; | ||
return await get(endpoint); | ||
}; | ||
|
||
const CoSignAuthentication = ({form, submissionUuid, authPlugin}) => { | ||
const loginOption = form.loginOptions.find(opt => opt.identifier === authPlugin); | ||
if (!loginOption) { | ||
return ( | ||
<ErrorMessage> | ||
<FormattedMessage | ||
description="Co-sign auth option not available on form" | ||
defaultMessage="Something went wrong while presenting the login option. Please contact the municipality." | ||
/> | ||
</ErrorMessage> | ||
); | ||
} | ||
|
||
// add the co-sign submission parameter to the login URL | ||
const loginUrl = getLoginUrl(loginOption, {coSignSubmission: submissionUuid}); | ||
const modifiedLoginOption = { | ||
...loginOption, | ||
url: loginUrl, | ||
label: ( | ||
<FormattedMessage | ||
description="Login button label" | ||
defaultMessage="Login with {provider}" | ||
values={{provider: loginOption.label}} | ||
/> | ||
), | ||
}; | ||
|
||
return ( | ||
<LoginOptionsDisplay | ||
loginAsYourselfOptions={[modifiedLoginOption]} | ||
loginAsGemachtigdeOptions={[]} | ||
/> | ||
); | ||
}; | ||
|
||
CoSignAuthentication.propTypes = { | ||
form: Types.Form.isRequired, | ||
submissionUuid: PropTypes.string.isRequired, | ||
authPlugin: PropTypes.string.isRequired, | ||
}; | ||
|
||
const CoSignOld = ({ | ||
submissionUuid, | ||
interactive = true, | ||
form = null, | ||
saveStepData, | ||
authPlugin = 'digid-mock', | ||
}) => { | ||
const {baseUrl} = useContext(ConfigContext); | ||
const {submission} = useContext(SubmissionContext); | ||
|
||
if (!submissionUuid) { | ||
submissionUuid = submission.id; | ||
} | ||
|
||
const { | ||
loading, | ||
value: coSignState, | ||
error, | ||
} = useAsync( | ||
async () => await getCosignStatus(baseUrl, submissionUuid), | ||
[baseUrl, submissionUuid] | ||
); | ||
|
||
// log errors to the console if any | ||
if (error) console.error(error); | ||
|
||
// while loading, display spinner | ||
if (loading) { | ||
return <Loader modifiers={['small']} />; | ||
} | ||
const {coSigned, representation} = coSignState; | ||
|
||
if (!coSigned) { | ||
if (!interactive) { | ||
return ( | ||
<FormattedMessage | ||
description="Not co-signed (summary) message" | ||
defaultMessage="Not co-signed" | ||
/> | ||
); | ||
} | ||
|
||
if (!form || !saveStepData) { | ||
throw new Error('Interactive co-sign components require the "form" and "saveStepData" props'); | ||
} | ||
|
||
return ( | ||
<CoSignAuthentication | ||
form={form} | ||
submissionUuid={submissionUuid} | ||
saveStepData={saveStepData} | ||
authPlugin={authPlugin} | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<Body component="div"> | ||
<div className={getBEMClassName('co-sign__representation')}> | ||
{representation ?? ( | ||
<FormattedMessage | ||
description="Co-signed without representation fallback message" | ||
defaultMessage="Something went wrong while processing the co-sign authentication. Please contact the municipality." | ||
/> | ||
)} | ||
</div> | ||
</Body> | ||
); | ||
}; | ||
|
||
CoSignOld.propTypes = { | ||
interactive: PropTypes.bool, | ||
form: Types.Form, | ||
submissionUuid: PropTypes.string, // fall back to context if not provided | ||
saveStepData: PropTypes.func, | ||
authPlugin: PropTypes.string, | ||
}; | ||
|
||
export default CoSignOld; | ||
export {CoSignAuthentication}; |
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,29 @@ | ||
import PropTypes from 'prop-types'; | ||
import React, {useContext} from 'react'; | ||
|
||
const CosignContext = React.createContext({ | ||
reportDownloadUrl: '', | ||
onCosignComplete: () => {}, | ||
}); | ||
|
||
CosignContext.displayName = 'CosignContext'; | ||
|
||
const CosignProvider = ({reportDownloadUrl, onCosignComplete, children}) => ( | ||
<CosignContext.Provider | ||
value={{ | ||
reportDownloadUrl, | ||
onCosignComplete, | ||
}} | ||
> | ||
{children} | ||
</CosignContext.Provider> | ||
); | ||
|
||
CosignProvider.propTypes = { | ||
reportDownloadUrl: PropTypes.string.isRequired, | ||
onCosignComplete: PropTypes.func.isRequired, | ||
}; | ||
|
||
const useCosignContext = () => useContext(CosignContext); | ||
|
||
export {CosignProvider, useCosignContext}; |
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
Oops, something went wrong.