Skip to content

Commit

Permalink
Use react query getQueryData method instead of regular fetch
Browse files Browse the repository at this point in the history
Co-authored-by: mab <[email protected]>
  • Loading branch information
dtrucs and mabhub committed Jul 16, 2024
1 parent 01cfc84 commit bcd0134
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 86 deletions.
28 changes: 16 additions & 12 deletions frontend/src/pages/event/[touristicEvent].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { routes } from 'services/routes';
import { redirectIfWrongUrl } from 'modules/utils/url';
import { getCommonDictionaries } from 'modules/dictionaries/connector';
import { isRessourceMissing } from 'services/routeUtils';
import { TouristicEventDetails } from 'modules/touristicEvent/interface';
import { getTouristicEventDetails } from '../../modules/touristicEvent/connector';
import { isUrlString } from '../../modules/utils/string';
import Custom404 from '../404';
Expand All @@ -32,22 +33,25 @@ export const getServerSideProps: GetServerSideProps = async context => {
queryFn: () => getCommonDictionaries(locale),
});

const details = await getTouristicEventDetails(id, locale, commonDictionaries);
await queryClient.prefetchQuery({
queryKey: ['touristicEventDetails', id, locale],
queryFn: () => details,
queryFn: () => getTouristicEventDetails(id, locale, commonDictionaries),
});

const details = queryClient.getQueryData<TouristicEventDetails>(['touristicEventDetails', id, locale]);

const redirect = redirectIfWrongUrl(
id,
details.name,
{ ...context, locale },
routes.TOURISTIC_EVENT,
);
if (redirect)
return {
redirect,
};
if (details !== undefined) {
const redirect = redirectIfWrongUrl(
id,
details.name,
{ ...context, locale },
routes.TOURISTIC_EVENT,
);
if (redirect)
return {
redirect,
};
}

