Skip to content

Commit

Permalink
padawan - don't wrap emojis in promise
Browse files Browse the repository at this point in the history
  • Loading branch information
dcordz committed Nov 15, 2024
1 parent a207c6f commit 706e801
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 48 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ gem "shortener"
# https://medium.com/@dejanvu.developer/implementing-web-push-notifications-in-a-ruby-on-rails-application-dcd829e02df0
gem "web-push"

# Logs in a single line
# https://github.com/roidrage/lograge
gem "lograge"

gem "stackprof"
gem "sentry-ruby"
gem "sentry-rails"
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ GEM
language_server-protocol (3.17.0.3)
lint_roller (1.1.0)
logger (1.6.1)
lograge (0.14.0)
actionpack (>= 4)
activesupport (>= 4)
railties (>= 4)
request_store (~> 1.0)
loofah (2.23.1)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand Down Expand Up @@ -306,6 +311,8 @@ GEM
declarative (< 0.1.0)
trailblazer-option (>= 0.1.1, < 0.2.0)
uber (< 0.2.0)
request_store (1.7.0)
rack (>= 1.4)
retriable (3.1.2)
rexml (3.3.9)
rgeo (3.0.1)
Expand Down Expand Up @@ -513,6 +520,7 @@ DEPENDENCIES
google-cloud-storage (~> 1.5)
inertia_rails
jbuilder
lograge
newrelic_rpm
pry
puma (>= 5)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/bill_of_the_week_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class BillOfTheWeekController < ApplicationController
def index
b = T.cast(Bill.of_the_week(sway_locale_id: current_sway_locale.id), T.nilable(T.any(Bill, T::Array[Bill])))
b = T.cast(Bill.of_the_week(sway_locale: current_sway_locale), T.nilable(T.any(Bill, T::Array[Bill])))
b = b.first if b.is_a?(Array)

if b.present?
Expand Down
13 changes: 5 additions & 8 deletions app/frontend/components/bill/BillCreatorSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@ interface IProps {
}

const BillCreatorSummary = forwardRef(({ field }: IProps, ref: React.Ref<string>) => {
const summaryRef = ref as React.MutableRefObject<string>;

const [formikField] = useField(field.name);
const [summary, setSummary] = useState<string>(formikField.value || "");

const handleSetSummary = useCallback(async (_fieldname: string, string: string) => {
withEmojis(string)
.then((emojis) => {
setSummary(emojis);
})
.catch(console.error);
setSummary(withEmojis(string));
}, []);

useEffect(() => {
// @ts-expect-error - weird ref types
ref.current = summary;
}, [ref, summary]);
summaryRef.current = summary;
}, [summaryRef, summary]);

