Skip to content

Commit

Permalink
fix: 🐛 adjust shelters types to edit and create shelter
Browse files Browse the repository at this point in the history
  • Loading branch information
willMoraes committed Jun 2, 2024
1 parent 3517bd0 commit f7a71c7
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
7 changes: 4 additions & 3 deletions src/app/user/shelters/[id]/edit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ export default function EditShelterPage({
}: {
params: { id: number };
}) {
const { data, isLoading, error } = api.shelter.findUserShelterById.useQuery({
id: Number(params.id),
});
const { data, isLoading, error } =
api.userShelters.findUserShelterById.useQuery({
id: Number(params.id),
});

if (error?.data?.httpStatus === 404 && !isLoading) {
notFound();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export function DialogDelete({ shelterId }: { shelterId: number }) {
const router = useRouter();
const [open, setOpen] = useState(false);

const deleteShelter = api.shelter.delete.useMutation({
const deleteShelter = api.userShelters.delete.useMutation({
onSuccess: () => {
toast.success("Abrigo excluído com sucesso!");
router.replace("/");
router.replace("/user/shelters");
setOpen(false);
},
onError: (error) => {
Expand Down
19 changes: 11 additions & 8 deletions src/app/user/shelters/_components/form-edit-register/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
FormDescription,
} from "~/components/ui/form";
import { cepMask, phoneMask, socialMediaMask } from "~/lib/masks";
import { shelterSchema } from "~/schemas/shelter";
import { shelterSchema, type apiShelterSchema } from "~/schemas/shelter";
import { api } from "~/trpc/react";
import { useRouter } from "next/navigation";
import { TagInput } from "~/components/tag-input";
Expand All @@ -28,7 +28,7 @@ import { googleMaps } from "~/lib/google-maps";
import { useState } from "react";

interface FormEditRegisterProps {
shelter?: z.infer<typeof shelterSchema> | null;
shelter?: z.infer<typeof apiShelterSchema> | null;
}

export function FormEditRegister({ shelter }: FormEditRegisterProps = {}) {
Expand Down Expand Up @@ -79,7 +79,7 @@ export function FormEditRegister({ shelter }: FormEditRegisterProps = {}) {
state,
});

const shelter = {
const formShelter = {
...values,
address: {
...values.address,
Expand All @@ -88,8 +88,13 @@ export function FormEditRegister({ shelter }: FormEditRegisterProps = {}) {
},
};

const mutation = isEditing ? updateShelter : createShelter;
mutation.mutate(shelter);
if (isEditing) {
updateShelter.mutate({ ...formShelter, id: shelter.id });

return;
}

createShelter.mutate(formShelter);
} catch (error) {
toast.error(
"Ops! Houve um erro ao buscar as coordenadas do endereço e o abrigo não foi criado. Tente novamente!.",
Expand Down Expand Up @@ -434,9 +439,7 @@ export function FormEditRegister({ shelter }: FormEditRegisterProps = {}) {
>
{isLoading ? <Loader2 className="animate-spin" /> : "Salvar"}
</Button>
{isEditing && shelter.id && (
<DialogDelete shelterId={shelter.id} />
)}
{isEditing && <DialogDelete shelterId={shelter.id} />}
</div>
</form>
</Form>
Expand Down
1 change: 0 additions & 1 deletion src/schemas/shelter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { z } from "zod";

export const shelterSchema = z.object({
id: z.number().optional(),
name: z
.string({
message: "Campo obrigatório",
Expand Down
4 changes: 2 additions & 2 deletions src/server/api/routers/user/shelters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TRPCError } from "@trpc/server";

import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
import { db } from "~/server/db";
import { apiShelterSchema } from "~/schemas/shelter";
import { apiShelterSchema, createShelterSchema } from "~/schemas/shelter";

const InputIdParams = z.object({
id: z.number(),
Expand Down Expand Up @@ -87,7 +87,7 @@ export const userSheltersRouter = createTRPCRouter({
};
}),
create: protectedProcedure
.input(apiShelterSchema)
.input(createShelterSchema)
.mutation(async ({ ctx, input }) => {
await db.shelter.create({
data: {
Expand Down

0 comments on commit f7a71c7

Please sign in to comment.