Skip to content

Commit

Permalink
padawan - use iso dates
Browse files Browse the repository at this point in the history
  • Loading branch information
dcordz committed Jan 23, 2025
1 parent 1166f94 commit 0e93924
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 46 deletions.
6 changes: 2 additions & 4 deletions app/controllers/bill_of_the_week_schedule_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ class BillOfTheWeekScheduleController < ApplicationController
before_action :set_bill, only: %i[update]

def update
new_release = bill_of_the_week_schedule_params[:scheduled_release_date_utc]

if @bill.present?
if @bill.update(scheduled_release_date_utc: new_release.present? ? Date.strptime(new_release, "%m/%d/%Y") : nil)
flash[:notice] = new_release.blank? ? "Bill - #{@bill.title} - removed from schedule." : "Added bill - #{@bill.title} - to schedule."
if @bill.update(scheduled_release_date_utc: bill_of_the_week_schedule_params[:scheduled_release_date_utc])
flash[:notice] = @bill.scheduled_release_date_utc.blank? ? "Bill - #{@bill.title} - removed from schedule." : "Added bill - #{@bill.title} - to schedule."
route_component(edit_bill_path(@bill.id, tabKey: bill_of_the_week_schedule_params[:tab_key]))
else
flash[:alert] = "Failed to update bill schedule."
Expand Down
11 changes: 6 additions & 5 deletions app/controllers/bills_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,21 @@ def edit

