Skip to content

Commit

Permalink
refactor_api(fe):making the code more extensible
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel0347 authored and Javi111003 committed Nov 23, 2024
1 parent b9f3f54 commit 536f689
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<mat-form-field appearance="fill">
<mat-label>Oficina</mat-label>
<mat-select formControlName="secondSelect" (selectionChange)="onSelectionChange()">
<mat-option *ngFor="let option of officeOptions" [value]="option.value">
<mat-option *ngFor="let option of officeOptions" [value]="option.id">
{{ option.label }}
</mat-option>
</mat-select>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Component, OnInit } from '@angular/core';
import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { FormGroup, FormBuilder } from '@angular/forms';
import { ConfigColumn } from '../../../../shared/components/table/table.component';

import { MatTableDataSource } from '@angular/material/table';
import { MatPaginator } from '@angular/material/paginator';

@Component({
selector: 'app-equipment',
templateUrl: './equipment.component.html',
styleUrls: ['./equipment.component.css']
})
export class EquipmentComponent implements OnInit {
export class EquipmentComponent implements OnInit, AfterViewInit {

constructor(private fb: FormBuilder) {
this.form = this.fb.group({
Expand All @@ -22,20 +23,11 @@ export class EquipmentComponent implements OnInit {
showTable = false;
isSecondSelectDisabled = true; // initially inactive
CenterOptions: any[] = [];
officeOptions: { value: string; label: string; }[] = [];
officeOptions: { id: number; label: string; }[] = [];
// Example data for the table
dataSource: MatTableDataSource<any> = new MatTableDataSource([
{ position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H' },
{ position: 2, name: 'Helium', weight: 4.0026, symbol: 'He' },
{ position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li' },
{ position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be' },
{ position: 5, name: 'Boron', weight: 10.811, symbol: 'B' },
{ position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C' },
{ position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N' },
{ position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O' },
{ position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F' },
{ position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne' },
]);
dataSource: MatTableDataSource<any> = new MatTableDataSource([0]);
@ViewChild(MatPaginator) paginator: MatPaginator = [][0];

// Table Columns
displayedColumns: ConfigColumn[] = [
{
Expand All @@ -58,6 +50,10 @@ export class EquipmentComponent implements OnInit {
this.getCenterList();
}

ngAfterViewInit(): void {
this.dataSource.paginator = this.paginator;
}

/**
* This function subscribes to the changes of the first select control.
* If the value is true, it enables the second select control.
Expand All @@ -79,7 +75,10 @@ export class EquipmentComponent implements OnInit {
*/
onSelectionChange(): void {
const { firstSelect, secondSelect } = this.form.value;
this.showTable = firstSelect && secondSelect;
if (firstSelect && secondSelect) {
this.getEquipmentsByOffice(secondSelect);
this.showTable = true;
}
}

/**
Expand All @@ -92,6 +91,8 @@ export class EquipmentComponent implements OnInit {
this.form.get('secondSelect')?.reset();
this.getOfficesByCenter(selectedCentroID);
this.form.get('secondSelect')?.enable();

this.showTable = false;
}

/**
Expand All @@ -103,22 +104,46 @@ export class EquipmentComponent implements OnInit {
// this is a test
if (centerID === 1)
this.officeOptions = [
{ value: 'oficina1', label: 'Oficina 1.1' },
{ value: 'oficina2', label: 'Oficina 1.2' },
{ id: 1, label: 'Oficina 1.1' },
{ id: 2, label: 'Oficina 1.2' },
];
else if (centerID === 2)
this.officeOptions = [
{ value: 'oficina3', label: 'Oficina 2.1' },
{ value: 'oficina4', label: 'Oficina 2.2' },
{ id: 3, label: 'Oficina 2.1' },
{ id: 4, label: 'Oficina 2.2' },
];
else {
this.officeOptions = [
{ value: 'oficina5', label: 'Oficina 3.1' },
{ value: 'oficina6', label: 'Oficina 3.2' },
{ id: 5, label: 'Oficina 3.1' },
{ id: 6, label: 'Oficina 3.2' },
];
}
}

/**
* This function retrieves the list of equipment based on the selected office.
* It updates the dataSource for the MatTable with the list of equipment.
* @param officeID The ID of the selected office.
*/
getEquipmentsByOffice(officeID : number): void {
const equipments =[
{ position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H' },
{ position: 2, name: 'Helium', weight: 4.0026, symbol: 'He' },
{ position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li' },
{ position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be' },
{ position: 5, name: 'Boron', weight: 10.811, symbol: 'B' },
{ position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C' },
{ position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N' },
{ position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O' },
{ position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F' },
{ position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne' },
];

this.dataSource = new MatTableDataSource(equipments);
this.dataSource.paginator = this.paginator;
console.log(this.dataSource);
}

/**
* Retrieves the list of centers.
* This function updates the CenterOptions array with the list of available centers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@ export class AutocompleteComponent implements OnInit {
);
}

/**
* Emits the selected option to the parent component.
* @param event The event emitted by the option selection.
*/
onOptionSelected(event: any) {
this.optionSelected.emit(event.option.value);
}

/**
* Filters the options based on the input value.
* @param value The input value to filter options against.
* @returns An array of options that match the input value.
*/
private _filter(value: string): string[] {
const filterValue = value.toLowerCase();

return this.options.filter(option => option.toLowerCase().startsWith(filterValue));
}

onOptionSelected(event: any) {
this.optionSelected.emit(event.option.value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export class DatepickerComponent {
@Input() label: string = 'Elige una fecha';
@Output() dateSelected = new EventEmitter<Date>();

// Método para emitir la fecha seleccionada
/**
* Emits the selected date to the parent component.
* @param event The event emitted by the date picker.
*/
onDateChange(event: any) {
const selectedDate = event.value;
this.dateSelected.emit(selectedDate);
Expand Down
4 changes: 0 additions & 4 deletions frontend/src/app/shared/components/table/table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,4 @@ export class TableComponent implements OnInit, AfterViewInit {
edit() {
throw new Error('Method not implemented.');
}

createNew() {
throw new Error('Method not implemented.');
}
}

0 comments on commit 536f689

Please sign in to comment.