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

Memo-ized GQL useEffects params so they do not continuously make XHR … #94

Merged
merged 3 commits into from
Oct 31, 2023
Merged
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
9 changes: 5 additions & 4 deletions react-app/src/api/usePersistedQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ async function fetchPersistedQuery(persistedQueryName, queryParameters) {
*
* @returns an array of Adventure JSON objects, and array of errors
*/
export function useAdventuresByActivity(adventureActivity, params = {}) {

export function useAdventuresByActivity(adventureActivity, params) {
const [adventures, setAdventures] = useState(null);
const [errors, setErrors] = useState(null);

Expand Down Expand Up @@ -104,7 +103,7 @@ export function useAdventuresByActivity(adventureActivity, params = {}) {
* @param {String!} slugName the adventure slug
* @returns a JSON object representing the Adventure
*/
export function useAdventureBySlug(slugName, params = {}) {
export function useAdventureBySlug(slugName, params) {
const [adventure, setAdventure] = useState(null);
const [references, setReferences] = useState(null);
const [errors, setErrors] = useState(null);
Expand Down Expand Up @@ -146,4 +145,6 @@ export function useAdventureBySlug(slugName, params = {}) {
}, [slugName, params]);

return { adventure, references, errors };
}
}


6 changes: 3 additions & 3 deletions react-app/src/components/AdventureDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ NOTICE: Adobe permits you to use, modify, and distribute this file in
accordance with the terms of the Adobe license agreement accompanying
it.
*/
import React from 'react';
import React, { useMemo } from 'react';
import { Link, useParams } from "react-router-dom";
import { useAdventureBySlug } from "../api/usePersistedQueries";
import { addAemHost } from "../api/aemHeadlessClient";
Expand All @@ -21,9 +21,9 @@ function AdventureDetail() {

// Read the slug value which is the parameter used to query for the adventure's details
const { slug } = useParams();

const queryParameters = useMemo(() => ({ format: 'JPG', preferWebp: true, width: 1200}), []);
// Query AEM for the Adventures's details, using the `slug`
const { adventure, references, error } = useAdventureBySlug(slug, { format: 'JPG', preferWebp: true, width: 1200});
const { adventure, references, error } = useAdventureBySlug(slug, queryParameters);

// Handle error and loading conditions
if (error) {
Expand Down
6 changes: 3 additions & 3 deletions react-app/src/components/Adventures.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ NOTICE: Adobe permits you to use, modify, and distribute this file in
accordance with the terms of the Adobe license agreement accompanying
it.
*/
import React from "react";
import React, { useMemo } from "react";
import { Link } from "react-router-dom";
import { useAdventuresByActivity } from "../api/usePersistedQueries";
import { addAemHost } from "../api/aemHeadlessClient";
Expand All @@ -15,8 +15,8 @@ import Loading from "./Loading";
import './Adventures.scss';

function Adventures({ adventureActivity }) {
const assetTransform = { format: 'JPG', preferWebp: true, size: { width: 240, height: 200 } };
const { adventures, error } = useAdventuresByActivity(adventureActivity, assetTransform);
const queryParameters = useMemo(() => ({ format: 'JPG', preferWebp: true, size: { width: 240, height: 200 } }), []);
const { adventures, error } = useAdventuresByActivity(adventureActivity, queryParameters);

// Handle error and loading conditions
if (error) {
Expand Down
Loading