Skip to content

Commit

Permalink
Moving date formatting functionality to a separate utility module
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephLai241 committed Jan 14, 2024
1 parent a83a3a8 commit 0297a40
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
43 changes: 8 additions & 35 deletions frontend/src/traits/popup.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Contains the trait and its implementations for creating map marker popups (the `violence`
//! page).

use chrono::{DateTime, NaiveDateTime, Utc};
use chrono_tz::America::Chicago;
use sha2::{Digest, Sha256};
use wasm_bindgen::{JsCast, JsValue};
use web_sys::{
Expand All @@ -12,6 +10,7 @@ use web_sys::{
use crate::{
errors::StaccError,
models::chicago::{ShotData, ViolenceData},
utils::date::format_date,
};

/// This trait enables the data for a given struct to be converted into an HTML popup by
Expand All @@ -20,7 +19,7 @@ pub trait Popup {
/// Neatly display the data found in the struct in a popup.
fn into_popup(&self) -> Result<JsValue, StaccError> {
let hash_id = self.generate_id();
let timestamp = self.format_date();
let timestamp = self.get_date();

let document = gloo_utils::document();

Expand Down Expand Up @@ -93,8 +92,8 @@ pub trait Popup {
/// popup HTML element by referencing this value in the element's `id` attribute.
fn generate_id(&self) -> String;

/// Format the date to a more human-readable format.
fn format_date(&self) -> String;
/// Get the human-readable date of this incident.
fn get_date(&self) -> String;
}

impl Popup for ShotData {
Expand Down Expand Up @@ -169,21 +168,8 @@ impl Popup for ShotData {
hex::encode(hash_result)
}

fn format_date(&self) -> String {
let parsed_datetime = NaiveDateTime::parse_from_str(&self.date, "%Y-%m-%dT%H:%M:%S%.3f")
.ok()
.map(|datetime| DateTime::<Utc>::from_utc(datetime, Utc));

let chicago_datetime = parsed_datetime.map(|datetime| datetime.with_timezone(&Chicago));

let formatted_str =
chicago_datetime.map(|datetime| datetime.format("%Y/%m/%d %H:%M:%S %Z").to_string());

if let Some(formatted_date) = formatted_str {
formatted_date
} else {
self.date.clone()
}
fn get_date(&self) -> String {
format_date(&self.date)
}
}

Expand Down Expand Up @@ -275,20 +261,7 @@ impl Popup for ViolenceData {
hex::encode(hash_result)
}

fn format_date(&self) -> String {
let parsed_datetime = NaiveDateTime::parse_from_str(&self.date, "%Y-%m-%dT%H:%M:%S%.3f")
.ok()
.map(|datetime| DateTime::<Utc>::from_utc(datetime, Utc));

let chicago_datetime = parsed_datetime.map(|datetime| datetime.with_timezone(&Chicago));

let formatted_str =
chicago_datetime.map(|datetime| datetime.format("%Y/%m/%d %H:%M:%S %Z").to_string());

if let Some(formatted_date) = formatted_str {
formatted_date
} else {
self.date.clone()
}
fn get_date(&self) -> String {
format_date(&self.date)
}
}
22 changes: 22 additions & 0 deletions frontend/src/utils/date.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//! Contains miscellaneous utilities for formatting dates.

use chrono::{DateTime, NaiveDateTime, Utc};
use chrono_tz::America::Chicago;

/// Format a raw date with the format `%Y-%m-%dT%H:%M:%S%.3f` into an more human-readable format.
pub fn format_date(raw_date: &str) -> String {
let parsed_datetime = NaiveDateTime::parse_from_str(raw_date, "%Y-%m-%dT%H:%M:%S%.3f")
.ok()
.map(|datetime| DateTime::<Utc>::from_utc(datetime, Utc));

let chicago_datetime = parsed_datetime.map(|datetime| datetime.with_timezone(&Chicago));

let formatted_str =
chicago_datetime.map(|datetime| datetime.format("%Y/%m/%d %H:%M:%S %Z").to_string());

if let Some(formatted_date) = formatted_str {
formatted_date
} else {
raw_date.to_string()
}
}
1 change: 1 addition & 0 deletions frontend/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Contains miscellaneous utilities for the site.

pub mod background;
pub mod date;
pub mod open_graph;

0 comments on commit 0297a40

Please sign in to comment.