-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add password confirmation. * Move user form page to components. * Add settings page to update active user's profile. * Switch app user management to use context. * Use contexts to update current user after their profile is updated
- Loading branch information
1 parent
b10f985
commit d112347
Showing
17 changed files
with
259 additions
and
51 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
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
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,124 @@ | ||
import * as React from 'react'; | ||
import { withRouter, RouteComponentProps, Link } from 'react-router-dom'; | ||
|
||
import { Button } from '@patternfly/react-core'; | ||
|
||
import { | ||
LoadingPageWithHeader, | ||
UserFormPage, | ||
AlertType, | ||
AlertList, | ||
closeAlertMixin, | ||
} from '../../components'; | ||
import { UserType, ActiveUserAPI } from '../../api'; | ||
import { Paths } from '../../paths'; | ||
import { mapErrorMessages } from '../../utilities'; | ||
import { AppContext } from '../../loaders/standalone/app-context'; | ||
|
||
interface IState { | ||
user: UserType; | ||
errorMessages: object; | ||
inEditMode: boolean; | ||
alerts: AlertType[]; | ||
} | ||
|
||
class UserProfile extends React.Component<RouteComponentProps, IState> { | ||
private initialState: UserType; | ||
|
||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
user: undefined, | ||
errorMessages: {}, | ||
inEditMode: false, | ||
alerts: [], | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
const id = this.props.match.params['userID']; | ||
ActiveUserAPI.getUser() | ||
.then(result => { | ||
// The api doesn't return a value for the password, so set a blank one here | ||
// to keep react from getting confused | ||
result.password = ''; | ||
this.initialState = { ...result }; | ||
this.setState({ user: result }); | ||
}) | ||
.catch(() => this.props.history.push(Paths.notFound)); | ||
} | ||
|
||
render() { | ||
const { user, errorMessages, inEditMode, alerts } = this.state; | ||
|
||
if (!user) { | ||
return <LoadingPageWithHeader></LoadingPageWithHeader>; | ||
} | ||
return ( | ||
<> | ||
<AlertList | ||
alerts={alerts} | ||
closeAlert={i => this.closeAlert(i)} | ||
></AlertList> | ||
<UserFormPage | ||
user={user} | ||
breadcrumbs={[{ name: 'Settings' }, { name: 'My profile' }]} | ||
title='My profile' | ||
errorMessages={errorMessages} | ||
updateUser={user => this.setState({ user: user })} | ||
saveUser={this.saveUser} | ||
isReadonly={!inEditMode} | ||
onCancel={() => | ||
this.setState({ | ||
user: this.initialState, | ||
inEditMode: false, | ||
errorMessages: {}, | ||
}) | ||
} | ||
extraControls={ | ||
!inEditMode && ( | ||
<div style={{ display: 'flex', justifyContent: 'flex-end' }}> | ||
<div> | ||
<Button onClick={() => this.setState({ inEditMode: true })}> | ||
Edit | ||
</Button> | ||
</div> | ||
</div> | ||
) | ||
} | ||
></UserFormPage> | ||
</> | ||
); | ||
} | ||
|
||
private saveUser = () => { | ||
const { user } = this.state; | ||
ActiveUserAPI.saveUser(user) | ||
.then(result => { | ||
this.setState( | ||
{ | ||
inEditMode: false, | ||
alerts: this.state.alerts.concat([ | ||
{ variant: 'success', title: 'Profile saved.' }, | ||
]), | ||
}, | ||
() => this.context.setUser(result.data), | ||
); | ||
}) | ||
.catch(err => { | ||
this.setState({ errorMessages: mapErrorMessages(err) }); | ||
}); | ||
}; | ||
|
||
private get closeAlert() { | ||
return closeAlertMixin('alerts'); | ||
} | ||
} | ||
|
||
export default withRouter(UserProfile); | ||
|
||
// For some reason react complains about setting context type in the class itself. | ||
// I think that it happens because withRouter confuses react into thinking that the | ||
// component is a functional compent when it's actually a class component. | ||
UserProfile.contextType = AppContext; |
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,9 @@ | ||
import * as React from 'react'; | ||
import { UserType } from '../../api'; | ||
|
||
interface IAppContextType { | ||
user: UserType; | ||
setUser: (user: UserType) => void; | ||
} | ||
|
||
export const AppContext = React.createContext<IAppContextType>(undefined); |
Oops, something went wrong.