Skip to content

Commit af8656e

Browse files
author
Phil Hawksworth
authored
Merge pull request #512 from jamstack/zl/476
Adds filters by country to User Groups on Community page. @zachleat is crafty.
2 parents 6e5f044 + 4a1c8bc commit af8656e

File tree

5 files changed

+79
-19
lines changed

5 files changed

+79
-19
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"dotenv": "^8.2.0",
3030
"fast-glob": "^3.2.4",
3131
"gray-matter": "^4.0.2",
32+
"i18n-iso-countries": "^6.4.0",
3233
"js-yaml": "^3.14.0",
3334
"lodash": "^4.17.20",
3435
"luxon": "^1.25.0",
@@ -38,6 +39,7 @@
3839
"npm-run-all": "^4.1.5",
3940
"postcss": "^8.1.10",
4041
"postcss-cli": "^8.3.0",
42+
"placename": "^1.1.2",
4143
"postcss-import": "^13.0.0",
4244
"spdx-correct": "^3.1.1",
4345
"tailwindcss": "^2.0.1"

src/css/tailwind.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ details[open] .summary-swap-open {
318318
.filter-typeofcms--hide,
319319
.filter-language--hide,
320320
.filter-template--hide,
321-
.filter-license--hide {
321+
.filter-license--hide,
322+
.filter-country--hide {
322323
display: none !important;
323324
}
324325
/* purgecss end ignore */

src/site/_data/community.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
- name: Boston
1010
link: https://www.meetup.com/Jamstack-Boston/
1111
feed: https://api.meetup.com/Jamstack-Boston/events
12+
country: United States of America
1213

1314
- name: Porto
1415
link: https://www.meetup.com/Jamstack-Porto/
1516
feed: https://api.meetup.com/Jamstack-Porto/events
1617

17-
- name: New York city
18+
- name: New York City
1819
link: https://www.meetup.com/Jamstack-nyc/
1920
feed: https://api.meetup.com/Jamstack-nyc/events
2021

src/site/_data/eleventyComputed.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const placename = require("placename");
2+
const lookupCountryName = require("i18n-iso-countries");
3+
4+
async function getCountryCode(searchTerm) {
5+
return new Promise(resolve => {
6+
placename(searchTerm, function (err, rows) {
7+
if(rows.length) {
8+
resolve(rows[0].country);
9+
} else {
10+
resolve();
11+
}
12+
})
13+
});
14+
}
15+
16+
function getCountryName(countryCode) {
17+
if(countryCode) {
18+
return lookupCountryName.getName(countryCode, "en", {select: "official"});
19+
}
20+
}
21+
22+
module.exports = {
23+
community: async function(data) {
24+
let community = data.community.filter(e => true);
25+
for(let meetup of community) {
26+
// skip if populated in the YAML file
27+
if(meetup.country) {
28+
continue;
29+
}
30+
31+
// sry for await in loop
32+
let countryCode = await getCountryCode(meetup.name);
33+
if(countryCode) {
34+
meetup.country = getCountryName(countryCode);
35+
}
36+
}
37+
38+
return community;
39+
}
40+
};

src/site/community.njk

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,40 @@ layout: layouts/base.njk
7070

7171
<section class="cards">
7272
<h2 class="mb-12">Find a Local User Group</h2>
73-
<ul class="grid grid-cols-2 lg:grid-cols-3 gap-8">
73+
<filter-container>
74+
<form class="pb-4">
75+
<div class="pb-2">
76+
<strong class="pr-4">Filter</strong>
77+
<span data-filter-results></span>
78+
</div>
79+
<label class="inline-flex pr-4 pb-2">
80+
<span class="sr-only">Country</span>
81+
<select data-filter-bind="country" class="text-black py-1 px-2 rounded-default border border-white bg-white">
82+
<option selected value="">All Countries</option>
83+
{%- for country in community | select("country") | flatten | unique | sort(false, false) %}
84+
<option value="{{ country | lower }}">{{ country }}</option>
85+
{%- endfor %}
86+
</select>
87+
</label>
88+
</form>
89+
<ul class="grid grid-cols-2 lg:grid-cols-3 gap-8">
90+
{% set themes = [
91+
"bg-gradient-card-sunrise hover:card-shadow-sunrise",
92+
"bg-gradient-card-blue hover:card-shadow-blue-seafoam",
93+
"bg-gradient-card-seafoam hover:card-shadow-seafoam",
94+
"bg-gradient-card-gold hover:card-shadow-gold"]
95+
%}
96+
{%- for item in community | sort(false, false, 'name') %}
7497

75-
76-
{% set themes = [
77-
"bg-gradient-card-sunrise hover:card-shadow-sunrise",
78-
"bg-gradient-card-blue hover:card-shadow-blue-seafoam",
79-
"bg-gradient-card-seafoam hover:card-shadow-seafoam",
80-
"bg-gradient-card-gold hover:card-shadow-gold"]
81-
%}
82-
{%- for item in community | sort(false, false, 'name') %}
83-
84-
{# chose a pseudorandom theme based on the city name #}
85-
{% set theme = item.name.length % 4 %}
86-
<li>
87-
{{ meetup.card( item.name, item.link, themes[theme] ) }}
88-
</li>
89-
{%- endfor %}
90-
</ul>
98+
{# chose a pseudorandom theme based on the city name #}
99+
{% set theme = item.name.length % 4 %}
100+
<li
101+
{%- if item.country %} data-filter-country="{{ item.country | lower }}"{% endif %}>
102+
{{ meetup.card( item.name, item.link, themes[theme] ) }}
103+
</li>
104+
{%- endfor %}
105+
</ul>
106+
</filter-container>
91107
</section>
92108

93109
<section>

0 commit comments

Comments
 (0)