-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixed new room page form validation #7098
Changes from 27 commits
53d88f6
846a446
a09e43d
40730c4
bfc0b49
f212fa4
7b9dd79
9355c74
63cf1cc
44e21a6
2808bb5
f87da98
90cb904
f2ad3ca
0d6b94b
7f8c79d
a82fb4b
015d94a
e4a8afa
a80ce22
e045a5e
7191f3a
76acdbb
59cb4fe
c0e595c
3ddfae9
474d1b0
62fa9e6
1edf336
d27b049
faee357
21cd056
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -526,6 +526,7 @@ const CONST = { | |
ROLE: { | ||
ADMIN: 'admin', | ||
}, | ||
ROOM_PREFIX: '#', | ||
}, | ||
|
||
TERMS: { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -321,6 +321,34 @@ function doesFailCharacterLimit(maxLength, valuesToBeValidated) { | |
return _.map(valuesToBeValidated, value => value.length > maxLength); | ||
} | ||
|
||
/** | ||
* Checks if is one of the certain names which are reserved for default rooms | ||
* and should not be used for policy rooms. | ||
* | ||
* @param {String} roomName | ||
* @returns {Boolean} | ||
*/ | ||
function isReservedRoomName(roomName) { | ||
return _.contains(CONST.REPORT.RESERVED_ROOM_NAMES, roomName); | ||
} | ||
|
||
/** | ||
* Checks if the room name already exists. We don't care if it matches the original name provided in this | ||
* component because then we are not changing the room's name | ||
* | ||
* @param {String} roomName | ||
* @param {Object[]} reports | ||
* @param {String} policyID | ||
* @returns {Boolean} | ||
*/ | ||
function isExistingRoomName(roomName, reports, policyID) { | ||
return _.some( | ||
_.values(reports), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
report => report && report.policyID === policyID | ||
&& report.reportName === roomName, | ||
); | ||
} | ||
|
||
export { | ||
meetsAgeRequirements, | ||
isValidAddress, | ||
|
@@ -344,4 +372,6 @@ export { | |
isValidRoutingNumber, | ||
isValidSSNLastFour, | ||
doesFailCharacterLimit, | ||
isReservedRoomName, | ||
isExistingRoomName, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import Button from '../components/Button'; | |
import RoomNameInput from '../components/RoomNameInput'; | ||
import Picker from '../components/Picker'; | ||
import withFullPolicy, {fullPolicyDefaultProps, fullPolicyPropTypes} from './workspace/withFullPolicy'; | ||
import * as ValidationUtils from '../libs/ValidationUtils'; | ||
|
||
|
||
const propTypes = { | ||
|
@@ -49,6 +50,18 @@ const propTypes = { | |
notificationPreference: PropTypes.string, | ||
}).isRequired, | ||
|
||
/** All reports shared with the user */ | ||
reports: PropTypes.shape({ | ||
/** The report name */ | ||
reportName: PropTypes.string, | ||
|
||
/** The report type */ | ||
type: PropTypes.string, | ||
|
||
/** ID of the policy */ | ||
policyID: PropTypes.string, | ||
}).isRequired, | ||
|
||
/** The policies which the user has access to and which the report could be tied to */ | ||
policies: PropTypes.shape({ | ||
/** The policy name */ | ||
|
@@ -84,12 +97,58 @@ class ReportSettingsPage extends Component { | |
|
||
this.state = { | ||
newRoomName: this.props.report.reportName, | ||
error: '', | ||
errors: {}, | ||
}; | ||
|
||
this.validateAndRenameReport = this.validateAndRenameReport.bind(this); | ||
} | ||
|
||
validateAndRenameReport() { | ||
if (!this.validate() || this.props.report.reportName === this.state.newRoomName) { | ||
ABee-Tech marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return; | ||
} | ||
Report.renameReport(this.props.report.reportID, this.state.newRoomName); | ||
} | ||
|
||
validate() { | ||
const errors = {}; | ||
|
||
// We error if the user doesn't enter a room name or left blank | ||
if (!this.state.newRoomName || this.state.newRoomName === CONST.POLICY.ROOM_PREFIX) { | ||
errors.newRoomName = this.props.translate('newRoomPage.pleaseEnterRoomName'); | ||
} | ||
|
||
// We error if the room name already exists. We don't care if it matches the original name provided in this | ||
// component because then we are not changing the room's name. | ||
if (ValidationUtils.isExistingRoomName(this.state.newRoomName, this.props.reports, this.props.report.policyID) && this.state.newRoomName !== this.props.report.reportName) { | ||
errors.newRoomName = this.props.translate('newRoomPage.roomAlreadyExistsError'); | ||
} | ||
|
||
// Certain names are reserved for default rooms and should not be used for policy rooms. | ||
if (ValidationUtils.isReservedRoomName(this.state.newRoomName)) { | ||
errors.newRoomName = this.props.translate('newRoomPage.roomNameReservedError'); | ||
} | ||
|
||
this.setState({errors}); | ||
return _.isEmpty(errors); | ||
} | ||
|
||
/** | ||
* @param {String} inputKey | ||
* @param {String} value | ||
*/ | ||
clearErrorAndSetValue(inputKey, value) { | ||
this.setState(prevState => ({ | ||
[inputKey]: value, | ||
errors: { | ||
...prevState.errors, | ||
[inputKey]: '', | ||
}, | ||
})); | ||
} | ||
ABee-Tech marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
render() { | ||
const shouldDisableRename = ReportUtils.isDefaultRoom(this.props.report) || ReportUtils.isArchivedRoom(this.props.report) || this.props.isLoadingRenamePolicyRoom; | ||
const shouldDisableRename = ReportUtils.isDefaultRoom(this.props.report) || ReportUtils.isArchivedRoom(this.props.report); | ||
ABee-Tech marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const linkedWorkspace = _.find(this.props.policies, policy => policy.id === this.props.report.policyID); | ||
|
||
return ( | ||
|
@@ -128,26 +187,22 @@ class ReportSettingsPage extends Component { | |
<View style={[styles.flexRow]}> | ||
<View style={[styles.flex3]}> | ||
<RoomNameInput | ||
onChangeText={newRoomName => this.setState({newRoomName})} | ||
onChangeError={error => this.setState({error})} | ||
initialValue={this.state.newRoomName} | ||
disabled={shouldDisableRename} | ||
policyID={linkedWorkspace && linkedWorkspace.id} | ||
errorText={this.state.errors.newRoomName} | ||
onChangeText={newRoomName => this.clearErrorAndSetValue('newRoomName', newRoomName)} | ||
disabled={shouldDisableRename} | ||
/> | ||
</View> | ||
<Button | ||
success={!shouldDisableRename} | ||
text={this.props.translate('common.save')} | ||
onPress={() => Report.renameReport(this.props.report.reportID, this.state.newRoomName)} | ||
onPress={this.validateAndRenameReport} | ||
style={[styles.ml2, styles.flex1]} | ||
textStyles={[styles.label]} | ||
innerStyles={[styles.reportSettingsChangeNameButton]} | ||
isDisabled={Boolean( | ||
shouldDisableRename | ||
|| this.state.newRoomName === this.props.report.reportName | ||
|| this.state.error, | ||
)} | ||
isLoading={this.props.isLoadingRenamePolicyRoom} | ||
isDisabled={shouldDisableRename} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NAB (as we are conducting an audit of all forms now) but no form buttons should be disabled There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this needs to be disabled for this case when user is seeing settings for default policies #admins, etc. I haven't seen the new Form style draft so I can't think of anything apart from it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll release the guidelines soon. Basically, no forms should be disabled for any reason. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. Thanks. Also, the input is read-only here so I think disabling the button is justified. Another approach could be to hide the Rename form for default policies. |
||
/> | ||
</View> | ||
</View> | ||
|
@@ -199,5 +254,8 @@ export default compose( | |
isLoadingRenamePolicyRoom: { | ||
key: ONYXKEYS.IS_LOADING_RENAME_POLICY_ROOM, | ||
}, | ||
reports: { | ||
key: ONYXKEYS.COLLECTION.REPORT, | ||
}, | ||
}), | ||
)(ReportSettingsPage); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.