# POST /bills or /bills.json
def create
b = Bill.find_or_initialize_by(
b = Bill.find_by(
external_id: bill_params[:external_id],
sway_locale_id: bill_params[:sway_locale_id] || current_sway_locale&.id
)

b.assign_attributes(**bill_params.except(*vote_params))
if b.nil?
b = Bill.new(**bill_params.except(*vote_params))
end

b.legislator = Legislator.find(bill_params[:legislator_id])

if b.save
create_vote(b)

redirect_to edit_bill_path(b.id, {saved: "Bill Created", event_key: "legislator_votes"}), inertia: {errors: {}}
route_component(edit_bill_path(b.id, {saved: "Bill Created", event_key: "legislator_votes"}))
else
Rails.logger.error("Error saving bill - #{b.errors.flat_map(&:message).join(" | ")}")
redirect_to new_bill_path({event_key: "bill"}), inertia: {
errors: b.errors
}
Expand Down
48 changes: 24 additions & 24 deletions app/frontend/components/admin/creator/BillCreatorBill.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import { useTempStorage } from "app/frontend/components/admin/creator/hooks/useT
import { IApiBillCreator } from "app/frontend/components/admin/creator/types";
import { BILL_INPUTS } from "app/frontend/components/bill/creator/inputs";
import FormContext from "app/frontend/components/contexts/FormContext";
import { useAxiosPost } from "app/frontend/hooks/useAxios";
import { useLocale } from "app/frontend/hooks/useLocales";
import { useSearchParams } from "app/frontend/hooks/useSearchParams";
import { notify, SWAY_STORAGE } from "app/frontend/sway_utils";
import { handleError, notify, SWAY_STORAGE } from "app/frontend/sway_utils";
import { useCallback, useEffect, useMemo, useRef } from "react";
import { sway } from "sway";
import { useInertiaForm } from "use-inertia-form";
Expand All @@ -30,6 +31,10 @@ const BillCreatorBill = ({ setCreatorDirty }: IProps) => {
const form = useInertiaForm<IApiBillCreator>(initialValues);
const summaryRef = useRef<string>("");

const { post } = useAxiosPost<IApiBillCreator>(initialValues.id ? `/bills/${initialValues.id}` : "/bills", {
method: initialValues.id ? "put" : "post",
});

const {
entries: { saved },
remove,
Expand All @@ -51,24 +56,22 @@ const BillCreatorBill = ({ setCreatorDirty }: IProps) => {
(e) => {
e.preventDefault();

form.transform((data) => {
return {
...data,
summary: summaryRef.current,
status: (typeof form.data.status === "string"
? form.data.status
: form.data.status?.value) as sway.TBillStatus,
category: (typeof form.data.category === "string"
? form.data.category
: form.data.category?.value) as sway.TBillCategory,
chamber: ((typeof form.data.chamber === "string" ? form.data.chamber : form.data.chamber?.value) ||
"council") as sway.TBillChamber,
legislator_id: (typeof form.data.legislator_id === "number"
? form.data.legislator_id
: form.data.legislator_id?.value) as number,
sway_locale_id: swayLocale.id,
};
});
const body = {
...form.data,
summary: summaryRef.current,
status: (typeof form.data.status === "string"
? form.data.status
: form.data.status?.value) as sway.TBillStatus,
category: (typeof form.data.category === "string"
? form.data.category
: form.data.category?.value) as sway.TBillCategory,
chamber: ((typeof form.data.chamber === "string" ? form.data.chamber : form.data.chamber?.value) ||
"council") as sway.TBillChamber,
legislator_id: (typeof form.data.legislator_id === "number"
? form.data.legislator_id
: form.data.legislator_id?.value) as number,
sway_locale_id: swayLocale.id,
};

if (!form.data.status) {
notify({
Expand Down Expand Up @@ -99,12 +102,9 @@ const BillCreatorBill = ({ setCreatorDirty }: IProps) => {
return;
}

const caller = initialValues.id ? form.put : form.post;
const route = initialValues.id ? `/bills/${initialValues.id}` : "/bills";

caller(route, { preserveScroll: true, async: true });
post(body).catch(handleError);
},
[form, initialValues.id, swayLocale.id],
[form, post, swayLocale.id],
);

const toStore = useMemo(
Expand Down
2 changes: 1 addition & 1 deletion app/frontend/components/admin/creator/fields/DateField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const DateField = <T,>({ swayField, fieldGroupLength, onBlur }: IFieldProps<T>)
(changed: Date | null) => {
if (changed && isValid(changed)) {
try {
setData(swayField.name as KeyOf<IApiBillCreator>, changed.toLocaleDateString("en-US"));
setData(swayField.name as KeyOf<IApiBillCreator>, changed.toISOString());
} catch (error) {
notify({
level: "error",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const BillScheduleCalendar: React.FC<IBillScheduleCalendarProps> = ({
}

const release = new Date(scheduledReleaseDateUtc);
release.setMinutes(release.getMinutes() + release.getTimezoneOffset());
return getMonth(release) === month && getYear(release) === year;
})
.map(({ scheduledReleaseDateUtc }) => getDate(scheduledReleaseDateUtc)),
Expand Down Expand Up @@ -60,7 +61,7 @@ const BillScheduleCalendar: React.FC<IBillScheduleCalendarProps> = ({
b &&
(bill?.id !== b.id || ((selectedBill.value as number) > 0 && bill?.scheduledReleaseDateUtc))
) {
handleSelectBill(b, { [BILL_SCHEDULER_PARAMS_KEY]: newValue.toLocaleDateString("en-US") });
handleSelectBill(b, { [BILL_SCHEDULER_PARAMS_KEY]: newValue.toISOString() });
}
}
}}
Expand All @@ -69,7 +70,11 @@ const BillScheduleCalendar: React.FC<IBillScheduleCalendarProps> = ({
<BillScheduleCalendarDay
{...props}
bill={bills.find((b) => {
if (!b.scheduledReleaseDateUtc) {
return false;
}
const d = new Date(b.scheduledReleaseDateUtc);
d.setMinutes(d.getMinutes() + d.getTimezoneOffset());
return getDate(d) === getDate(props.day) && getYear(d) === getYear(props.day);
})}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export default function BillScheduleCalendarDay(
) {
const { bill, highlightedDays = [], day, outsideCurrentMonth, ...other } = props;

day.setMinutes(day.getMinutes() + day.getTimezoneOffset());

const isSelected = !props.outsideCurrentMonth && highlightedDays.indexOf(getDate(day)) >= 0;

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const BillScheduleCalendarSelectedBill: React.FC<Omit<IBillScheduleCalendarProps
});
} else {
put({
scheduled_release_date_utc: newScheduleDate?.toLocaleDateString?.("en-US") || null,
scheduled_release_date_utc: newScheduleDate?.toISOString() || null,
bill_id: selectedBill.value as number,
tab_key: ETab.Schedule,
})
Expand Down
3 changes: 2 additions & 1 deletion app/frontend/hooks/useAxios.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { router } from "@inertiajs/react";
import axios, { AxiosError, AxiosResponse } from "axios";
import { useCallback, useEffect, useMemo, useState } from "react";
import { sway } from "sway";
Expand Down Expand Up @@ -54,7 +55,7 @@ const handleRoutedResponse = (result: IRoutableResponse) => {
// localStorage.setItem("@sway/phone", removeNonDigits(result.phone));
}
if (result.route) {
// router.visit(result.route);
router.visit(result.route);
}
return result;
};
Expand Down
3 changes: 1 addition & 2 deletions app/frontend/sway_utils/datetimes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ export const getDateFromString = (date?: string) => {
}
};
export const formatDate = (datetime: string): string => {
// return new Date(datetime).toLocaleDateString("en-US");
return new Date(datetime).toLocaleDateString("en-US");
return new Date(datetime).toISOString();
};
3 changes: 1 addition & 2 deletions app/models/bill.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ def render(current_user, current_sway_locale)

sig { returns(Jbuilder) }
def to_builder
# sig {params(b: Bill).returns(T::Hash[String, String])}
Jbuilder.new do |b|
b.id id
b.external_id external_id
Expand Down Expand Up @@ -171,7 +170,7 @@ def vote_date_time_utc
end
end

# after initialize
# before_validation

def determine_level
return if level.present? || sway_locale.nil?
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/bill_of_the_week_schedule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def get_params(sway_locale, partial_bill: {}, partial_sponsor: {}, partial_vote:
bill_of_the_week_schedule: {
bill_id: bill.id,
tab_key: nil,
scheduled_release_date_utc: Time.zone.today.strftime("%m/%d/%Y")
scheduled_release_date_utc: Time.zone.today.to_s
}
}

Expand Down
9 changes: 5 additions & 4 deletions spec/requests/bills_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def get_params(sway_locale, partial_bill: {}, partial_sponsor: {}, partial_vote:
title: bill.title,
link: bill.link,
chamber: bill.chamber,
introduced_date_time_utc: bill.introduced_date_time_utc,
house_vote_date_time_utc: bill.house_vote_date_time_utc,
senate_vote_date_time_utc: bill.senate_vote_date_time_utc,
introduced_date_time_utc: bill.introduced_date_time_utc&.to_s,
house_vote_date_time_utc: bill.house_vote_date_time_utc&.to_s,
senate_vote_date_time_utc: bill.senate_vote_date_time_utc&.to_s,
category: bill.category,
level: bill.level,
summary: bill.summary,
Expand Down Expand Up @@ -112,7 +112,8 @@ def get_params(sway_locale, partial_bill: {}, partial_sponsor: {}, partial_vote:

post "/bills", params: params

expect(response).to have_http_status(302)
expect(response).to have_http_status(200)

expect(Bill.count).to eql(count_bills + 1)
expect(Bill.last.external_id).to eql(bill.external_id)
expect(Vote.count).to eql(count_votes + 1)
Expand Down

0 comments on commit 0e93924

Please sign in to comment.