Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
feat(app): update app to migrate to a new website address
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao committed Feb 25, 2023
1 parent 4ec26c5 commit 8493375
Show file tree
Hide file tree
Showing 176 changed files with 672 additions and 20,942 deletions.
97 changes: 3 additions & 94 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
import { lazy, useEffect, useState } from 'react';
import { useEffect, useState } from 'react';
import { createHashRouter, RouterProvider } from 'react-router-dom';
import { useRecoilValue, useSetRecoilState } from 'recoil';
import { ThemeProvider, createTheme } from '@mui/material/styles';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import FingerprintJS from '@fingerprintjs/fingerprintjs-pro';
import CssBaseline from '@mui/material/CssBaseline';
import { apiHostState, isLightThemeState, isOnlineState, visitorIDState } from './states/main';
import { InternetChecker } from './features/internetChecker';
import { appSnackOpenState } from './states/notification';
import DashboardMenu from './pages/DashboardMenu';
import NotificationWrapper from './features/notificationWrapper';
import Layout from './components/Layout';
import PrivateRoot from './components/PrivateRoot';
import { congAccountConnectedState } from './states/congregation';
import WeeklyAssignments from './pages/WeeklyAssignments';
import CongregationSettings from './pages/CongregationSettings';
import ErrorBoundary from './components/ErrorBoundary';
import backupWorkerInstance from './workers/backupWorker';

// lazy loading
const Administration = lazy(() => import('./pages/Administration'));
const Persons = lazy(() => import('./pages/Persons'));
const PersonDetails = lazy(() => import('./pages/PersonDetails'));
const Schedules = lazy(() => import('./pages/Schedules'));
const ScheduleDetails = lazy(() => import('./pages/ScheduleDetails'));
const S89 = lazy(() => import('./pages/S89'));
const S140 = lazy(() => import('./pages/S140'));
const ScheduleWeekDetails = lazy(() => import('./pages/ScheduleWeekDetails'));
const Settings = lazy(() => import('./pages/UserSettings'));
const SourceMaterials = lazy(() => import('./pages/SourceMaterials'));
const SourceWeekDetails = lazy(() => import('./pages/SourceWeekDetails'));
const CongregationPersonDetails = lazy(() => import('./pages/CongregationPersonDetails'));
import Migration from './pages/Migration';

