✨ TripDay creation: use date
This commit is contained in:
parent
96d0fadf23
commit
81c1b2a010
@ -293,7 +293,7 @@ def create_tripday(
|
|||||||
if db_trip.archived:
|
if db_trip.archived:
|
||||||
raise HTTPException(status_code=400, detail="Bad request")
|
raise HTTPException(status_code=400, detail="Bad request")
|
||||||
|
|
||||||
new_day = TripDay(label=td.label, trip_id=trip_id)
|
new_day = TripDay(label=td.label, dt=td.dt, trip_id=trip_id)
|
||||||
|
|
||||||
session.add(new_day)
|
session.add(new_day)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import { Component, OnInit } from '@angular/core';
|
|||||||
import { ApiService } from '../../services/api.service';
|
import { ApiService } from '../../services/api.service';
|
||||||
import { ButtonModule } from 'primeng/button';
|
import { ButtonModule } from 'primeng/button';
|
||||||
import { SkeletonModule } from 'primeng/skeleton';
|
import { SkeletonModule } from 'primeng/skeleton';
|
||||||
import { TripBase, TripInvitation } from '../../types/trip';
|
import { TripBase, TripInvitation, TripDay } from '../../types/trip';
|
||||||
import { DialogService, DynamicDialogRef } from 'primeng/dynamicdialog';
|
import { DialogService, DynamicDialogRef } from 'primeng/dynamicdialog';
|
||||||
import { TripCreateModalComponent } from '../../modals/trip-create-modal/trip-create-modal.component';
|
import { TripCreateModalComponent } from '../../modals/trip-create-modal/trip-create-modal.component';
|
||||||
import { Router } from '@angular/router';
|
import { Router } from '@angular/router';
|
||||||
@ -81,8 +81,8 @@ export class TripsComponent implements OnInit {
|
|||||||
let dayCount = 0;
|
let dayCount = 0;
|
||||||
|
|
||||||
if (trip.daterange && trip.daterange.length === 2) {
|
if (trip.daterange && trip.daterange.length === 2) {
|
||||||
const obs$ = this.generateDaysLabel(trip.daterange).map((label) =>
|
const obs$ = this.generateTripDays(trip.daterange).map((td) =>
|
||||||
this.apiService.postTripDay({ id: -1, label: label, items: [] }, new_trip.id),
|
this.apiService.postTripDay({ id: -1, label: td.label!, dt: td.dt, items: [] }, new_trip.id),
|
||||||
);
|
);
|
||||||
dayCount = obs$.length;
|
dayCount = obs$.length;
|
||||||
forkJoin(obs$).pipe(take(1)).subscribe();
|
forkJoin(obs$).pipe(take(1)).subscribe();
|
||||||
@ -106,26 +106,22 @@ export class TripsComponent implements OnInit {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
generateDaysLabel(daterange: Date[]): string[] {
|
generateTripDays(daterange: Date[]): Partial<TripDay>[] {
|
||||||
const from = daterange[0];
|
const [from, to] = daterange;
|
||||||
const to = daterange[1];
|
const tripDays: Partial<TripDay>[] = [];
|
||||||
const labels: string[] = [];
|
|
||||||
const sameMonth = from.getFullYear() === to.getFullYear() && from.getMonth() === to.getMonth();
|
const sameMonth = from.getFullYear() === to.getFullYear() && from.getMonth() === to.getMonth();
|
||||||
|
|
||||||
const months = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'Jun.', 'Jul.', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.'];
|
const months = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'Jun.', 'Jul.', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.'];
|
||||||
|
|
||||||
const current = new Date(from);
|
const current = new Date(from);
|
||||||
while (current <= to) {
|
while (current <= to) {
|
||||||
let label = '';
|
const day = current.getDate().toString().padStart(2, '0');
|
||||||
if (sameMonth) {
|
const month = current.getMonth();
|
||||||
label = `${current.getDate().toString().padStart(2, '0')} ${months[current.getMonth()]}`;
|
const label = sameMonth ? `${day} ${months[month]}` : `${(month + 1).toString().padStart(2, '0')}/${day}`;
|
||||||
} else {
|
tripDays.push({ label, dt: current.toISOString().split('T')[0] });
|
||||||
label = `${(current.getMonth() + 1).toString().padStart(2, '0')}/${current.getDate().toString().padStart(2, '0')}`;
|
|
||||||
}
|
|
||||||
labels.push(label);
|
|
||||||
current.setDate(current.getDate() + 1);
|
current.setDate(current.getDate() + 1);
|
||||||
}
|
}
|
||||||
return labels;
|
return tripDays;
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleInvitations() {
|
toggleInvitations() {
|
||||||
|
|||||||
@ -1,9 +1,15 @@
|
|||||||
<section>
|
<section>
|
||||||
<div [formGroup]="dayForm">
|
<div [formGroup]="dayForm" class="grid md:grid-cols-3 gap-4">
|
||||||
<p-floatlabel variant="in">
|
<p-floatlabel variant="in" class="md:col-span-2">
|
||||||
<input id="label" formControlName="label" pInputText fluid (keyup.enter)="closeDialog()" />
|
<input id="label" formControlName="label" autofocus pInputText fluid (keyup.enter)="closeDialog()" />
|
||||||
<label for="label">Label</label>
|
<label for="label">Label</label>
|
||||||
</p-floatlabel>
|
</p-floatlabel>
|
||||||
|
|
||||||
|
<p-floatlabel variant="in">
|
||||||
|
<p-datepicker id="date" formControlName="dt" [iconDisplay]="'input'" [showIcon]="true" appendTo="body" fluid
|
||||||
|
(keyup.enter)="closeDialog()" />
|
||||||
|
<label for="date">Date</label>
|
||||||
|
</p-floatlabel>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="mt-4 text-right">
|
<div class="mt-4 text-right">
|
||||||
|
|||||||
@ -4,18 +4,17 @@ import { ButtonModule } from 'primeng/button';
|
|||||||
import { DynamicDialogConfig, DynamicDialogRef } from 'primeng/dynamicdialog';
|
import { DynamicDialogConfig, DynamicDialogRef } from 'primeng/dynamicdialog';
|
||||||
import { FloatLabelModule } from 'primeng/floatlabel';
|
import { FloatLabelModule } from 'primeng/floatlabel';
|
||||||
import { InputTextModule } from 'primeng/inputtext';
|
import { InputTextModule } from 'primeng/inputtext';
|
||||||
import { TripDay } from '../../types/trip';
|
import { DatePickerModule } from 'primeng/datepicker';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-trip-create-day-modal',
|
selector: 'app-trip-create-day-modal',
|
||||||
imports: [FloatLabelModule, InputTextModule, ButtonModule, ReactiveFormsModule],
|
imports: [FloatLabelModule, InputTextModule, DatePickerModule, ButtonModule, ReactiveFormsModule],
|
||||||
standalone: true,
|
standalone: true,
|
||||||
templateUrl: './trip-create-day-modal.component.html',
|
templateUrl: './trip-create-day-modal.component.html',
|
||||||
styleUrl: './trip-create-day-modal.component.scss',
|
styleUrl: './trip-create-day-modal.component.scss',
|
||||||
})
|
})
|
||||||
export class TripCreateDayModalComponent {
|
export class TripCreateDayModalComponent {
|
||||||
dayForm: FormGroup;
|
dayForm: FormGroup;
|
||||||
days: TripDay[] = [];
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private ref: DynamicDialogRef,
|
private ref: DynamicDialogRef,
|
||||||
@ -24,12 +23,15 @@ export class TripCreateDayModalComponent {
|
|||||||
) {
|
) {
|
||||||
this.dayForm = this.fb.group({
|
this.dayForm = this.fb.group({
|
||||||
id: -1,
|
id: -1,
|
||||||
|
dt: null,
|
||||||
label: ['', Validators.required],
|
label: ['', Validators.required],
|
||||||
});
|
});
|
||||||
|
|
||||||
if (this.config.data) {
|
if (this.config.data) {
|
||||||
if (this.config.data.day) this.dayForm.patchValue(this.config.data.day);
|
this.dayForm.patchValue({
|
||||||
this.days.push(...this.config.data.days);
|
...this.config.data,
|
||||||
|
dt: this.config.data.dt ? new Date(this.config.data.dt) : null,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,6 +39,7 @@ export class TripCreateDayModalComponent {
|
|||||||
// Normalize data for API POST
|
// Normalize data for API POST
|
||||||
let ret = this.dayForm.value;
|
let ret = this.dayForm.value;
|
||||||
if (!ret['label']) return;
|
if (!ret['label']) return;
|
||||||
|
if (ret['dt']) ret['dt'] = ret['dt'].toISOString().split('T')[0];
|
||||||
this.ref.close(ret);
|
this.ref.close(ret);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user