remix-demo

react router (remix) demo

git clone https://9o.is/git/remix-demo.git

report.tsx

(1663B)


      1 import { useLoaderData } from "@remix-run/react";
      2 import type { LoaderArgs, MetaFunction } from "@remix-run/server-runtime";
      3 import Heading from "~/components/Heading";
      4 import CenteredLayout from "~/components/layouts/CenteredLayout";
      5 import Timeline from "~/components/layouts/Timeline";
      6 import { generateReport } from "~/controllers/report.server";
      7 
      8 export const meta: MetaFunction = () => ({
      9   title: "Report | Calories Counter",
     10 });
     11 
     12 type Result = {
     13   threshold: number;
     14   history: { [index: string]: number };
     15 };
     16 
     17 export async function loader({ request }: LoaderArgs) {
     18   return generateReport(request);
     19 }
     20 
     21 export default function Report() {
     22   const { threshold, history }: Result = useLoaderData();
     23 
     24   const timelineValues = Object.keys(history).map((date) => {
     25     const calories = history[date];
     26     const failed = calories > threshold;
     27     const style = failed ? "text-red-500 font-semibold" : "text-gray-900";
     28 
     29     return {
     30       id: date,
     31       date: date,
     32       node: (
     33         <div key={date} className={style}>
     34           {calories}{" "}
     35           <abbr title="calories" className="no-underline">
     36             cals
     37           </abbr>
     38           {failed ? <div>Over the threshold!</div> : null}
     39         </div>
     40       ),
     41     };
     42   });
     43 
     44   return (
     45     <section>
     46       <CenteredLayout>
     47         <Heading>Report</Heading>
     48         <p className="mb-6 text-lg text-gray-500">
     49           Below is a list of your calories intake per day. Remember, your
     50           calories threshold limit is{" "}
     51           <em className="font-semibold">{threshold} calories</em>.
     52         </p>
     53         <Timeline values={timelineValues} />
     54       </CenteredLayout>
     55     </section>
     56   );
     57 }