From 551cfa2b85b891e86338d90be33b76a59371df41 Mon Sep 17 00:00:00 2001 From: coelhotatiane <53581377+coelhotatiane@users.noreply.github.com> Date: Wed, 5 Jun 2024 12:58:27 -0300 Subject: [PATCH] feat: add PetsRs link to shelter card (#302) * feat: add PetsRs link to shelter card * Add style to link * refactor: simplify clear shelter name logic --- .../CardAboutShelter/CardAboutShelter.tsx | 31 +++++++++++++++++-- src/components/CardAboutShelter/utils.ts | 6 +++- .../petsRsShelter/petsRsShelter.service.ts | 12 +++++++ src/service/petsRsShelter/types.ts | 18 +++++++++++ 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 src/service/petsRsShelter/petsRsShelter.service.ts create mode 100644 src/service/petsRsShelter/types.ts diff --git a/src/components/CardAboutShelter/CardAboutShelter.tsx b/src/components/CardAboutShelter/CardAboutShelter.tsx index 34525112..6c65543a 100644 --- a/src/components/CardAboutShelter/CardAboutShelter.tsx +++ b/src/components/CardAboutShelter/CardAboutShelter.tsx @@ -7,23 +7,40 @@ import { Smartphone, Building, MapPinned, + Link, } from 'lucide-react'; import { Card } from '../ui/card'; import { ICardAboutShelter } from './types'; import { InfoRow } from './components'; -import { checkAndFormatAddress } from './utils'; +import { checkAndFormatAddress, getShelterNameBeforeSeparator } from './utils'; import { ShelterCategory } from '@/hooks/useShelter/types'; import { Fragment } from 'react/jsx-runtime'; +import { PetsRsShelterServices } from '@/service/petsRsShelter/petsRsShelter.service'; +import { useEffect, useState } from 'react'; const CardAboutShelter = (props: ICardAboutShelter) => { const { shelter } = props; + const [ petsRsShelterUrl, setPetsRsShelterUrl ] = useState(''); const check = (v?: string | number | boolean | null) => { return v !== undefined && v !== null; }; - const formatAddress = checkAndFormatAddress(shelter, false); + const getPetsRsShelterUrl = async (name: string) => { + const cleanShelterName = getShelterNameBeforeSeparator(name); + const data = await PetsRsShelterServices.getByName(cleanShelterName); + const petsRsShelterUrl = data?.id ? `https://petsrs.com.br/abrigo/${data.id}` : 'https://petsrs.com.br/abrigos'; + return petsRsShelterUrl; + }; + + useEffect(() => { + if(shelter.petFriendly) { + getPetsRsShelterUrl(shelter.name).then((url) => setPetsRsShelterUrl(url) ); + } + },[shelter.petFriendly, shelter.name]); + + const formatAddress = checkAndFormatAddress(shelter, false); return (
Sobre o abrigo
@@ -89,6 +106,16 @@ const CardAboutShelter = (props: ICardAboutShelter) => { value={check(shelter.pix) ? `${shelter.pix}` : 'Não informado'} clipboardButton={check(shelter.pix)} /> + {(petsRsShelterUrl != '') ? ( + } + label={ + + Confira o abrigo em petsrs.com.br + + } + /> + ) : ''}
); diff --git a/src/components/CardAboutShelter/utils.ts b/src/components/CardAboutShelter/utils.ts index 58d00b67..39b013a0 100644 --- a/src/components/CardAboutShelter/utils.ts +++ b/src/components/CardAboutShelter/utils.ts @@ -25,4 +25,8 @@ const checkAndFormatAddress = ( ); }; -export { checkAndFormatAddress }; +function getShelterNameBeforeSeparator(input: string): string { + return input.replace(/[(\-[{].*$/, ''); +} + +export { checkAndFormatAddress, getShelterNameBeforeSeparator }; diff --git a/src/service/petsRsShelter/petsRsShelter.service.ts b/src/service/petsRsShelter/petsRsShelter.service.ts new file mode 100644 index 00000000..c5de3871 --- /dev/null +++ b/src/service/petsRsShelter/petsRsShelter.service.ts @@ -0,0 +1,12 @@ +import axios from 'axios'; + +import { IPetsRsShelter } from './types'; + +const PetsRsShelterServices = { + getByName: async (name: string): Promise => { + const { data } = await axios.get(`https://cms.petsrs.com.br/api/abrigos?filters[Nome][$containsi]=${name}`); + return data?.data[0]; + }, +} + +export { PetsRsShelterServices }; diff --git a/src/service/petsRsShelter/types.ts b/src/service/petsRsShelter/types.ts new file mode 100644 index 00000000..30de3cc0 --- /dev/null +++ b/src/service/petsRsShelter/types.ts @@ -0,0 +1,18 @@ +export interface Attributes { + Nome: string; + Endereco: string; + Longitude: string; + Latitude: string; + createdAt: string; + updatedAt: string; + publishedAt: string; + Telefone: string | null; + nome_responsavel: string | null; + instagramUrl: string | null; + observacao: string | null; +} + +export interface IPetsRsShelter { + id: number; + attributes: Attributes; +}