Skip to content

Commit

Permalink
inertia - fix notification subscriptions create/destroy, fix census l…
Browse files Browse the repository at this point in the history
…ookup of congressional districts
  • Loading branch information
dcordz committed Aug 22, 2024
1 parent f55da02 commit 4d80b4c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def destroy
private

def set_subscription
@subscription = current_user&.push_notification_subscriptions&.filter { |s| s.endpoint == push_notification_subscription_params[:endpoint] }
@subscription = current_user&.push_notification_subscriptions&.find { |s| s.endpoint == push_notification_subscription_params[:endpoint] }
end

def push_notification_subscription_params
Expand Down
4 changes: 3 additions & 1 deletion app/frontend/hooks/useLocales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { router, usePage } from "@inertiajs/react";
import { useCallback, useMemo } from "react";
import { ISelectOption, sway } from "sway";
import { SWAY_STORAGE, sessionGet, toFormattedLocaleName } from "../sway_utils";
import { SWAY_STORAGE, logDev, sessionGet, toFormattedLocaleName } from "../sway_utils";

export const getDefaultSwayLocale = () => {
const sessionLocale = sessionGet(SWAY_STORAGE.Session.User.Locale);
Expand All @@ -23,6 +23,8 @@ export const useLocales = () => {

export const useLocale = (): [sway.ISwayLocale, (localeId: number) => void] => {
const swayLocale = usePage<sway.IPageProps>().props.swayLocale;
logDev("useLocale - using swayLocale from props -", `${swayLocale.id} - ${swayLocale.name}`);

const getLocale = useCallback((localeId: number) => {
router.visit(`${window.location.origin}${window.location.pathname}?sway_locale_id=${localeId}`);
}, []);
Expand Down
14 changes: 11 additions & 3 deletions app/frontend/pages/Notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const Notifications: React.FC<IProps> = ({ user: _user, subscriptions }) => {

const registerServiceWorker = useCallback(() => {
// Check if the browser supports service workers
if ("serviceWorker" in navigator && "VAPID_PUBLIC_KEY" in window) {
if (navigator && "serviceWorker" in navigator && "VAPID_PUBLIC_KEY" in window) {
// Register the service worker script (service_worker.js)
navigator.serviceWorker
.register("service_worker.js")
Expand Down Expand Up @@ -147,7 +147,7 @@ const Notifications: React.FC<IProps> = ({ user: _user, subscriptions }) => {

const [subscription, setSubscription] = useState<any>();
useEffect(() => {
if (subscriptions) {
if (navigator && "serviceWorker" in navigator && subscriptions) {
navigator.serviceWorker.getRegistration().then((r) => {
if (!r) return;

Expand Down Expand Up @@ -182,7 +182,7 @@ const Notifications: React.FC<IProps> = ({ user: _user, subscriptions }) => {
</div>
</div>
);
} else {
} else if (navigator && "serviceWorker" in navigator) {
return (
<div className="col text-center mt-5 vh-50">
<div className="mx-3 my-5">
Expand All @@ -195,6 +195,14 @@ const Notifications: React.FC<IProps> = ({ user: _user, subscriptions }) => {
</div>
</div>
);
} else {
return (
<div className="col text-center mt-5 vh-50">
<div className="mx-3 my-5">
Your current browser window or tab does not support notifications, are you using a private tab?
</div>
</div>
);
}
};

Expand Down
8 changes: 6 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@
resources :notifications, only: %i[index]
namespace :notifications do
resources :push_notifications, only: %i[create]
resources :push_notification_subscriptions, only: %i[create]
post :destroy
resources :push_notification_subscriptions, only: %i[create] do
collection do
post "destroy", to: "push_notification_subscriptions#destroy"
end
end
# post :destroy, to: "push_notification_subscriptions#destroy"
end

# https://github.com/cedarcode/webauthn-rails-demo-app/blob/master/config/routes.rb
Expand Down
11 changes: 4 additions & 7 deletions lib/sway_geocode.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# typed: true

module SwayGeocode

class << self
extend T::Sig

sig { params(sway_locale: SwayLocale, address: Address).returns(T.any(Congress, Local)) }
def build(sway_locale, address)
sway_locale.congress? ? Congress.new(address) : Local.new(sway_locale, address)
sway_locale.congress? ? Congress.new(address) : Local.new(sway_locale, address)
end

class Congress
Expand All @@ -23,7 +22,6 @@ def districts
d = Census::Congress.new(@address).congressional_district
d.present? ? [0, d.to_i] : []
end

end

class Local
Expand All @@ -36,11 +34,11 @@ def initialize(sway_locale, address)

sig { returns(T::Array[Integer]) }
def districts
load_feature
load_feature

return [] unless @feature.present?
return [] unless @feature.present?

[0, T.cast(@feature, RGeo::GeoJSON::Feature).district.to_i]
[0, T.cast(@feature, RGeo::GeoJSON::Feature).district.to_i]
end

private
Expand All @@ -58,5 +56,4 @@ def load_geojson_for_locale
end
end
end

end

0 comments on commit 4d80b4c

Please sign in to comment.