Skip to content

Commit

Permalink
District news
Browse files Browse the repository at this point in the history
  • Loading branch information
faisalnjs committed Oct 25, 2024
1 parent f7229a9 commit ff308e9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
1 change: 1 addition & 0 deletions district.json

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions frontend/pages/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@
</div>
</div>
<% } %>
<% var districtNews = JSON.parse(vars.districtNews).slice(0, 6); if (districtNews.length > 0) { %>
<div class="articles">
<h2>District News</h2>
<hr>
<div class="grid hideBorder<%= districtNews.length % 3 %>">
<% const articles = districtNews.sort((a, b) => new Date(b.date) - new Date(a.date)); for (let i = 0; i < articles.length; i++) { const article = articles[i]; %>
<a class="article<% if (i % 3 === 0) { %> left<% } else if (i === 1 || (i - 1) % 3 === 0) { %> middle<% } else if ((i - 2) % 3 === 0) { %> right<% } %>" href="<%= article.link %>" title="<%= article.title.rendered %>">
<div class="inner">
<h3><%= article.title.rendered %></h3>
<% if (article.excerpt.rendered != null) { %><h4><% if (article.excerpt.rendered.length > 50) { %><%- article.excerpt.rendered.slice(0, 47) %>...<% } else { %><%- article.excerpt.rendered %><% } %></h4><% } %>
<p><% var date = new Date(article.date); if (date.isToday()) { %>Today<% } else if (ifYesterday(date)) { %>Yesterday<% } else { %><%= date.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' }) %><% } %> / VSCHSD</p>
</div>
</a>
<% } %>
</div>
</div>
<% } %>
<%- include('../partials/footer.ejs'); %>
</body>
<%- include('../partials/foot.ejs'); %>
Expand Down
32 changes: 30 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,19 @@ async function startApp() {
} catch { };
if (weather.toString() === '{}') getWeather();

var districtNews = {};
try {
districtNews = await fs.readFile('district.json', 'utf8');
} catch { };
if (districtNews.toString() === '{}') getDistrictNews();

var defaults = {
environment: process.env.NODE_ENV || 'testing',
domain: process.env.NODE_ENV === 'production' ? cms.siteDetails[0]['domain-production'] : process.env.NODE_ENV === 'development' ? cms.siteDetails[0]['domain-development'] : '../../../../..',
asset_prefix: process.env.CMS_ASSET_PREFIX,
asset_url: process.env.CMS_ASSET_URL,
weather
weather,
districtNews,
};

// Functions
Expand Down Expand Up @@ -177,12 +184,32 @@ async function startApp() {
})
.then(w => w.json())
.then(async w => {
await fs.writeFile('weather.json', JSON.stringify(w), 'utf8');
await fs.writeFile('weather.json', JSON.stringify(w), (err) => {
if (err) return console.error(err);
});
await new Promise(resolve => setTimeout(resolve, 2000));
defaults.weather = JSON.stringify(w);
});
};

async function getDistrictNews() {
await fetch(`https://vschsd.org/wp-json/wp/v2/posts?search=south`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
}
})
.then(w => w.json())
.then(async w => {
await fs.writeFile('district.json', JSON.stringify(w), (err) => {
if (err) return console.error(err);
});
await new Promise(resolve => setTimeout(resolve, 2000));
defaults.districtNews = JSON.stringify(w);
});
};

// Routes

app.get('/', async (req, res) => {
Expand Down Expand Up @@ -433,6 +460,7 @@ async function startApp() {
app.get('/cron', async function (req, res) {
if (!req.query.weather || (req.query.weather != process.env.OPENWEATHERMAP_API_KEY)) return res.send('INVALID');
await getWeather();
await getDistrictNews();
return res.send('OK');
});

Expand Down

0 comments on commit ff308e9

Please sign in to comment.