-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcountry.js
67 lines (57 loc) · 1.62 KB
/
country.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
import { stringToHex } from "./stringToColor.js";
import { html } from "./html.js";
import { update } from "./update.js";
const unique = list => [...new Set(list)];
export const AddCountry = currentCountry => state =>
update({
...state,
currentCountry,
selectedCountries: unique([...state.selectedCountries, currentCountry])
});
export const RemoveCountry = country => state =>
update({
...state,
selectedCountries: state.selectedCountries.filter(c => c !== country)
});
export const isActive = ({ selectedCountries }) => country =>
selectedCountries.includes(country);
export const countryAction = state => country => {
const isCountryActive = isActive(state)(country);
if (isCountryActive) {
return RemoveCountry(country);
} else {
return AddCountry(country);
}
};
export const countryHighlight = state => country => {
const isCountryActive = isActive(state)(country);
if (isCountryActive) {
return { "font-weight": "bold", color: stringToHex(country) };
} else {
return {};
}
};
const truncate = name => name.length > 15 ? name.substring(0, 12) + "..." : name;
export const countryChip = name => state => html`
<span
class="chip c-hand"
onclick=${countryAction(state)(name)}
style=${countryHighlight(state)(name)}
>
${truncate(name)}
<span
class="btn btn-clear"
href="#"
aria-label="Remove Country"
role="button"
></span>
</span>
`;
export const countryChips = state =>
html`
<div class="m-2 country-chips">
${Array.from(state.selectedCountries).map(name =>
countryChip(name)(state)
)}
</div>
`;