Skip to content

Commit 4f82430

Browse files
feat: reigioning it! (#249)
1 parent 576d967 commit 4f82430

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

app/controllers/shop_controller.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,16 @@ def create_order
107107
private
108108

109109
def user_region
110-
return current_user.region if current_user.region.present?
110+
if current_user
111+
return current_user.region if current_user.region.present?
111112

112-
primary_address = current_user.addresses.find { |a| a["primary"] } || current_user.addresses.first
113-
country = primary_address&.dig("country")
114-
Shop::Regionalizable.country_to_region(country)
113+
primary_address = current_user.addresses.find { |a| a["primary"] } || current_user.addresses.first
114+
country = primary_address&.dig("country")
115+
region_from_address = Shop::Regionalizable.country_to_region(country)
116+
return region_from_address if region_from_address != "XX" || country.present?
117+
end
118+
119+
Shop::Regionalizable.timezone_to_region(cookies[:timezone])
115120
end
116121

117122
def user_ordered_free_stickers?
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Controller } from "@hotwired/stimulus";
2+
3+
export default class extends Controller {
4+
connect() {
5+
if (!this.hasTimezoneCookie()) {
6+
this.setTimezoneCookie();
7+
}
8+
}
9+
10+
hasTimezoneCookie() {
11+
return document.cookie.split(";").some((c) => c.trim().startsWith("timezone="));
12+
}
13+
14+
setTimezoneCookie() {
15+
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
16+
if (timezone) {
17+
document.cookie = `timezone=${encodeURIComponent(timezone)};path=/;max-age=31536000;SameSite=Lax`;
18+
}
19+
}
20+
}

app/models/concerns/shop/regionalizable.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,38 @@ def self.region_name(region_code)
9494
def self.countries_for_region(region_code)
9595
REGIONS.dig(region_code.upcase, :countries) || []
9696
end
97+
98+
TIMEZONE_TO_REGION = {
99+
# United States
100+
"America/New_York" => "US", "America/Chicago" => "US", "America/Denver" => "US",
101+
"America/Los_Angeles" => "US", "America/Phoenix" => "US", "America/Anchorage" => "US",
102+
"Pacific/Honolulu" => "US", "America/Detroit" => "US", "America/Indiana/Indianapolis" => "US",
103+
# Canada
104+
"America/Toronto" => "CA", "America/Vancouver" => "CA", "America/Edmonton" => "CA",
105+
"America/Winnipeg" => "CA", "America/Halifax" => "CA", "America/St_Johns" => "CA",
106+
# United Kingdom
107+
"Europe/London" => "UK",
108+
# EU countries
109+
"Europe/Paris" => "EU", "Europe/Berlin" => "EU", "Europe/Rome" => "EU",
110+
"Europe/Madrid" => "EU", "Europe/Amsterdam" => "EU", "Europe/Brussels" => "EU",
111+
"Europe/Vienna" => "EU", "Europe/Stockholm" => "EU", "Europe/Copenhagen" => "EU",
112+
"Europe/Helsinki" => "EU", "Europe/Warsaw" => "EU", "Europe/Prague" => "EU",
113+
"Europe/Budapest" => "EU", "Europe/Athens" => "EU", "Europe/Bucharest" => "EU",
114+
"Europe/Sofia" => "EU", "Europe/Dublin" => "EU", "Europe/Lisbon" => "EU",
115+
"Europe/Zagreb" => "EU", "Europe/Ljubljana" => "EU", "Europe/Bratislava" => "EU",
116+
"Europe/Tallinn" => "EU", "Europe/Riga" => "EU", "Europe/Vilnius" => "EU",
117+
"Europe/Luxembourg" => "EU", "Europe/Malta" => "EU",
118+
# India
119+
"Asia/Kolkata" => "IN", "Asia/Calcutta" => "IN",
120+
# Australia/NZ
121+
"Australia/Sydney" => "AU", "Australia/Melbourne" => "AU", "Australia/Brisbane" => "AU",
122+
"Australia/Perth" => "AU", "Australia/Adelaide" => "AU", "Australia/Hobart" => "AU",
123+
"Pacific/Auckland" => "AU", "Pacific/Fiji" => "AU"
124+
}.freeze
125+
126+
def self.timezone_to_region(timezone)
127+
return "XX" if timezone.blank?
128+
TIMEZONE_TO_REGION[timezone] || "XX"
129+
end
97130
end
98131
end

app/views/layouts/application.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<%= Sentry.get_trace_propagation_meta.html_safe %> <%# erb_lint:disable ErbSafety %>
3838
</head>
3939

40-
<body class="<%= current_user.present? ? 'signed-in' : '' %>">
40+
<body class="<%= current_user.present? ? 'signed-in' : '' %>" data-controller="timezone">
4141
<% if current_user.present? %>
4242
<%= render "shared/sidebar" %>
4343
<% end %>

0 commit comments

Comments
 (0)