Skip to content

Commit

Permalink
Enable analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
margni-stephen committed Oct 14, 2020
1 parent 74cd689 commit a4116c3
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 43 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ REACT_APP_FIREBASE_PROJECT_ID="margni-note"
REACT_APP_FIREBASE_STORAGE_BUCKET="margni-note.appspot.com"
REACT_APP_FIREBASE_MESSAGING_SENDER_ID=1014018088230
REACT_APP_FIREBASE_APP_ID="1:1014018088230:web:67f4ad959540df2d6235e7"
REACT_APP_FIREBASE_MEASUREMENT_ID=G-0DLND3PD1K
30 changes: 14 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.0.4",
"@testing-library/user-event": "^12.1.6",
"@testing-library/user-event": "^12.1.7",
"cross-env": "^7.0.2",
"firebase": "^7.23.0",
"jest-junit": "^12.0.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react": "^17.0.0-rc.3",
"react-dom": "^17.0.0-rc.3",
"react-scripts": "^3.4.3"
},
"devDependencies": {
Expand Down
4 changes: 3 additions & 1 deletion src/component/MainWithNotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const filter = (tag, notes) =>
tag ? notes.filter((note) => note.tags.includes(tag)) : notes;

// TODO this is a temporary solution and should be refactored urgently.
export default ({ onSignOut }) => {
const MainWithNotes = ({ onSignOut }) => {
const {
create,
update,
Expand Down Expand Up @@ -139,3 +139,5 @@ export default ({ onSignOut }) => {
/>
);
};

export default MainWithNotes;
8 changes: 4 additions & 4 deletions src/context/AuthContext.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import React, { useContext, useEffect, useState } from 'react';
import firebase from 'firebase/app';

import { Firebase } from '../firebase';
import { firebaseApp } from '../firebaseApp';

export const AuthContext = React.createContext(null);

// TODO add multiple sign-in options
const signInWithGoogle = () => {
const provider = new firebase.auth.GoogleAuthProvider();

Firebase.auth().signInWithRedirect(provider);
firebaseApp.auth().signInWithRedirect(provider);
};

const signOut = () => {
Firebase.auth().signOut();
firebaseApp.auth().signOut();
};

export const AuthProvider = ({ children }) => {
const [user, setUser] = useState(null);

useEffect(() => Firebase.auth().onAuthStateChanged(setUser), []);
useEffect(() => firebaseApp.auth().onAuthStateChanged(setUser), []);

return (
<AuthContext.Provider value={{ signInWithGoogle, signOut, user }}>
Expand Down
8 changes: 4 additions & 4 deletions src/context/AuthContext.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jest.mock('firebase/app', () => ({
},
}));

jest.mock('../firebase', () => {
jest.mock('../firebaseApp', () => {
const auth = {
onAuthStateChanged: jest.fn(),
signInWithRedirect: jest.fn(),
Expand All @@ -21,7 +21,7 @@ jest.mock('../firebase', () => {
};
});

import { Firebase } from '../firebase';
import { firebaseApp } from '../firebaseApp';

import { AuthProvider, useAuth } from './AuthContext';

Expand All @@ -46,6 +46,6 @@ test('AuthProvider', () => {
fireEvent.click(getByText('SIGNOUT'));
fireEvent.click(getByText('SIGNIN'));

expect(Firebase.auth().signOut).toHaveBeenCalled();
expect(Firebase.auth().signInWithRedirect).toHaveBeenCalled();
expect(firebaseApp.auth().signOut).toHaveBeenCalled();
expect(firebaseApp.auth().signInWithRedirect).toHaveBeenCalled();
});
4 changes: 2 additions & 2 deletions src/context/NoteContext.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { createContext, useContext, useEffect, useState } from 'react';
import firebase from 'firebase/app';

import { Firebase } from '../firebase';
import { firebaseApp } from '../firebaseApp';
import { AuthContext } from './AuthContext';

export const NoteContext = createContext();

const collection = Firebase.firestore().collection('note');
const collection = firebaseApp.firestore().collection('note');

const create = (user) => {
const ref = collection.doc();
Expand Down
18 changes: 9 additions & 9 deletions src/firebase.js → src/firebaseApp.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/firestore';
import 'firebase/analytics';

const firebaseConfig = {
// Initialize Firebase
export const firebaseApp = firebase.initializeApp({
apiKey: process.env.REACT_APP_FIREBASE_KEY,
authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
databaseURL: process.env.REACT_APP_FIREBASE_DATABASE_URL,
projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.REACT_APP_FIREBASE_APP_ID,
};
measurementId: process.env.REACT_APP_FIREBASE_MEASUREMENT_ID,
});

// Initialize Firebase
export const Firebase = firebase.initializeApp(firebaseConfig);
firebaseApp.analytics();

Firebase.firestore()
firebaseApp
.firestore()
.enablePersistence({ synchronizeTabs: true })
.then(
() => null,
(error) => console.error(error)
);
.catch((error) => console.error(error));
10 changes: 6 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import ReactDOM from 'react-dom';

import * as serviceWorker from './serviceWorker';
import { App } from './layout/App';
import { Firebase } from './firebase';
import { firebaseApp } from './firebaseApp';

import './index.css';

Firebase.auth().onAuthStateChanged(() =>
ReactDOM.render(<App />, document.getElementById('root'))
);
firebaseApp
.auth()
.onAuthStateChanged(() =>
ReactDOM.render(<App />, document.getElementById('root'))
);

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
Expand Down

0 comments on commit a4116c3

Please sign in to comment.