/** * Created by rburson on 3/14/16. */ import * as React from 'react' import { DialogException } from 'catavolt-sdk' import { CvState, CvProps, CvBaseMixin, CvEvent, CvLoginResult, CvLogin, CvContext, CvLoginCallback, CvResultCallback, CvActionFiredResult, CvValueAdapter, CvValueProvider, CvValueListener } from 'catreact' import {openPopupPrompt} from "./CvPopupPrompt"; export interface CvLoginPanelState extends CvState { clientType: string; directUrl: string; gatewayUrl: string; password: string; newPassword: string; newPassword2: string; tenantId: string; userId: string; error:any; passwordExpired: boolean; focusOnPassword: boolean; } export interface CvLoginPanelProps extends CvProps { loginListeners?: Array<(loginEvent:CvEvent)=>void> actionListeners?:Array<(event:CvEvent)=>void>; defaultClientType?: string; defaultDirectUrl?: string; defaultGatewayUrl?: string; defaultPassword?: string; defaultTenantId?: string; defaultUserId?: string; showClientType?: boolean; showDirectUrl?: boolean; showGatewayUrl?: boolean; showTenantId?: boolean; } /* *************************************************** * Render a LoginPanel *************************************************** */ export var CvLoginPanel = React.createClass({ mixins: [CvBaseMixin], getChildContext: function () { return this.getDefaultChildContext(); }, getDefaultProps: function () { return { loginListeners: [], actionListeners: [], defaultClientType: 'RICH_CLIENT', defaultDirectUrl: '', defaultGatewayUrl: 'www.catavolt.net', defaultPassword: '', defaultNewPassword: '', defaultNewPassword2: '', defaultTenantId: '', defaultUserId: '', showClientType: true, showDirectUrl: true, showGatewayUrl: true, showTenantId: true } }, getInitialState: function () { return { directUrl: this.props.defaultDirectUrl, tenantId: this.props.defaultTenantId, gatewayUrl: this.props.defaultGatewayUrl, userId: this.props.defaultUserId, password: this.props.defaultPassword, newPassword: this.props.defaultNewPassword, newPassword2: this.props.defaultNewPassword2, clientType: this.props.defaultClientType, error:null, passwordExpired:false, focusOnPassword: false } }, render: function () { return ( { let answer = null; if(!callback.isLoggedIn()) { if (this.state.passwordExpired) { answer = this.promptForChangePassword(callback); } else { answer = this.promptForLogon(callback); } } return answer; }}/> ); }, handleChange: function (field:string, e:any) { let nextState = {}; nextState[field] = e.target.value; nextState["focusOnPassword"] = false; this.setState(nextState); }, handleRadioChange: function (field, value, e:any) { let nextState = {}; nextState[field] = value; this.setState(nextState); }, handleSubmit: function (callback:CvLoginCallback, e:any) { e.preventDefault(); this.setState({error:null}); const resultCallback:CvResultCallback = (success:CvLoginResult, error:any)=>{ if(error) { let nextState = {error: error}; if (error instanceof DialogException && (error as DialogException).name == "com.dgoi.core.security.PasswordExpiredException") { let cont = ()=>{ this.setState({passwordExpired: true, focusOnPassword: true, password: ''}) }; let cancel = ()=>{ /* do nothing */ }; openPopupPrompt("Password Expired", "Your password has expired. Would you like to change your password now?" , ["Change Password Now", "Cancel"], [cont, cancel]); ; } else { this.setState({error: error}); } } }; if(this.state.passwordExpired) { if (this.state.newPassword != this.state.newPassword2) { this.setState({error: "Your new password is not the same in the two fields."}); } else { let host = this.state.directUrl ? this.state.directUrl : this.state.gatewayUrl; callback.changePasswordAndLogin(host, this.state.tenantId, this.state.clientType, this.state.userId, this.state.password, this.state.newPassword, resultCallback); } } else if(this.state.directUrl) { callback.loginDirectly(this.state.directUrl, this.state.tenantId, this.state.clientType, this.state.userId, this.state.password, resultCallback); } else { callback.login(this.state.gatewayUrl, this.state.tenantId, this.state.clientType, this.state.userId, this.state.password, resultCallback); } }, promptForChangePassword: function(callback:CvLoginCallback) { return
{(()=>{ return this.buildHiddenPromptFields(); })()}
this.state.focusOnPassword && input && input.focus()} value={this.state.password} onChange={this.handleChange.bind(this, 'password')} required/>
{(()=>{ if(this.props.showClientType) { return (
) }})()}
{this.state.error ?
{typeof this.state.error === 'string' ? this.state.error : 'Invalid Login'}
: null}
}, promptForLogon: function(callback:CvLoginCallback) { return
{(()=>{ return this.buildHiddenPromptFields(); })()}
{(()=>{ if(this.props.showClientType) { return (
) }})()}
{this.state.error ?
Invalid Login
: null}
}, buildHiddenPromptFields: function() { let answer = []; if(this.props.showDirectUrl) { answer.push(
); } if(this.props.showGatewayUrl) { answer.push(
); } if(this.props.showTenantId) { answer.push(
); } return answer; } });