Minor optimizations

This commit is contained in:
itskovacs 2025-08-06 20:23:29 +02:00
parent 999eb1426c
commit abbfdd9d21
4 changed files with 90 additions and 79 deletions

View File

@ -11,6 +11,7 @@ import { FloatLabelModule } from "primeng/floatlabel";
import { InputTextModule } from "primeng/inputtext"; import { InputTextModule } from "primeng/inputtext";
import { FocusTrapModule } from "primeng/focustrap"; import { FocusTrapModule } from "primeng/focustrap";
import { ColorPickerModule } from "primeng/colorpicker"; import { ColorPickerModule } from "primeng/colorpicker";
import { Category } from "../../types/poi";
@Component({ @Component({
selector: "app-category-create-modal", selector: "app-category-create-modal",
@ -28,7 +29,6 @@ import { ColorPickerModule } from "primeng/colorpicker";
}) })
export class CategoryCreateModalComponent { export class CategoryCreateModalComponent {
categoryForm: FormGroup; categoryForm: FormGroup;
previous_image: string | null = null;
updatedImage = false; updatedImage = false;
constructor( constructor(
@ -51,10 +51,8 @@ export class CategoryCreateModalComponent {
image: null, image: null,
}); });
if (this.config.data) { const patchValue = this.config.data?.category as Category | undefined;
let patchValue = this.config.data.category; if (patchValue) this.categoryForm.patchValue(patchValue);
this.categoryForm.patchValue(patchValue);
}
} }
closeDialog() { closeDialog() {
@ -67,7 +65,7 @@ export class CategoryCreateModalComponent {
onFileSelected(event: Event) { onFileSelected(event: Event) {
const input = event.target as HTMLInputElement; const input = event.target as HTMLInputElement;
if (input.files && input.files.length > 0) { if (input.files?.length) {
const file = input.files[0]; const file = input.files[0];
const reader = new FileReader(); const reader = new FileReader();
@ -83,5 +81,6 @@ export class CategoryCreateModalComponent {
clearImage() { clearImage() {
this.categoryForm.get("image")?.setValue(null); this.categoryForm.get("image")?.setValue(null);
this.updatedImage = false;
} }
} }

View File

@ -1,4 +1,5 @@
import { Component } from "@angular/core"; import { Component } from "@angular/core";
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
import { import {
FormBuilder, FormBuilder,
FormGroup, FormGroup,
@ -97,12 +98,13 @@ export class PlaceCreateModalComponent {
gpx: null, gpx: null,
}); });
if (this.config.data) { const patchValue = this.config.data?.place as Place | undefined;
let patchValue: Place = this.config.data.place; if (patchValue) this.placeForm.patchValue(patchValue);
this.placeForm.patchValue(patchValue);
}
this.placeForm.get("place")?.valueChanges.subscribe({ this.placeForm
.get("place")
?.valueChanges.pipe(takeUntilDestroyed())
.subscribe({
next: (value: string) => { next: (value: string) => {
const isGoogleMapsURL = const isGoogleMapsURL =
/^(https?:\/\/)?(www\.)?google\.[a-z.]+\/maps/.test(value); /^(https?:\/\/)?(www\.)?google\.[a-z.]+\/maps/.test(value);
@ -112,7 +114,10 @@ export class PlaceCreateModalComponent {
}, },
}); });
this.placeForm.get("lat")?.valueChanges.subscribe({ this.placeForm
.get("lat")
?.valueChanges.pipe(takeUntilDestroyed())
.subscribe({
next: (value: string) => { next: (value: string) => {
const result = checkAndParseLatLng(value); const result = checkAndParseLatLng(value);
if (!result) return; if (!result) return;
@ -135,8 +140,6 @@ export class PlaceCreateModalComponent {
let ret = this.placeForm.value; let ret = this.placeForm.value;
ret["category_id"] = ret["category"]; ret["category_id"] = ret["category"];
delete ret["category"]; delete ret["category"];
if (!ret["price"]) ret["price"] = null;
if (!ret["duration"]) ret["duration"] = null;
if (ret["image_id"]) { if (ret["image_id"]) {
delete ret["image"]; delete ret["image"];
delete ret["image_id"]; delete ret["image_id"];
@ -148,9 +151,7 @@ export class PlaceCreateModalComponent {
parseGoogleMapsUrl(url: string): void { parseGoogleMapsUrl(url: string): void {
const [place, latlng] = this.utilsService.parseGoogleMapsUrl(url); const [place, latlng] = this.utilsService.parseGoogleMapsUrl(url);
if (!place || !latlng) { if (!place || !latlng) return;
return;
}
const [lat, lng] = latlng.split(","); const [lat, lng] = latlng.split(",");
this.placeForm.get("place")?.setValue(place); this.placeForm.get("place")?.setValue(place);
@ -163,7 +164,7 @@ export class PlaceCreateModalComponent {
onImageSelected(event: Event) { onImageSelected(event: Event) {
const input = event.target as HTMLInputElement; const input = event.target as HTMLInputElement;
if (input.files && input.files.length > 0) { if (input.files?.length) {
const file = input.files[0]; const file = input.files[0];
const reader = new FileReader(); const reader = new FileReader();

View File

@ -16,6 +16,7 @@ import { TextareaModule } from "primeng/textarea";
import { InputMaskModule } from "primeng/inputmask"; import { InputMaskModule } from "primeng/inputmask";
import { UtilsService } from "../../services/utils.service"; import { UtilsService } from "../../services/utils.service";
import { checkAndParseLatLng, formatLatLng } from "../../shared/latlng-parser"; import { checkAndParseLatLng, formatLatLng } from "../../shared/latlng-parser";
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
@Component({ @Component({
selector: "app-trip-create-day-item-modal", selector: "app-trip-create-day-item-modal",
@ -84,25 +85,34 @@ export class TripCreateDayItemModalComponent {
], ],
}); });
if (this.config.data) { const data = this.config.data;
const item = this.config.data.item; if (data) {
if (item) this.places = data.places ?? [];
this.itemForm.patchValue({ ...item, place: item.place?.id || null }); this.days = data.days ?? [];
this.places = this.config.data.places; if (data.item)
this.days = this.config.data.days; this.itemForm.patchValue({
if (this.config.data.selectedDay) ...data.item,
this.itemForm.get("day_id")?.setValue(this.config.data.selectedDay); place: data.item.place?.id ?? null,
});
if (data.selectedDay)
this.itemForm.get("day_id")?.setValue(data.selectedDay);
} }
this.itemForm.get("place")?.valueChanges.subscribe({ this.itemForm
.get("place")
?.valueChanges.pipe(takeUntilDestroyed())
.subscribe({
next: (value?: number) => { next: (value?: number) => {
if (!value) { if (!value) {
this.itemForm.get("lat")?.setValue(""); this.itemForm.get("lat")?.setValue("");
this.itemForm.get("lng")?.setValue(""); this.itemForm.get("lng")?.setValue("");
return;
} }
if (value) {
const p: Place = this.places.find((p) => p.id === value) as Place; const p: Place = this.places.find((p) => p.id === value) as Place;
if (p) {
this.itemForm.get("lat")?.setValue(p.lat); this.itemForm.get("lat")?.setValue(p.lat);
this.itemForm.get("lng")?.setValue(p.lng); this.itemForm.get("lng")?.setValue(p.lng);
this.itemForm.get("price")?.setValue(p.price || 0); this.itemForm.get("price")?.setValue(p.price || 0);
@ -112,7 +122,10 @@ export class TripCreateDayItemModalComponent {
}, },
}); });
this.itemForm.get("lat")?.valueChanges.subscribe({ this.itemForm
.get("lat")
?.valueChanges.pipe(takeUntilDestroyed())
.subscribe({
next: (value: string) => { next: (value: string) => {
const result = checkAndParseLatLng(value); const result = checkAndParseLatLng(value);
if (!result) return; if (!result) return;

View File

@ -14,12 +14,12 @@ function _dmsToDecimal(
sec: number, sec: number,
dir: string, dir: string,
): number { ): number {
let dec = deg + min / 60 + sec / 3600; const dec = deg + min / 60 + sec / 3600;
return /[SW]/i.test(dir) ? -dec : dec; return /[SW]/i.test(dir) ? -dec : dec;
} }
function _dmmToDecimal(deg: number, min: number, dir: string): number { function _dmmToDecimal(deg: number, min: number, dir: string): number {
let dec = deg + min / 60; const dec = deg + min / 60;
return /[SW]/i.test(dir) ? -dec : dec; return /[SW]/i.test(dir) ? -dec : dec;
} }
@ -31,9 +31,7 @@ export function formatLatLng(num: number): string {
export function checkAndParseLatLng( export function checkAndParseLatLng(
value: string | number, value: string | number,
): [number, number] | undefined { ): [number, number] | undefined {
if (value.constructor != String) { if (typeof value !== "string") return undefined;
return;
}
// Parse PlusCode // Parse PlusCode
if (OpenLocationCode.isValid(value)) { if (OpenLocationCode.isValid(value)) {