diff --git a/app/components/Selector/index.tsx b/app/components/Selector/index.tsx new file mode 100644 index 0000000..c263286 --- /dev/null +++ b/app/components/Selector/index.tsx @@ -0,0 +1,47 @@ +import * as api from '@/app/redux/services/api'; + +interface CitySelectorProps { + value: string + onChange: (v: string) => void +} + +export const CitySelector = ({value, onChange}: CitySelectorProps) => { + const { data, isLoading } = api.useGetCitiesQuery(); + + return ( + onChange(e.target.value)}> + {data && data.map((v, i) => {v})} + + ) +} + +interface DistrictSelectorProps { + city: string + value: number + onChange: (v: number) => void +} + +export const DistrictSelector = ({city, value, onChange}: DistrictSelectorProps) => { + const { data, isLoading } = api.useGetDistrictsQuery(city); + + return ( + onChange(Number(e.target.value))}> + {data && data.map((v, i) => {v.district})} + + ) +} + +interface PetTypeSelectorProps { + value: string + onChange: (v: string) => void +} + +export const PetTypeSelector = ({value, onChange}: PetTypeSelectorProps) => { + const { data, isLoading } = api.useGetPetTypesQuery(); + + return ( + onChange(e.target.value)}> + {data && data.map((v, i) => {v})} + + ) +} diff --git a/app/page.tsx b/app/page.tsx index eb0150a..1a1bf98 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -2,7 +2,6 @@ import { useDispatch, useSelector } from "react-redux"; import styles from "./page.module.css"; -import { useGetSomethingByNameQuery } from "@redux/services/api"; import { Skeleton } from "@components/Skeleton"; /* главная страница, которая будет открываться по / */ diff --git a/app/redux/services/api.ts b/app/redux/services/api.ts index 8fa8e86..4b3f9fc 100644 --- a/app/redux/services/api.ts +++ b/app/redux/services/api.ts @@ -10,6 +10,11 @@ interface ISomeType { name: string; } +interface District { + id: number; + district: string; +} + export const api = createApi({ reducerPath: 'api', baseQuery: fetchBaseQuery({ @@ -26,8 +31,17 @@ export const api = createApi({ }), endpoints: (builder) => ({ /* в дженерике первым аргументом передается тип, который мы получаем, вторым аргументом - тип, который передается в кверю*/ - getSomethingByName: builder.query({ - query: (name) => `something/${name}`, + getCities: builder.query({ + query: () => `location/cities`, + }), + getDistricts: builder.query({ + query: (city) => ({ + url: `location/districts`, + params: { city } + }) + }), + getPetTypes: builder.query({ + query: () => `pets/types`, }), addSomething: builder.mutation>({ query: (body) => ({ @@ -39,6 +53,9 @@ export const api = createApi({ }), }) +/* хуки, которые потом используем в компонентах, генерируются автоматически */ +export const { useGetCitiesQuery, useGetDistrictsQuery, useGetPetTypesQuery } = api + async function postData(url: string, data: object): Promise { let headers: Record = { 'Accept': 'application/json', @@ -70,6 +87,3 @@ export async function login(credentials: ILoginCredentials): Promise export async function logout(): Promise { await postData("auth/logout", {}) as IApiToken; } - -/* хуки, которые потом используем в компонентах, генерируются автоматически */ -export const { useGetSomethingByNameQuery } = api diff --git a/app/search/page.tsx b/app/search/page.tsx index 0bbaccb..0cc7224 100644 --- a/app/search/page.tsx +++ b/app/search/page.tsx @@ -3,17 +3,19 @@ import { useState } from "react"; import styles from './styles.module.css'; import cn from 'classnames'; +import { CitySelector, DistrictSelector, PetTypeSelector } from '@components/Selector'; function Modal() { + const [ petType, setPetType ] = useState("Санкт-Петербург"); + const [ city, setCity ] = useState("Санкт-Петербург"); + const [ district, setDistrict ] = useState(0); + return ( Данные для поиска Тип животного - - Кошка - Собака - + setPetType(v)}/> Группа крови @@ -24,17 +26,11 @@ function Modal() { Город - - Санкт-Петербург - Москва - + setCity(v)}/> Район - - Шушары - Нижний Тагил - + setDistrict(v)}/> Количество крови (мл)