// creating theme
const lightTheme = createTheme({
Expand All @@ -54,82 +34,14 @@ const App = ({ updatePwa }) => {
const isOnline = useRecoilValue(isOnlineState);
const isLight = useRecoilValue(isLightThemeState);
const appSnackOpen = useRecoilValue(appSnackOpenState);
const isCongAccountConnected = useRecoilValue(congAccountConnectedState);

const [activeTheme, setActiveTheme] = useState(darkTheme);

const router = createHashRouter([
{
element: <Layout updatePwa={updatePwa} />,
errorElement: <ErrorBoundary />,
children: [
{ path: '/', element: <DashboardMenu /> },
{
path: '/persons',
element: <Persons />,
},
{
path: '/persons/new',
element: <PersonDetails />,
},
{
path: '/persons/:id',
element: <PersonDetails />,
},
{
path: '/schedules/view/:weekToFormat',
element: <WeeklyAssignments />,
},
{
path: '/schedules',
element: <Schedules />,
},
{
path: '/schedules/:schedule',
element: <ScheduleDetails />,
},
{
path: '/schedules/:schedule/:weekToFormat',
element: <ScheduleWeekDetails />,
},
{
path: '/assignment-form',
element: <S89 />,
},
{
path: '/midweek-meeting-schedule',
element: <S140 />,
},
{
path: '/source-materials',
element: <SourceMaterials />,
},
{
path: '/source-materials/:weekToFormat',
element: <SourceWeekDetails />,
},
{
path: '/user-settings',
element: <Settings />,
},
{
path: '/congregation-settings',
element: <CongregationSettings />,
},
{
element: <PrivateRoot isCongAccountConnected={isCongAccountConnected} />,
children: [
{
path: '/administration',
element: <Administration />,
},
{
path: '/administration/members/:id',
element: <CongregationPersonDetails />,
},
],
},
],
children: [{ path: '/', element: <Migration /> }],
},
]);

Expand Down Expand Up @@ -157,7 +69,6 @@ const App = ({ updatePwa }) => {
} while (visitorId.length === 0);

setVisitorID(visitorId);
backupWorkerInstance.setVisitorID(visitorId);
};

if (isOnline) {
Expand All @@ -182,7 +93,6 @@ const App = ({ updatePwa }) => {
}

setApiHost(apiHost);
backupWorkerInstance.setApiHost(apiHost);
}, [setApiHost]);

useEffect(() => {
Expand All @@ -202,7 +112,6 @@ const App = ({ updatePwa }) => {
<QueryClientProvider client={queryClient}>
<ThemeProvider theme={activeTheme}>
<CssBaseline />
<InternetChecker />
{appSnackOpen && <NotificationWrapper />}

<RouterProvider router={router} />
Expand Down
152 changes: 94 additions & 58 deletions src/api/auth.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { getAuth } from 'firebase/auth';
import { getI18n } from 'react-i18next';
import { promiseGetRecoil, promiseSetRecoil } from 'recoil-outside';
import { dbGetAppSettings, dbUpdateAppSettings } from '../indexedDb/dbAppSettings';
import { initAppDb, isDbExist } from '../indexedDb/dbUtility';
import { congIDState, isAdminCongState, pocketMembersState } from '../states/congregation';
import { isOAuthAccountUpgradeState, qrCodePathState, secretTokenPathState, userIDState } from '../states/main';
import { congIDState } from '../states/congregation';
import { isOAuthAccountUpgradeState } from '../states/main';
import { appMessageState, appSeverityState, appSnackOpenState } from '../states/notification';
import { loadApp } from '../utils/app';
import { getProfile } from './common';
import backupWorkerInstance from '../workers/backupWorker';

export const apiSendAuthorization = async () => {
try {
const { apiHost, isOnline, visitorID } = await getProfile();

const auth = await getAuth();
const user = auth.currentUser;
if (isOnline && apiHost !== '') {
const auth = await getAuth();
const user = auth.currentUser;

if (isOnline && apiHost !== '' && visitorID && user) {
const res = await fetch(`${apiHost}user-login`, {
method: 'POST',
headers: {
Expand All @@ -31,11 +27,6 @@ export const apiSendAuthorization = async () => {
if (res.status === 200) {
return { isVerifyMFA: true };
} else {
if (data.secret && data.qrCode) {
await promiseSetRecoil(secretTokenPathState, data.secret);
await promiseSetRecoil(qrCodePathState, data.qrCode);
return { isSetupMFA: true };
}
if (data.message) {
await promiseSetRecoil(appMessageState, data.message);
await promiseSetRecoil(appSeverityState, 'warning');
Expand Down Expand Up @@ -81,55 +72,19 @@ export const apiHandleVerifyOTP = async (userOTP, isSetup, trustedDevice) => {
await promiseSetRecoil(appMessageState, data.message);
await promiseSetRecoil(appSeverityState, 'warning');
await promiseSetRecoil(appSnackOpenState, true);
return {};
}

if (!isSetup && data.secret) {
await promiseSetRecoil(secretTokenPathState, data.secret);
await promiseSetRecoil(qrCodePathState, data.qrCode);
return { reenroll: true };
return { tokenInvalid: true };
}
}

const { id, cong_id, cong_name, cong_role, cong_number, pocket_members } = data;
const { cong_id, cong_name, cong_role } = data;

if (cong_name.length === 0) return { createCongregation: true };

if (cong_role.length === 0) return { unauthorized: true };

if (!cong_role.includes('lmmo') && !cong_role.includes('lmmo-backup')) return { unauthorized: true };

backupWorkerInstance.setCongID(cong_id);
await promiseSetRecoil(congIDState, cong_id);

if (!isSetup) {
const settings = await dbGetAppSettings();
if (settings.isCongUpdated2 === undefined) {
return { updateCongregation: true };
}
}

if (cong_role.includes('admin')) {
await promiseSetRecoil(isAdminCongState, true);
}

const isMainDb = await isDbExist('lmm_oa');
if (!isMainDb) await initAppDb();

// save congregation update if any
let obj = {};
obj.username = data.username;
obj.cong_name = cong_name;
obj.cong_number = cong_number;
obj.isLoggedOut = false;
obj.pocket_members = pocket_members;
await dbUpdateAppSettings(obj);

await promiseSetRecoil(userIDState, id);
await promiseSetRecoil(pocketMembersState, pocket_members);

await loadApp();

return { success: true };
}
}
Expand Down Expand Up @@ -212,12 +167,6 @@ export const apiUpdatePasswordlessInfo = async (uid) => {
localStorage.removeItem('emailForSignIn');
return { isVerifyMFA: true, tmpEmail };
} else {
if (data.secret && data.qrCode) {
localStorage.removeItem('emailForSignIn');
await promiseSetRecoil(secretTokenPathState, data.secret);
await promiseSetRecoil(qrCodePathState, data.qrCode);
return { isSetupMFA: true, tmpEmail };
}
if (data.message) {
await promiseSetRecoil(appMessageState, t('verifyEmailError', { ns: 'ui' }));
await promiseSetRecoil(appSeverityState, 'warning');
Expand All @@ -233,3 +182,90 @@ export const apiUpdatePasswordlessInfo = async (uid) => {
return {};
}
};

export const apiPocketSignUp = async (code) => {
const { apiHost, visitorID } = await getProfile();

try {
if (apiHost !== '') {
const res = await fetch(`${apiHost}api/sws-pocket/signup`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ visitorid: visitorID, otp_code: code.toUpperCase() }),
});

const data = await res.json();

return { status: res.status, data };
}
} catch (err) {
throw new Error(err);
}
};

export const apiPocketValidate = async () => {
const { apiHost, visitorID } = await getProfile();

try {
if (apiHost !== '') {
const res = await fetch(`${apiHost}api/sws-pocket/validate-me`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
visitorid: visitorID,
},
});

const data = await res.json();

return { status: res.status, data };
}
} catch (err) {
throw new Error(err);
}
};

export const apiFetchPocketSessions = async () => {
const { apiHost, userID, visitorID } = await getProfile();

try {
if (apiHost !== '') {
const res = await fetch(`${apiHost}api/sws-pocket/${userID}/devices`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
visitorid: visitorID,
},
});

return res.json();
}
} catch (err) {
throw new Error(err);
}
};

export const apiPocketDeviceDelete = async (pocket_visitorid) => {
const { apiHost, userID, visitorID } = await getProfile();

try {
if (apiHost !== '') {
const res = await fetch(`${apiHost}api/sws-pocket/${userID}/devices`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
visitorid: visitorID,
},
body: JSON.stringify({ pocket_visitorid }),
});

const data = await res.json();

return { status: res.status, data };
}
} catch (err) {
throw new Error(err);
}
};
5 changes: 2 additions & 3 deletions src/api/common.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { promiseGetRecoil } from 'recoil-outside';
import { apiHostState, appLangState, isOnlineState, sourceLangState, visitorIDState } from '../states/main';
import { apiHostState, appLangState, isOnlineState, visitorIDState } from '../states/main';

export const getProfile = async () => {
const apiHost = await promiseGetRecoil(apiHostState);
const visitorID = await promiseGetRecoil(visitorIDState);
const appLang = await promiseGetRecoil(appLangState);
const sourceLang = await promiseGetRecoil(sourceLangState);
const isOnline = await promiseGetRecoil(isOnlineState);

return { apiHost, appLang, visitorID, sourceLang, isOnline };
return { apiHost, appLang, visitorID, isOnline };
};
Loading

0 comments on commit 8493375

Please sign in to comment.