我正在使用 React 创建一个基于网络的现场音乐指南。它将显示本周的所有演出。为此,我制作了一个 CSS 网格,并使用 Day.js 分配了当前的工作日和日期。我最终想显示演出,以便它们与演出的日期相匹配,大致如下:
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|
我正在努力弄清楚如何做到这一点-我需要以某种方式找到演出日期和网格日期之间是否匹配,并将匹配的演出推入正确的网格方格中。关于我如何做到这一点的任何建议?
这是我到目前为止所做的。我使用 CSS-grid 和 day.js 创建了一个网格。这将创建 7 个网格方块,每个方块都有一个工作日和相应的日期 - 代码如下:
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%;
}
在网格组件中,我进行了一个数据库 API 调用,它将以下列形式返回 gig 数据:
[
{
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"
}
]
所以这是主要问题:我需要映射这个演出数据,然后将每个演出映射到网格中的正确日期