Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
feat(page:home): fetch live submissions and display it
Browse files Browse the repository at this point in the history
  ## what
  - fetch live submissions and display it
    - fetch data
    - display `loading` when fetching data
    - display `error` when fetching has failed
    - display live submissions after fetching data
    - refetch data after `5000ms`

  ## how
    - store the initial pollId to 0 using React `useRef`
    - fetch the data using hook `useFetchLiveSubmission`
      - requires pollId
      - refetch data after `5000ms`
    - store the pollId to the latest submission ID after a successfully
      fetching data
    - display `Loading data` when initially fetching data
    - display `Error fetching data` when there's an error fetching data
    - display the fetched data using `LiveSubmissionTable` component

  ## why
  - to display live submissions data

  ## where
  - ./src/app/page.tsx

  ## usage
  • Loading branch information
Clumsy-Coder committed Dec 31, 2023
1 parent d2ca3b8 commit 1d67137
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
"use client";

import { useRef } from "react";

import { useFetchLiveSubmission } from "@/hooks";
import LiveSubmissionTable from "./LiveSubmissionTable";

export default function Home() {
return <div>home page</div>;
// used for setting the pollId.
// not using useState, because that will force a re-render every time it's set
// then it will fetch the new data, which will set the state, which will render the page
// which will refetch the from the server. Causing an infinite loop
let pollIdRef = useRef(0);
const { data, isLoading, isError, isSuccess } = useFetchLiveSubmission(pollIdRef.current);

if (isLoading || !data) {
return <div>Fetching data</div>;
}

if (isError) {
return <div>Error fetching data</div>;
}

if (isSuccess) {
pollIdRef.current = data[0].msg.sid;
}

return (
<div>
<LiveSubmissionTable data={data} />
</div>
);
}

0 comments on commit 1d67137

Please sign in to comment.