Skip to content
This repository has been archived by the owner on Feb 24, 2025. It is now read-only.

less prescriptive api call #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/components/EstablishmentsTable.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import React from "react";
import React, {useState} from "react";
import PropTypes from "prop-types";
import useSWR from "swr";

import { EstablishmentsTableRow } from "./EstablishmentsTableRow";
import { fetcher } from "../utils";

const headerStyle = {
paddingBottom: "10px",
Expand All @@ -12,8 +10,16 @@ const headerStyle = {
};

export const EstablishmentsTable = ({ pageNumber }) => {
const [data, setData] = useState([])
const [error, setError] = useState(null)

const url = `https://api.ratings.food.gov.uk/Establishments/basic/${pageNumber}/10`;
const { data, error } = useSWR(url, fetcher);

fetch(url, { headers: { "x-api-version": "2" } })
.then(res => res.json())
.then(json => setData(json))
.catch(err => setError(err))
Comment on lines +18 to +21
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added useSWR to help with the slow API but I do like the simplicity of this :)


const { establishments } = data || {};

if (error) return <div>Error: {error}</div>;
Expand Down