return {
props: {
Expand Down
44 changes: 24 additions & 20 deletions frontend/src/pages/information/[flatPage].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getSuggestionsFromContent } from 'modules/flatpage/utils';
import { isUrlString } from 'modules/utils/string';
import { redirectIfWrongUrl } from 'modules/utils/url';
import { isRessourceMissing } from 'services/routeUtils';
import { FlatPageDetails } from 'modules/flatpage/interface';
import Custom404 from '../404';

export const getServerSideProps: GetServerSideProps = async context => {
Expand All @@ -32,33 +33,36 @@ export const getServerSideProps: GetServerSideProps = async context => {
queryFn: () => getCommonDictionaries(locale),
});

const details = await getFlatPageDetails(id, locale, commonDictionaries);
await queryClient.prefetchQuery({
queryKey: ['flatPageDetails', id, locale],
queryFn: () => details,
queryFn: () => getFlatPageDetails(id, locale, commonDictionaries),
});

const suggestions = getSuggestionsFromContent(details.content);
const details = queryClient.getQueryData<FlatPageDetails>(['flatPageDetails', id, locale]);

const activitySuggestionIds = suggestions.flatMap(suggestion =>
'ids' in suggestion ? suggestion.ids : [suggestion.type],
);
if (details !== undefined) {
const suggestions = getSuggestionsFromContent(details.content);

await queryClient.prefetchQuery({
queryKey: ['activitySuggestions', ...activitySuggestionIds, id, locale],
queryFn: () => getActivitySuggestions(suggestions, locale, commonDictionaries),
});
const activitySuggestionIds = suggestions.flatMap(suggestion =>
'ids' in suggestion ? suggestion.ids : [suggestion.type],
);

await queryClient.prefetchQuery({
queryKey: ['activitySuggestions', ...activitySuggestionIds, id, locale],
queryFn: () => getActivitySuggestions(suggestions, locale, commonDictionaries),
});

const redirect = redirectIfWrongUrl(
id,
details.title,
{ ...context, locale },
routes.FLAT_PAGE,
);
if (redirect)
return {
redirect,
};
const redirect = redirectIfWrongUrl(
id,
details.title,
{ ...context, locale },
routes.FLAT_PAGE,
);
if (redirect)
return {
redirect,
};
}

return {
props: {
Expand Down
27 changes: 15 additions & 12 deletions frontend/src/pages/outdoor-course/[outdoorCourse].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { routes } from 'services/routes';
import { redirectIfWrongUrl } from 'modules/utils/url';
import { getCommonDictionaries } from 'modules/dictionaries/connector';
import { isRessourceMissing } from 'services/routeUtils';
import { getOutdoorCourseDetails } from '../../modules/outdoorCourse/connector';
import { isUrlString } from '../../modules/utils/string';
import { OutdoorCourseDetails } from 'modules/outdoorCourse/interface';
import { getOutdoorCourseDetails } from 'modules/outdoorCourse/connector';
import { isUrlString } from 'modules/utils/string';
import Custom404 from '../404';

export const getServerSideProps: GetServerSideProps = async context => {
Expand All @@ -33,22 +34,24 @@ export const getServerSideProps: GetServerSideProps = async context => {
queryFn: () => getCommonDictionaries(locale),
});

const details = await getOutdoorCourseDetails(id, locale, commonDictionaries);
await queryClient.prefetchQuery({
queryKey: ['outdoorCourseDetails', id, locale],
queryFn: () => details,
queryFn: () => getOutdoorCourseDetails(id, locale, commonDictionaries),
});
const details = queryClient.getQueryData<OutdoorCourseDetails>(['outdoorCourseDetails', id, locale]) ;

const redirect = redirectIfWrongUrl(
id,
details.name,
{ ...context, locale },
routes.OUTDOOR_COURSE,
);
if (redirect)
return {
if (details !==undefined) {
const redirect = redirectIfWrongUrl(
id,
details.name,
{ ...context, locale },
routes.OUTDOOR_COURSE,
);
if (redirect)
return {
redirect,
};
}

return {
props: {
Expand Down
30 changes: 17 additions & 13 deletions frontend/src/pages/outdoor-site/[outdoorSite].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { dehydrate, QueryCache, QueryClient } from '@tanstack/react-query';
import { routes } from 'services/routes';
import { getCommonDictionaries } from 'modules/dictionaries/connector';
import { isRessourceMissing } from 'services/routeUtils';
import { getOutdoorSiteDetails } from '../../modules/outdoorSite/connector';
import { isUrlString } from '../../modules/utils/string';
import { redirectIfWrongUrl } from '../../modules/utils/url';
import { OutdoorSiteDetails } from 'modules/outdoorSite/interface';
import { getOutdoorSiteDetails } from 'modules/outdoorSite/connector';
import { isUrlString } from 'modules/utils/string';
import { redirectIfWrongUrl } from 'modules/utils/url';
import Custom404 from '../404';

export const getServerSideProps: GetServerSideProps = async context => {
Expand All @@ -31,22 +32,25 @@ export const getServerSideProps: GetServerSideProps = async context => {
queryFn: () => getCommonDictionaries(locale),
});

const details = await getOutdoorSiteDetails(id, locale, commonDictionaries);
await queryClient.prefetchQuery({
queryKey: ['outdoorSiteDetails', id, locale],
queryFn: () => details,
queryFn: () => getOutdoorSiteDetails(id, locale, commonDictionaries),
});

const details = queryClient.getQueryData<OutdoorSiteDetails>(['outdoorSiteDetails', id, locale]);

const redirect = redirectIfWrongUrl(
id,
details.name,
{ ...context, locale },
routes.OUTDOOR_SITE,
);
if (redirect)
return {
if (details !== undefined) {
const redirect = redirectIfWrongUrl(
id,
details.name,
{ ...context, locale },
routes.OUTDOOR_SITE,
);
if (redirect)
return {
redirect,
};
}

return {
props: {
Expand Down
37 changes: 22 additions & 15 deletions frontend/src/pages/service/[touristicContent].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { dehydrate, QueryCache, QueryClient } from '@tanstack/react-query';
import { routes } from 'services/routes';
import { getCommonDictionaries } from 'modules/dictionaries/connector';
import { isRessourceMissing } from 'services/routeUtils';
import { getTouristicContentDetails } from '../../modules/touristicContent/connector';
import { isUrlString } from '../../modules/utils/string';
import { redirectIfWrongUrl } from '../../modules/utils/url';
import { TouristicContentDetails } from 'modules/touristicContent/interface';
import { getTouristicContentDetails } from 'modules/touristicContent/connector';
import { isUrlString } from 'modules/utils/string';
import { redirectIfWrongUrl } from 'modules/utils/url';
import Custom404 from '../404';

export const getServerSideProps: GetServerSideProps = async context => {
Expand All @@ -33,22 +34,28 @@ export const getServerSideProps: GetServerSideProps = async context => {
queryFn: () => getCommonDictionaries(locale),
});

const details = await getTouristicContentDetails(id, locale, commonDictionaries);
await queryClient.prefetchQuery({
queryKey: ['touristicContentDetails', id, locale],
queryFn: () => details,
queryFn: () => getTouristicContentDetails(id, locale, commonDictionaries),
});

const redirect = redirectIfWrongUrl(
const details = queryClient.getQueryData<TouristicContentDetails>([
'touristicContentDetails',
id,
details.name,
{ ...context, locale },
routes.TOURISTIC_CONTENT,
);
if (redirect)
return {
redirect,
};
locale,
]);

if (details !== undefined) {
const redirect = redirectIfWrongUrl(
id,
details.name,
{ ...context, locale },
routes.TOURISTIC_CONTENT,
);
if (redirect)
return {
redirect,
};
}

return {
props: {
Expand Down
33 changes: 19 additions & 14 deletions frontend/src/pages/trek/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { getDefaultLanguage } from 'modules/header/utills';
import { routes } from 'services/routes';
import { getCommonDictionaries } from 'modules/dictionaries/connector';
import { isRessourceMissing } from 'services/routeUtils';
import { redirectIfWrongUrl } from '../../modules/utils/url';
import { Details } from 'modules/details/interface';
import { redirectIfWrongUrl } from 'modules/utils/url';
import Custom404 from '../404';

export const getServerSideProps: GetServerSideProps = async context => {
Expand All @@ -33,25 +34,29 @@ export const getServerSideProps: GetServerSideProps = async context => {
queryFn: () => getCommonDictionaries(locale),
});

const details = await getDetails(id, locale, commonDictionaries);
await queryClient.prefetchQuery({ queryKey: ['details', id, locale], queryFn: () => getDetails(id, locale, commonDictionaries) });

const details = queryClient.getQueryData<Details>(['details', id, locale]);

await queryClient.prefetchQuery({ queryKey: ['details', id, locale], queryFn: () => details });
await queryClient.prefetchQuery({
queryKey: ['trekFamily', parentIdString, locale],
queryFn: () => getTrekFamily(parentIdString, locale),
});

const redirect = redirectIfWrongUrl(
id,
details.title,
{ ...context, locale },
routes.TREK,
Number(parentIdString),
);
if (redirect)
return {
redirect,
};
if (details !== undefined) {

const redirect = redirectIfWrongUrl(
id,
details.title,
{ ...context, locale },
routes.TREK,
Number(parentIdString),
);
if (redirect)
return {
redirect,
};
}

return {
props: {
Expand Down

0 comments on commit bcd0134

Please sign in to comment.