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

📝: Refactor firebase code #94

Open
paulywill opened this issue Apr 28, 2019 · 4 comments
Open

📝: Refactor firebase code #94

paulywill opened this issue Apr 28, 2019 · 4 comments
Assignees
Labels
task-item task/work item as part of a larger feature

Comments

@paulywill
Copy link
Contributor

paulywill commented Apr 28, 2019

Task Description

Some of the problems and confusing is stemming from a firebase tutorial I used to implement the "google sign-in" button.

firebase has a much simpler solution : firebaseui-web-react

Getting the example they provided did take some time but I finally resolved.

Function

Using the firebaseui-web-react will simplify our components big time!
This allows easy flexibility in the future... ie adding Github, Facebook, etc.
This should also eliminate the need for so many nested components.

My only concern/observation is the elimination or need for 404 routing.

Here's the basic setup:

/
// React core.
import React from 'react';
import ReactDOM from 'react-dom';

// Firebase.
import firebase from 'firebase/app';
import 'firebase/auth';
import StyledFirebaseAuth from 'react-firebaseui/StyledFirebaseAuth';

// Styles
import styles from './app.css'; // This uses CSS modules.
import './firebaseui-styling.global.css'; // Import globally.

// Get the Firebase config from the auto generated file.
const firebaseConfig = require('./firebase-config.json').result;

// Instantiate a Firebase app.
const firebaseApp = firebase.initializeApp(firebaseConfig);

/**
 * The Splash Page containing the login UI.
 */
class App extends React.Component {
  uiConfig = {
    signInFlow: 'popup',
    signInOptions: [
      firebase.auth.GoogleAuthProvider.PROVIDER_ID,
      firebase.auth.EmailAuthProvider.PROVIDER_ID,
    ],
    callbacks: {
      signInSuccessWithAuthResult: () => false,
    },
  };

  state = {
    isSignedIn: undefined,
  };

  /**
   * @inheritDoc
   */
  componentDidMount() {
    this.unregisterAuthObserver = firebaseApp.auth().onAuthStateChanged((user) => {
      this.setState({isSignedIn: !!user});
    });
  }

  /**
   * @inheritDoc
   */
  componentWillUnmount() {
    this.unregisterAuthObserver();
  }

  /**
   * @inheritDoc
   */
  render() {
    return (
      <div className={styles.container}>
        <div className={styles.logo}>
          <i className={styles.logoIcon + ' material-icons'}>photo</i> My App
        </div>
        <div className={styles.caption}>This is a cool demo app</div>
        {this.state.isSignedIn !== undefined && !this.state.isSignedIn &&
          <div>
            <StyledFirebaseAuth className={styles.firebaseUi} uiConfig={this.uiConfig}
                                firebaseAuth={firebaseApp.auth()}/>
          </div>
        }
        {this.state.isSignedIn &&
          <div className={styles.signedIn}>
            Hello {firebaseApp.auth().currentUser.displayName}. You are now signed In!
            <a className={styles.button} onClick={() => firebaseApp.auth().signOut()}>Sign-out</a>
          </div>
        }
      </div>
    );
  }
}

// Load the app in the browser.
ReactDOM.render(<App/>, document.getElementById('app'));

Feature

#90 - Hamburger / Settings page

@paulywill
Copy link
Contributor Author

As far as the 404 error it looks as though Facebook still loads everything except the page in the middle of the content if that page does not exist:

image

@paulywill
Copy link
Contributor Author

paulywill commented Apr 28, 2019

WIP: https://github.com/paulywill/v8-geckos-team-07/tree/fix/firebase-ui

This change is breaking the working state of our dev build (ie all the links and passing state) however already there's massive improvement IMO.

Goal:

  • eliminate the need for the following:
    • Layout.js
    • withAuthentication
    • HeaderLoggedIn.js
    • SocialButtonList.js
    • SocialProfileList.js
  • place App.js back into default location as best practices IMO

App.js:

<div className={styles.container}>
        {this.state.isSignedIn !== undefined && !this.state.isSignedIn &&
          //Not signed-in yet
          <div>
            <Header />
            <div className="circle">
              <h2>A simple way to measure whether you did a habit.</h2>
              <ol>
                <li>Sign in using Google</li>
                <li>Create a habit to track</li>
                <li>Check in to reflect and log your&nbsp;progress</li>
                <li>Review your data to help you along your journey</li>
              </ol>
              <p>Happy Tracking!</p>
            </div>  
            <StyledFirebaseAuth className={styles.firebaseUi} uiConfig={this.uiConfig}
              firebaseAuth={firebaseApp.auth()} />
            <Footer />
          </div>
        }

        {this.state.isSignedIn &&
          //Signed in
          <div className={styles.signedIn} id="content-wrap">
            <Header providerData={this.state.providerData} />
            <Dashboard />
          <button><a className={styles.button} onClick={() => firebaseApp.auth().signOut()}>Sign-out</a></button>
            <Footer /> 
          </div>
        }      
      </div>

Header.js:

import React from 'react';

