Skip to content

Commit

Permalink
integrate veterinarians list api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
firasrg committed Sep 22, 2024
1 parent 3f999da commit 415e41e
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 53 deletions.
1 change: 1 addition & 0 deletions spring-petclinic-reactjs-client/src/constants/resources.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const OWNERS = "owners";
export const PETS = "pets";
export const PET_TYPES = "pettypes";
export const VETERINARIANS = "vets";
7 changes: 4 additions & 3 deletions spring-petclinic-reactjs-client/src/data-providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { combineDataProviders } from "react-admin";
import { OWNERS, PET_TYPES } from "@constants/resources";
import { OWNERS, PET_TYPES, VETERINARIANS } from "@constants/resources";
import ownersDataProvider from "./ownersDataProvider";
import petTypesDataProvider from "./petTypesDataProvider";
import generalDataProvider from "./generalDataProvider.ts";

const dataProviders = combineDataProviders((resource) => {
switch (resource) {
case OWNERS:
return ownersDataProvider;
case VETERINARIANS:
case PET_TYPES:
return petTypesDataProvider;
return generalDataProvider;
default:
throw new Error(`Unknown resource: ${resource}`);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { IApiPetType } from "@models/api/IApiPetType";
import { IApiEnumItem } from "@models/api/IApiEnumItem.ts";
import { PetFormSchema } from "@models/form/PetFormSchema";
import { EPetForm } from "@models/enums/EPetForm";
import { CreateParams } from "react-admin";

export interface CreateOrEditOwnerPetData extends CreateParams {
meta: { ownerId: number };
data: Omit<PetFormSchema, EPetForm.PET_TYPE> & { petId?: number; type: IApiPetType };
data: Omit<PetFormSchema, EPetForm.PET_TYPE> & { petId?: number; type: IApiEnumItem };
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface IApiPetType {
export interface IApiEnumItem {
id: number;
name: string;
}
4 changes: 2 additions & 2 deletions spring-petclinic-reactjs-client/src/models/api/IApiPet.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { IApiPetType } from "@models/api/IApiPetType";
import { IApiEnumItem } from "@models/api/IApiEnumItem.ts";

export interface IApiPet {
id: number;
ownerId: number;
name: string;
birthDate: string;
visits: [];
type: IApiPetType;
type: IApiEnumItem;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IApiEnumItem } from "@models/api/IApiEnumItem.ts";

export interface IApiVeterinarian {
id: number;
firstName: string;
lastName: string;
specialties: IApiEnumItem[];
}
36 changes: 12 additions & 24 deletions spring-petclinic-reactjs-client/src/pages/Veterinarians.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { useGetList } from "react-admin";
import { VETERINARIANS } from "@constants/resources.ts";
import { IApiVeterinarian } from "@models/api/IApiVeterinarian.ts";
import { formatPersonFullName } from "../utils";

export default function VeterinariansPage() {
const { data: veterinariansList } = useGetList<IApiVeterinarian>(VETERINARIANS);
return (
<div className="container xd-container">
<h2 id="veterinarians">Veterinarians</h2>
Expand All @@ -12,30 +18,12 @@ export default function VeterinariansPage() {
</tr>
</thead>
<tbody>
<tr>
<td>James Carter</td>
<td>none</td>
</tr>
<tr>
<td>Linda Douglas</td>
<td>dentistry surgery</td>
</tr>
<tr>
<td>Sharon Jenkins</td>
<td>none</td>
</tr>
<tr>
<td>Helen Leary</td>
<td>radiology</td>
</tr>
<tr>
<td>Rafael Ortega</td>
<td>surgery</td>
</tr>
<tr>
<td>Henry Stevens</td>
<td>radiology</td>
</tr>
{veterinariansList?.map(({ id, firstName, lastName, specialties }) => (
<tr key={id}>
<td>{formatPersonFullName(firstName, lastName)}</td>
<td>{specialties.map(({ name }) => name).join(" ")}</td>
</tr>
))}
</tbody>
</table>
</div>
Expand Down
28 changes: 16 additions & 12 deletions spring-petclinic-reactjs-client/src/pages/forms/PetForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import { SubmitHandler, useForm } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import { PetFormSchema } from "@models/form/PetFormSchema";
import { IApiOwner } from "@models/api/IApiOwner";
import { formatOwnerFullName } from "../../utils/businessUtils";
import * as Routes from "@constants/routes";
import { OWNERS as ROUTE_OWNERS } from "@constants/routes";
import { GET_OWNER, OWNERS as ROUTE_OWNERS } from "@constants/routes";
import { FormError } from "@components/FormError";
import { IApiPetType } from "@models/api/IApiPetType";
import { IApiEnumItem } from "@models/api/IApiEnumItem.ts";
import { OwnersDataProvider } from "@models/api/OwnersDataProvider";
import { formatPersonFullName } from "../../utils";

const yupSchema = yup
.object()
Expand Down Expand Up @@ -52,26 +52,30 @@ export default function PetForm() {
const isEdit = !!petId;

const { data: owner, isPending: ownerPending } = useGetOne<IApiOwner>(OWNERS, { id: ownerId });
const { data: petTypes, isPending: petTypesPending } = useGetList<IApiPetType>(PET_TYPES);
const { data: petTypes, isPending: petTypesPending } = useGetList<IApiEnumItem>(PET_TYPES);

const dataProvider = useDataProvider<OwnersDataProvider>();

const navigate = useNavigate();

useEffect(() => {
const getPetAsync = async () => {
const {
data: { name, birthDate, type }
} = await dataProvider.getPet(OWNERS, { id: ownerId, meta: { petId } });
const petTypeId = type.id;
reset({ [EPetForm.NAME]: name, [EPetForm.BIRTH_DATE]: new Date(birthDate), [EPetForm.PET_TYPE]: petTypeId });
try {
const {
data: { name, birthDate, type }
} = await dataProvider.getPet(OWNERS, { id: ownerId, meta: { petId } });
const petTypeId = type.id;
reset({ [EPetForm.NAME]: name, [EPetForm.BIRTH_DATE]: new Date(birthDate), [EPetForm.PET_TYPE]: petTypeId });
} catch (error) {
navigate(`${GET_OWNER}/${ownerId}`);
}
};

if (isEdit) {
getPetAsync();
}
}, [isEdit]);

const navigate = useNavigate();

if (ownerPending || petTypesPending) {
return <Loading />;
}
Expand Down Expand Up @@ -106,7 +110,7 @@ export default function PetForm() {
<div className="form-group has-feedback">
<div className="form-group">
<label className="col-sm-2 control-label">Owner</label>
<div className="col-sm-10">{formatOwnerFullName(owner)}</div>
<div className="col-sm-10">{formatPersonFullName(owner.firstName, owner.lastName)}</div>
</div>

<div className="form-group ">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { OWNERS_FIND } from "@constants/routes";
import { useGetOne } from "react-admin";
import { OWNERS } from "@constants/resources";
import { IApiOwner } from "@models/api/IApiOwner";
import { formatOwnerFullName } from "../../utils/businessUtils";
import { formatPersonFullName } from "../../utils";

export default function OwnerDetails() {
const { id } = useParams();
Expand All @@ -23,7 +23,7 @@ export default function OwnerDetails() {
<tr>
<th id="name">Name</th>
<td headers="name">
<strong>{formatOwnerFullName(owner)}</strong>
<strong>{formatPersonFullName(owner.firstName, owner.lastName)}</strong>
</td>
</tr>
<tr>
Expand Down
5 changes: 0 additions & 5 deletions spring-petclinic-reactjs-client/src/utils/businessUtils.ts

This file was deleted.

3 changes: 3 additions & 0 deletions spring-petclinic-reactjs-client/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function formatPersonFullName(firstName: string, lastName: string): string {
return firstName + " " + lastName;
}
3 changes: 2 additions & 1 deletion spring-petclinic-reactjs-client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"@pages/*": ["./pages/*"],
"@constants/*": ["./constants/*"],
"@models/*": ["./models/*"],
"@components/*": ["./components/*"]
"@components/*": ["./components/*"],
"@utils/*": ["./utils/*"]
}
},
"include": [
Expand Down
3 changes: 2 additions & 1 deletion spring-petclinic-reactjs-client/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export default defineConfig({
"@pages": path.resolve(__dirname, "./src/pages"),
"@constants": path.resolve(__dirname, "./src/constants"),
"@models": path.resolve(__dirname, "./src/models"),
"@components": path.resolve(__dirname, "./src/components")
"@components": path.resolve(__dirname, "./src/components"),
"@utils": path.resolve(__dirname, "./src/utils")
},
},
});

0 comments on commit 415e41e

Please sign in to comment.