Skip to content

Commit

Permalink
[PUI] Table query params (#8279)
Browse files Browse the repository at this point in the history
* Pass more information through in redirect after login

- Include query parameters in redirect

* InvenTreeTable: Update filters based on URL query parameters

* Add button to remove custom URL query filters
  • Loading branch information
SchrodingersGat authored Oct 14, 2024
1 parent 7d3eb43 commit eeab8d3
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 13 deletions.
9 changes: 6 additions & 3 deletions src/frontend/src/components/forms/AuthenticationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import { useLocation, useNavigate } from 'react-router-dom';

import { api } from '../../App';
import { ApiEndpoints } from '../../enums/ApiEndpoints';
import { doBasicLogin, doSimpleLogin } from '../../functions/auth';
import {
doBasicLogin,
doSimpleLogin,
followRedirect
} from '../../functions/auth';
import { showLoginNotification } from '../../functions/notifications';
import { apiUrl, useServerApiState } from '../../states/ApiState';
import { useUserState } from '../../states/UserState';
Expand Down Expand Up @@ -51,8 +55,7 @@ export function AuthenticationForm() {
title: t`Login successful`,
message: t`Logged in successfully`
});

navigate(location?.state?.redirectFrom ?? '/home');
followRedirect(navigate, location?.state);
} else {
showLoginNotification({
title: t`Login failed`,
Expand Down
9 changes: 8 additions & 1 deletion src/frontend/src/components/nav/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ export const ProtectedRoute = ({ children }: { children: JSX.Element }) => {

if (!isLoggedIn()) {
return (
<Navigate to="/logged-in" state={{ redirectFrom: location.pathname }} />
<Navigate
to="/logged-in"
state={{
redirectUrl: location.pathname,
queryParams: location.search,
anchor: location.hash
}}
/>
);
}

Expand Down
19 changes: 15 additions & 4 deletions src/frontend/src/functions/auth.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { t } from '@lingui/macro';
import { notifications } from '@mantine/notifications';
import axios from 'axios';
import { NavigateFunction } from 'react-router-dom';
import { Navigate, NavigateFunction } from 'react-router-dom';

import { api, setApiDefaults } from '../App';
import { ApiEndpoints } from '../enums/ApiEndpoints';
Expand All @@ -11,6 +11,17 @@ import { useUserState } from '../states/UserState';
import { fetchGlobalStates } from '../states/states';
import { showLoginNotification } from './notifications';

export function followRedirect(navigate: NavigateFunction, redirect: any) {
let url = redirect?.redirectUrl ?? '/home';

if (redirect?.queryParams) {
// Construct and appand query parameters
url = url + '?' + new URLSearchParams(redirect.queryParams).toString();
}

navigate(url);
}

/**
* sends a request to the specified url from a form. this will change the window location.
* @param {string} path the path to send the post request to
Expand Down Expand Up @@ -177,7 +188,7 @@ export function handleReset(navigate: any, values: { email: string }) {
*/
export const checkLoginState = async (
navigate: any,
redirect?: string,
redirect?: any,
no_redirect?: boolean
) => {
setApiDefaults();
Expand All @@ -197,13 +208,13 @@ export const checkLoginState = async (

fetchGlobalStates();

navigate(redirect ?? '/home');
followRedirect(navigate, redirect);
};

// Callback function when login fails
const loginFailure = () => {
if (!no_redirect) {
navigate('/login', { state: { redirectFrom: redirect } });
navigate('/login', { state: redirect });
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/pages/Auth/Logged-In.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function Logged_In() {
const location = useLocation();

useEffect(() => {
checkLoginState(navigate, location?.state?.redirectFrom);
checkLoginState(navigate, location?.state);
}, [navigate]);

return (
Expand Down
10 changes: 7 additions & 3 deletions src/frontend/src/pages/Auth/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import {
} from '../../components/forms/AuthenticationForm';
import { InstanceOptions } from '../../components/forms/InstanceOptions';
import { defaultHostKey } from '../../defaults/defaultHostList';
import { checkLoginState, doBasicLogin } from '../../functions/auth';
import {
checkLoginState,
doBasicLogin,
followRedirect
} from '../../functions/auth';
import { useServerApiState } from '../../states/ApiState';
import { useLocalState } from '../../states/LocalState';

Expand Down Expand Up @@ -49,15 +53,15 @@ export default function Login() {
ChangeHost(defaultHostKey);
}

checkLoginState(navigate, location?.state?.redirectFrom, true);
checkLoginState(navigate, location?.state, true);

// check if we got login params (login and password)
if (searchParams.has('login') && searchParams.has('password')) {
doBasicLogin(
searchParams.get('login') ?? '',
searchParams.get('password') ?? ''
).then(() => {
navigate(location?.state?.redirectFrom ?? '/home');
followRedirect(navigate, location?.state);
});
}
}, []);
Expand Down
34 changes: 33 additions & 1 deletion src/frontend/src/tables/InvenTreeTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import {
IconBarcode,
IconFilter,
IconFilterCancel,
IconRefresh,
IconTrash
} from '@tabler/icons-react';
Expand All @@ -28,7 +29,7 @@ import React, {
useMemo,
useState
} from 'react';
import { useNavigate } from 'react-router-dom';
import { useNavigate, useSearchParams } from 'react-router-dom';

import { api } from '../App';
import { Boundary } from '../components/Boundary';
Expand Down Expand Up @@ -155,10 +156,14 @@ export function InvenTreeTable<T extends Record<string, any>>({
setTableSorting,
loader
} = useLocalState();

const [fieldNames, setFieldNames] = useState<Record<string, string>>({});

const navigate = useNavigate();

// Extract URL query parameters (e.g. ?active=true&overdue=false)
const [urlQueryParams, setUrlQueryParams] = useSearchParams();

// Construct table filters - note that we can introspect filter labels from column names
const filters: TableFilter[] = useMemo(() => {
return (
Expand Down Expand Up @@ -361,6 +366,13 @@ export function InvenTreeTable<T extends Record<string, any>>({
);
}

// Allow override of filters based on URL query parameters
if (urlQueryParams) {
for (let [key, value] of urlQueryParams) {
queryParams[key] = value;
}
}

// Add custom search term
if (tableState.searchTerm) {
queryParams.search = tableState.searchTerm;
Expand Down Expand Up @@ -522,6 +534,11 @@ export function InvenTreeTable<T extends Record<string, any>>({
refetchOnMount: true
});

// Refetch data when the query parameters change
useEffect(() => {
refetch();
}, [urlQueryParams]);

useEffect(() => {
tableState.setIsLoading(
isFetching ||
Expand Down Expand Up @@ -699,6 +716,21 @@ export function InvenTreeTable<T extends Record<string, any>>({
onToggleColumn={toggleColumn}
/>
)}
{urlQueryParams.size > 0 && (
<ActionIcon
variant="transparent"
color="red"
aria-label="table-clear-query-filters"
>
<Tooltip label={t`Clear custom query filters`}>
<IconFilterCancel
onClick={() => {
setUrlQueryParams({});
}}
/>
</Tooltip>
</ActionIcon>
)}
{tableProps.enableFilters && filters.length > 0 && (
<Indicator
size="xs"
Expand Down

0 comments on commit eeab8d3

Please sign in to comment.