Search code examples
reactjscss-griddayjs

CSS grid, Day.js, React: In my grid-based calendar, how do I push grid items with specific dates into grid tiles with the corresponding date?


I'm creating a web-based live music guide using React. It will show all the gigs for the current week. To accomplish this, I made a CSS grid, and assigned the current weekdays and dates using Day.js. I eventually want to display gigs so that they're matched with the day on which they're being performed, roughly as follows:

Monday          | Tuesday         |Wednesday       |        
October 5th 2020| October 6th 2020|October 7th 2020|

Monday gig 1     |Tuesday gig 1    |Wednesday gig 1|
Monday gig 2     |Tuesday gig 2    |Wednesday gig 2|
Monday gig 3     |Tuesday gig 3    |Wednesday gig 3|

I'm struggling to figure out how to do this- I need to somehow find if there is a match between the gig date and the grid date, and push matching gigs into their correct grid square. Any suggestions on how I can accomplish this?

Here's what I've done so far. I've created a grid using CSS-grid and day.js. This creates 7 grid squares, each with a weekday and corresponding date - code as follows:

Grid component

import React, {useEffect,useState} from 'react'
import Giglisting from './Giglisting'
import axios from 'axios'
import dayjs from "dayjs";
import en from "dayjs/locale/en";

const Grid = () => {

  const [gigs, setGigs] = useState([])
  const [isLoading, setLoading] = useState(false)


  useEffect(()=>{
    setLoading(true)
    axios.get('https://us-central1-gig-fort.cloudfunctions.net/api/getGigListings')
    .then(res=> {
      setGigs(res.data)
      setLoading(false)
    })
  },[])

  dayjs.locale({
    ...en,
    weekStart: 1 
  });

  const startOfWeek = dayjs().startOf("week");

  const weekdays = new Array(7)
  .fill(startOfWeek)
  .map((day, idx) => day.add(idx, "day")
  .format("dddd, MMMM D "));

  const grid =  <div className="grid-container">
  {weekdays.map((day) => (
    <div key={day} className="grid-item">
      <h3>{day}</h3>
    </div>
  ))}
</div>


const loadingMessage = <h1 className = 'h1-loading'>gigs loading...</h1>
const display = isLoading === false ? grid : loadingMessage

  return (
    <>
    {display}
    </>
  );
};

export default Grid

Grid CSS

.grid-container {
    display: grid;
    justify-items: center;
    margin-top:5%;
    grid-template-rows: repeat(3, 200px);
    grid-row-gap: 1px;
    grid-template-columns: repeat(3, 1fr);
    grid-column-gap: 1px;
  
  }
  
  .grid-item {
    color:#a5262b;
    font-family: 'Canela-Medium', sans-serif;
    width:auto;
    width:80%;
  }

In the grid component, I'm making a database API call that will return gig data in the following form:

[
    {
    date:"2020-10-05",
    genre:"Jazz",
    name:"Jazz quartet",
    price:"$10",
    tickets:"ticket URL",
    time:"21:30",
    venueName:"The Third Eye"
    },
    {
    date:"2020-10-06",
    genre:"Funk",
    name:"The Fades at Rogue",
    price:"$10",
    tickets:"ticket URL",
    time:"21:30",
    venueName:"Rogue and Vagabond"
    },
    {
    date:"2020-10-07",
    genre:"Latin",
    name:"Son Clave",
    price:"$10",
    tickets:"ticket URL",
    time:"21:30",
    venueName:"Havana"
    }
]

So here's the main issue: I need to map through this gig data, and then map each gig to the correct date in the grid


Solution

  • I took all the code and made it work in this sandbox,

    Note,

    • The api sometims return date as "" and creation date exists (which usually doesn't), its ur call
    • Asssuming the api date format is YYYY-MM-DD

    Because of first reason some days are missing gigs