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

feat: add firebase remote config #376

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions client/src/config/firebase.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { initializeApp } from 'firebase/app';
import { getAuth, connectAuthEmulator } from 'firebase/auth';
import { getRemoteConfig, isSupported } from 'firebase/remote-config';

const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
Expand All @@ -15,6 +16,9 @@ export const firebaseApp = initializeApp(firebaseConfig);

export const auth = firebaseApp ? getAuth(firebaseApp) : null;

export const remoteConfig = async () =>
(await isSupported()) && firebaseApp && getRemoteConfig(firebaseApp);

if (process.env.NODE_ENV !== 'production' && auth) {
connectAuthEmulator(auth, 'http://localhost:9099');
}
20 changes: 20 additions & 0 deletions client/src/config/remoteConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { fetchAndActivate, getValue } from 'firebase/remote-config';
import { remoteConfig } from './firebase';

export let isLivePageVisible = false;

export async function initRemoteConfig() {
const remote = await remoteConfig();
if (remote) {
remote.settings.minimumFetchIntervalMillis = 3600000; // 3 hours,for testing set this value to 0

remote.defaultConfig = {
isLivePageVisible: false,
};

await fetchAndActivate(remote);

isLivePageVisible = getValue(remote, 'isLivePageVisible').asBoolean();
}
}
initRemoteConfig();
Copy link
Member

Choose a reason for hiding this comment

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

This is not a good practice. Make this call as part of some init firebase script where authentication and everything is being initialized.

Copy link
Member

Choose a reason for hiding this comment

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

A solution would be to run this in the FirebaseContext we have wrapped over the app.

Copy link
Member Author

@120EE0692 120EE0692 Jan 4, 2023

Choose a reason for hiding this comment

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

@rutajdash I think this method is better as in future we may required many more values from remote config, in that case context will looked up bit messy, this style is referred from their docs.

Copy link
Member

Choose a reason for hiding this comment

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

Then find a way to keep context clean. You never define a function and then add the call right there below it. That is a very bar practice.

Copy link
Member Author

Choose a reason for hiding this comment

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

@rutajdash where I should exactly call this.

27 changes: 23 additions & 4 deletions client/src/pages/live.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
import React from 'react';
import React, { useState, useEffect } from 'react';
import Head from 'next/head';

//Components
import Marginals from '../components/marginals/Marginals';
import Live from '../screens/Live';
import BlockScreen from '../screens/BlockScreen';
import ActivityIndicator from '../components/shared/ActivityIndicator';

//firebase remote config
import { isLivePageVisible, initRemoteConfig } from '../config/remoteConfig';

const LivePage = () => {
const [isPageVisible, setIsPageVisible] = useState(isLivePageVisible);
const [loading, setLoading] = useState(true);

useEffect(() => {
setLoading(true);
(async () => {
await initRemoteConfig();
setIsPageVisible(isLivePageVisible);
setLoading(false);
})();
}, []);

return (
<>
<Head>
Expand Down Expand Up @@ -67,9 +84,11 @@ const LivePage = () => {
content='Monday Morning is the Media Body of National Institute Of Technology Rourkela. Monday Morning covers all the events, issues and activities going on inside the campus. Monday morning also serves as a link between the administration and the students.'
/>
</Head>
<Marginals>
<Live />
</Marginals>
{loading ? (
<ActivityIndicator />
) : (
<Marginals>{isPageVisible ? <Live /> : <BlockScreen />}</Marginals>
)}
</>
);
};
Expand Down
175 changes: 175 additions & 0 deletions client/src/screens/BlockScreen.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import React from 'react';

import Link from 'next/link';
import Image from 'next/image';

import { Grid, Typography } from '@mui/material';
import makeStyles from '@mui/styles/makeStyles';
import { ArrowForward } from '@mui/icons-material';

import logo from '../assets/images/logo_mm.png';
import LINKS from '../utils/getLinks';

const SOCIALS = [
{
link: 'https://www.facebook.com/mondaymorningnitr/',
icons: 'fab fa-facebook-f',
},
{
link: 'https://twitter.com/mmnitrkl?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor',
icons: 'fab fa-twitter',
},
{
link: 'https://www.youtube.com/c/MondayMorningNITR',
icons: 'fab fa-youtube',
},
{
link: 'https://in.linkedin.com/company/monday-morning-the-official-student-media-body-of-nit-rourkela',
icons: 'fab fa-linkedin',
},
{
link: 'https://www.instagram.com/mondaymorningnitrofficial/?hl=en',
icons: 'fab fa-instagram',
},
];

const PageNotFound = () => {
const classes = useStyles();
return (
<div className={classes.root}>
<Grid
container
direction='column'
justifyContent='center'
alignItems='center'
>
<Grid item className={classes.gridContainerLogo}>
<Image src={logo} alt='MM Logo' />
</Grid>
<Grid item className={classes.gridContainer}>
<Typography className={classes.subTitle}>Page blocked.</Typography>
<Typography className={classes.body}>
This page has been temporarily closed by the{' '}
<Link href={'https://website.nitrkl.ac.in/Placement/'}>
<a target='_blank' rel='noreferrer'>
Career Development Center of the National Institute of
Technology, Rourkela.
</a>
</Link>
120EE0692 marked this conversation as resolved.
Show resolved Hide resolved
</Typography>
</Grid>
<Grid item className={classes.gridContainer}>
<Link passHref href={LINKS.CATEGORIES.HOME} className={classes.link}>
<Typography className={classes.homeLink}>
Go back home <ArrowForward />
</Typography>
</Link>
</Grid>
</Grid>
<div className={classes.socialIcons}>
{SOCIALS.map(({ icons, link }) => (
<Link key={link} href={link}>
<a target='_blank' rel='noreferrer'>
<span className={classes.socialIcon}>
<i className={icons} />
</span>
</a>
</Link>
))}
</div>
</div>
);
};

export default PageNotFound;

const useStyles = makeStyles((theme) => ({
root: {
width: '100%',
height: '100%',
textAlign: 'center',
display: 'flex',
alignItems: 'center',
flexDirection: 'column',
[theme.breakpoints.down('lg')]: {
padding: '20px',
},
},
gridContainerLogo: {
marginTop: '70px',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
flexDirection: 'column',
height: 84,
width: 86,
[theme.breakpoints.down('lg')]: {
height: 60,
width: 60,
marginTop: '20px',
},
},
gridContainer: {
marginTop: '70px',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
flexDirection: 'column',
[theme.breakpoints.down('lg')]: {
marginTop: '20px',
},
},
subTitle: {
fontSize: '52px',
fontWeight: '700',
[theme.breakpoints.down('md')]: {
fontSize: '30px',
},
},
body: {
marginTop: '28px',
fontSize: '24px',
color: '#6E6E6E',
[theme.breakpoints.down('md')]: {
fontSize: '19px',
},
[theme.breakpoints.up('sm')]: {
maxWidth: '60vw',
},
},
link: {
cursor: 'pointer',
textDecoration: 'none',
},
homeLink: {
fontSize: '24px',
fontWeight: '600',
color: '#006DCC',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
cursor: 'default',
[theme.breakpoints.down('md')]: {
fontSize: '18px',
},
},
socialIcons: {
display: 'flex',
justifyContent: 'center',
alignItems: 'flex-end',
marginTop: '105px',
[theme.breakpoints.down('lg')]: {
marginTop: '20px',
},
},
socialIcon: {
marginLeft: '20px',
fontSize: '21px',
color: '#999999',
cursor: 'pointer',
[theme.breakpoints.down('md')]: {
marginLeft: '10px',
fontSize: '15px',
},
},
}));