Skip to content

Commit

Permalink
refactor: Replace LocalStorage get/set with LOCALSTORAGE constants
Browse files Browse the repository at this point in the history
  • Loading branch information
kaungmyatlwin committed Oct 17, 2020
1 parent a2f8456 commit c21b99d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
44 changes: 37 additions & 7 deletions pages/candidates/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import ConstituencyPlace from '../../components/Candidates/ConstituencyPlace/Con
import CandidateList from '../../components/Candidates/CandidateList/CandidateList';
import StateRegionCandidateList from '../../components/Candidates/StateRegionCandidateList/StateRegionCandidateList';
import { hasFullLocation } from '../../utils/helpers';
import { LOCALSTORAGE_KEYS } from '../../utils/constants';

import './candidates.module.scss';
import useAPI from '../../hooks/useAPI';
import Prompt from '../../components/Common/Prompt/Prompt';

const { TabPanel, Tab } = Tabs;

Expand All @@ -24,16 +26,17 @@ const Candidates = () => {
const [stateCandidates, setStateCandidates] = useState(null);
const [stateOrRegion, setStateOrRegion] = useState('');
const [shouldShowLocationLink, setShowLocationLink] = useState(true);
const [isPromptDismissed, setPromptDismissed] = useState(false);
const [, fetchData] = useAPI();

function getConstituency(house) {
return constituencies.find(constituency => constituency.house === house);
}

async function fetchWardDetails() {
const stateRegion = localStorage.getItem('stateRegion');
const township = localStorage.getItem('township');
const ward = localStorage.getItem('wardVillage');
const stateRegion = localStorage.getItem(LOCALSTORAGE_KEYS.STATE_REGION);
const township = localStorage.getItem(LOCALSTORAGE_KEYS.TOWNSHIP);
const ward = localStorage.getItem(LOCALSTORAGE_KEYS.WARD_VILLAGE);

const { data } = await fetchData('/api/locations', {
type: 'details',
Expand Down Expand Up @@ -99,24 +102,32 @@ const Candidates = () => {
}

function isNPT() {
const stateRegion = localStorage.getItem('stateRegion');
const township = localStorage.getItem('township');
const wardVillage = localStorage.getItem('wardVillage');
const stateRegion = localStorage.getItem(LOCALSTORAGE_KEYS.STATE_REGION);
const township = localStorage.getItem(LOCALSTORAGE_KEYS.TOWNSHIP);
const wardVillage = localStorage.getItem(LOCALSTORAGE_KEYS.WARD_VILLAGE);
return stateRegion.includes('နေပြည်တော်') || township === wardVillage;
}

// Pre-fetch constituencies
useEffect(() => {
ReactGA.pageview(window.location.pathname);

const candidatePromptDismissed = localStorage.getItem(
LOCALSTORAGE_KEYS.CANDIDATE_PROMPT_DISMISS
);

if (candidatePromptDismissed) {
setPromptDismissed(true);
}

// Because we can't access localStorage before React is initiated.
if (hasFullLocation()) {
setShowLocationLink(false);
} else {
return;
}

const stateRegion = localStorage.getItem('stateRegion');
const stateRegion = localStorage.getItem(LOCALSTORAGE_KEYS.STATE_REGION);

if (isNPT()) {
setStateOrRegion('တိုင်းဒေသကြီး'); // NPT defaults to တိုင်းဒေသ
Expand All @@ -136,6 +147,11 @@ const Candidates = () => {
}
}, [constituencies]);

function dismissPrompt() {
localStorage.setItem(LOCALSTORAGE_KEYS.CANDIDATE_PROMPT_DISMISS, true);
setPromptDismissed(true);
}

function onClickTab(value) {
// Fetch each candidates filtered on values
const constituency = getConstituency(value);
Expand Down Expand Up @@ -283,6 +299,20 @@ const Candidates = () => {
</TabPanel>
</div>
)}
<div className="my-2">
{!isPromptDismissed && (
<Prompt onClose={dismissPrompt}>
<span className="color-white">
ကိုယ်စားလှယ်လောင်းမည်သူမဆို ၎င်း၏ ကိုယ်ရေးအချက်အလက်များကို
အများပြည်သူထံ မထုတ်ပြန်စေလိုပါက
ပြည်ထောင်စုရွေးကောက်ပွဲကော်မရှင်သို့ စာဖြင့်
တင်ပြတောင်းဆိုနိုင်ပါသည်။ ပြည်ထောင်စုရွေးကောက်ပွဲကော်မရှင်သည်
mVoter2020 အက်ပလီကေးရှင်းမှ ၎င်း၏ ကိုယ်ရေးအချက်အလက်များကို
ဖယ်ရှားရန် ဆောင်ရွက်သွားမည် ဖြစ်ပါသည်။
</span>
</Prompt>
)}
</div>
</Layout>
);
};
Expand Down
13 changes: 7 additions & 6 deletions pages/location/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import WardVillageModal from '../../components/Location/WardVillageModal';

import './location.module.scss';
import { hasFullLocation } from '../../utils/helpers';
import { LOCALSTORAGE_KEYS } from '../../utils/constants';

const Location = () => {
const [townshipModalOpen, setTownshipModalOpen] = useState(false);
Expand All @@ -25,16 +26,16 @@ const Location = () => {
useEffect(() => {
if (hasFullLocation()) {
setIsAppStart(false);
setStateRegion(localStorage.getItem('stateRegion'));
setTownship(localStorage.getItem('township'));
setWardVillage(localStorage.getItem('wardVillage'));
setStateRegion(localStorage.getItem(LOCALSTORAGE_KEYS.STATE_REGION));
setTownship(localStorage.getItem(LOCALSTORAGE_KEYS.TOWNSHIP));
setWardVillage(localStorage.getItem(LOCALSTORAGE_KEYS.WARD_VILLAGE));
}
}, []);

function onClickDone() {
localStorage.setItem('stateRegion', stateRegion);
localStorage.setItem('township', township);
localStorage.setItem('wardVillage', wardVillage);
localStorage.setItem(LOCALSTORAGE_KEYS.STATE_REGION, stateRegion);
localStorage.setItem(LOCALSTORAGE_KEYS.TOWNSHIP, township);
localStorage.setItem(LOCALSTORAGE_KEYS.WARD_VILLAGE, wardVillage);
router.push('/candidates');
}

Expand Down
7 changes: 4 additions & 3 deletions utils/helpers.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import moment from 'moment';
import { LOCALSTORAGE_KEYS } from './constants';

export function hasFullLocation() {
return (
localStorage.getItem('stateRegion') &&
localStorage.getItem('township') &&
localStorage.getItem('wardVillage')
localStorage.getItem(LOCALSTORAGE_KEYS.STATE_REGION) &&
localStorage.getItem(LOCALSTORAGE_KEYS.TOWNSHIP) &&
localStorage.getItem(LOCALSTORAGE_KEYS.WARD_VILLAGE)
);
}

Expand Down

0 comments on commit c21b99d

Please sign in to comment.