Skip to content

Modificação nas telas de abrigos para adicionar cidades #110

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

Merged
merged 17 commits into from
May 15, 2024
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
12 changes: 11 additions & 1 deletion src/components/CardAboutShelter/CardAboutShelter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,34 @@ import {
PawPrint,
Landmark,
Smartphone,
Building,
MapPinned,
} from 'lucide-react';

import { Card } from '../ui/card';
import { ICardAboutShelter } from './types';
import { InfoRow } from './components';
import { checkAndFormatAddress } from './utils';

const CardAboutShelter = (props: ICardAboutShelter) => {
const { shelter } = props;

const check = (v?: string | number | boolean | null) => {
return v !== undefined && v !== null;
};
const formatAddress = checkAndFormatAddress(shelter, false);

return (
<Card className="flex flex-col gap-2 p-4 bg-[#E8F0F8] text-sm">
<div className="text-[#646870] font-medium">Sobre o abrigo</div>
<div className="flex flex-col flex-wrap gap-3">
<InfoRow icon={<Home />} label={shelter.address} />
<InfoRow icon={<Home />} label={formatAddress} />
{Boolean(shelter.city) && (
<InfoRow icon={<Building />} label="Cidade:" value={shelter.city} />
)}
{Boolean(shelter.zipCode) && (
<InfoRow icon={<MapPinned />} label="CEP:" value={shelter.zipCode} />
)}
<InfoRow
icon={<PawPrint />}
label={
Expand Down
16 changes: 12 additions & 4 deletions src/components/CardAboutShelter/components/InfoRow/InfoRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ import { IInfoRowProps } from './types';

const InfoRow = React.forwardRef<HTMLDivElement, IInfoRowProps>(
(props, ref) => {
const { icon, label, value, clipboardButton = false, className = '', ...rest } = props;
const {
icon,
label,
value,
clipboardButton = false,
className = '',
...rest
} = props;
const isLink = value?.startsWith('http');
const ValueComp = !value ? (
<Fragment />
) : isLink ? (
<a href={value} target='_blank'
<a
href={value}
target="_blank"
className="text-blue-500 break-all cursor-pointer hover:underline"
>
{value}
Expand Down Expand Up @@ -37,7 +46,7 @@ const InfoRow = React.forwardRef<HTMLDivElement, IInfoRowProps>(
</span>
<span className="md:flex">
{ValueComp}
{clipboardButton && (
{clipboardButton && value && (
<div
className="text-blue-600 mx-2 hover:cursor-pointer active:text-blue-800"
onClick={() => navigator.clipboard.writeText(value)}
Expand All @@ -46,7 +55,6 @@ const InfoRow = React.forwardRef<HTMLDivElement, IInfoRowProps>(
</div>
)}
</span>

</div>
</div>
);
Expand Down
5 changes: 3 additions & 2 deletions src/components/CardAboutShelter/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CardAboutShelter } from "./CardAboutShelter";
import { CardAboutShelter } from './CardAboutShelter';
import { checkAndFormatAddress } from './utils';

export { CardAboutShelter };
export { CardAboutShelter, checkAndFormatAddress };
18 changes: 18 additions & 0 deletions src/components/CardAboutShelter/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { IUseSheltersData } from '@/hooks/useShelters/types';
import { ICardAboutShelter } from './types';

const formatShelterAddressFields = (
shelter: ICardAboutShelter['shelter'] | IUseSheltersData
) =>
[shelter.street, shelter.streetNumber, shelter.neighbourhood]
.filter(Boolean)
.join(', ');

export const checkAndFormatAddress = (
shelter: ICardAboutShelter['shelter'] | IUseSheltersData,
showCity = true
) =>
shelter.address ??
`${formatShelterAddressFields(shelter)}${
showCity ? ` - ${shelter.city}` : ''
}`;
10 changes: 8 additions & 2 deletions src/components/SearchInput/SearchInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ import { cn } from '@/lib/utils';

const SearchInput = React.forwardRef<HTMLDivElement, ISearchInputProps>(
(props, ref) => {
const { value, onChange, className, ...rest } = props;
const {
value,
onChange,
className,
placeholder = 'Buscar por abrigo ou endereço',
...rest
} = props;

return (
<div ref={ref} className={cn(className, 'relative')} {...rest}>
<Input
value={value}
placeholder="Buscar por abrigo ou endereço"
placeholder={placeholder}
className="h-12 text-md font-medium text-zinc-600 pl-10 pr-4"
onChange={onChange}
/>
Expand Down
6 changes: 6 additions & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { useFetch } from './useFetch';
import { usePaginatedQuery } from './usePaginatedQuery';
import { useThrottle } from './useThrottle';
import { useShelter } from './useShelter';
import { useShelterCities } from './useShelterCities';
import { useDebouncedValue } from './useDebouncedValue';
import { useSupplyCategories } from './useSupplyCategories';
import { useSupplies } from './useSupplies';
import { useViaCep } from './useViaCep';
import { usePartners } from './usePartners';

export {
Expand All @@ -13,7 +16,10 @@ export {
usePaginatedQuery,
useThrottle,
useShelter,
useShelterCities,
useDebouncedValue,
useSupplyCategories,
useSupplies,
useViaCep,
usePartners,
};
3 changes: 3 additions & 0 deletions src/hooks/useDebouncedValue/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { useDebouncedValue } from './useDebouncedValue';

export { useDebouncedValue };
27 changes: 27 additions & 0 deletions src/hooks/useDebouncedValue/useDebouncedValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useState, useEffect } from 'react';

export const useDebouncedValue = (value: string, delay: number): string => {
const [debouncedValue, setDebouncedValue] = useState<string>(value);
const [currentValue, setCurrentValue] = useState<string>(value);

useEffect(() => {
setCurrentValue(value);
}, [value]);

useEffect(() => {
let timeoutId: NodeJS.Timeout | null = null;

if (currentValue !== debouncedValue) {
if (timeoutId) clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
setDebouncedValue(currentValue);
}, delay);
}

return () => {
if (timeoutId) clearTimeout(timeoutId);
};
}, [currentValue, debouncedValue, delay]);

return debouncedValue;
};
15 changes: 10 additions & 5 deletions src/hooks/useFetch/useFetch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { api } from '@/api';
import { IServerResponse } from '@/types';
import { IUseFetchOptions } from './types';

function useFetch<T = any>(path: string, options: IUseFetchOptions<T> = {}) {
function useFetch<T = any>(path?: string, options: IUseFetchOptions<T> = {}) {
const { cache, initialValue } = options;
const [loading, setLoading] = useState<boolean>(true);
const [data, setData] = useState<T>(initialValue || ({} as T));
Expand All @@ -15,10 +15,15 @@ function useFetch<T = any>(path: string, options: IUseFetchOptions<T> = {}) {
const headers = config?.headers ?? {};
if (cache) headers['x-app-cache'] = 'true';
setLoading(true);
api
.get<IServerResponse<T>>(path, { ...config, headers })
.then(({ data }) => setData(data.data))
.finally(() => setLoading(false));

if (path) {
api
.get<IServerResponse<T>>(path, { ...config, headers })
.then(({ data }) => setData(data.data ?? (data as T)))
.finally(() => setLoading(false));
} else {
setLoading(false);
}
},
[cache, path]
);
Expand Down
1 change: 1 addition & 0 deletions src/hooks/usePaginatedQuery/paths.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum PaginatedQueryPath {
Shelters = '/shelters',
ShelterCities = '/shelters/cities',
SupplyCategories = '/supply-categories',
Supplies = '/supplies',
}
5 changes: 5 additions & 0 deletions src/hooks/useShelter/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export interface IUseShelterData {
id: string;
name: string;
street?: string;
neighbourhood?: string;
city?: string;
streetNumber?: string | null;
zipCode?: string;
address: string;
pix?: string | null;
shelteredPeople?: number | null;
Expand Down
3 changes: 3 additions & 0 deletions src/hooks/useShelterCities/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { useShelterCities } from './useShelterCities';

export { useShelterCities };
4 changes: 4 additions & 0 deletions src/hooks/useShelterCities/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface IShelterCitiesData {
city: string;
sheltersCount: string;
}
9 changes: 9 additions & 0 deletions src/hooks/useShelterCities/useShelterCities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useFetch } from '../useFetch';
import { PaginatedQueryPath } from '../usePaginatedQuery/paths';
import { IShelterCitiesData } from './types';

export const useShelterCities = () => {
return useFetch<IShelterCitiesData[]>(PaginatedQueryPath.ShelterCities, {
cache: true,
});
};
5 changes: 5 additions & 0 deletions src/hooks/useShelters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { ShelterTagType } from '@/pages/Home/components/ShelterListItem/types';
export interface IUseSheltersData {
id: string;
name: string;
street?: string;
neighbourhood?: string;
city?: string;
streetNumber?: string | null;
zipCode?: string;
address: string;
pix?: string | null;
shelteredPeople?: number | null;
Expand Down
3 changes: 3 additions & 0 deletions src/hooks/useViaCep/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { useViaCep } from './useViaCep';

export { useViaCep };
12 changes: 12 additions & 0 deletions src/hooks/useViaCep/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export interface IViaCepData {
cep: string;
logradouro: string;
complemento: string;
bairro: string;
localidade: string;
uf: string;
ibge: string;
gia: string;
dd: string;
siafi: string;
}
10 changes: 10 additions & 0 deletions src/hooks/useViaCep/useViaCep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useFetch } from '..';
import { IViaCepData } from './types';

export const useViaCep = (cep: string | undefined) => {
const createdPath =
!cep || cep.length < 8
? undefined
: `https://viacep.com.br/ws/${cep}/json/`;
return useFetch<IViaCepData>(createdPath);
};
Loading