Skip to content

Commit

Permalink
Add "Sign Up / Log In" button to load previously entered info
Browse files Browse the repository at this point in the history
This partially addresses #18
by making it easier for returning users to log in on a different browser/device
(where they don't have everything in localStorage).

Note the TODO in the code.
  • Loading branch information
josephfrazier committed Jun 19, 2018
1 parent 142667d commit ae326d7
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
65 changes: 64 additions & 1 deletion src/routes/home/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ class Home extends React.Component {
this.state = initialState;
this.initialStatePerSubmission = initialStatePerSubmission;
this.initialStatePersistent = initialStatePersistent;
this.userFormSubmitRef = React.createRef();
}

componentDidMount() {
Expand Down Expand Up @@ -559,6 +560,64 @@ class Home extends React.Component {
</div>
</label>

<button
type="button"
disabled={this.state.isUserInfoSaving}
onClick={async () => {
this.setState({ isUserInfoSaving: true });

const { data } = await axios
.post('/logIn', this.state)
.catch(handleAxiosError)
.then(user => {
this.setState({ isUserInfoSaving: false });
return user;
});

const {
FirstName,
LastName,
Building,
StreetName,
Apt,
Borough,
Phone,
testify,
} = data;

this.setState(
{
FirstName,
LastName,
Building,
StreetName,
Apt,
Borough,
Phone,
testify,

// TODO decide between this and the TODO below
// // if all required fields are present, go to the submission UI
// isUserInfoOpen: !(
// FirstName &&
// LastName &&
// Building &&
// StreetName &&
// Borough &&
// Phone
// ),
},
() => {
this.saveStateToLocalStorage();
// TODO decide between this and the TODO above
this.userFormSubmitRef.current.click();
},
);
}}
>
Sign Up / Log In
</button>

<label>
First Name:{' '}
<input
Expand Down Expand Up @@ -656,7 +715,11 @@ class Home extends React.Component {
}
</label>

<button type="submit" disabled={this.state.isUserInfoSaving}>
<button
type="submit"
disabled={this.state.isUserInfoSaving}
ref={this.userFormSubmitRef}
>
{this.state.isUserInfoSaving ? 'Saving...' : 'Save'}
</button>
</fieldset>
Expand Down
7 changes: 7 additions & 0 deletions src/routes/home/__snapshots__/Home.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ exports[`Home renders children correctly 1`] = `
</button>
</div>
</label>
<button
disabled={false}
onClick={[Function]}
type="button"
>
Sign Up / Log In
</button>
<label>
First Name:
Expand Down
6 changes: 6 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ async function logIn({ email, password }) {
});
}

app.use('/logIn', (req, res) => {
logIn(req.body)
.then(user => res.json(user))
.catch(handlePromiseRejection(res));
});

async function saveUser({
email,
password,
Expand Down

0 comments on commit ae326d7

Please sign in to comment.