Skip to content

Commit

Permalink
Merge pull request #30 from FuBoTeam/feature/google-analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
ben196888 authored Jan 11, 2022
2 parents 56edb2e + c1fe168 commit e6f4dbd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
40 changes: 24 additions & 16 deletions src/Board/index.tsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
import React, { useState, useEffect } from 'react';
import React, { useState, useEffect, useCallback } from 'react';
import { RouteComponentProps } from 'react-router';
import { useDatabase } from '../Provider/FirebaseApp';
import { logEvent } from 'firebase/analytics';
import './board.scss';

import { useAnalytics, useDatabase } from '../Provider/FirebaseApp';
import Dialog from './Dialog';
import { Background } from './Background';
import { subscribePost } from './subscribePost';

const Board: React.FC<RouteComponentProps> = (props) => {
// TODO: remove this and find a proper filter way on GA
const analytics = useAnalytics();
useEffect(() => {
logEvent(analytics, 'board_page_landed');
}, [analytics]);

const [modalDisplay, setModalDisplay] = useState(false);
const [user, setUser] = useState({});
const openModalAndClose = useCallback<(timeout?: number) => void>((timeout = 5000) => {
setModalDisplay(true);
setTimeout(() => {
setModalDisplay(false);
}, timeout);
}, [setModalDisplay]);

const [post, setPost] = useState({});
const database = useDatabase();

useEffect(() => {
// subscribe post while component did mount
const unsubscribe = subscribePost(database)((user) => {
if (user) {
setUser(user);
const displayAndHide = () => {
setModalDisplay(true);

setTimeout(() => {
setModalDisplay(false);
}, 5000);
};
displayAndHide();
const unsubscribe = subscribePost(database)((newPost) => {
if (newPost) {
setPost(newPost);
openModalAndClose();
}
});
return () => {
// unsubscribe post while component will unmount
unsubscribe();
};
}, [database]);
}, [database, openModalAndClose]);

return (
<React.Fragment>
<Background />
<Dialog user={user} show={modalDisplay} />
<Dialog user={post} show={modalDisplay} />
<a className="message-link" href={`${props.match.url}/greetings`}>&lt;&lt; 留下你的祝福</a>
<a className="game-link" href={`${props.match.url}/lottery`}>去遊戲頁</a>
</React.Fragment>
Expand Down
12 changes: 10 additions & 2 deletions src/Greeting/Greeting.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import React, { useCallback, useState } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import { RouteComponentProps } from 'react-router-dom';
import { logEvent } from 'firebase/analytics';
import './greeting.scss';

import { useAnalytics } from '../Provider/FirebaseApp';
import { GreetingForm } from './GreetingForm';
import Dialog from '../Board/Dialog';
import { getUpperUrl } from '../utils/urlHelpers';
import { RouteComponentProps } from 'react-router-dom';

export const Greeting: React.FC<RouteComponentProps> = (props) => {
// TODO: remove this and find a proper filter way on GA
const analytics = useAnalytics();
useEffect(() => {
logEvent(analytics, 'greeting_page_landed');
}, [analytics]);

const [modalDisplay, setModalDisplay] = useState(false);

const [user, setUser] = useState({ name: '', greetings: '', imgUrl: '' });
Expand Down
9 changes: 8 additions & 1 deletion src/Lottery/Lottery.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, { useCallback, useEffect, useState } from 'react';
import { RouteComponentProps } from 'react-router-dom';
import { logEvent } from 'firebase/analytics';
import './lottery.scss';

import { useDatabase } from '../Provider/FirebaseApp';
import { useAnalytics, useDatabase } from '../Provider/FirebaseApp';
import { listPosts } from '../api';
import { permutationList } from '../utils/random';
import { getUpperUrl } from '../utils/urlHelpers';
Expand All @@ -20,6 +21,12 @@ enum Stage {
}

export const Lottery: React.FC<RouteComponentProps> = (props) => {
// TODO: remove this and find a proper filter way on GA
const analytics = useAnalytics();
useEffect(() => {
logEvent(analytics, 'lottery_page_landed');
}, [analytics]);

const database = useDatabase();
const [posts, setPosts] = useState<WeddiApp.Post.Data[]>([]);
const [stage, setStage] = useState<Stage>(Stage.Init);
Expand Down
4 changes: 4 additions & 0 deletions src/Provider/FirebaseApp/useHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ export const useDatabase = (): FirebaseContextType['database'] => {
export const useStorage = (): FirebaseContextType['storage'] => {
return useContext(FirebaseContext).storage;
};

export const useAnalytics = (): FirebaseContextType['analytics'] => {
return useContext(FirebaseContext).analytics;
};

0 comments on commit e6f4dbd

Please sign in to comment.