Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…all-2024-atvie into fix-ui
  • Loading branch information
Eprince-hub committed Nov 4, 2024
2 parents 3b44549 + db772cb commit f45f3a0
Show file tree
Hide file tree
Showing 10 changed files with 1,347 additions and 1,213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ type Props = {
};

export default async function DeleteAnimalNaivePage(props: Props) {
const animal = await deleteAnimalInsecure({
id: Number((await props.params).animalId),
});
const animal = await deleteAnimalInsecure(
Number((await props.params).animalId),
);

if (!animal) {
notFound();
Expand Down
43 changes: 12 additions & 31 deletions app/animals/dashboard/AnimalsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,11 @@ export default function AnimalsForm(props: Props) {
if (!response.ok) {
let newErrorMessage = 'Error deleting animal';

try {
const responseBody: AnimalResponseBodyDelete =
await response.json();

if ('error' in responseBody) {
newErrorMessage = responseBody.error;
}
} catch (error) {
// Don't fail if response JSON body
// cannot be parsed
console.error(error);
const responseBody: AnimalResponseBodyDelete =
await response.json();

if ('error' in responseBody) {
newErrorMessage = responseBody.error;
}

// TODO: Use toast instead of showing
Expand Down Expand Up @@ -152,16 +146,10 @@ export default function AnimalsForm(props: Props) {
if (!response.ok) {
let newErrorMessage = 'Error updating animal';

try {
const body: AnimalResponseBodyPut = await response.json();
const body: AnimalResponseBodyPut = await response.json();

if ('error' in body) {
newErrorMessage = body.error;
}
} catch (error) {
// Don't fail if response JSON body cannot
// be parsed
console.error(error);
if ('error' in body) {
newErrorMessage = body.error;
}

setErrorMessage(newErrorMessage);
Expand All @@ -186,17 +174,10 @@ export default function AnimalsForm(props: Props) {
if (!response.ok) {
let newErrorMessage = 'Error creating animal';

try {
const body: AnimalsResponseBodyPost =
await response.json();

if ('error' in body) {
newErrorMessage = body.error;
}
} catch (error) {
// Don't fail if response JSON body cannot
// be parsed
console.error(error);
const body: AnimalsResponseBodyPost = await response.json();

if ('error' in body) {
newErrorMessage = body.error;
}

setErrorMessage(newErrorMessage);
Expand Down
4 changes: 1 addition & 3 deletions app/api/animals/[animalId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ export async function DELETE(
): Promise<NextResponse<AnimalResponseBodyDelete>> {
console.log(Number((await params).animalId));

// const animal = await deleteAnimalInsecure({
// id: Number((await params).animalId),
// });
// const animal = await deleteAnimalInsecure(Number((await params).animalId));

const sessionTokenCookie = (await cookies()).get('sessionToken');

Expand Down
37 changes: 25 additions & 12 deletions database/animals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,16 +269,29 @@ export const updateAnimalInsecure = cache(async (updatedAnimal: Animal) => {
return animal;
});

export const deleteAnimalInsecure = cache(
async (deletedAnimal: Pick<Animal, 'id'>) => {
const [animal] = await sql<Animal[]>`
DELETE FROM animals
WHERE
id = ${deletedAnimal.id}
RETURNING
animals.*
`;
export const deleteAnimalInsecure = cache(async (animalId: Animal['id']) => {
const [animal] = await sql<Animal[]>`
DELETE FROM animals
WHERE
id = ${animalId}
RETURNING
animals.*
`;

return animal;
},
);
return animal;
});

// Alternative: Using the TypeScript Pick utility type
// export const deleteAnimalInsecure = cache(
// async (deletedAnimal: Pick<Animal, 'id'>) => {
// const [animal] = await sql<Animal[]>`
// DELETE FROM animals
// WHERE
// id = ${deletedAnimal.id}
// RETURNING
// animals.*
// `;

// return animal;
// },
// );
4 changes: 2 additions & 2 deletions fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ primary_region = "otp"
[http_service]
internal_port = 8080
force_https = true
auto_stop_machines = true
auto_start_machines = true
auto_stop_machines = "suspend"
auto_start_machines = true
6 changes: 4 additions & 2 deletions jest.config.js → jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/** @jest-config-loader esbuild-register */

import type { Config } from 'jest';
import nextJest from 'next/jest.js';

// https://nextjs.org/docs/app/building-your-application/testing/jest
const createJestConfig = nextJest({
dir: './',
});

/** @type {import('jest').Config} */
const config = {
const config: Config = {
testEnvironment: 'jest-environment-jsdom',
testPathIgnorePatterns: ['<rootDir>/playwright/'],
};
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@
"zod": "^3.23.8"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@jest/globals": "^30.0.0-alpha.6",
"@playwright/test": "^1.47.2",
"@ts-safeql/eslint-plugin": "^3.4.7",
"@types/bcrypt": "^5.0.2",
"@types/dotenv-safe": "^8.1.6",
"@types/node": "^22.7.4",
"@types/react": "^18.3.10",
"@types/react-dom": "^18.3.0",
"esbuild-register": "^3.6.0",
"eslint": "^9.11.1",
"eslint-config-upleveled": "^8.8.0",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"jest": "^30.0.0-alpha.6",
"jest-environment-jsdom": "^30.0.0-alpha.6",
"libpg-query": "16.2.0",
"prettier": "^3.3.3",
"prettier-plugin-embed": "^0.4.15",
Expand Down
Loading

0 comments on commit f45f3a0

Please sign in to comment.