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

UHF-X: Fix triggering re-render even when no params present #1170

Merged
merged 1 commit into from
Jan 29, 2025
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
2 changes: 1 addition & 1 deletion dist/js/health-station-search.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/js/maternity-and-child-health-clinic-search.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/js/ploughing-schedule.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/js/school-search.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type SubmitFormType = HTMLFormElement & {
address: HTMLInputElement;
};

const ProximityFormContainer = ({ initialParams }: { initialParams?: SearchParams }) => {
const ProximityFormContainer = ({ initialParams }: { initialParams?: SearchParams|null }) => {
const stagedParams = useAtomValue(stagedParamsAtom);
const setParams = useSetAtom(paramsAtom);
const setStagedParams = useSetAtom(stagedParamsAtom);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ResultsList from '../components/ResultsList';
const ProximityResultsContainer = () => {
const params = useAtomValue(paramsAtom);
const setParams = useSetAtom(updateParamsAtom);

const updatePage = (page: number) => {
setParams({
...params,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const SearchContainer = () => {
if (initialParams) {
setParams(initialParams);
}
}, [initialParams, setParams]);
}, []);

return (
<Suspense fallback={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type SubmitFormType = HTMLFormElement & {
address: HTMLInputElement;
};

const ProximityFormContainer = ({ initialParams }: {initialParams: SearchParams}) => {
const ProximityFormContainer = ({ initialParams }: {initialParams: SearchParams|null}) => {
const stagedParams = useAtomValue(stagedParamsAtom);
const setParams = useSetAtom(paramsAtom);
const setStagedParams = useSetAtom(stagedParamsAtom);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type SuggestionItemType = {
value: string;
};

const FormContainer = ({ initialParams }: { initialParams?: SearchParams}) => {
const FormContainer = ({ initialParams }: { initialParams?: SearchParams|null}) => {
const setParams = useSetAtom(paramsAtom);
const [address, setAddress] = useState(initialParams?.address);
const { baseUrl, index } = useAtomValue(configurationsAtom);
Expand All @@ -22,8 +22,6 @@ const FormContainer = ({ initialParams }: { initialParams?: SearchParams}) => {
setParams(params);
};

console.log(address, initialParams);

const getSuggestions = (searchString: string) => new Promise<SuggestionItemType[]>((resolve, reject) => {
const suggestions = fetch(`${baseUrl}/${index}/_search`, {
method: 'POST',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const SearchContainer = () => {
};

useEffect(() => {
if (initialParams.address) {
if (initialParams?.address) {
setParams({
keyword: initialParams.address
});
Expand Down
5 changes: 3 additions & 2 deletions src/js/react/common/hooks/useInitialParams.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

const coerce = (value: string, type: string) => {
switch (type) {
case 'boolean':
Expand All @@ -14,19 +13,21 @@ const useInitialParams = <T extends Record<string, any>>(params: T) => {
const initialParams = new URLSearchParams(window.location.search);
const entries = initialParams.entries();
let result = entries.next();
let hits = 0;

while (!result.done) {
const [key, value] = result.value;

if (value && key in params) {
hits++;
// @ts-ignore
params[key as keyof T] = coerce(value, typeof params[key as keyof T]);
}

result = entries.next();
}

return params;
return hits ? params : null;
};

export default useInitialParams;
Loading