diff --git a/src/shared/components/Settings/Profile/Education/index.jsx b/src/shared/components/Settings/Profile/Education/index.jsx index 844aea8221..6ad7229999 100644 --- a/src/shared/components/Settings/Profile/Education/index.jsx +++ b/src/shared/components/Settings/Profile/Education/index.jsx @@ -14,6 +14,7 @@ import ConsentComponent from 'components/Settings/ConsentComponent'; import { PrimaryButton } from 'topcoder-react-ui-kit'; import DatePicker from 'components/challenge-listing/Filters/DatePicker'; import ErrorMessage from 'components/Settings/ErrorMessage'; +import { validateStartDate, validateEndDate } from 'utils/settings'; import ConfirmationModal from '../../CofirmationModal'; import EducationList from './List'; @@ -39,6 +40,10 @@ export default class Education extends ConsentComponent { const { userTraits } = props; this.state = { formInvalid: false, + startDateInvalid: false, + startDateInvalidMsg: '', + endDateInvalid: false, + endDateInvalidMsg: '', educationTrait: this.loadEducationTrait(userTraits), personalizationTrait: this.loadPersonalizationTrait(userTraits), newEducation: { @@ -48,7 +53,7 @@ export default class Education extends ConsentComponent { timePeriodTo: '', graduated: false, }, - inputChanged: false, + isSubmit: false, isMobileView: false, screenSM: 767, isEdit: false, @@ -69,7 +74,11 @@ export default class Education extends ConsentComponent { educationTrait, personalizationTrait, formInvalid: false, - inputChanged: false, + startDateInvalid: false, + startDateInvalidMsg: '', + endDateInvalid: false, + endDateInvalidMsg: '', + isSubmit: false, newEducation: { schoolCollegeName: '', major: '', @@ -91,30 +100,24 @@ export default class Education extends ConsentComponent { */ onCheckFormValue(newEducation) { let invalid = false; - const currentDate = new Date().setHours(0, 0, 0, 0); if (!_.trim(newEducation.schoolCollegeName).length) { invalid = true; } - if (!_.isEmpty(newEducation.timePeriodFrom) && _.isEmpty(newEducation.timePeriodTo)) { - const fromDate = new Date(newEducation.timePeriodFrom).setHours(0, 0, 0, 0); - - if (fromDate > currentDate) { - invalid = true; // Start Date should be in past or current - } - } else if (!_.isEmpty(newEducation.timePeriodFrom) && !_.isEmpty(newEducation.timePeriodTo)) { - const fromDate = new Date(newEducation.timePeriodFrom).setHours(0, 0, 0, 0); - const toDate = new Date(newEducation.timePeriodTo).setHours(0, 0, 0, 0); - - if (fromDate > currentDate // Start Date is in past or current - || fromDate >= toDate // Start Date is before End Date - || (newEducation.graduated && toDate > currentDate)) { // End Date is in past or current - invalid = true; - } - } + const fromDateValidResult = validateStartDate(newEducation.graduated, + newEducation.timePeriodFrom, newEducation.timePeriodTo); + const endDateValidResult = validateEndDate(newEducation.graduated, + newEducation.timePeriodFrom, newEducation.timePeriodTo); + const formInvalid = invalid || fromDateValidResult.invalid || endDateValidResult.invalid; - this.setState({ formInvalid: invalid }); + this.setState({ + formInvalid, + startDateInvalid: fromDateValidResult.invalid, + startDateInvalidMsg: fromDateValidResult.message, + endDateInvalidMsg: endDateValidResult.message, + endDateInvalid: endDateValidResult.invalid, + }); return invalid; } @@ -167,8 +170,7 @@ export default class Education extends ConsentComponent { result.message = 'End Date should be in past or current'; } } - - return result; + return formInvalid; } onHandleDeleteEducation(indexNo) { @@ -183,7 +185,7 @@ export default class Education extends ConsentComponent { const { newEducation: oldEducation } = this.state; const newEducation = { ...oldEducation }; newEducation[timePeriod] = date; - this.setState({ newEducation, inputChanged: true }); + this.setState({ newEducation, isSubmit: false }); } } @@ -215,8 +217,13 @@ export default class Education extends ConsentComponent { this.setState({ showConfirmation: false, indexNo: null, - inputChanged: false, + isSubmit: false, isEdit: false, + formInvalid: false, + startDateInvalid: false, + startDateInvalidMsg: '', + endDateInvalid: false, + endDateInvalidMsg: '', }); } @@ -234,8 +241,14 @@ export default class Education extends ConsentComponent { timePeriodTo: _.isEmpty(educationTrait.traits.data[indexNo].timePeriodTo) ? '' : educationTrait.traits.data[indexNo].timePeriodTo, graduated: educationTrait.traits.data[indexNo].graduated, }, + isSubmit: false, isEdit: true, indexNo, + formInvalid: false, + startDateInvalid: false, + startDateInvalidMsg: '', + endDateInvalid: false, + endDateInvalidMsg: '', }); } @@ -299,7 +312,7 @@ export default class Education extends ConsentComponent { newEducation: empty, isEdit: false, indexNo: null, - inputChanged: false, + isSubmit: false, }); // save personalization if (_.isEmpty(personalizationTrait)) { @@ -334,7 +347,7 @@ export default class Education extends ConsentComponent { } } - this.setState({ newEducation, inputChanged: true }); + this.setState({ newEducation, isSubmit: false }); } /** @@ -346,7 +359,7 @@ export default class Education extends ConsentComponent { const { newEducation: oldEducation } = this.state; const newEducation = { ...oldEducation }; newEducation[option.key] = option.name; - this.setState({ newEducation, inputChanged: true }); + this.setState({ newEducation, isSubmit: false }); } } @@ -357,7 +370,7 @@ export default class Education extends ConsentComponent { onHandleAddEducation(e) { e.preventDefault(); const { newEducation } = this.state; - this.setState({ inputChanged: true }); + this.setState({ isSubmit: true }); if (this.onCheckFormValue(newEducation)) { return; } @@ -394,7 +407,7 @@ export default class Education extends ConsentComponent { if (isEdit) { this.setState({ isEdit: false, - inputChanged: false, + isSubmit: false, indexNo: null, newEducation: { schoolCollegeName: '', @@ -403,6 +416,11 @@ export default class Education extends ConsentComponent { timePeriodTo: '', graduated: false, }, + formInvalid: false, + startDateInvalid: false, + startDateInvalidMsg: '', + endDateInvalid: false, + endDateInvalidMsg: '', }); } } @@ -417,13 +435,19 @@ export default class Education extends ConsentComponent { isEdit, showConfirmation, indexNo, + formInvalid, + startDateInvalid, + startDateInvalidMsg, + endDateInvalid, + endDateInvalidMsg, + isSubmit, } = this.state; const tabs = settingsUI.TABS.PROFILE; const currentTab = settingsUI.currentProfileTab; const containerStyle = currentTab === tabs.EDUCATION ? '' : 'hide'; const educationItems = educationTrait.traits ? educationTrait.traits.data.slice() : []; - const { newEducation, inputChanged } = this.state; + const { newEducation } = this.state; return ( @@ -478,7 +502,11 @@ export default class Education extends ConsentComponent {
* Required - + { + isSubmit && formInvalid && ( + + ) + }
@@ -509,10 +537,14 @@ export default class Education extends ConsentComponent { onDateChange={date => this.onUpdateDate(date, 'timePeriodFrom')} placeholder="dd/mm/yyyy" /> - + { + isSubmit && ( + + ) + }
@@ -546,10 +578,14 @@ export default class Education extends ConsentComponent { /> ) } - + { + isSubmit && ( + + ) + }
@@ -622,7 +658,11 @@ export default class Education extends ConsentComponent { - + { + isSubmit && formInvalid && ( + + ) + }
diff --git a/src/shared/components/Settings/Profile/Hobby/index.jsx b/src/shared/components/Settings/Profile/Hobby/index.jsx index 6fb5acd72d..a8f700fa30 100644 --- a/src/shared/components/Settings/Profile/Hobby/index.jsx +++ b/src/shared/components/Settings/Profile/Hobby/index.jsx @@ -35,7 +35,7 @@ export default class Hobby extends ConsentComponent { const { userTraits } = props; this.state = { formInvalid: false, - inputChanged: false, + isSubmit: false, hobbyTrait: this.loadHobbyTrait(userTraits), personalizationTrait: this.loadPersonalizationTrait(userTraits), newHobby: { @@ -62,7 +62,7 @@ export default class Hobby extends ConsentComponent { hobbyTrait, personalizationTrait, formInvalid: false, - inputChanged: false, + isSubmit: false, newHobby: { hobby: '', description: '', @@ -81,7 +81,7 @@ export default class Hobby extends ConsentComponent { onHandleAddHobby(e) { e.preventDefault(); const { newHobby } = this.state; - this.setState({ inputChanged: true }); + this.setState({ isSubmit: true }); if (this.onCheckFormValue(newHobby)) { return; } @@ -138,7 +138,7 @@ export default class Hobby extends ConsentComponent { this.setState({ showConfirmation: false, indexNo: null, - inputChanged: false, + isSubmit: false, }); } @@ -207,7 +207,7 @@ export default class Hobby extends ConsentComponent { const { newHobby: oldHobby } = this.state; const newHobby = { ...oldHobby }; newHobby[e.target.name] = e.target.value; - this.setState({ newHobby, inputChanged: true }); + this.setState({ newHobby, isSubmit: false }); } /** @@ -248,6 +248,8 @@ export default class Hobby extends ConsentComponent { }, isEdit: true, indexNo, + formInvalid: false, + isSubmit: false, }); } @@ -257,7 +259,8 @@ export default class Hobby extends ConsentComponent { this.setState({ isEdit: false, indexNo: null, - inputChanged: false, + isSubmit: false, + formInvalid: false, newHobby: { hobby: '', description: '', @@ -273,7 +276,8 @@ export default class Hobby extends ConsentComponent { const { hobbyTrait, isMobileView, - showConfirmation, indexNo, isEdit, + showConfirmation, indexNo, isEdit, isSubmit, + formInvalid, } = this.state; const canModifyTrait = !this.props.traitRequestCount; const tabs = settingsUI.TABS.PROFILE; @@ -281,7 +285,7 @@ export default class Hobby extends ConsentComponent { const containerStyle = currentTab === tabs.HOBBY ? '' : 'hide'; const hobbyItems = hobbyTrait.traits ? hobbyTrait.traits.data.slice() : []; - const { newHobby, inputChanged } = this.state; + const { newHobby } = this.state; return (
@@ -331,7 +335,11 @@ export default class Hobby extends ConsentComponent {
* Required - + { + isSubmit && formInvalid && ( + + ) + }
@@ -399,7 +407,11 @@ export default class Hobby extends ConsentComponent { - + { + isSubmit && formInvalid && ( + + ) + }
diff --git a/src/shared/components/Settings/Profile/Language/index.jsx b/src/shared/components/Settings/Profile/Language/index.jsx index 3af900c43e..387c34aef5 100644 --- a/src/shared/components/Settings/Profile/Language/index.jsx +++ b/src/shared/components/Settings/Profile/Language/index.jsx @@ -39,6 +39,8 @@ export default class Language extends ConsentComponent { const { userTraits } = props; this.state = { formInvalid: false, + languageNameInvalid: false, + languageNameInvalidMsg: '', languageTrait: this.loadLanguageTrait(userTraits), personalizationTrait: this.loadPersonalizationTrait(userTraits), newLanguage: { @@ -51,7 +53,7 @@ export default class Language extends ConsentComponent { isEdit: false, indexNo: null, showConfirmation: false, - inputChanged: false, + isSubmit: false, }; } @@ -67,11 +69,14 @@ export default class Language extends ConsentComponent { languageTrait, personalizationTrait, formInvalid: false, + languageNameInvalid: false, + languageNameInvalidMsg: '', newLanguage: { language: '', spokenLevel: '', writtenLevel: '', }, + isSubmit: false, }); } @@ -87,7 +92,7 @@ export default class Language extends ConsentComponent { const { newLanguage: oldLanguage } = this.state; const newLanguage = { ...oldLanguage }; newLanguage[e.target.name] = e.target.value.trim(); - this.setState({ newLanguage, inputChanged: true }); + this.setState({ newLanguage, isSubmit: false }); } onHandleDeleteLanguage(indexNo) { @@ -107,6 +112,10 @@ export default class Language extends ConsentComponent { newLanguageTrait.traits.data.splice(indexNo, 1); this.setState({ languageTrait: newLanguageTrait, + isEdit: false, + formInvalid: false, + languageNameInvalid: false, + languageNameInvalidMsg: '', }); const { @@ -124,7 +133,7 @@ export default class Language extends ConsentComponent { this.setState({ showConfirmation: false, indexNo: null, - inputChanged: false, + isSubmit: false, }); } @@ -142,6 +151,10 @@ export default class Language extends ConsentComponent { }, isEdit: true, indexNo, + formInvalid: false, + languageNameInvalid: false, + languageNameInvalidMsg: '', + isSubmit: false, }); } @@ -152,29 +165,39 @@ export default class Language extends ConsentComponent { */ onCheckFormValue(newLanguage) { let invalid = false; + let languageNameInvalid = false; + let languageNameInvalidMsg = ''; - if (!_.trim(newLanguage.language).length) { + if (_.isEmpty(newLanguage.language.trim())) { invalid = true; + languageNameInvalid = true; + languageNameInvalidMsg = 'Language cannot be empty'; } - const { languageTrait, isEdit } = this.state; + const { languageTrait } = this.state; if (!_.isEmpty(languageTrait)) { const result = _.filter(languageTrait.traits.data, item => ( item.language.toLowerCase() === newLanguage.language.toLowerCase() )); - if (result && result.length > 0 && !isEdit) { + if (result && result.length > 0) { invalid = true; + languageNameInvalid = true; + languageNameInvalidMsg = 'Language already added'; } } - this.setState({ formInvalid: invalid }); + this.setState({ + formInvalid: invalid, + languageNameInvalid, + languageNameInvalidMsg, + }); return invalid; } onHandleAddLanguage(e) { e.preventDefault(); const { newLanguage } = this.state; - this.setState({ inputChanged: true }); + this.setState({ isSubmit: true }); if (this.onCheckFormValue(newLanguage)) { return; } @@ -229,7 +252,7 @@ export default class Language extends ConsentComponent { newLanguage: empty, isEdit: false, indexNo: null, - inputChanged: false, + isSubmit: false, }); // save personalization if (_.isEmpty(personalizationTrait)) { @@ -253,7 +276,7 @@ export default class Language extends ConsentComponent { const { newLanguage: oldLanguage } = this.state; const newLanguage = { ...oldLanguage }; newLanguage[option.key] = option.name; - this.setState({ newLanguage, inputChanged: true }); + this.setState({ newLanguage, isSubmit: false }); } } @@ -263,7 +286,10 @@ export default class Language extends ConsentComponent { this.setState({ isEdit: false, indexNo: null, - inputChanged: false, + isSubmit: false, + formInvalid: false, + languageNameInvalid: false, + languageNameInvalidMsg: '', newLanguage: { language: '', spokenLevel: '', @@ -306,9 +332,12 @@ export default class Language extends ConsentComponent { languageTrait, isMobileView, isEdit, + formInvalid, showConfirmation, indexNo, - inputChanged, + languageNameInvalid, + languageNameInvalidMsg, + isSubmit, } = this.state; const tabs = settingsUI.TABS.PROFILE; const currentTab = settingsUI.currentProfileTab; @@ -378,7 +407,15 @@ export default class Language extends ConsentComponent { valueKey="name" clearable={false} /> - + { + isSubmit && formInvalid && ( + + ) + }
@@ -476,7 +513,15 @@ export default class Language extends ConsentComponent { valueKey="name" clearable={false} /> - + { + isSubmit && formInvalid && ( + + ) + }
diff --git a/src/shared/components/Settings/Profile/Work/index.jsx b/src/shared/components/Settings/Profile/Work/index.jsx index 0f65dccce9..8480ef8337 100644 --- a/src/shared/components/Settings/Profile/Work/index.jsx +++ b/src/shared/components/Settings/Profile/Work/index.jsx @@ -14,12 +14,12 @@ import ConsentComponent from 'components/Settings/ConsentComponent'; import { PrimaryButton } from 'topcoder-react-ui-kit'; import DatePicker from 'components/challenge-listing/Filters/DatePicker'; import ErrorMessage from 'components/Settings/ErrorMessage'; +import { validateStartDate, validateEndDate } from 'utils/settings'; import ConfirmationModal from '../../CofirmationModal'; import WorkList from './List'; import './styles.scss'; - export default class Work extends ConsentComponent { constructor(props) { super(props); @@ -38,7 +38,11 @@ export default class Work extends ConsentComponent { const { userTraits } = props; this.state = { formInvalid: false, - inputChanged: false, + startDateInvalid: false, + startDateInvalidMsg: '', + endDateInvalid: false, + endDateInvalidMsg: '', + isSumbit: false, workTrait: this.loadWorkTrait(userTraits), personalizationTrait: this.loadPersonalizationTrait(userTraits), newWork: { @@ -69,8 +73,12 @@ export default class Work extends ConsentComponent { this.setState({ workTrait, personalizationTrait, + isSubmit: false, formInvalid: false, - inputChanged: false, + startDateInvalid: false, + startDateInvalidMsg: '', + endDateInvalid: false, + endDateInvalidMsg: '', newWork: { company: '', position: '', @@ -94,7 +102,7 @@ export default class Work extends ConsentComponent { onHandleAddWork(e) { e.preventDefault(); const { newWork } = this.state; - this.setState({ inputChanged: true }); + this.setState({ isSubmit: true }); if (this.onCheckFormValue(newWork)) { return; } @@ -108,117 +116,26 @@ export default class Work extends ConsentComponent { */ onCheckFormValue(newWork) { let invalid = false; - const currentDate = new Date().setHours(0, 0, 0, 0); if (!_.trim(newWork.company).length) { invalid = true; } - if (newWork.working) { - if (!_.isEmpty(newWork.timePeriodTo)) { - invalid = true; // timePeriodTo should be null - } - - if (!_.isEmpty(newWork.timePeriodFrom)) { - const fromDate = new Date(newWork.timePeriodFrom).setHours(0, 0, 0, 0); - - if (fromDate > currentDate) { - invalid = true; // Start Date should be in past or current - } - } - } else if (!newWork.working) { - if (_.isEmpty(newWork.timePeriodFrom) && !_.isEmpty(newWork.timePeriodTo)) { - // If enter End Date, the other becomes mandatory. - // Not as per requirement, both are optional - invalid = true; - } - - if (!_.isEmpty(newWork.timePeriodFrom)) { - const fromDate = new Date(newWork.timePeriodFrom).setHours(0, 0, 0, 0); - - if (fromDate > currentDate) { - invalid = true; // Start Date should be in past or current - } - - if (!_.isEmpty(newWork.timePeriodTo)) { - const toDate = new Date(newWork.timePeriodTo).setHours(0, 0, 0, 0); - if (fromDate >= toDate) { - invalid = true; // Start Date should be before End Date - } - } - } - - if (!_.isEmpty(newWork.timePeriodTo)) { - const toDate = new Date(newWork.timePeriodTo).setHours(0, 0, 0, 0); - if (toDate > currentDate) { - invalid = true; // End Date should be in past or current - } - } - } - - this.setState({ formInvalid: invalid }); - return invalid; - } - - onCheckStartDate() { - const { newWork } = this.state; - const currentDate = new Date().setHours(0, 0, 0, 0); - const result = { - invalid: false, - message: '', - }; - - if (newWork.working) { - if (!_.isEmpty(newWork.timePeriodFrom)) { - const fromDate = new Date(newWork.timePeriodFrom).setHours(0, 0, 0, 0); + const fromDateValidResult = validateStartDate(newWork.working, + newWork.timePeriodFrom, newWork.timePeriodTo); + const endDateValidResult = validateEndDate(newWork.working, + newWork.timePeriodFrom, newWork.timePeriodTo); + const formInvalid = invalid || fromDateValidResult.invalid || endDateValidResult.invalid; - if (fromDate > currentDate) { - result.invalid = true; - result.message = 'Start Date should be in past or current'; - } - } - } else if (!newWork.working) { - if (!_.isEmpty(newWork.timePeriodFrom)) { - const fromDate = new Date(newWork.timePeriodFrom).setHours(0, 0, 0, 0); - - if (fromDate > currentDate) { - result.invalid = true; - result.message = 'Start Date should be in past or current'; - } - - if (!_.isEmpty(newWork.timePeriodTo)) { - const toDate = new Date(newWork.timePeriodTo).setHours(0, 0, 0, 0); - if (fromDate >= toDate) { - result.invalid = true; - result.message = 'Start Date should be before End Date'; - } - } - } - } - return result; - } - - onCheckEndDate() { - const { newWork } = this.state; - const currentDate = new Date().setHours(0, 0, 0, 0); - const result = { - invalid: false, - message: '', - }; + this.setState({ + formInvalid, + startDateInvalid: fromDateValidResult.invalid, + startDateInvalidMsg: fromDateValidResult.message, + endDateInvalid: endDateValidResult.invalid, + endDateInvalidMsg: endDateValidResult.message, + }); - if (newWork.working) { - if (!_.isEmpty(newWork.timePeriodTo)) { - result.invalid = true; - result.message = 'End Date should be null'; - } - } else if (!newWork.working && !_.isEmpty(newWork.timePeriodTo)) { - const toDate = new Date(newWork.timePeriodTo).setHours(0, 0, 0, 0); - if (toDate > currentDate) { - result.invalid = true; - result.message = 'End Date should be in past or current'; - } - } - return result; + return formInvalid; } onHandleDeleteWork(indexNo) { @@ -233,7 +150,7 @@ export default class Work extends ConsentComponent { const { newWork: oldWork } = this.state; const newWork = { ...oldWork }; newWork[timePeriod] = date; - this.setState({ newWork, inputChanged: true }); + this.setState({ newWork, isSubmit: false }); } } @@ -265,7 +182,12 @@ export default class Work extends ConsentComponent { this.setState({ showConfirmation: false, indexNo: null, - inputChanged: false, + isSubmit: false, + formInvalid: false, + startDateInvalid: false, + startDateInvalidMsg: '', + endDateInvalid: false, + endDateInvalidMsg: '', }); } @@ -287,6 +209,12 @@ export default class Work extends ConsentComponent { }, isEdit: true, indexNo, + formInvalid: false, + startDateInvalid: false, + startDateInvalidMsg: '', + endDateInvalid: false, + endDateInvalidMsg: '', + isSubmit: false, }); } @@ -344,7 +272,7 @@ export default class Work extends ConsentComponent { this.setState({ isEdit: false, indexNo: null, - inputChanged: false, + isSubmit: false, }); // save personalization if (_.isEmpty(personalizationTrait)) { @@ -370,11 +298,8 @@ export default class Work extends ConsentComponent { newWork[e.target.name] = e.target.value; } else { newWork[e.target.name] = e.target.checked; - if (e.target.checked) { - newWork.timePeriodTo = ''; - } } - this.setState({ newWork, inputChanged: true }); + this.setState({ newWork, isSubmit: false }); } /** @@ -407,7 +332,7 @@ export default class Work extends ConsentComponent { if (isEdit) { this.setState({ isEdit: false, - inputChanged: false, + isSubmit: false, indexNo: null, newWork: { company: '', @@ -418,6 +343,11 @@ export default class Work extends ConsentComponent { industry: '', working: false, }, + formInvalid: false, + startDateInvalid: false, + startDateInvalidMsg: '', + endDateInvalid: false, + endDateInvalidMsg: '', }); } } @@ -432,13 +362,19 @@ export default class Work extends ConsentComponent { isEdit, showConfirmation, indexNo, + formInvalid, + startDateInvalid, + startDateInvalidMsg, + endDateInvalid, + endDateInvalidMsg, + isSubmit, } = this.state; const tabs = settingsUI.TABS.PROFILE; const currentTab = settingsUI.currentProfileTab; const containerStyle = currentTab === tabs.WORK ? '' : 'hide'; const workItems = workTrait.traits ? workTrait.traits.data.slice() : []; - const { newWork, inputChanged } = this.state; + const { newWork } = this.state; return (
@@ -492,7 +428,11 @@ export default class Work extends ConsentComponent {
* Required - + { + isSubmit && formInvalid && ( + + ) + }
@@ -545,10 +485,14 @@ export default class Work extends ConsentComponent { onDateChange={date => this.onUpdateDate(date, 'timePeriodFrom')} placeholder="dd/mm/yyyy" /> - + { + isSubmit && formInvalid && ( + + ) + }
@@ -560,7 +504,6 @@ export default class Work extends ConsentComponent {
this.onUpdateDate(date, 'timePeriodTo')} placeholder="dd/mm/yyyy" /> - + { + isSubmit && formInvalid && ( + + ) + }
@@ -645,7 +592,11 @@ export default class Work extends ConsentComponent { - + { + isSubmit && formInvalid && ( + + ) + }
diff --git a/src/shared/components/Settings/Tools/Devices/index.jsx b/src/shared/components/Settings/Tools/Devices/index.jsx index c60d39c9a5..77d6bb2076 100644 --- a/src/shared/components/Settings/Tools/Devices/index.jsx +++ b/src/shared/components/Settings/Tools/Devices/index.jsx @@ -36,7 +36,7 @@ export default class Devices extends ConsentComponent { const { userTraits } = props; this.state = { - inputChanged: false, + isSubmit: false, formInvalid: false, deviceTrait: this.loadDeviceTrait(userTraits), personalizationTrait: this.loadPersonalizationTrait(userTraits), @@ -68,7 +68,7 @@ export default class Devices extends ConsentComponent { deviceTrait, personalizationTrait, formInvalid: false, - inputChanged: false, + isSubmit: false, newDevice: { deviceType: '', manufacturer: '', @@ -91,7 +91,7 @@ export default class Devices extends ConsentComponent { onHandleAddDevice(e) { e.preventDefault(); const { newDevice } = this.state; - this.setState({ inputChanged: true }); + this.setState({ isSubmit: true }); if (this.onCheckFormValue(newDevice)) { return; } @@ -132,7 +132,8 @@ export default class Devices extends ConsentComponent { this.setState({ showConfirmation: false, indexNo: null, - inputChanged: false, + isSubmit: false, + formInvalid: false, }); } @@ -153,6 +154,7 @@ export default class Devices extends ConsentComponent { }, isEdit: true, indexNo, + isSubmit: false, }); } @@ -214,7 +216,7 @@ export default class Devices extends ConsentComponent { newDevice: empty, isEdit: false, indexNo: null, - inputChanged: false, + isSubmit: false, }); // save personalization if (_.isEmpty(personalizationTrait)) { @@ -255,7 +257,7 @@ export default class Devices extends ConsentComponent { const { newDevice: device } = this.state; const newDevice = { ...device }; newDevice[e.target.name] = e.target.value; - this.setState({ newDevice, inputChanged: false }); + this.setState({ newDevice, isSubmit: false }); } /** @@ -267,7 +269,10 @@ export default class Devices extends ConsentComponent { const { newDevice: device } = this.state; const newDevice = { ...device }; newDevice[option.key] = option.name; - this.setState({ newDevice, inputChanged: true }); + this.setState({ + newDevice, + isSubmit: false, + }); } } @@ -306,8 +311,9 @@ export default class Devices extends ConsentComponent { if (isEdit) { this.setState({ isEdit: false, - inputChanged: false, + isSubmit: false, indexNo: null, + formInvalid: false, newDevice: { deviceType: '', manufacturer: '', @@ -323,7 +329,7 @@ export default class Devices extends ConsentComponent { render() { const { deviceTrait, isMobileView, showConfirmation, indexNo, isEdit, - inputChanged, + formInvalid, isSubmit, } = this.state; const deviceItems = deviceTrait.traits ? deviceTrait.traits.data.slice() : []; @@ -387,7 +393,11 @@ export default class Devices extends ConsentComponent { clearable={false} disabled={!canModifyTrait} /> - + { + isSubmit && ( + + ) + }
@@ -500,7 +510,11 @@ export default class Devices extends ConsentComponent { clearable={false} disabled={!canModifyTrait} /> - + { + isSubmit && ( + + ) + }
@@ -368,7 +375,11 @@ export default class ServiceProviders extends ConsentComponent {
* Required - + { + isSubmit && ( + + ) + }
@@ -426,7 +437,11 @@ export default class ServiceProviders extends ConsentComponent { clearable={false} disabled={!canModifyTrait} /> - + { + isSubmit && ( + + ) + }
- + { + isSubmit && ( + + ) + }
diff --git a/src/shared/components/Settings/Tools/Software/index.jsx b/src/shared/components/Settings/Tools/Software/index.jsx index 9073d3825c..a1bc02e312 100644 --- a/src/shared/components/Settings/Tools/Software/index.jsx +++ b/src/shared/components/Settings/Tools/Software/index.jsx @@ -38,7 +38,7 @@ export default class Software extends ConsentComponent { const { userTraits } = props; this.state = { formInvalid: false, - inputChanged: false, + isSubmit: false, softwareTrait: this.loadSoftwareTrait(userTraits), personalizationTrait: this.loadPersonalizationTrait(userTraits), newSoftware: { @@ -65,7 +65,7 @@ export default class Software extends ConsentComponent { softwareTrait, personalizationTrait, formInvalid: false, - inputChanged: false, + isSubmit: false, newSoftware: { softwareType: '', name: '', @@ -84,7 +84,7 @@ export default class Software extends ConsentComponent { onHandleAddSoftware(e) { e.preventDefault(); const { newSoftware } = this.state; - this.setState({ inputChanged: true }); + this.setState({ isSubmit: true }); if (this.onCheckFormValue(newSoftware)) { return; } @@ -115,6 +115,7 @@ export default class Software extends ConsentComponent { this.setState({ showConfirmation: true, indexNo, + isSubmit: false, }); } @@ -145,7 +146,8 @@ export default class Software extends ConsentComponent { this.setState({ showConfirmation: false, indexNo: null, - inputChanged: false, + isSubmit: false, + formInvalid: false, }); } @@ -162,6 +164,7 @@ export default class Software extends ConsentComponent { }, isEdit: true, indexNo, + isSubmit: false, }); } @@ -202,6 +205,7 @@ export default class Software extends ConsentComponent { isEdit: false, indexNo: null, inputChanged: false, + isSubmit: false, }); // save personalization if (_.isEmpty(personalizationTrait)) { @@ -224,7 +228,7 @@ export default class Software extends ConsentComponent { const { newSoftware: oldSoftware } = this.state; const newSoftware = { ...oldSoftware }; newSoftware[e.target.name] = e.target.value; - this.setState({ newSoftware, inputChanged: true }); + this.setState({ newSoftware, isSubmit: false }); } /** @@ -236,7 +240,7 @@ export default class Software extends ConsentComponent { const { newSoftware: oldSoftware } = this.state; const newSoftware = { ...oldSoftware }; newSoftware[option.key] = option.name; - this.setState({ newSoftware, inputChanged: true }); + this.setState({ newSoftware, isSubmit: false }); } } @@ -280,10 +284,12 @@ export default class Software extends ConsentComponent { isEdit: false, indexNo: null, inputChanged: false, + formInvalid: false, newSoftware: { softwareType: '', name: '', }, + isSubmit: false, }); } } @@ -291,7 +297,7 @@ export default class Software extends ConsentComponent { render() { const { softwareTrait, isMobileView, showConfirmation, indexNo, isEdit, - inputChanged, + formInvalid, isSubmit, } = this.state; const softwareItems = softwareTrait.traits ? softwareTrait.traits.data.slice() : []; @@ -355,7 +361,11 @@ export default class Software extends ConsentComponent { clearable={false} disabled={!canModifyTrait} /> - + { + isSubmit && ( + + ) + }
@@ -368,7 +378,11 @@ export default class Software extends ConsentComponent {
* Required - + { + isSubmit && ( + + ) + }
@@ -426,7 +440,11 @@ export default class Software extends ConsentComponent { clearable={false} disabled={!canModifyTrait} /> - + { + isSubmit && ( + + ) + }
- + { + isSubmit && ( + + ) + }
diff --git a/src/shared/components/Settings/Tools/Subscriptions/index.jsx b/src/shared/components/Settings/Tools/Subscriptions/index.jsx index 2c713cec54..b4691b4cd9 100644 --- a/src/shared/components/Settings/Tools/Subscriptions/index.jsx +++ b/src/shared/components/Settings/Tools/Subscriptions/index.jsx @@ -35,7 +35,7 @@ export default class Subscription extends ConsentComponent { const { userTraits } = props; this.state = { formInvalid: false, - inputChanged: false, + isSubmit: false, subscriptionTrait: this.loadSubscriptionTrait(userTraits), personalizationTrait: this.loadPersonalizationTrait(userTraits), newSubscription: { @@ -61,7 +61,7 @@ export default class Subscription extends ConsentComponent { subscriptionTrait, personalizationTrait, formInvalid: false, - inputChanged: false, + isSubmit: false, newSubscription: { name: '', }, @@ -79,7 +79,7 @@ export default class Subscription extends ConsentComponent { onHandleAddSubscription(e) { e.preventDefault(); const { newSubscription } = this.state; - this.setState({ inputChanged: true }); + this.setState({ isSubmit: true }); if (this.onCheckFormValue(newSubscription)) { return; } @@ -98,6 +98,7 @@ export default class Subscription extends ConsentComponent { }, isEdit: true, indexNo, + isSubmit: false, }); } @@ -150,8 +151,9 @@ export default class Subscription extends ConsentComponent { } this.setState({ showConfirmation: false, - inputChanged: false, indexNo: null, + formInvalid: false, + isSubmit: false, }); } @@ -191,6 +193,7 @@ export default class Subscription extends ConsentComponent { isEdit: false, indexNo: null, inputChanged: false, + isSubmit: false, }); // save personalization if (_.isEmpty(personalizationTrait)) { @@ -213,7 +216,7 @@ export default class Subscription extends ConsentComponent { const { newSubscription: oldSubscription } = this.state; const newSubscription = { ...oldSubscription }; newSubscription[e.target.name] = e.target.value; - this.setState({ newSubscription, inputChanged: true }); + this.setState({ newSubscription, isSubmit: false }); } /** @@ -225,7 +228,7 @@ export default class Subscription extends ConsentComponent { const { newSubscription: oldSubscription } = this.state; const newSubscription = { ...oldSubscription }; newSubscription[option.key] = option.name; - this.setState({ newSubscription }); + this.setState({ newSubscription, isSubmit: false }); } } @@ -259,8 +262,9 @@ export default class Subscription extends ConsentComponent { if (isEdit) { this.setState({ isEdit: false, - inputChanged: false, indexNo: null, + formInvalid: false, + isSubmit: false, newSubscription: { name: '', }, @@ -270,11 +274,11 @@ export default class Subscription extends ConsentComponent { render() { const { - subscriptionTrait, isMobileView, showConfirmation, indexNo, isEdit, + subscriptionTrait, isMobileView, showConfirmation, indexNo, isEdit, isSubmit, } = this.state; const subscriptionItems = subscriptionTrait.traits ? subscriptionTrait.traits.data.slice() : []; - const { newSubscription, inputChanged } = this.state; + const { newSubscription, formInvalid } = this.state; const canModifyTrait = !this.props.traitRequestCount; return ( @@ -325,7 +329,11 @@ export default class Subscription extends ConsentComponent {
* Required - + { + isSubmit && ( + + ) + }
@@ -373,7 +381,11 @@ export default class Subscription extends ConsentComponent { - + { + isSubmit && ( + + ) + } diff --git a/src/shared/utils/settings.js b/src/shared/utils/settings.js new file mode 100644 index 0000000000..87a5969f2b --- /dev/null +++ b/src/shared/utils/settings.js @@ -0,0 +1,76 @@ +/** + * Provides misc settings page helper functions. + */ +import _ from 'lodash'; + +export function validateStartDate(flag, fromDate, toDate) { + const result = { + invalid: false, + message: '', + }; + + const currentDate = new Date().setHours(0, 0, 0, 0); + + if (!_.isEmpty(fromDate)) { + const startDate = new Date(fromDate).setHours(0, 0, 0, 0); + + if (startDate > currentDate) { + result.invalid = true; + result.message = 'Start Date should be in past or current'; + } + } + + if (!_.isEmpty(toDate)) { + const endDate = new Date(toDate).setHours(0, 0, 0, 0); + if (flag) { + if (!_.isEmpty(fromDate)) { + const startDate = new Date(fromDate).setHours(0, 0, 0, 0); + if (startDate >= endDate) { + result.invalid = true; + result.message = 'Start Date should be before End Date'; + } + } + } else if (!flag && !_.isEmpty(fromDate)) { + const startDate = new Date(fromDate).setHours(0, 0, 0, 0); + if (startDate >= endDate) { + result.invalid = true; + result.message = 'Start Date should be before End Date'; + } + } + } + + return result; +} + +export function validateEndDate(flag, fromDate, toDate) { + const result = { + invalid: false, + message: '', + }; + + const currentDate = new Date().setHours(0, 0, 0, 0); + + if (!_.isEmpty(toDate)) { + const endDate = new Date(toDate).setHours(0, 0, 0, 0); + if (flag) { + if (!_.isEmpty(fromDate)) { + if (endDate > currentDate) { + result.invalid = true; + result.message = 'End Date should be current or in past'; + } + } else if (_.isEmpty(fromDate)) { + if (endDate > currentDate) { + result.invalid = true; + result.message = 'End Date should be current or in past'; + } + } + } + } + + return result; +} + +export default { + validateStartDate, + validateEndDate, +};