Skip to content

Commit

Permalink
feat: ✨ update edit route to use uuid instead id
Browse files Browse the repository at this point in the history
  • Loading branch information
willMoraes committed Jun 3, 2024
1 parent c25e955 commit 1c688fc
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import { Alert, AlertDescription, AlertTitle } from "~/components/ui/alert";
export default function EditShelterPage({
params,
}: {
params: { id: number };
params: { uuid: string };
}) {
const { data, isLoading, error } =
api.userShelters.findUserShelterById.useQuery({
id: Number(params.id),
api.userShelters.findUserShelterByUuid.useQuery({
uuid: params.uuid,
});

if (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useState } from "react";
import { api } from "~/trpc/react";
import { useRouter } from "next/navigation";

export function DialogDelete({ shelterId }: { shelterId: number }) {
export function DialogDelete({ shelterUuid }: { shelterUuid: string }) {
const router = useRouter();
const [open, setOpen] = useState(false);

Expand Down Expand Up @@ -62,7 +62,7 @@ export function DialogDelete({ shelterId }: { shelterId: number }) {
<Button
type="button"
onClick={() => {
deleteShelter.mutate({ id: shelterId });
deleteShelter.mutate({ uuid: shelterUuid });
}}
>
Excluir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ export function FormEditRegister({ shelter }: FormEditRegisterProps = {}) {
};

if (isEditing) {
updateShelter.mutate({ ...formShelter, id: shelter.id });
updateShelter.mutate({
...formShelter,
id: shelter.id,
uuid: shelter.uuid,
});

return;
}
Expand Down Expand Up @@ -439,7 +443,7 @@ export function FormEditRegister({ shelter }: FormEditRegisterProps = {}) {
>
{isLoading ? <Loader2 className="animate-spin" /> : "Salvar"}
</Button>
{isEditing && <DialogDelete shelterId={shelter.id} />}
{isEditing && <DialogDelete shelterUuid={shelter.uuid} />}
</div>
</form>
</Form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const columns: ColumnDef<ShelterTable>[] = [

return (
<div>
<Link href={`/user/shelters/${shelter.id}/edit`}>
<Link href={`/user/shelters/${shelter.uuid}/edit`}>
<span className="sr-only">Editar</span>
<FiEdit size={20} className="cursor-pointer" />
</Link>
Expand Down
1 change: 1 addition & 0 deletions src/schemas/shelter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@ export const apiShelterSchema = z.object({
...shelterSchema.shape,
...addressSchema.shape,
id: z.number(),
uuid: z.string(),
});
20 changes: 11 additions & 9 deletions src/server/api/routers/user/shelters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
import { db } from "~/server/db";
import { apiShelterSchema, createShelterSchema } from "~/schemas/shelter";

const InputIdParams = z.object({
id: z.number(),
const InputUuiDParams = z.object({
uuid: z.string(),
});

export const userSheltersRouter = createTRPCRouter({
Expand All @@ -23,6 +23,7 @@ export const userSheltersRouter = createTRPCRouter({

return result.map((shelter) => ({
id: shelter.id,
uuid: shelter.uuid,
name: shelter.name,
phone: shelter.phone,
capacity: shelter.capacity.toString(),
Expand All @@ -44,13 +45,13 @@ export const userSheltersRouter = createTRPCRouter({
},
}));
}),
findUserShelterById: protectedProcedure
.input(InputIdParams)
findUserShelterByUuid: protectedProcedure
.input(InputUuiDParams)
.query(async ({ ctx, input }) => {
const result = await db.shelter.findFirst({
where: {
createdById: ctx.session.user.id,
id: input.id,
uuid: input.uuid,
},
});

Expand All @@ -63,6 +64,7 @@ export const userSheltersRouter = createTRPCRouter({

return {
id: result.id,
uuid: result.uuid,
name: result.name,
phone: result.phone,
capacity: result.capacity.toString(),
Expand Down Expand Up @@ -118,7 +120,7 @@ export const userSheltersRouter = createTRPCRouter({
const result = await db.shelter.findFirst({
where: {
createdById: ctx.session.user.id,
id: input.id,
uuid: input.uuid,
},
});

Expand All @@ -128,8 +130,8 @@ export const userSheltersRouter = createTRPCRouter({

await db.shelter.update({
where: {
id: result.id,
createdById: ctx.session.user.id,
id: result.id,
},
data: {
name: input.name,
Expand All @@ -153,12 +155,12 @@ export const userSheltersRouter = createTRPCRouter({
});
}),
delete: protectedProcedure
.input(InputIdParams)
.input(InputUuiDParams)
.mutation(async ({ ctx, input }) => {
const result = await db.shelter.findFirst({
where: {
createdById: ctx.session.user.id,
id: input.id,
uuid: input.uuid,
},
});

Expand Down

0 comments on commit 1c688fc

Please sign in to comment.