-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[candidate_parameters] Add 'Date of Birth' tab (#4915)
Add a tab to the candidate parameter's page to allow users with appropriate permissions to modify the candidate's date of birth.
- Loading branch information
Showing
8 changed files
with
242 additions
and
2 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,5 @@ | ||
INSERT INTO permissions (code, description, categoryID) VALUES | ||
("candidate_dob_edit","Edit dates of birth",2); | ||
|
||
INSERT INTO user_perm_rel VALUES | ||
(1, (SELECT permID FROM permissions WHERE code='candidate_dob_edit')); |
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,170 @@ | ||
import React, {Component} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import Loader from 'Loader'; | ||
|
||
class CandidateDOB extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
data: {}, | ||
formData: { | ||
dob: null, | ||
}, | ||
error: false, | ||
isLoaded: false, | ||
}; | ||
|
||
this.fetchData = this.fetchData.bind(this); | ||
this.setFormData = this.setFormData.bind(this); | ||
this.handleSubmit = this.handleSubmit.bind(this); | ||
} | ||
|
||
componentDidMount() { | ||
this.fetchData() | ||
.then(() => this.setState({isLoaded: true})); | ||
} | ||
|
||
fetchData() { | ||
return fetch(this.props.dataURL, {credentials: 'same-origin'}) | ||
.then((resp) => resp.json()) | ||
.then((data) => this.setState({data: data, formData: data})) | ||
.catch((error) => { | ||
this.setState({error: true}); | ||
console.error(error); | ||
}); | ||
} | ||
|
||
setFormData(formElement, value) { | ||
let formData = this.state.formData; | ||
formData[formElement] = value; | ||
this.setState({ | ||
formData: formData, | ||
}); | ||
} | ||
|
||
render() { | ||
if (this.state.error) { | ||
return <h3>An error occured while loading the page.</h3>; | ||
} | ||
|
||
if (!this.state.isLoaded) { | ||
return <Loader/>; | ||
} | ||
|
||
let disabled = true; | ||
let updateButton = null; | ||
if (loris.userHasPermission('candidate_dob_edit')) { | ||
disabled = false; | ||
updateButton = <ButtonElement label='Update' />; | ||
} | ||
return ( | ||
<div className='row'> | ||
<FormElement | ||
name='candidateDOB' | ||
onSubmit={this.handleSubmit} | ||
ref='form' | ||
class='col-md-6' | ||
> | ||
<StaticElement | ||
label='PSCID' | ||
text={this.state.data.pscid} | ||
/> | ||
<StaticElement | ||
label='DCCID' | ||
text={this.state.data.candID} | ||
/> | ||
<StaticElement | ||
label='Disclaimer:' | ||
text='Any changes to the date of birth requires an administrator to run the fix_candidate_age script.' | ||
class='form-control-static text-danger bg-danger col-sm-10' | ||
/> | ||
<DateElement | ||
label='Date Of Birth:' | ||
name='dob' | ||
value={this.state.formData.dob} | ||
onUserInput={this.setFormData} | ||
disabled={disabled} | ||
required={true} | ||
/> | ||
{updateButton} | ||
</FormElement> | ||
</div> | ||
); | ||
} | ||
|
||
/** | ||
* Handles form submission | ||
* | ||
* @param {event} e - Form submission event | ||
*/ | ||
handleSubmit(e) { | ||
e.preventDefault(); | ||
|
||
let today = new Date(); | ||
let dd = String(today.getDate()).padStart(2, '0'); | ||
let mm = String(today.getMonth() + 1).padStart(2, '0'); // January is 0! | ||
let yyyy = today.getFullYear(); | ||
today = yyyy + '-' + mm + '-' + dd; | ||
|
||
let dob = this.state.formData.dob ? | ||
this.state.formData.dob : null; | ||
if (dob > today) { | ||
swal({ | ||
title: 'Error!', | ||
text: 'Date of birth cannot be later than today!', | ||
type: 'error', | ||
confrimButtonText: 'OK', | ||
}); | ||
return; | ||
} | ||
// Set form data and upload the media file | ||
let formData = this.state.formData; | ||
let formObject = new FormData(); | ||
for (let key in formData) { | ||
if (formData.hasOwnProperty(key)) { | ||
if (formData[key] !== '') { | ||
formObject.append(key, formData[key]); | ||
} | ||
} | ||
} | ||
|
||
formObject.append('tab', this.props.tabName); | ||
|
||
fetch(this.props.action, { | ||
method: 'POST', | ||
cache: 'no-cache', | ||
credentials: 'same-origin', | ||
body: formObject, | ||
}) | ||
.then((resp) => { | ||
if (resp.ok && resp.status === 200) { | ||
swal({ | ||
title: 'Success!', | ||
text: 'Date of birth updated!', | ||
type: 'success', | ||
confrimButtonText: 'OK', | ||
}); | ||
if (result.value) { | ||
this.fetchData(); | ||
} | ||
} else { | ||
swal({ | ||
title: 'Error!', | ||
text: 'Something went wrong.', | ||
type: 'error', | ||
confrimButtonText: 'OK', | ||
}); | ||
} | ||
}) | ||
.catch((error) => { | ||
console.error(error); | ||
}); | ||
} | ||
} | ||
CandidateDOB.propTypes = { | ||
dataURL: PropTypes.string, | ||
tabName: PropTypes.string, | ||
action: PropTypes.string, | ||
}; | ||
export default CandidateDOB; |
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