-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHome.js
120 lines (101 loc) · 3.52 KB
/
Home.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
let PROJECT_ID = '9qmqbv2y';
let DATASET = 'production';
let QUERY = encodeURIComponent(`*[_type in ["home", "event"]]{
_type == "home" => {
headline,
"imageUrl": mainImage.asset->url,
"imageAlt": mainImage.alt,
firstSectionHeadline,
firstSectionText,
donateHeadline,
secondSectionText,
eventsHeadline,
noEventsText,
},
_type == "event" => {
headline,
date,
address1,
address2,
address3,
"imageUrl": image.asset->url
}
}`);
let URL = `https://${PROJECT_ID}.api.sanity.io/v2021-10-21/data/query/${DATASET}?query=${QUERY}`;
const headlineContainer = document.querySelector('#headline');
const imageElement = document.querySelector('#main-image-container');
const firstSectionHeadlineContainer = document.querySelector(
'#first-section-headline'
);
const firstSectionTextContainer = document.querySelector('#first-section-text');
const donateHeadlineContainer = document.querySelector('#donate-headline');
const donateTextContainer = document.querySelector('#second-section-text');
const eventsHeadlineContainer = document.querySelector('#events-headline');
const eventsContainer = document.querySelector('#events-container');
const noEventsTextContainer = document.querySelector('#no-events-text');
const dateOptions = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
};
const timeOptions = {
hour: 'numeric',
minute: '2-digit',
};
// fetch the content
fetch(URL)
.then((res) => res.json())
.then(({ result }) => {
const page = result.find((element) => !!element.eventsHeadline);
const events = result.filter((element) => !element.eventsHeadline);
// Handle Page
const headline = page.headline;
headlineContainer.innerText = headline;
const imageAlt = page.imageAlt;
const imageSrc = page.imageUrl;
imageElement.src = imageSrc;
imageElement.alt = imageAlt;
const firstSectionHeadline = page.firstSectionHeadline;
firstSectionHeadlineContainer.innerText = firstSectionHeadline;
sanityBlockContent(firstSectionTextContainer, page.firstSectionText);
const donateHeadline = page.donateHeadline;
donateHeadlineContainer.innerText = donateHeadline;
sanityBlockContent(donateTextContainer, page.secondSectionText);
const eventsHeadline = page.eventsHeadline;
eventsHeadlineContainer.innerText = eventsHeadline;
const upcomingEvents = events.filter(function (event) {
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
return new Date(event.date).getTime() > tomorrow.getTime();
});
// Handle Events
if (upcomingEvents.length) {
noEventsTextContainer.remove();
events.forEach((event) => {
const container = document.createElement('div');
container.classList.add('upcoming-event');
container.style.backgroundImage = `url(${event.imageUrl})`;
const markup = `
<div class="content-box">
<h3>${event.headline}</h3>
<div class="date">${new Date(event.date).toLocaleDateString(
'en-US',
dateOptions
)}
<div>${new Date(event.date).toLocaleTimeString('en-US', timeOptions)}</div>
</div>
<div class="Address1">${event.address1}</div>
<div class="Address2">${event.address2}</div>
<div class="Address3">${event.address3}</div>
</div>`;
container.innerHTML = markup;
eventsContainer.appendChild(container);
});
} else {
// No events!
eventsContainer.remove();
sanityBlockContent(noEventsTextContainer, page.noEventsText);
}
})
.catch((err) => console.error(err));