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

Fix useEffect async Bug #179

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions client/src/components/AdminContainer/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState, useEffect } from 'react';
import { NavLink, useHistory, Link } from 'react-router-dom';
import PropTypes from 'prop-types';
import { Layout, Button } from 'antd';
Expand All @@ -11,11 +11,37 @@ import * as ROUTES from '../../constants/routes';
const { Header } = Layout;

const AdminContainer = ({ children, buttonContent, buttonRoute }) => {
const [prevPath, setPrevPath] = useState();
const history = useHistory();
const {
location: { pathname },
goBack,
push,
} = history;

useEffect(() => {
setPrevPath(pathname);
}, []);

const checkGoBack = () => {
if (
prevPath.includes('add/remotely') ||
prevPath.includes('add/internal') ||
prevPath.includes('edit')
) {
goBack();
} else if (
prevPath.includes('students') ||
prevPath.includes('add') ||
prevPath.includes('projects')
) {
push(ROUTES.ADMIN_COHORT_PAGE);
} else if (prevPath.includes('cohorts')) {
push('/admin/statistics');
} else {
goBack();
}
};
return (
<>
<Layout>
Expand Down Expand Up @@ -62,7 +88,7 @@ const AdminContainer = ({ children, buttonContent, buttonRoute }) => {
<Button
className="admin-back-btn"
type="primary"
onClick={goBack}
onClick={checkGoBack}
>
❮ Back
</Button>
Expand Down
6 changes: 4 additions & 2 deletions client/src/components/Charts/pageviewReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ const PageviewsReport = () => {
});
setReportData(newReportData);
};

useEffect(async () => {
const reactAppView = async () => {
const { REACT_APP_VIEW_ID } = await getSecrets();
const request = {
viewID: REACT_APP_VIEW_ID,
Expand All @@ -58,6 +57,9 @@ const PageviewsReport = () => {
}),
1000
);
};
useEffect(() => {
reactAppView();
}, [startDate, endDate]);

return (
Expand Down
12 changes: 8 additions & 4 deletions client/src/containers/CohortsAlumniPage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class CohortsAlumni extends Component {
const {
data: { data },
} = cohortsData;
this.setState({ data, isCohortPages: false });
this.setState({ data, isCohortPages: false, activeItemIndex: 0 });
} catch (err) {
notification.error({
message: 'Internal Server Error',
Expand All @@ -70,7 +70,7 @@ class CohortsAlumni extends Component {
const {
data: { data },
} = alumniData;
this.setState({ data, isCohortPages: false });
this.setState({ data, isCohortPages: false, activeItemIndex: 0 });
} catch (err) {
notification.error({
message: 'Internal Server Error',
Expand All @@ -93,8 +93,12 @@ class CohortsAlumni extends Component {
data: { data },
} = cohortsAlumniData;
this.getCohortName(Number(cohortId));
this.setState({ data, isCohortPages: true, cohortId });
this.setState({ data, isCohortPages: true });
this.setState({
data,
isCohortPages: true,
cohortId,
activeItemIndex: 0,
});
} catch (err) {
notification.error({
message: 'Internal Server Error',
Expand Down