Skip to content

Conversation

@mvolkmann
Copy link
Collaborator

No description provided.

@mvolkmann mvolkmann marked this pull request as draft April 30, 2024 20:41
@mvolkmann mvolkmann marked this pull request as ready for review May 1, 2024 17:20
export const useQueryParameters = qps => {
export const useQueryParameters = (
qps,
requirements = [],
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some pages require data to be loaded before query parameters can be processed. This page is one of them. We must have an array of all possible PDL objects in order to call setSelectedPdls with an array of PDL objects that have ids that match those in the pdls query parameter. Support this was extremely difficult and did not increase my love of React. ;-)

requirements is an array of data values that must be set in order to process the query parameters. It defaults to an empty array because most pages do not need this.

processedQPs is an object created by a call to useRef in the calling page. It holds a Boolean that indicates whether we have finished processing the query parameters. This can only happen after all the requirements values are present. I'm using a ref because the value needs to be maintained across multiple renders of the page which React loves to do. I'm open to suggestions for other ways to maintain this flag.

// This examines all the query parameters and
// sets the state value associated with each of them.
useEffect(() => {
if (processedQPs?.current) return;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the page supplied a value for processedQPs AND the value it holds is true then we don't need to run the code that follows again.

useEffect(() => {
if (processedQPs?.current) return;

const haveRequirements = requirements.every(req =>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If requirements is an empty array, this will be set to true which is what we want.

if (processedQPs?.current) return;

const haveRequirements = requirements.every(req =>
Array.isArray(req) ? req.length > 0 : req !== null && req !== undefined
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a required value is an array, the requirement is met when the array is not empty.
Otherwise the requirement is met when the value is not null or undefined.

const haveRequirements = requirements.every(req =>
Array.isArray(req) ? req.length > 0 : req !== null && req !== undefined
);
if (!haveRequirements) return;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the requirements are not met then we cannot processed the query parameters yet.

}
}
}, []);
if (processedQPs) processedQPs.current = true;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the caller supplied this ref, we need to set its value to to true to include that we have processed the query parameters. This prevents use from doing it again later.

// This updates the query parameters in the URL
// when their associated state values change.
useEffect(() => {
const haveRequirements = requirements.every(req =>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the requirements have not been met then we cannot update the query parameters.

const search = Object.keys(params).length
? '?' + new URLSearchParams(params).toString()
: '';
if (search !== url.search) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only update the URL if the query parameters have changed.

import React, { useContext, useEffect, useState } from 'react';
import React, { useContext, useEffect, useRef, useState } from 'react';

import { TextField } from '@mui/material';
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor reordering of imports.

}
}
],
[pdls],
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The requirements for processing query parameters for this page are to have entries in the pdl array.

const [filteredPdls, setFilteredPdls] = useState(pdls);

const processedQPs = useRef(false);
useQueryParameters(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds support for the pdl query parameter which keeps track of the PDLs the user has selected.

@mvolkmann mvolkmann requested review from mkimberlin and vhscom May 1, 2024 17:36
Copy link
Contributor

@vhscom vhscom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@mvolkmann mvolkmann merged commit e57be25 into develop May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants