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

Implement Hook to clean up how query parameters are added to frontend API calls #706

Open
PThorpe92 opened this issue Jan 30, 2025 · 0 comments · May be fixed by #696
Open

Implement Hook to clean up how query parameters are added to frontend API calls #706

PThorpe92 opened this issue Jan 30, 2025 · 0 comments · May be fixed by #696
Labels
feature - addition New feature or request for a new feature frontend refactor - frontend

Comments

@PThorpe92
Copy link
Member

PThorpe92 commented Jan 30, 2025

We should do something like this:

import { useState, useMemo } from 'react';

export interface QueryParams {
    page?: number;
    per_page?: number;
    visibility?: string;
    search?: string;
    category?: string;
    order_by?: string;
    order?: string;
    tags?: string[];
    facility_id?: number;
    user_id?: number;
    include?: string;
    all?: number;
    role?: string;
}

export function useQueryParams(initialParams: QueryParams = {}) {
    const [params, setParams] = useState<QueryParams>(initialParams);

    const updateQuery = (newParams: Partial<QueryParams>) => {
        setParams((prev) => ({ ...prev, ...newParams }));
    };

    const queryString = useMemo(() => {
        const searchParams = new URLSearchParams();
        Object.entries(params).forEach(([key, value]) => {
            if (value !== undefined && value !== null && value !== '') {
                searchParams.append(key, String(value));
            }
        });
        return searchParams.toString();
    }, [params]);

    return { updateQuery, queryString };
}

That would allow us to have something like this:

    const { updateQuery, queryString } = useQueryParams({
        page: 1,
        per_page: 20,
        visibility: getFilter(),
        search: '',
        role: role
    });
    const {
        data: libraries,
        mutate: mutateLibraries,
        error: librariesError,
        isLoading: librariesLoading
    } = useSWR<ServerResponseMany<Library>, AxiosError>(
        `/api/libraries?${queryString}`
    );

instead of currently we do:

    const [page, setPage] = useState<number>(1);
    const [perPage, setPerPage] = useState<number>(20);
    const [searchTerm, setSearchTerm] = useState<string>('');
    const [filterLibraries, setFilterLibraries] = useState<string>(
        FilterLibraries['All Libraries']
    );
    const [filterLibrariesAdmin, setFilterLibrariesAdmin] = useState<string>(
        LibraryAdminVisibility['All Libraries']
    );
    const [orderBy, setOrderBy] = useState<string>(
        FilterLibrariesVidsandHelpfulLinksAdmin['Title (A to Z)']
    );
        const {
        data: libraries,
        mutate: mutateLibraries,
        error: librariesError,
        isLoading: librariesLoading
    } = useSWR<ServerResponseMany<Library>, AxiosError>(
        `/api/libraries?page=${pageQuery}&per_page=${perPage}&visibility=${isAdministrator(user) && !adminWithStudentView() ? filterVisibilityAdmin : 'visible'}&search=${searchTerm}&${categoryQueryString}
    );```
@PThorpe92 PThorpe92 added frontend feature - addition New feature or request for a new feature refactor - frontend labels Jan 31, 2025
@jtucholski jtucholski linked a pull request Feb 7, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature - addition New feature or request for a new feature frontend refactor - frontend
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant