mirror of
https://github.com/Nezumi-2711/react-training.git
synced 2026-09-22 13:38:51 +00:00
35 lines
879 B
TypeScript
35 lines
879 B
TypeScript
import toast from "react-hot-toast";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import { useSearchParams } from "react-router-dom";
|
|
|
|
// Services
|
|
import { getAllRooms } from "@service/roomServices";
|
|
|
|
/**
|
|
* Fetch room from database
|
|
* @returns The status of loading rooms from database and data of rooms
|
|
*/
|
|
const useRooms = () => {
|
|
const [searchParams] = useSearchParams();
|
|
const sortByValue = searchParams.get('sortBy') || 'id';
|
|
const orderByValue = searchParams.get('orderBy') || 'asc';
|
|
const phoneSearch = searchParams.get('search') || ''
|
|
|
|
const {
|
|
isLoading,
|
|
data: rooms,
|
|
error
|
|
} = useQuery({
|
|
queryKey: ['rooms', sortByValue, orderByValue, phoneSearch],
|
|
queryFn: () => getAllRooms(sortByValue, orderByValue, phoneSearch),
|
|
});
|
|
|
|
if (error) {
|
|
toast.error(error.message);
|
|
}
|
|
|
|
return { isLoading, rooms };
|
|
};
|
|
|
|
export { useRooms };
|