Skip to content

Commit

Permalink
Integrate identifier rules into name/email fields in preferences
Browse files Browse the repository at this point in the history
 - Provides dialog error message with disallowed string
 - Disables save
  • Loading branch information
nerdneha committed Dec 12, 2017
1 parent 58ac070 commit ce18135
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions app/src/ui/preferences/preferences.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Git } from './git'
import { assertNever } from '../../lib/fatal-error'
import { Button } from '../lib/button'
import { ButtonGroup } from '../lib/button-group'
import { Dialog, DialogFooter } from '../dialog'
import { Dialog, DialogFooter, DialogError } from '../dialog'
import {
getGlobalConfigValue,
setGlobalConfigValue,
Expand All @@ -20,6 +20,7 @@ import {
import { lookupPreferredEmail } from '../../lib/email'
import { Shell, getAvailableShells } from '../../lib/shells'
import { getAvailableEditors } from '../../lib/editors/lookup'
import { disallowedCharacters } from './identifier-rules'

interface IPreferencesProps {
readonly dispatcher: Dispatcher
Expand Down Expand Up @@ -117,13 +118,20 @@ export class Preferences extends React.Component<
}

public render() {
const disallowedCharactersError = this.disallowedCharacterErrorMessage(
this.state.committerName,
this.state.committerEmail
)
const hasDisallowedCharacter = disallowedCharactersError !== null

return (
<Dialog
id="preferences"
title={__DARWIN__ ? 'Preferences' : 'Options'}
onDismissed={this.props.onDismissed}
onSubmit={this.onSave}
>
{this.renderDisallowedCharactersError(disallowedCharactersError)}
<TabBar
onTabClicked={this.onTabClicked}
selectedIndex={this.state.selectedIndex}
Expand All @@ -134,7 +142,7 @@ export class Preferences extends React.Component<
</TabBar>

{this.renderActiveTab()}
{this.renderFooter()}
{this.renderFooter(hasDisallowedCharacter)}
</Dialog>
)
}
Expand All @@ -153,6 +161,31 @@ export class Preferences extends React.Component<
this.props.dispatcher.removeAccount(account)
}

private disallowedCharacterErrorMessage(name: string, email: string) {
const disallowedNameCharacters = disallowedCharacters(name)
const disallowedEmailCharacters = disallowedCharacters(email)

if (disallowedNameCharacters) {
return `Git name field cannot be a disallowed character "${
disallowedNameCharacters
}"`
} else if (disallowedEmailCharacters) {
return `Git email field cannot be a disallowed character "${
disallowedEmailCharacters
}"`
} else {
return null
}
}

private renderDisallowedCharactersError(message: string | null) {
if (message) {
return <DialogError>{message}</DialogError>
} else {
return null
}
}

private renderActiveTab() {
const index = this.state.selectedIndex
switch (index) {
Expand Down Expand Up @@ -232,7 +265,7 @@ export class Preferences extends React.Component<
this.setState({ selectedShell: shell })
}

private renderFooter() {
private renderFooter(hasDisabledError: boolean) {
const index = this.state.selectedIndex
switch (index) {
case PreferencesTab.Accounts:
Expand All @@ -242,7 +275,9 @@ export class Preferences extends React.Component<
return (
<DialogFooter>
<ButtonGroup>
<Button type="submit">Save</Button>
<Button type="submit" disabled={hasDisabledError}>
Save
</Button>
<Button onClick={this.props.onDismissed}>Cancel</Button>
</ButtonGroup>
</DialogFooter>
Expand Down

0 comments on commit ce18135

Please sign in to comment.