feat: can_stale and periodicity
This commit is contained in:
parent
3ffdf8f0d7
commit
b361c1ab58
4 changed files with 108 additions and 17 deletions
|
|
@ -132,6 +132,14 @@ mod get {
|
|||
div { (name) }
|
||||
}
|
||||
}
|
||||
@if contact.status() != "normal" {
|
||||
label { "status" }
|
||||
div { (contact.status()) }
|
||||
}
|
||||
@if contact.status() == "normal" && contact.periodicity.is_positive() {
|
||||
label { "periodicity" }
|
||||
div { (format!("{:#}", contact.periodicity)) }
|
||||
}
|
||||
@if let Some(bday) = &contact.birthday {
|
||||
label { "birthday" }
|
||||
div {
|
||||
|
|
@ -235,7 +243,7 @@ mod get {
|
|||
div #error;
|
||||
}
|
||||
|
||||
div #fields {
|
||||
#fields x-data=(json!({ "status": contact.status() })){
|
||||
label { @if contact.names.len() > 1 { "names" } @else { "name" }}
|
||||
div #names x-data=(format!("{{ names: {:?}, new_name: '' }}", &contact.names)) {
|
||||
template x-for="(name, idx) in names" {
|
||||
|
|
@ -251,6 +259,19 @@ mod get {
|
|||
input type="button" value="Add" x-on:click="names.push(new_name); new_name = ''";
|
||||
}
|
||||
}
|
||||
label { "status" }
|
||||
div {
|
||||
select name="status" x-model=("status") {
|
||||
option value="normal" { "Normal" }
|
||||
option value="permanent" { "Cannot go stale" }
|
||||
option value="inactive" { "Inactive" }
|
||||
}
|
||||
}
|
||||
label x-show="status === 'normal'" { "minimum stale time" }
|
||||
div x-show="status === 'normal'"{
|
||||
input name="periodicity" value=(format!("{:#}", contact.periodicity));
|
||||
span .hint { code { "[0-9]+ (yr|mo|wk|day|h|m|s)" } "(" a href="https://docs.rs/jiff/latest/jiff/struct.Span.html#parsing-and-printing" { "details" } ")" }
|
||||
}
|
||||
label { "birthday" }
|
||||
div {
|
||||
input name="birthday" value=(contact.birthday.clone().map_or("".to_string(), |b| b.serialize()));
|
||||
|
|
@ -326,6 +347,8 @@ mod put {
|
|||
#[derive(Deserialize)]
|
||||
pub struct PutContact {
|
||||
name: Option<Vec<String>>,
|
||||
status: String,
|
||||
periodicity: Option<String>,
|
||||
birthday: String,
|
||||
manually_freshened_at: String,
|
||||
lives_with: String,
|
||||
|
|
@ -365,6 +388,10 @@ mod put {
|
|||
)
|
||||
};
|
||||
|
||||
let active: bool = payload.status != "inactive";
|
||||
let can_stale: bool = payload.status != "permanent";
|
||||
let periodicity: String = payload.periodicity.unwrap_or("P0D".to_string());
|
||||
|
||||
let text_body = if payload.text_body.is_empty() {
|
||||
None
|
||||
} else {
|
||||
|
|
@ -377,13 +404,19 @@ mod put {
|
|||
|
||||
sqlx::query!(
|
||||
"update contacts set
|
||||
(birthday, manually_freshened_at, lives_with, text_body) =
|
||||
($1, $2, $3, $4)
|
||||
where id = $5",
|
||||
(
|
||||
birthday, manually_freshened_at, lives_with, text_body,
|
||||
active, can_stale, periodicity
|
||||
) =
|
||||
(?, ?, ?, ?, ?, ?, ?)
|
||||
where id = ?",
|
||||
birthday,
|
||||
manually_freshened_at,
|
||||
payload.lives_with,
|
||||
text_body,
|
||||
active,
|
||||
can_stale,
|
||||
periodicity,
|
||||
contact_id
|
||||
)
|
||||
.execute(pool)
|
||||
|
|
|
|||
|
|
@ -134,7 +134,11 @@ pub mod get {
|
|||
let mut freshens: Vec<ContactFreshness> = contacts
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|contact| {
|
||||
.filter_map(|contact| {
|
||||
if !contact.can_stale || !contact.active {
|
||||
return None;
|
||||
}
|
||||
|
||||
let zero = jiff::civil::Date::ZERO;
|
||||
let fresh_date = std::cmp::max(
|
||||
contact
|
||||
|
|
@ -144,17 +148,17 @@ pub mod get {
|
|||
contact.last_mention_date.unwrap_or(zero),
|
||||
);
|
||||
if fresh_date == zero {
|
||||
ContactFreshness {
|
||||
Some(ContactFreshness {
|
||||
contact_id: contact.id,
|
||||
display: contact.display_name(),
|
||||
fresh_date,
|
||||
fresh_str: "never".to_string(),
|
||||
elapsed_str: "".to_string(),
|
||||
}
|
||||
})
|
||||
} else {
|
||||
let utc = TimeZone::UTC;
|
||||
let todate = Timestamp::now().to_zoned(utc.clone()).date();
|
||||
let duration = todate
|
||||
let elapsed = todate
|
||||
.since(&fresh_date.to_zoned(utc).unwrap())
|
||||
.unwrap()
|
||||
.round(
|
||||
|
|
@ -165,19 +169,25 @@ pub mod get {
|
|||
)
|
||||
.unwrap();
|
||||
|
||||
let elapsed_str = if duration.is_zero() {
|
||||
if let Some(cmp) = elapsed.compare((contact.periodicity, todate)).ok() {
|
||||
if cmp == std::cmp::Ordering::Less {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
let elapsed_str = if elapsed.is_zero() {
|
||||
"today".to_string()
|
||||
} else {
|
||||
format!("{:#}", duration)
|
||||
format!("{:#}", elapsed)
|
||||
};
|
||||
|
||||
ContactFreshness {
|
||||
Some(ContactFreshness {
|
||||
contact_id: contact.id,
|
||||
display: contact.display_name(),
|
||||
fresh_date,
|
||||
fresh_str: fresh_date.to_string(),
|
||||
elapsed_str,
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue