Skip to content

Commit

Permalink
Use hook instead of fetcher in useGetReservationGroups()
Browse files Browse the repository at this point in the history
There is no need for the extra states and we get the expected return 
type by default.
  • Loading branch information
kasperg committed Nov 14, 2023
1 parent f612fb0 commit e66feb0
Showing 1 changed file with 13 additions and 30 deletions.
43 changes: 13 additions & 30 deletions src/core/utils/useGetReservationGroups.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useState } from "react";

Check warning on line 1 in src/core/utils/useGetReservationGroups.ts

View workflow job for this annotation

GitHub Actions / Lint .js and .jsx

'useEffect' is defined but never used

Check warning on line 1 in src/core/utils/useGetReservationGroups.ts

View workflow job for this annotation

GitHub Actions / Lint .js and .jsx

'useState' is defined but never used
import { groupBy, map, min, reduce } from "lodash";
import { getReservationsV2 } from "../fbs/fbs";
import { getReservationsV2, useGetReservationsV2 } from "../fbs/fbs";

Check warning on line 3 in src/core/utils/useGetReservationGroups.ts

View workflow job for this annotation

GitHub Actions / Lint .js and .jsx

'getReservationsV2' is defined but never used
import { ReservationDetailsV2 } from "../fbs/model";

/**
Expand Down Expand Up @@ -61,36 +61,19 @@ function groupReservations(data: ReservationDetailsV2[]) {
* Custom version of the generated useGetReservations hook which returns our
* custom reservation details type.
*/
const useGetReservationGroups = (): {
isLoading: boolean;
isSuccess: boolean;
isError: boolean;
error: Error | null;
type UseGetReservationGroupsResult = Omit<
ReturnType<typeof useGetReservationsV2>,
"data"
> & {
data: ReservationGroupDetails[] | null;
} => {
const [loading, setLoading] = useState<boolean>(true);
const [success, setSuccess] = useState<boolean>(false);
const [error, setError] = useState<Error | null>(null);
const [reservations, setReservations] = useState<
ReservationGroupDetails[] | null
>(null);

useEffect(() => {
getReservationsV2()
.then((data) => {
if (data) {
setReservations(groupReservations(data));
setLoading(false);
setSuccess(true);
}
})
.catch((errorResponse) => {
setError(errorResponse);
setLoading(false);
});
}, [setLoading, setError, setReservations]);

return { isLoading: loading, isSuccess: success, isError: !!error, error, data: reservations };
};
const useGetReservationGroups = (): UseGetReservationGroupsResult => {
const result = useGetReservationsV2();
const resultWithGroups = {
...result,
data: result.data ? groupReservations(result.data) : null
};
return resultWithGroups;
};

export default useGetReservationGroups;
Expand Down

0 comments on commit e66feb0

Please sign in to comment.