const Header = (props) => (
    
    <header className="App-header">            
        {props.providerData && <button           
            aria-haspopup="true"
            aria-expanded={"false"}
            id="hamburger-menu" >
            <i className="main-button-icon fa fa-bars fa-3x"></i>
        </button>}
            <h1>Habit Tracker</h1>         
        {props.providerData &&            
                        <img src={props.providerData[0].photoURL}
                             alt={props.providerData[0].providerName}
                             className="container__profile--photo" />                 
               }           
    </header>
);

export default Header;

paulywill added a commit to paulywill/v8-geckos-team-07 that referenced this issue Apr 29, 2019
The state is working nicely with one header. In addition I've started to remove redundent components no longer needed. chingu-voyages#94
paulywill added a commit to paulywill/v8-geckos-team-07 that referenced this issue Apr 29, 2019
The firebase folder contained redundent files and components that are no longer being used. chingu-voyages#94
paulywill added a commit to paulywill/v8-geckos-team-07 that referenced this issue Apr 29, 2019
firebase-web-ui react component takes care of what SocialButton|ProfileList use to do. chingu-voyages#94
paulywill added a commit to paulywill/v8-geckos-team-07 that referenced this issue Apr 29, 2019
paulywill added a commit to paulywill/v8-geckos-team-07 that referenced this issue Apr 29, 2019
@paulywill
Copy link
Contributor Author

Errr... was making massive progress and now stuck.

Strangely previous code we have running on dev build passes user's data correctly when running Firebase function right in component. However when trying to fetch values from props when passing to Dashboard.js ... no luck.

Opened a Stackoverflow question: https://stackoverflow.com/questions/55897496/firebase-providerdata-passed-in-props-trouble-accessing-in-componentdidmount

Stumped on state / props / and whether super constructor is still required.

paulywill added a commit to paulywill/v8-geckos-team-07 that referenced this issue Apr 29, 2019
I was having a hell of time trying to pass the user's email to componentDidMount... after a lengthy post on Stackoverflow https://stackoverflow.com/questions/55904459/passing-props-to-componentdidmount finally got and answer. Will re-add all the code I removed for testing. chingu-voyages#94
paulywill added a commit to paulywill/v8-geckos-team-07 that referenced this issue Apr 29, 2019
paulywill added a commit to paulywill/v8-geckos-team-07 that referenced this issue Apr 29, 2019
paulywill added a commit to paulywill/v8-geckos-team-07 that referenced this issue Apr 29, 2019
paulywill added a commit to paulywill/v8-geckos-team-07 that referenced this issue Apr 29, 2019
There still 2 warnings; 1 for unused var that's breaks everything if I remove it: checkInComplet checkInComp = null; The other warning message is for the sign-out button link. Want's a valid href in the <a> tag.  chingu-voyages#94
@paulywill
Copy link
Contributor Author

paulywill commented Apr 29, 2019

Starting to see some light at the end of the tunnel again.

Still, have to bring back to working order:

  • Get modals to pop-up again
  • All buttons to work correctly/flow (ie Exit button on New Habit)
  • hamburger menu to show up again -> 📝: Button/link in the hamburger menu for user settings #90
  • Refresh after <CurrentHabit {...this.state.habitData} /> finishes fetching data
  • resolve two outstanding console warnings
  • test on separate heroku / response time before PR
2224dc0:
There still 2 warnings; 1 for unused var that's breaks everything if I remove it: checkInComplet checkInComp = null; The other warning message is for the sign-out button link. Want's a valid href in the <a> tag.

paulywill added a commit to paulywill/v8-geckos-team-07 that referenced this issue Apr 29, 2019
The modal for New habit is working once again... however the Exit and Submit are not performing as intended. Some of the code that easily read state is no longer working. There is some logic for example that passes around boolean values to determine which buttons to show/hide. Why this was working I'm sure if it was correct or why now there's so much difficulty. Again I think it goes that to 'proper' state/prop passing. Just starting to get the gist of it but there's some sticking points still. I feel as though out dev build was throwing values around like crazy and it worked... now trying to go by best practices I'm stuck figuring our what's happening. It would explain to some degree how the Layout was working previously. I removed it thinking it was a useless wrapper... not I'm thinking it was what was responsible for passing things again.   chingu-voyages#94
paulywill added a commit to paulywill/v8-geckos-team-07 that referenced this issue Apr 29, 2019
paulywill added a commit to paulywill/v8-geckos-team-07 that referenced this issue Apr 29, 2019
paulywill added a commit to paulywill/v8-geckos-team-07 that referenced this issue Apr 29, 2019
Moved the components into the this.state.habitExist; this of course breaks some logic. We're setting the habitExist based on clicking the button for the modal not the submission button. chingu-voyages#94
paulywill added a commit to paulywill/v8-geckos-team-07 that referenced this issue Apr 29, 2019
The habitExist was been set just for opening modal. chingu-voyages#94
paulywill added a commit to paulywill/v8-geckos-team-07 that referenced this issue Apr 29, 2019
Following inspiration from https://github.com/hamza-mirza/react-weather-app, making some of our forms statless chingu-voyages#94
paulywill added a commit to paulywill/v8-geckos-team-07 that referenced this issue Apr 29, 2019
paulywill added a commit to paulywill/v8-geckos-team-07 that referenced this issue Apr 30, 2019
Still playing with form refactor chingu-voyages#94
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
task-item task/work item as part of a larger feature
Projects
None yet
Development

No branches or pull requests

4 participants