Skip to content

Commit

Permalink
Merge pull request #72 from sparcs-kaist/migration@redux-root-setting
Browse files Browse the repository at this point in the history
Complete the integration of TypeScript with Redux
  • Loading branch information
sboh1214 authored May 27, 2024
2 parents ccd274c + 54b4f9c commit de39597
Show file tree
Hide file tree
Showing 91 changed files with 439 additions and 458 deletions.
263 changes: 0 additions & 263 deletions src/App.jsx

This file was deleted.

133 changes: 133 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import axios from 'axios';
import React, { useEffect, useState } from 'react';
import ReactGA from 'react-ga4';
import { useDispatch } from 'react-redux';
import { useLocation, Outlet } from 'react-router-dom';

import { CAMPAIGN_KEY, STORAGE_KEY } from '@/const';
import Header from '@/common/guideline/components/Header';
import BannerPopup from '@/common/components/popup/bannerPopup/BannerPopup';
import CampaignPopupImage from '@/features/campaign/components/popup/CampaignPopupImage';
import PopupMenu from '@/features/campaign/components/popup/PopupMenu';
import { setIsPortrait, setSemesters, setTracks, setUser } from '@/redux/actions/common';

const App: React.FC = () => {
const dispatch = useDispatch();
const location = useLocation();
const [popupOpen, setPopupOpen] = useState(localStorage.getItem(STORAGE_KEY) !== CAMPAIGN_KEY);
const portraitMediaQuery = window.matchMedia('(max-aspect-ratio: 4/3)');

useEffect(() => {
ReactGA.send({ hitType: 'pageview', page: location.pathname });
}, [location]);

useEffect(() => {
fetchUser();
fetchSemesters();
updateSizeProperty();
window.addEventListener('resize', updateSizeProperty);
updateIsPortrait();
portraitMediaQuery.addEventListener('change', updateIsPortrait);
return () => {
window.removeEventListener('resize', updateSizeProperty);
portraitMediaQuery.removeEventListener('change', updateIsPortrait);
};
}, []);

const fetchUser = () => {
axios
.get('/session/info', {
metadata: {
gaCategory: 'User',
gaVariable: 'Get / Instance',
},
})
.then((response) => {
dispatch(setUser(response.data));
})
.catch((error) => {
if (error.response && error.response.status === 401) {
dispatch(setUser(null));
}
});
};

const fetchSemesters = () => {
axios
.get('/api/semesters', {
params: {
order: ['year', 'semester'],
},
metadata: {
gaCategory: 'Semester',
gaVariable: 'GET / List',
},
})
.then((response) => {
dispatch(setSemesters(response.data));
})
.catch(() => {});

axios
.get('/api/tracks', {
params: {},
metadata: {
gaCategory: 'Track',
gaVariable: 'GET / List',
},
})
.then((response) => {
dispatch(setTracks(response.data));
})
.catch(() => {});
};

const updateSizeProperty = () => {
document.documentElement.style.setProperty('--window-inner-height', `${window.innerHeight}px`);
};

const updateIsPortrait = () => {
dispatch(setIsPortrait(portraitMediaQuery.matches));
};

const setDoNotShow = (state: boolean) => {
if (state) {
// setDoNotShowAgain(true);
localStorage.setItem(STORAGE_KEY, CAMPAIGN_KEY);
} else {
// setDoNotShowAgain(false);
localStorage.removeItem(STORAGE_KEY);
}
};

return (
<>
<Header />
<Outlet />
<section>
<BannerPopup popupOpen={popupOpen} setPopupOpen={setPopupOpen}>
<CampaignPopupImage closePopup={() => setPopupOpen(false)} />
<PopupMenu
onClose={() => {
ReactGA.event({
category: 'Campaign',
action: 'popup-close',
});
setPopupOpen(false);
}}
onDoNotShow={() => {
ReactGA.event({
category: 'Campaign',
action: 'popup-do-not-show',
});
setDoNotShow(true);
setPopupOpen(false);
}}
/>
</BannerPopup>
</section>
</>
);
};

export default App;
Loading

0 comments on commit de39597

Please sign in to comment.