Fix bug where clearing ticks on one day would clear all ticks for the track on all days #14
|
@ -15,7 +15,7 @@ const className = computed(() => isSet.value ? "button is-rounded is-info" : "bu
|
|||
|
||||
async function toggle() {
|
||||
if (isSet.value) {
|
||||
await state.taskMarkedIncomplete(props.track)
|
||||
await state.taskMarkedIncomplete(props.track, props.date)
|
||||
} else
|
||||
await state.taskCompleted(props.track, props.date)
|
||||
}
|
||||
|
|
|
@ -8,6 +8,15 @@ enum State {
|
|||
Fetched,
|
||||
}
|
||||
|
||||
function dateQuery(date: Date): URLSearchParams {
|
||||
let query = new URLSearchParams()
|
||||
query.set("year", date.getUTCFullYear().toString())
|
||||
query.set("month", (date.getUTCMonth() + 1).toString())
|
||||
// good thing I still had this ^^^^^^^^^^^^^^ in mind when I wrote this 😬
|
||||
query.set("day", date.getUTCDate().toString())
|
||||
return query
|
||||
}
|
||||
|
||||
export const state = reactive({
|
||||
tracks: new Array<Track>,
|
||||
state: State.Unfetched,
|
||||
|
@ -62,11 +71,7 @@ export const state = reactive({
|
|||
this.state = State.Fetched
|
||||
},
|
||||
async taskCompleted(track: Track, date: Date): Promise<Tick> {
|
||||
let query = new URLSearchParams()
|
||||
query.append("year", date.getUTCFullYear().toString())
|
||||
query.append("month", (date.getUTCMonth() + 1).toString())
|
||||
// good thing I still had this ^^^^^^^^^^^^^^ in mind when I wrote this 😬
|
||||
query.append("day", date.getUTCDate().toString())
|
||||
const query = dateQuery(date)
|
||||
const response: Response = await fetch(`/api/v1/tracks/${track.id}/ticked?${query.toString()}`, { method: "PATCH" })
|
||||
const body = await response.text()
|
||||
if (!response.ok) {
|
||||
|
@ -75,8 +80,9 @@ export const state = reactive({
|
|||
}
|
||||
return JSON.parse(body)
|
||||
},
|
||||
async taskMarkedIncomplete(track: Track) {
|
||||
const { ok, status, statusText } = await fetch(`/api/v1/tracks/${track.id}/all-ticks`, { method: 'DELETE' })
|
||||
async taskMarkedIncomplete(track: Track, date: Date) {
|
||||
const query = dateQuery(date)
|
||||
const { ok, status, statusText } = await fetch(`/api/v1/tracks/${track.id}/all-ticks?${query.toString()}`, { method: 'DELETE' })
|
||||
if (!ok)
|
||||
error(`error deleting ticks for ${track.id}: ${statusText} (${status})`)
|
||||
}
|
||||
|
|
|
@ -87,6 +87,7 @@ pub(crate) fn start_server(db: DatabaseConnection) -> Rocket<Build> {
|
|||
ticked,
|
||||
ticked_on_date,
|
||||
clear_all_ticks,
|
||||
clear_all_ticks_on_day,
|
||||
],
|
||||
)
|
||||
.mount(
|
||||
|
|
|
@ -158,3 +158,28 @@ pub(super) async fn clear_all_ticks(
|
|||
}
|
||||
Ok(Right(Json(ticks)))
|
||||
}
|
||||
|
||||
#[delete("/<id>/all-ticks?<year>&<month>&<day>")]
|
||||
pub(super) async fn clear_all_ticks_on_day(
|
||||
db: &State<DatabaseConnection>,
|
||||
tx: &State<Sender<Update>>,
|
||||
id: i32,
|
||||
year: i32,
|
||||
month: u32,
|
||||
day: u32,
|
||||
) -> ApiResult<Json<Vec<ticks::Model>>> {
|
||||
let db = db as &DatabaseConnection;
|
||||
let ticks = Ticks::find()
|
||||
.filter(ticks::Column::TrackId.eq(id))
|
||||
.filter(ticks::Column::Year.eq(year))
|
||||
.filter(ticks::Column::Month.eq(month))
|
||||
.filter(ticks::Column::Day.eq(day))
|
||||
.all(db)
|
||||
.await
|
||||
.map_err(Error::from)?;
|
||||
for tick in ticks.clone() {
|
||||
tick.clone().delete(db).await.map_err(Error::from)?;
|
||||
Update::tick_cancelled(tick).send(&tx)?;
|
||||
}
|
||||
Ok(Json(ticks))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue