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

Post user to API #81

Merged
merged 12 commits into from
Feb 7, 2018
Merged

Post user to API #81

merged 12 commits into from
Feb 7, 2018

Conversation

jamesvclements
Copy link
Contributor

@jamesvclements jamesvclements commented Jan 19, 2018

Our first API call 👶

This PR includes some overhead to stay DRY and future-proof for all those other API jawns we'll be doing at some point. It has:

  • a Redux middleware that allows you dispatch a particular type of "Call API" action. This middleware snags these actions and automatically dispatches the corresponding REQUEST, SUCCESS, and FAILURE actions per the response from the call

  • an inheritance based system for defining APIs. There is a base API class that constructs with a URL and has a generic call method. For any API that we want to use (just our BarTop API for now, but v soon the Auth0 API), we inherit this class, construct it w/ the proper URL, and add methods that correspond to endpoints. So there's a createUser method in the BarTopAPI which will perform the POST, etc. Behind the scenes, these methods are just calling the BaseAPI call method w/ different endpoints and init options (like POSTS, headers, etc)

  • adjustments to our authentication flow to call the POST when we detect that it is the user's first login

Copy link
Collaborator

@dpopp07 dpopp07 left a comment

Choose a reason for hiding this comment

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

Looks great. Just had a couple questions so I can understand it a little better before I approve for merge

@@ -51,18 +56,31 @@ export const actions = {
if (authResult && authResult.accessToken && authResult.idToken) {
try {
const decodedIdToken = jwtDecode(authResult.idToken);
// TODO - alter API singleton so it auto-syncs to redux state / local storage
Copy link
Collaborator

Choose a reason for hiding this comment

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

Have you made an issue for this?

Copy link
Contributor Author

@jamesvclements jamesvclements Feb 7, 2018

Choose a reason for hiding this comment

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

Gooood idea #95

Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

@@ -0,0 +1,3 @@
import BarTopAPI from '../apis/bartop';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Out of curiosity, why even add this layer? Why not import from the apis/ folder directly?

Copy link
Contributor Author

@jamesvclements jamesvclements Feb 7, 2018

Choose a reason for hiding this comment

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

Hmm, so the apis folder is declaring the API classes, but these singletons are the actual instantiations. I thought the distinction between the class & the instance was nice. But there very well may be a better way to handle / organize singletons in JS, I'll look into it

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see what you're saying. I think that's fine, I hadn't really thought about it like that and it seemed like it may be overkill. But if that's helping your organization, looks good to me.

@@ -77,9 +95,7 @@ export const actions = {
} else if (err) {
errorOut(err);
Copy link
Collaborator

Choose a reason for hiding this comment

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

What does errorOut() do? Couldn't find documentation for it at first glance

Copy link
Contributor Author

Choose a reason for hiding this comment

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

just some shorthand, check out line 47 above it:

// shorthand for when we encounter an error during this authentication process
      const errorOut = err => {
        console.error(err);
        dispatch(actions.loginFailure(err));
        // TODO - go somewhere for failure or unauthenticated jawns
        history.replace('/');
      };

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh I forgot that I'm a huge idiot haha carry on

@@ -0,0 +1,24 @@
import { errors } from '../strings';

export default class API {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Abstraction is hype

Copy link
Collaborator

@dpopp07 dpopp07 left a comment

Choose a reason for hiding this comment

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

Let's do it

@@ -0,0 +1,3 @@
import BarTopAPI from '../apis/bartop';
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see what you're saying. I think that's fine, I hadn't really thought about it like that and it seemed like it may be overkill. But if that's helping your organization, looks good to me.

@jamesvclements jamesvclements merged commit 6d7241b into dev Feb 7, 2018
@jamesvclements jamesvclements deleted the post-user-to-api branch March 17, 2018 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants