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

Refine routes definitions #4579

Merged
merged 5 commits into from
Jan 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
59 changes: 0 additions & 59 deletions client/app/components/ApplicationArea/AuthenticatedPageWrapper.jsx

This file was deleted.

41 changes: 0 additions & 41 deletions client/app/components/ApplicationArea/SignedOutPageWrapper.jsx

This file was deleted.

42 changes: 42 additions & 0 deletions client/app/components/ApplicationArea/withApiKeySession.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React, { useEffect, useState, useContext } from "react";
import PropTypes from "prop-types";
import { ErrorBoundaryContext } from "@/components/ErrorBoundary";
import { Auth } from "@/services/auth";

export default function withApiKeySession(WrappedComponent) {
function ApiKeySessionWrapper({ apiKey, ...props }) {
const [isAuthenticated, setIsAuthenticated] = useState(false);
const { handleError } = useContext(ErrorBoundaryContext);

useEffect(() => {
let isCancelled = false;
Auth.setApiKey(apiKey);
Auth.loadConfig()
.then(() => {
if (!isCancelled) {
setIsAuthenticated(true);
}
})
.catch(() => {
if (!isCancelled) {
setIsAuthenticated(false);
}
});
return () => {
isCancelled = true;
};
}, [apiKey]);

if (!isAuthenticated) {
return null;
}

return <WrappedComponent onError={handleError} apiKey={apiKey} {...props} />;
}

ApiKeySessionWrapper.propTypes = {
apiKey: PropTypes.string.isRequired,
};

return ApiKeySessionWrapper;
}
67 changes: 67 additions & 0 deletions client/app/components/ApplicationArea/withUserSession.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React, { useEffect, useState } from "react";
import PropTypes from "prop-types";
import ErrorBoundary, { ErrorBoundaryContext } from "@/components/ErrorBoundary";
import { Auth } from "@/services/auth";
import organizationStatus from "@/services/organizationStatus";
import ApplicationHeader from "./ApplicationHeader";
import ErrorMessage from "./ErrorMessage";

export default function withUserSession(WrappedComponent) {
function UserSessionWrapper({ bodyClass, ...props }) {
const [isAuthenticated, setIsAuthenticated] = useState(!!Auth.isAuthenticated());

useEffect(() => {
let isCancelled = false;
Promise.all([Auth.requireSession(), organizationStatus.refresh()])
.then(() => {
if (!isCancelled) {
setIsAuthenticated(!!Auth.isAuthenticated());
}
})
.catch(() => {
if (!isCancelled) {
setIsAuthenticated(false);
}
});
return () => {
isCancelled = true;
};
}, []);

useEffect(() => {
if (bodyClass) {
document.body.classList.toggle(bodyClass, true);
return () => {
document.body.classList.toggle(bodyClass, false);
};
}
}, [bodyClass]);

if (!isAuthenticated) {
return null;
}

return (
<>
<ApplicationHeader />
<ErrorBoundary renderError={error => <ErrorMessage error={error} />}>
<ErrorBoundaryContext.Consumer>
{({ handleError }) => <WrappedComponent onError={handleError} {...props} />}
kravets-levko marked this conversation as resolved.
Show resolved Hide resolved
</ErrorBoundaryContext.Consumer>
</ErrorBoundary>
</>
);
}

UserSessionWrapper.propTypes = {
bodyClass: PropTypes.string,
children: PropTypes.node,
};

UserSessionWrapper.defaultProps = {
bodyClass: null,
children: null,
};

return UserSessionWrapper;
}
13 changes: 4 additions & 9 deletions client/app/pages/admin/Jobs.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import { axios } from "@/services/axios";
import Alert from "antd/lib/alert";
import Tabs from "antd/lib/tabs";
import * as Grid from "antd/lib/grid";
import AuthenticatedPageWrapper from "@/components/ApplicationArea/AuthenticatedPageWrapper";
import withUserSession from "@/components/ApplicationArea/withUserSession";
import Layout from "@/components/admin/Layout";
import { ErrorBoundaryContext } from "@/components/ErrorBoundary";
import { CounterCard, WorkersTable, QueuesTable, OtherJobsTable } from "@/components/admin/RQStatus";

import location from "@/services/location";
Expand Down Expand Up @@ -121,14 +120,10 @@ class Jobs extends React.Component {
}
}

const JobsPage = withUserSession(Jobs);
kravets-levko marked this conversation as resolved.
Show resolved Hide resolved

export default {
path: "/admin/queries/jobs",
title: "RQ Status",
render: currentRoute => (
<AuthenticatedPageWrapper key={currentRoute.key}>
<ErrorBoundaryContext.Consumer>
{({ handleError }) => <Jobs {...currentRoute.routeParams} onError={handleError} />}
</ErrorBoundaryContext.Consumer>
</AuthenticatedPageWrapper>
),
render: currentRoute => <JobsPage key={currentRoute.key} {...currentRoute.routeParams} />,
};
63 changes: 29 additions & 34 deletions client/app/pages/admin/OutdatedQueries.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import { axios } from "@/services/axios";

import Switch from "antd/lib/switch";
import * as Grid from "antd/lib/grid";
import AuthenticatedPageWrapper from "@/components/ApplicationArea/AuthenticatedPageWrapper";
import withUserSession from "@/components/ApplicationArea/withUserSession";
import Paginator from "@/components/Paginator";
import { QueryTagsControl } from "@/components/tags-control/TagsControl";
import SchedulePhrase from "@/components/queries/SchedulePhrase";
import TimeAgo from "@/components/TimeAgo";
import Layout from "@/components/admin/Layout";
import { ErrorBoundaryContext } from "@/components/ErrorBoundary";

import { wrap as itemsList, ControllerType } from "@/components/items-list/ItemsList";
import { ItemsSource } from "@/components/items-list/classes/ItemsSource";
Expand Down Expand Up @@ -147,43 +146,39 @@ class OutdatedQueries extends React.Component {
}
}

const OutdatedQueriesPage = itemsList(
OutdatedQueries,
() =>
new ItemsSource({
doRequest(request, context) {
return (
axios
.get("/api/admin/queries/outdated")
// eslint-disable-next-line camelcase
.then(({ queries, updated_at }) => {
context.setCustomParams({ lastUpdatedAt: parseFloat(updated_at) });
return queries;
})
);
},
processResults(items) {
return map(items, item => new Query(item));
},
isPlainList: true,
}),
() => new StateStorage({ orderByField: "created_at", orderByReverse: true })
const OutdatedQueriesPage = withUserSession(
itemsList(
OutdatedQueries,
() =>
new ItemsSource({
doRequest(request, context) {
return (
axios
.get("/api/admin/queries/outdated")
// eslint-disable-next-line camelcase
.then(({ queries, updated_at }) => {
context.setCustomParams({ lastUpdatedAt: parseFloat(updated_at) });
return queries;
})
);
},
processResults(items) {
return map(items, item => new Query(item));
},
isPlainList: true,
}),
() => new StateStorage({ orderByField: "created_at", orderByReverse: true })
)
);

export default {
path: "/admin/queries/outdated",
title: "Outdated Queries",
render: currentRoute => (
<AuthenticatedPageWrapper key={currentRoute.key}>
<ErrorBoundaryContext.Consumer>
{({ handleError }) => (
<OutdatedQueriesPage
routeParams={{ ...currentRoute.routeParams, currentPage: "outdated_queries" }}
currentRoute={currentRoute}
onError={handleError}
/>
)}
</ErrorBoundaryContext.Consumer>
</AuthenticatedPageWrapper>
<OutdatedQueriesPage
key={currentRoute.key}
routeParams={{ ...currentRoute.routeParams, currentPage: "outdated_queries" }}
currentRoute={currentRoute}
/>
),
};
13 changes: 4 additions & 9 deletions client/app/pages/admin/SystemStatus.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import React from "react";
import { axios } from "@/services/axios";
import PropTypes from "prop-types";

import AuthenticatedPageWrapper from "@/components/ApplicationArea/AuthenticatedPageWrapper";
import withUserSession from "@/components/ApplicationArea/withUserSession";
import Layout from "@/components/admin/Layout";
import { ErrorBoundaryContext } from "@/components/ErrorBoundary";
import * as StatusBlock from "@/components/admin/StatusBlock";
import recordEvent from "@/services/recordEvent";

Expand Down Expand Up @@ -81,14 +80,10 @@ class SystemStatus extends React.Component {
}
}

const SystemStatusPage = withUserSession(SystemStatus);

export default {
path: "/admin/status",
title: "System Status",
render: currentRoute => (
<AuthenticatedPageWrapper key={currentRoute.key}>
<ErrorBoundaryContext.Consumer>
{({ handleError }) => <SystemStatus {...currentRoute.routeParams} onError={handleError} />}
</ErrorBoundaryContext.Consumer>
</AuthenticatedPageWrapper>
),
render: currentRoute => <SystemStatusPage key={currentRoute.key} {...currentRoute.routeParams} />,
};
Loading