-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(image fetching): add proxy endpoint to bypass CORS (#10)
Also added image titles in the gallery
- Loading branch information
Showing
9 changed files
with
62 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,23 @@ | ||
"use client"; | ||
|
||
import Gallery from "../components/gallery/Gallery"; | ||
import useImageStore from "../store/image.store"; | ||
import { Image } from "../model/image"; | ||
import { getAllImages } from "../services/image.service"; | ||
import { useEffect, useState } from "react"; | ||
|
||
export default function HomePage() { | ||
|
||
const imageStore = useImageStore(); | ||
const [images, setImages] = useState<Image[] | null>(null); | ||
|
||
useEffect(() => { | ||
if (images) return; | ||
imageStore.getImages().then(setImages); | ||
}, [images, imageStore]); | ||
|
||
export default async function HomePage() { | ||
const images: Image[] = await getAllImages(process.env.imagesUrl ?? ""); | ||
return ( | ||
<section className="p-2 bg-bg-light" aria-label="Gallery Section"> | ||
<Gallery images={images} /> | ||
<Gallery images={images ?? []} /> | ||
</section> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { type NextApiRequest, NextApiResponse } from "next"; | ||
|
||
export default async function handler(_: NextApiRequest, res: NextApiResponse) { | ||
try { | ||
const response = await fetch(process.env.IMAGES_URL as string, { | ||
method: "GET" | ||
}); | ||
const data = await response.json(); | ||
res.status(response.status).json(data); | ||
} catch (error) { | ||
console.warn(`Could not fetch images :\n${error}`); | ||
res.status(500).json({ error: "Failed to fetch images" }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,9 @@ | ||
import { Image } from "../model/image"; | ||
|
||
export async function getAllImages(url: string): Promise<Image[]> { | ||
return new Promise<Image[]>((resolve, reject) => { | ||
fetch(url, {cache: "force-cache"}) | ||
.then((response) => { | ||
if (!response.ok) { | ||
reject(new Error("Failed to fetch images")); | ||
} else { | ||
resolve(response.json()); | ||
} | ||
}) | ||
.catch((error) => reject(new Error(error))); | ||
}); | ||
export async function getAllImages(): Promise<Image[]> { | ||
const url = new URL("/api/images", window.location.origin); | ||
|
||
const response = await fetch(url.toString(), {cache: "force-cache"}); | ||
if (!response.ok) return []; | ||
return await response.json(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,39 @@ | ||
import HomePage from "../../src/app/page"; | ||
import { expect } from "@jest/globals"; | ||
import { render, screen } from "@testing-library/react"; | ||
import useImageStore from "../../src/store/image.store"; | ||
import { act } from "react"; | ||
|
||
jest.mock("../../src/components/gallery/Gallery", () => () => <div />); | ||
|
||
// mock fetch | ||
global.fetch = jest.fn(() => | ||
Promise.resolve({ | ||
ok: true, | ||
json: () => Promise.resolve([]), | ||
} as Response) | ||
); | ||
) as jest.Mock; | ||
|
||
process.env.imagesUrl = "http://localhost:3000/assets/test.json"; | ||
jest.mock("../../src/store/image.store", () => ({ | ||
__esModule: true, | ||
default: jest.fn().mockReturnValue({ | ||
getImages: jest.fn().mockResolvedValue([]), | ||
}), | ||
})); | ||
|
||
describe("Home Page", () => { | ||
it("should render", async () => { | ||
// When | ||
render(await HomePage()); | ||
await act(() => render(<HomePage />)); | ||
|
||
// Then | ||
expect(screen.getByRole("region")).toBeInTheDocument(); | ||
}); | ||
|
||
it("should fetch image config", async () => { | ||
// When | ||
render(await HomePage()); | ||
await act(() => render(<HomePage />)); | ||
|
||
// Then | ||
expect(global.fetch).toHaveBeenCalledWith("http://localhost:3000/assets/test.json", { cache: "force-cache" }); | ||
expect(useImageStore).toHaveBeenCalled(); | ||
}); | ||
}); |