Skip to content

Commit

Permalink
Merge pull request #83 from tieme-ndo/create-user-password-confirmation
Browse files Browse the repository at this point in the history
created confirm passsword field
  • Loading branch information
Pav0l committed Sep 20, 2019
2 parents a5932a5 + 730d96a commit d657a32
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
18 changes: 16 additions & 2 deletions src/components/pages/AddStaff/AddStaff.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function AddStaff() {
const [state, updateState] = useState({
username: '',
password: '',
confirmPassword: '',
isAdmin: false,
createAccount: false,
errors: {}
Expand All @@ -36,7 +37,8 @@ function AddStaff() {

const credential = {
username: state.username,
password: state.password
password: state.password,
confirmPassword: state.confirmPassword
};

const { errors, isValid } = await validateAddStaffForm(credential);
Expand All @@ -49,6 +51,16 @@ function AddStaff() {
}));
}

if (state.password !== state.confirmPassword) {
return updateState(prevState => ({
...prevState,
createAccount: false,
password: '',
confirmPassword: '',
errors: [{ password: 'Passwords do not match' }]
}));
}

const response = await registrationHandler({
username: state.username,
password: state.password,
Expand All @@ -69,7 +81,8 @@ function AddStaff() {

return updateState(prevState => ({
...prevState,
createAccount: false
createAccount: false,
errors: {}
}));
} else if (response === 'New user created') {
toast.success('New user created');
Expand All @@ -78,6 +91,7 @@ function AddStaff() {
...prevState,
username: '',
password: '',
confirmPassword: '',
isAdmin: false,
createAccount: false
}));
Expand Down
2 changes: 2 additions & 0 deletions src/components/pages/AddStaff/AddStaff.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ it('renders without crashing', () => {
expect(usernameField).toBeInTheDocument();
const passwordField = getByTestId('password-field');
expect(passwordField).toBeInTheDocument();
const confirmPasswordField = getByTestId('confirm-password-field');
expect(confirmPasswordField).toBeInTheDocument();
const adminCheckbox = getByTestId('admin-checkbox');
expect(adminCheckbox).toBeInTheDocument();
});
16 changes: 14 additions & 2 deletions src/components/pages/AddStaff/AddStaffForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ const AddStaff = props => {
data-testid="password-field"
/>
</Form.Field>
<Form.Field>
<label htmlFor="confirmPassword">Confirm Password</label>
<Form.Input
fluid
id="confirmPassword"
name="confirmPassword"
type="password"
value={state.confirmPassword}
onChange={handleChange}
data-testid="confirm-password-field"
/>
</Form.Field>
<Form.Field>
<Checkbox
label="Check this box to make this user Admin"
Expand All @@ -88,8 +100,8 @@ const AddStaff = props => {
content="Loading..."
/>
) : (
<Button color="teal" fluid content="Add Staff" />
)}
<Button color="teal" fluid content="Add Staff" />
)}
</Segment>
</Form>
</Grid.Column>
Expand Down
6 changes: 6 additions & 0 deletions src/components/pages/AddStaff/AddStaffValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ const addStaffSchema = yup.object().shape({
.trim()
.required(),
password: yup
.string()
.min(6)
.max(40)
.required(),

confirmPassword: yup
.string()
.min(6)
.max(40)
Expand Down

0 comments on commit d657a32

Please sign in to comment.