Skip to content
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

feat: add disabled save button if userName not unique #31

Merged
merged 3 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
# [1.2.0](https://github.com/Greenstand/treetracker-admin-client/compare/v1.1.2...v1.2.0) (2021-02-25)


### Features

* header logo redirect to home ([#30](https://github.com/Greenstand/treetracker-admin-client/issues/30)) ([9e976a6](https://github.com/Greenstand/treetracker-admin-client/commit/9e976a641f126c8f46b3cb53d2ed833bd0ee662e))
- header logo redirect to home ([#30](https://github.com/Greenstand/treetracker-admin-client/issues/30)) ([9e976a6](https://github.com/Greenstand/treetracker-admin-client/commit/9e976a641f126c8f46b3cb53d2ed833bd0ee662e))

## [1.1.2](https://github.com/Greenstand/treetracker-admin-client/compare/v1.1.1...v1.1.2) (2021-02-24)


### Bug Fixes

* console errors on login ([#19](https://github.com/Greenstand/treetracker-admin-client/issues/19)) ([b1322e1](https://github.com/Greenstand/treetracker-admin-client/commit/b1322e19df5f2d1ca25f0e8845d3539e5c1cbf15))
- console errors on login ([#19](https://github.com/Greenstand/treetracker-admin-client/issues/19)) ([b1322e1](https://github.com/Greenstand/treetracker-admin-client/commit/b1322e19df5f2d1ca25f0e8845d3539e5c1cbf15))

## [1.1.1](https://github.com/Greenstand/treetracker-admin-client/compare/v1.1.0...v1.1.1) (2021-02-23)


### Bug Fixes

* planter org id filter ([#21](https://github.com/Greenstand/treetracker-admin-client/issues/21)) ([c2933c1](https://github.com/Greenstand/treetracker-admin-client/commit/c2933c164fbeb1fb9719c24ad8c70f6326498590))
- planter org id filter ([#21](https://github.com/Greenstand/treetracker-admin-client/issues/21)) ([c2933c1](https://github.com/Greenstand/treetracker-admin-client/commit/c2933c164fbeb1fb9719c24ad8c70f6326498590))

# [1.1.0](https://github.com/Greenstand/treetracker-admin-client/compare/v1.0.2...v1.1.0) (2021-02-23)


### Features

* add optimized images ([#20](https://github.com/Greenstand/treetracker-admin-client/issues/20)) ([f9e57ec](https://github.com/Greenstand/treetracker-admin-client/commit/f9e57eccb7cd7181fc48f140644dbab2f5877021))
- add optimized images ([#20](https://github.com/Greenstand/treetracker-admin-client/issues/20)) ([f9e57ec](https://github.com/Greenstand/treetracker-admin-client/commit/f9e57eccb7cd7181fc48f140644dbab2f5877021))

## [1.0.2](https://github.com/Greenstand/treetracker-admin-client/compare/v1.0.1...v1.0.2) (2021-02-20)


### Bug Fixes

* add releaserc file and changelog ([5167df6](https://github.com/Greenstand/treetracker-admin-client/commit/5167df62fa4784e06701f3896de9517288dd81ca))
- add releaserc file and changelog ([5167df6](https://github.com/Greenstand/treetracker-admin-client/commit/5167df62fa4784e06701f3896de9517288dd81ca))
25 changes: 20 additions & 5 deletions src/components/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ function Users(props) {
const [snackbarOpen, setSnackbarOpen] = React.useState(false);
const [snackbarMessage, setSnackbarMesssage] = React.useState('');
const [usersLoaded, setUsersLoaded] = React.useState(false);
const [userNameValid, setUserNameValid] = React.useState(true);

const ENABLED = 'Enabled';
const DISABLED = 'Disabled';
Expand Down Expand Up @@ -399,9 +400,16 @@ function Users(props) {
}

function handleUsernameChange(e) {
handleUniqueUsername(e);
setUserEditing({ ...userEditing, userName: e.target.value });
}

function handleUniqueUsername(e) {
if (users.find((user) => user.userName === e.target.value)) {
setUserNameValid(false);
} else setUserNameValid(true);
}

function handleFirstNameChange(e) {
setUserEditing({ ...userEditing, firstName: e.target.value });
}
Expand Down Expand Up @@ -452,6 +460,10 @@ function Users(props) {
: false;
};

function handelUserNameError() {
return userNameValid ? '' : 'Username already exists';
}

function handleSnackbarClose(event, reason) {
if (reason === 'clickaway') {
return;
Expand Down Expand Up @@ -516,7 +528,7 @@ function Users(props) {
</TableRow>
));
}

console.log(userNameValid);
return (
<>
<Grid container className={classes.box}>
Expand Down Expand Up @@ -608,7 +620,7 @@ function Users(props) {
<TextField
autoFocus
id="userName"
label="Username"
label={userNameValid ? 'Username' : 'Error'}
type="text"
variant="outlined"
fullWidth
Expand All @@ -619,9 +631,11 @@ function Users(props) {
userEditing && userEditing.id !== undefined ? true : false
}
value={(userEditing && userEditing.userName) || ''}
error={handleError(userEditing, ['userName'])}
error={handleError(userEditing, ['userName']) || !userNameValid}
helperText={
handleError(userEditing, ['userName']) ? 'No space allowed' : ''
handleError(userEditing, ['userName'])
? 'No space allowed'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This chained ternary operation is quite difficult to read.
At the very least, it needs to parentheses to make it clearer, but would benefit from being split out into a function.

: handelUserNameError()
}
className={classes.input}
onChange={handleUsernameChange}
Expand Down Expand Up @@ -776,7 +790,8 @@ function Users(props) {
!userEditing ||
!userEditing.userName ||
!right ||
right.length === 0
right.length === 0 ||
!userNameValid
}
>
{saveInProgress ? <CircularProgress size={21} /> : 'Save'}
Expand Down