Code Ionic
Code Ionic
@Component({
selector: "app-commerces",
templateUrl: "./commerces.page.html",
styleUrls: ["./commerces.page.scss"],
})
export class CommercesPage implements OnInit {
firstCommerce: any;
noCommerceMessage!: string;
commerceForm!: FormGroup;
villes: any[] = [];
villeName: string | null = null;
commerces: any[] = []; // Define commerces property
selectedCommerceId: number | null = null;
constructor(
private formBuilder: FormBuilder,
private userService: UserService,
private router: Router,
private commerceService: CommerceService,
private modalController: ModalController,
private commerceDataService: CommerceDataService
) {}
async ngOnInit() {
try {
this.commerceForm = this.formBuilder.group({
commercename: ["", Validators.required],
services: ["", Validators.required],
image_commerce: [""], // Assuming this field is optional
ville_id: [""],
// Add more form controls as needed for other fields
});
// Fetch businessOwnerId
const businessOwnerId = await this.userService.getBusinessOwnerId();
if (businessOwnerId !== null) {
const commerces1 =
await this.commerceService.getCommercesByBusinessOwnerId(
businessOwnerId
);
if (commerces1.length === 0) {
// No commerces belonging to the current user
} else {
const commerces =
await
this.commerceService.getCommercesByBusinessOwnerWithMonthlyFeePaid(
businessOwnerId
);
// Sort the commerces array by ID in ascending order
commerces.sort((a, b) => a.id - b.id);
lockApp() {
this.router.navigate(["/villes-commerces"]);
}
async openCommerceModal() {
try {
const businessOwnerId = await this.userService.getBusinessOwnerId();
const modal = await this.modalController.create({
component: CommerceModalPage,
componentProps: {
businessOwnerId: businessOwnerId,
},
});
await modal.present();
} catch (error) {
console.error("Error opening commerce modal:", error);
}
}
this.commerceService.setCommerces(this.commerces);
}
}
} catch (error) {
console.error("Error refreshing commerce data:", error);
// Handle error as needed
}
}
async refreshCommerceDataUp() {
try {
if (this.selectedCommerceId) {
const commerce = await this.commerceService.getCommerceById(
this.selectedCommerceId
);
if (!commerce) {
// If the commerce is not found, it means it has been deleted
// Clear the selected commerce ID and reset the form
this.selectedCommerceId = null;
this.commerceForm.reset();
} else {
// If the commerce still exists, update the form with its details
this.loadCommerceDetails(commerce);
}
this.commerceService.setCommerces(this.commerces);
}
} catch (error) {
console.error("Error refreshing commerce data:", error);
// Handle error as needed
}
}
async openUpdateCommerceModal() {
try {
// Check if selectedCommerceId is defined
if (!this.selectedCommerceId) {
console.error("No commerce selected.");
return;
}
// After updating the commerces list and firstCommerce, you might want to perform
additional actions
// For example, update UI, trigger any side effects, etc.
// You can also call other methods or services as needed here
} catch (error) {
console.error(
"Error refreshing commerce data for delete confirmation:",
error
);
// Handle error as needed
}
}
}