useEffect(() => {
if (formikField.value) {
Expand Down
24 changes: 19 additions & 5 deletions app/frontend/components/bill/creator/BillCreatorFormikForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ const BillCreatorFormikForm = forwardRef(({ setCreatorDirty }: IProps, _summaryR
window.clearTimeout(timeout);
window.clearInterval(interval);
};
}, [formik.values, summaryRef]);

// useEffect has a missing dependency "summaryRef"
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [formik.values, summaryRef.current]);

const positions = useMemo(
() =>
Expand Down Expand Up @@ -130,12 +133,18 @@ const BillCreatorFormikForm = forwardRef(({ setCreatorDirty }: IProps, _summaryR
return (
<>
<FormikForm>
<div className="row my-2">
<div className="col">
<div className="row align-items-center my-2">
<div className="col ">
<span className={countdown >= 0 && countdown <= 3 ? "visible" : "invisible"}>
Store Temporary Bill in: {countdown}
</span>
</div>
<div className="col text-center">
<Button disabled={formik.isSubmitting} variant="primary" type="submit">
<FiSave />
&nbsp;Save
</Button>
</div>
<div className="col text-end">
<Button
variant="outline-secondary"
Expand All @@ -145,7 +154,7 @@ const BillCreatorFormikForm = forwardRef(({ setCreatorDirty }: IProps, _summaryR
router.get(`${ROUTES.billOfTheWeekCreator}?${params.qs}`);
}}
>
Reset To New Bill
Don't Save + Reset New Bill
</Button>
</div>
</div>
Expand Down Expand Up @@ -174,7 +183,12 @@ const BillCreatorFormikForm = forwardRef(({ setCreatorDirty }: IProps, _summaryR
</FormikForm>
<hr />
<div className="bolder h2">Bill of the Week Preview</div>
<BillComponent bill={bill} positions={positions} sponsor={sponsor} legislatorVotes={legislatorVotes} />
<BillComponent
bill={formik.values}
positions={positions}
sponsor={sponsor}
legislatorVotes={legislatorVotes}
/>
</>
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ const BillCreatorOrganization: React.FC<IProps> = ({ swayFieldName, organization

const [summary, setSummary] = useState<string>(formikField?.value?.summary ?? "");
const handleChangeSummary = useCallback(async (_fieldname: string, fieldvalue: string) => {
withEmojis(fieldvalue)
.then((emojis) => {
setSummary(emojis);
})
.catch(console.error);
setSummary(withEmojis(fieldvalue));
}, []);

// Async updating of formik field using a useEffect and local summary state
Expand Down
25 changes: 6 additions & 19 deletions app/frontend/sway_utils/emoji.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
let EMOJIS = {};

const getEmojiFromName = async (name: string): Promise<string> => {
const emojis =
EMOJIS ||
(await fetch("./emojis.json")
.then((r) => r.json())
.then((j) => {
EMOJIS = j;
return j;
})
.catch((e) => {
console.error(e);
return {};
}));
import EMOJIS from "./emojis.json";

const getEmojiFromName = (name: string): string => {
if (name.startsWith(":")) {
return (emojis as Record<string, any>)[name.slice(1, -1)];
return (EMOJIS as Record<string, any>)[name.slice(1, -1)];
}
return (emojis as Record<string, any>)[name];
return (EMOJIS as Record<string, any>)[name];
};

export const withEmojis = async (string: string | undefined | null): Promise<string> => {
export const withEmojis = (string: string | undefined | null): string => {
const words = (string || "").split(" ");
const render = [] as string[];
let i = 0;
Expand All @@ -29,7 +16,7 @@ export const withEmojis = async (string: string | undefined | null): Promise<str
if (!word.startsWith(":")) {
render.push(word);
} else {
const e = await getEmojiFromName(word);
const e = getEmojiFromName(word);
if (e) {
render.push(e);
} else {
Expand Down
8 changes: 4 additions & 4 deletions app/models/bill.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ class Bill < ApplicationRecord

validates :external_id, uniqueness: {scope: :sway_locale_id, allow_nil: true}

sig { params(sway_locale_id: String).returns(Bill) }
def self.of_the_week(sway_locale_id:)
b = Bill.where(scheduled_release_date_utc: Time.zone.today, sway_locale_id:).first
b.presence || Bill.where(sway_locale_id:).order(created_at: :asc).limit(1).first
sig { params(sway_locale: SwayLocale).returns(Bill) }
def self.of_the_week(sway_locale:)
b = Bill.where(scheduled_release_date_utc: Time.zone.today, sway_locale:).first
b.presence || Bill.where(sway_locale:).order(created_at: :asc).limit(1).first
end

class Status
Expand Down
6 changes: 3 additions & 3 deletions app/services/score_updater_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ def update_user_legislator_scores

sig { returns(T::Array[District]) }
def districts
@districts ||= bill_districts.select { |d| user_districts.include?(d) }
T.cast(@districts ||= bill_districts.select { |d| user_districts.include?(d) }, T::Array[District])
end

sig { returns(T::Array[District]) }
def bill_districts
@bill_districts ||= sway_locale.districts
T.cast(@bill_districts ||= sway_locale.districts, T::Array[District])
end

sig { returns(T::Array[District]) }
def user_districts
@user_districts ||= user.districts(sway_locale)
T.cast(@user_districts ||= user.districts(sway_locale), T::Array[District])
end

sig { returns(User) }
Expand Down
12 changes: 12 additions & 0 deletions config/initializers/lograge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Rails.application.configure do
config.lograge.enabled = true

config.lograge.ignore_actions = %w[
Rails::HealthController#show
HealthController#show
::HealthController#show
HealthController
::HealthController
Rails::HealthController
]
end
6 changes: 3 additions & 3 deletions spec/models/bill.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
it "is the bill of the week" do
bill = create(:bill)
bill_2 = create(:bill)
expect(bill_2.sway_locale_id).to eql(bill.sway_locale_id)
expect(Bill.of_the_week(bill_2.sway_locale_id)).to_not eql(bill)
expect(Bill.of_the_week(bill_2.sway_locale_id)).to eql(bill_2)
expect(bill_2.sway_locale).to eql(bill.sway_locale)
expect(Bill.of_the_week(sway_locale: bill_2.sway_locale)).to_not eql(bill)
expect(Bill.of_the_week(sway_locale: bill_2.sway_locale)).to eql(bill_2)
end
end

Expand Down

0 comments on commit 706e801

Please sign in to comment.