Fix bug where clearing ticks on one day would clear all ticks for the track on all days
This commit is contained in:
parent
9b876511d9
commit
bfe0537750
|
@ -15,7 +15,7 @@ const className = computed(() => isSet.value ? "button is-rounded is-info" : "bu
|
||||||
|
|
||||||
async function toggle() {
|
async function toggle() {
|
||||||
if (isSet.value) {
|
if (isSet.value) {
|
||||||
await state.taskMarkedIncomplete(props.track)
|
await state.taskMarkedIncomplete(props.track, props.date)
|
||||||
} else
|
} else
|
||||||
await state.taskCompleted(props.track, props.date)
|
await state.taskCompleted(props.track, props.date)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,15 @@ enum State {
|
||||||
Fetched,
|
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({
|
export const state = reactive({
|
||||||
tracks: new Array<Track>,
|
tracks: new Array<Track>,
|
||||||
state: State.Unfetched,
|
state: State.Unfetched,
|
||||||
|
@ -62,11 +71,7 @@ export const state = reactive({
|
||||||
this.state = State.Fetched
|
this.state = State.Fetched
|
||||||
},
|
},
|
||||||
async taskCompleted(track: Track, date: Date): Promise<Tick> {
|
async taskCompleted(track: Track, date: Date): Promise<Tick> {
|
||||||
let query = new URLSearchParams()
|
const query = dateQuery(date)
|
||||||
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 response: Response = await fetch(`/api/v1/tracks/${track.id}/ticked?${query.toString()}`, { method: "PATCH" })
|
const response: Response = await fetch(`/api/v1/tracks/${track.id}/ticked?${query.toString()}`, { method: "PATCH" })
|
||||||
const body = await response.text()
|
const body = await response.text()
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
@ -75,8 +80,9 @@ export const state = reactive({
|
||||||
}
|
}
|
||||||
return JSON.parse(body)
|
return JSON.parse(body)
|
||||||
},
|
},
|
||||||
async taskMarkedIncomplete(track: Track) {
|
async taskMarkedIncomplete(track: Track, date: Date) {
|
||||||
const { ok, status, statusText } = await fetch(`/api/v1/tracks/${track.id}/all-ticks`, { method: 'DELETE' })
|
const query = dateQuery(date)
|
||||||
|
const { ok, status, statusText } = await fetch(`/api/v1/tracks/${track.id}/all-ticks?${query.toString()}`, { method: 'DELETE' })
|
||||||
if (!ok)
|
if (!ok)
|
||||||
error(`error deleting ticks for ${track.id}: ${statusText} (${status})`)
|
error(`error deleting ticks for ${track.id}: ${statusText} (${status})`)
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,6 +87,7 @@ pub(crate) fn start_server(db: DatabaseConnection) -> Rocket<Build> {
|
||||||
ticked,
|
ticked,
|
||||||
ticked_on_date,
|
ticked_on_date,
|
||||||
clear_all_ticks,
|
clear_all_ticks,
|
||||||
|
clear_all_ticks_on_day,
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
.mount(
|
.mount(
|
||||||
|
|
|
@ -158,3 +158,28 @@ pub(super) async fn clear_all_ticks(
|
||||||
}
|
}
|
||||||
Ok(Right(Json(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