Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | 121x 1x 1x 1x 1x 1x 1x 1x 1x 29x 1x 3x 3x 3x 1x 6x 1x 1x 36x 1x 30x 1x 31x 1x 1x 1x 1x 1x 1x 29x 29x 1x 1x 30x 30x 30x 2x 2x 2x 2x 2x 2x 2x 2x 2x 28x 1x 7x 6x 3x 6x 2x 4x 6x | import { Component, inject, signal, input, OnInit, computed } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { LucideAngularModule, X, Plus, Minus, Star } from 'lucide-angular';
import { TranslocoModule, TranslocoService } from '@jsverse/transloco';
import { ModalService } from '../../services/modal.service';
import { ShoppingItemRow, ShoppingListDataService, Unit } from '../../services/shopping-list-data.service';
import { Category } from '../../types';
import { PowerSyncService } from '../../services/powersync';
import { ShoppingListService } from '../../services/shopping-list.service';
import { FavouriteItem } from '../../models';
type EditableShoppingItem = Pick<ShoppingItemRow, 'id' | 'name' | 'category' | 'totalQuantity' | 'info' | 'size' | 'unit'>;
export interface AddItemData {
editItem?: EditableShoppingItem;
prefillName?: string;
prefillUnit?: Unit;
prefillSize?: number;
}
export interface AddItemResult {
id?: string; // Falls wir ein existierendes Item bearbeiten
name: string;
category: string;
quantity: number;
info?: string;
size?: number;
unit: Unit;
}
@Component({
selector: 'app-add-item-modal',
imports: [FormsModule, LucideAngularModule, TranslocoModule],
templateUrl: './add-item-modal.html',
styleUrl: './add-item-modal.scss',
})
export class AddItemModal implements OnInit {
private readonly modalService = inject(ModalService);
private readonly transloco = inject(TranslocoService);
private readonly shoppingListDataService = inject(ShoppingListDataService);
private readonly shoppingListService = inject(ShoppingListService);
// Input data from modal service
readonly data = input<AddItemData>();
readonly icons = { X, Plus, Minus, Star };
// Favourites
readonly favourites = this.shoppingListService.favourites;
readonly isCurrentFavourite = computed(() => {
return this.shoppingListService.isCurrentFavourite(this.name(), this.unit(), this.size());
});
readonly filteredFavourites = computed(() => {
const query = this.name().toLowerCase().trim();
const favs = this.favourites();
if (!query) {
return favs;
}
return favs.filter((fav: FavouriteItem) => fav.name.toLowerCase().includes(query));
});
// Form State
readonly name = signal('');
readonly showAutocomplete = signal(false);
readonly isNameEmpty = computed(() => !this.name().trim());
readonly shouldShowAutocomplete = computed(() =>
this.showAutocomplete() && !this.isNameEmpty() && this.filteredFavourites().length > 0
);
readonly shouldShowFavouritesList = computed(() =>
this.isNameEmpty() && this.favourites().length > 0
);
readonly category = signal('Sonstiges');
readonly quantity = signal(1);
readonly info = signal('');
readonly size = signal<number | undefined>(undefined);
readonly unit = signal<Unit>('Einheit');
// Edit mode
readonly isEditMode = signal(false);
private editItemId: string | undefined;
readonly modalTitle = computed(() => this.isEditMode() ? 'modals.addItem.titleEdit' : 'modals.addItem.titleAdd');
readonly submitButtonText = computed(() => this.isEditMode() ? 'common.save' : 'common.add');
// Predefined categories
readonly categories = signal<Category[]>([{id: BigInt(9), name: 'Sonstiges', created_at: new Date().toISOString()}]);
// Verfügbare Units
readonly units: Unit[] = [
'Einheit',
'g',
'dag',
'kg',
'mL',
'L',
'Flasche',
'Kiste',
'Dose',
'Packung'
];
// eslint-disable-next-line @typescript-eslint/no-misused-promises
async ngOnInit(): Promise<void> {
const inputData = this.data();
this.categories.set(await this.shoppingListDataService.getCategories());
if (inputData?.editItem) {
const item = inputData.editItem;
this.isEditMode.set(true);
this.editItemId = item.id;
this.name.set(item.name);
this.category.set(item.category);
this.quantity.set(item.totalQuantity);
this.info.set(item.info || '');
this.size.set(item.size);
this.unit.set(item.unit || 'Einheit');
I} else if (inputData) {
if (inputData.prefillName) {
this.name.set(inputData.prefillName);
}
if (inputData.prefillUnit) {
this.unit.set(inputData.prefillUnit);
}
if (inputData.prefillSize !== undefined) {
this.size.set(inputData.prefillSize);
}
}
}
close(): void {
this.modalService.dismiss();
}
incrementQuantity(): void {
this.quantity.update(q => q + 1);
}
decrementQuantity(): void {
if (this.quantity() > 1) {
this.quantity.update(q => q - 1);
}
}
submit(): void {
if (this.isNameEmpty()) {
return;
}
const result: AddItemResult = {
id: this.editItemId,
name: this.name().trim(),
category: this.category(),
quantity: this.quantity(),
info: this.info().trim() || undefined,
size: this.size() || undefined,
unit: this.unit()
};
this.modalService.close(result);
}
toggleFavourite(): void {
if (this.isNameEmpty()) return;
this.shoppingListService.toggleFavourite(this.name(), this.category(), this.unit(), this.size());
}
getFavouriteDetails(fav: FavouriteItem): string {
const sizeStr = fav.size ? `${fav.size} ` : '';
const unitStr = fav.unit !== 'Einheit' ? this.transloco.translate(`units.${fav.unit}`) : '';
return `${sizeStr}${unitStr}`;
}
hasFavouriteDetails(fav: FavouriteItem): boolean {
return !!(fav.size || fav.unit !== 'Einheit');
}
selectFavourite(fav: FavouriteItem): void {
this.name.set(fav.name);
this.unit.set(fav.unit);
this.size.set(fav.size);
this.showAutocomplete.set(false);
}
onNameChange(newName: string): void {
this.name.set(newName);
this.showAutocomplete.set(true);
}
hideAutocomplete(): void {
this.showAutocomplete.set(false);
}
}
|