Skip to content

Commit

Permalink
make sure reports always show reliably
Browse files Browse the repository at this point in the history
  • Loading branch information
BehshadBabai committed Dec 4, 2023
1 parent f27adda commit 473c9c5
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 32 deletions.
39 changes: 21 additions & 18 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,40 @@ import {
getAllReports,
getFileUrls
} from './Utilities/Util';
import { useAppDispatch } from './Redux/hooks';
import { useAppDispatch, useAppSelector } from './Redux/hooks';
import { changeReports } from './Redux/features/report/report-slice';
import Loading from './Components/Loading';

const { Content } = Layout;

const App = () => {
const [pending, setPending] = React.useState(true);
const account = useAppSelector((state) => state.account);
const dispatch = useAppDispatch();

React.useEffect(() => {
auth.onAuthStateChanged(async (user) => {
if (user) {
dispatch(changeLoggedIn(true));
dispatch(changeUserId(user.uid));
const docs = await fetchAllDocuments('users');
docs.forEach((doc) => {
const id = doc.id;
const data = doc.data();
const name = data.name as string;
const email = data.email as string;
const connectionId = data.connectionId as string;
const type = data.type as AccountType;
const view = data.view as ReportsViewType;
if (id === user.uid) {
dispatch(changeInfo({ name, email }));
dispatch(changeType(type));
dispatch(changeReportsViewType(view));
dispatch(changeConnectionId(connectionId));
}
});
if (!account.loggedIn) {
dispatch(changeUserId(user.uid));
const docs = await fetchAllDocuments('users');
docs.forEach((doc) => {
const id = doc.id;
const data = doc.data();
const name = data.name as string;
const email = data.email as string;
const connectionId = data.connectionId as string;
const type = data.type as AccountType;
const view = data.view as ReportsViewType;
if (id === user.uid) {
dispatch(changeInfo({ name, email }));
dispatch(changeType(type));
dispatch(changeReportsViewType(view));
dispatch(changeConnectionId(connectionId));
}
});
}
setPending(false);
} else {
dispatch(changeLoggedIn(false));
Expand Down
37 changes: 34 additions & 3 deletions src/Components/HomeParts/LoggedOut.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,26 @@ import {
import { auth } from '../../Firebase/firebase';
import { signInWithEmailAndPassword } from 'firebase/auth';
import { useNavigate } from 'react-router-dom';
import { AccountType } from '../../Redux/features/account/account-slice';
import {
AccountType,
ReportsViewType,
changeConnectionId,
changeInfo,
changeReportsViewType,
changeType,
changeUserId
} from '../../Redux/features/account/account-slice';
import { firebaseErrorCodes } from '../../Utilities/Constants';
import useScreenSize from '../../Hooks/useScreenSize';
import { useAppDispatch } from '../../Redux/hooks';
import { fetchAllDocuments } from '../../Utilities/Util';

const LoggedOut: React.FC = () => {
const [loginType, setLoginType] = React.useState<AccountType>(null);
const [loginLoading, setLoginLoading] = React.useState(false);
const screenSize = useScreenSize();
const dispatch = useAppDispatch();
const navigate = useNavigate();
const dispatch = useAppDispatch();

return (
<Row style={{ width: '100%' }} justify={'center'} gutter={[0, 40]}>
Expand Down Expand Up @@ -127,7 +136,29 @@ const LoggedOut: React.FC = () => {
? 'manager@gmail.com'
: 'supervisor@yahoo.com';
const password = '111111';
await signInWithEmailAndPassword(auth, email, password);
const res = await signInWithEmailAndPassword(
auth,
email,
password
);
const user = res.user;
dispatch(changeUserId(user.uid));
const docs = await fetchAllDocuments('users');
docs.forEach((doc) => {
const id = doc.id;
const data = doc.data();
const name = data.name as string;
const email = data.email as string;
const connectionId = data.connectionId as string;
const type = data.type as AccountType;
const view = data.view as ReportsViewType;
if (id === user.uid) {
dispatch(changeInfo({ name, email }));
dispatch(changeType(type));
dispatch(changeReportsViewType(view));
dispatch(changeConnectionId(connectionId));
}
});
message.success('Login Successful');
navigate('./reports');
} catch (error) {
Expand Down
37 changes: 26 additions & 11 deletions src/Pages/Reports.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,8 @@ const Reports: React.FC = () => {
const navigate = useNavigate();
const [selectLoading, setSelectLoading] = React.useState(false);
const [pageLoading, setPageLoading] = React.useState(true);
const initialReports = reports?.filter((report) => {
if (
report.creatorId === account.id ||
report.creatorId === account.connectionId
) {
return true;
}
return false;
});
const [reportsToShow, setReportsToShow] = React.useState(initialReports);
const [reportsToShow, setReportsToShow] = React.useState([]);
const [initialReports, setInitialReports] = React.useState([]);

React.useEffect(() => {
if (!loggedIn) {
Expand All @@ -42,8 +34,31 @@ const Reports: React.FC = () => {
}, []);

React.useEffect(() => {
setReportsToShow(initialReports);
if (reports) {
if (initialReports.length === 0) {
setInitialReports(
reports.filter((report) => {
if (
report.creatorId === account.id ||
report.creatorId === account.connectionId
) {
return true;
}
return false;
})
);
}
setReportsToShow(
reports.filter((report) => {
if (
report.creatorId === account.id ||
report.creatorId === account.connectionId
) {
return true;
}
return false;
})
);
setPageLoading(false);
}
}, [reports]);
Expand Down

0 comments on commit 473c9c5

Please sign in to comment.