From b65d15ba340b0cf7e85e90d7c9685774cdb74764 Mon Sep 17 00:00:00 2001 From: Gabor Juhasz Date: Mon, 11 Jul 2022 14:58:02 +0200 Subject: [PATCH] refactor to Drawer --- components/Box.tsx | 149 ++++++++++++++-------------------------- components/Calendar.tsx | 139 +++++++++++++++++++++++-------------- components/Detail.tsx | 45 ++++++++++++ components/Gradient.tsx | 30 ++++++++ styles/globals.css | 14 ++++ tsconfig.json | 1 + 6 files changed, 229 insertions(+), 149 deletions(-) create mode 100644 components/Detail.tsx create mode 100644 components/Gradient.tsx diff --git a/components/Box.tsx b/components/Box.tsx index 3a71465..a5a5a02 100644 --- a/components/Box.tsx +++ b/components/Box.tsx @@ -1,128 +1,83 @@ -import { Button, Group, Popover, Drawer } from "@mantine/core"; -import { Favorites, FavoriteState } from "pages"; -import { FC, useState } from "react"; -import { useLongPress } from "react-use"; +import { useMantineTheme } from "@mantine/core"; +import { Gradient } from "components/Gradient"; +import { Favorites } from "pages"; +import { FC } from "react"; import { Item } from "types"; -const getBackground = (itemState: FavoriteState) => { - switch (itemState) { - case "must": { - return "green"; - } - case "maybe": { - return "yellow"; - } - default: { - return "white"; - } - } -}; +export const SCALE_MINUTES_TO_PIXELS = 2.7; interface BoxProps { startOfDay: number; item: Item; favorites: Favorites; - setFavorites: React.Dispatch>; + setDetailId: React.Dispatch>; } -export const Box: FC = ({ item, startOfDay, favorites, setFavorites }) => { - const [popoverOpen, setPopoverOpen] = useState(false); - const setOverlayState = (state: boolean) => setPopoverOpen(state); - const longPressEvent = useLongPress(() => setOverlayState(true)); +export const Box: FC = ({ item, startOfDay, favorites, setDetailId }) => { + const theme = useMantineTheme(); const itemState = favorites[item.id]; if (itemState === "nope") { return null; } - const setFavoriteState = (state?: FavoriteState) => { - setFavorites((favs) => { - if (!favs) { - favs = {}; + const getBackground = () => { + switch (itemState) { + case "must": { + return theme.colors.green[4]; } - if (state) { - favs[item.id] = state; - } else { - delete favs[item.id]; + case "maybe": { + return theme.colors.yellow[4]; } - return favs; - }); - setOverlayState(false); + default: { + return "white"; + } + } }; return (
  • setDetailId(item.id)} style={{ listStyle: "none", position: "absolute", left: 0, - top: (item.relativeDateInMinutes - startOfDay) * 2, + top: (item.relativeDateInMinutes - startOfDay) * SCALE_MINUTES_TO_PIXELS, padding: 4, }} > - setOverlayState(false)} - position="bottom" - target={ -
    -
    -

    - {item.title} -

    -

    -

    -
    -

    {item.time?.name}

    -

    {item.duration} perc

    -
    -
    - } +
    - - {itemState && } - {itemState !== "must" && ( - - )} - {itemState !== "maybe" && ( - - )} - - - +
    +

    {item.title}

    +
    + + + +
    +

    {item.time?.name}

    +

    {item.duration} perc

    +
    +
  • ); }; diff --git a/components/Calendar.tsx b/components/Calendar.tsx index d5994db..41929ec 100644 --- a/components/Calendar.tsx +++ b/components/Calendar.tsx @@ -1,6 +1,8 @@ -import { Box } from "components/Box"; -import { Favorites } from "pages"; -import { FC } from "react"; +import { Drawer } from "@mantine/core"; +import { Box, SCALE_MINUTES_TO_PIXELS } from "components/Box"; +import { Detail } from "components/Detail"; +import { Favorites, FavoriteState } from "pages"; +import { FC, useState } from "react"; import { Item, Stage, Village } from "types"; const PADDING = 30; @@ -34,12 +36,15 @@ interface CalendarProps { setFavorites: React.Dispatch>; } export const Calendar: FC = ({ list, favorites, setFavorites }) => { + const [detailId, setDetailId] = useState(null); + // find the first program of the day // show an extra 30 minutes of padding before const startOfDay = list.reduce((smallest, current) => { return Math.min(smallest, current.relativeDateInMinutes); - }, Infinity) - PADDING; + }, Infinity) - + PADDING / 2; const endOfDay = list.reduce((biggest, current) => { return Math.max(biggest, current.relativeDateInMinutes + current.duration); @@ -49,58 +54,88 @@ export const Calendar: FC = ({ list, favorites, setFavorites }) = const groups = groupByVillageAndStage(list); + const detail = list.find((item) => item.id === detailId); + + const setFavoriteState = (item: Item, state?: FavoriteState) => { + setFavorites((favs) => { + if (!favs) { + favs = {}; + } + if (state) { + favs[item.id] = state; + } else { + delete favs[item.id]; + } + return favs; + }); + setDetailId(null); + }; + return ( -
      - {groups.map((group) => { - return ( -
    • -
      -

      {group.village}

      -

      {group.stage}

      -
      -
        + setDetailId(null)} + position="right" + padding="md" + size="80%" + zIndex={301} + > + {detail && } + + +
          + {groups.map((group) => { + return ( +
        • - {group.list.map((item) => { - return ( - - ); - })} -
        - - ); - })} -
      +
      +

      {group.village}

      +

      {group.stage}

      +
      +
        + {group.list.map((item) => { + return ( + + ); + })} +
      +
    • + ); + })} +
    + ); }; diff --git a/components/Detail.tsx b/components/Detail.tsx new file mode 100644 index 0000000..8175e15 --- /dev/null +++ b/components/Detail.tsx @@ -0,0 +1,45 @@ +import { Button, Group, Space, Title } from "@mantine/core"; +import { Gradient } from "components/Gradient"; +import { Favorites, FavoriteState } from "pages"; +import { FC } from "react"; +import { Item } from "types"; + +interface DetailProps { + item: Item; + favorites: Favorites; + setFavoriteState: (item: Item, state?: FavoriteState) => void; +} +export const Detail: FC = ({ item, favorites, setFavoriteState }) => { + const itemState = favorites[item.id]; + if (itemState === "nope") { + return null; + } + + return ( + +
    + {item.title} +

    +

    + + + + + {itemState && } + {itemState !== "must" && ( + + )} + {itemState !== "maybe" && ( + + )} + + +
    + ); +}; diff --git a/components/Gradient.tsx b/components/Gradient.tsx new file mode 100644 index 0000000..af8d83a --- /dev/null +++ b/components/Gradient.tsx @@ -0,0 +1,30 @@ +import { FC } from "react"; + +interface GradientProps { + color?: string; + position?: string; + height?: number | string; +} +export const Gradient: FC = ({ color = "white", position = "bottom", height = 100 }) => { + return ( +
    +
    +
    + ); +}; diff --git a/styles/globals.css b/styles/globals.css index ddba9d8..8a5bddb 100755 --- a/styles/globals.css +++ b/styles/globals.css @@ -90,6 +90,20 @@ abbr:where([title]) { text-decoration: underline dotted; } +/* +Remove the default font size and weight for headings. +*/ + +h1, +h2, +h3, +h4, +h5, +h6 { + font-size: inherit; + font-weight: inherit; +} + /* Reset links to optimize for opt-in styling instead of opt-out. */ diff --git a/tsconfig.json b/tsconfig.json index 8687e92..f718381 100755 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,6 +15,7 @@ "jsx": "preserve", "incremental": true, "downlevelIteration": true, + "strictNullChecks": true, "baseUrl": "." }, "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],