first commit
This commit is contained in:
@ -0,0 +1,145 @@
|
||||
<app-menu></app-menu>
|
||||
|
||||
<div class="container mt-4">
|
||||
<form [formGroup]="eventForm" (ngSubmit)="onSubmit()">
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<div class="form-group mb-3">
|
||||
<label for="code" class="form-label text-primary">Code</label>
|
||||
<input id="code" formControlName="code" class="form-control" />
|
||||
</div>
|
||||
<div class="form-group mb-3">
|
||||
<label for="year" class="form-label text-primary">Year</label>
|
||||
<input id="year" formControlName="year" class="form-control" />
|
||||
</div>
|
||||
<div class="form-group mb-3">
|
||||
<label for="subject" class="form-label text-primary">Subject</label>
|
||||
<input id="subject" formControlName="subject" class="form-control" />
|
||||
</div>
|
||||
<div class="form-group mb-3">
|
||||
<label for="title" class="form-label text-primary">Title</label>
|
||||
<input id="title" formControlName="title" class="form-control" />
|
||||
</div>
|
||||
<div class="form-group mb-3">
|
||||
<label for="subTitle" class="form-label text-primary">Subtitle</label>
|
||||
<input id="subTitle" formControlName="subTitle" class="form-control" />
|
||||
</div>
|
||||
<div class="form-group mb-3">
|
||||
<label for="date" class="form-label text-primary">Date</label>
|
||||
<input id="date" formControlName="date" class="form-control" />
|
||||
</div>
|
||||
<div class="form-group mb-3">
|
||||
<label for="phone" class="form-label text-primary">Phone</label>
|
||||
<input id="phone" formControlName="phone" class="form-control" />
|
||||
</div>
|
||||
<div class="form-group mb-3">
|
||||
<label for="email" class="form-label text-primary">Email</label>
|
||||
<input id="email" formControlName="email" class="form-control" />
|
||||
</div>
|
||||
<div class="form-group mb-3">
|
||||
<label for="isActive" class="form-label text-primary">Active</label>
|
||||
<input id="isActive" type="checkbox" formControlName="isActive" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-6">
|
||||
<div class="form-group mb-3">
|
||||
<label class="form-label text-primary">Venues</label>
|
||||
<div formArrayName="venues">
|
||||
<div *ngFor="let venue of venues.controls; let i = index" class="card mb-2">
|
||||
<div class="card-body">
|
||||
<div [formGroupName]="i">
|
||||
<div class="row mb-2">
|
||||
<div class="col-12">
|
||||
<input formControlName="title" placeholder="Title" class="form-control" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="row mb-2">
|
||||
<div class="col-6">
|
||||
<input formControlName="date" placeholder="Date" class="form-control" />
|
||||
</div>
|
||||
<div class="col-6">
|
||||
<input formControlName="address" placeholder="Address" class="form-control" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="row mb-2">
|
||||
<div class="col-12">
|
||||
<input formControlName="info" placeholder="Info" class="form-control" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<button type="button" class="btn btn-danger btn-sm float-end" (click)="removeVenue(i)">
|
||||
<!-- <i class="bi bi-trash"></i> -->X
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<button type="button" class="btn btn-primary" (click)="addVenue()">
|
||||
<i class="bi bi-plus"></i> Add Venue
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group mb-3">
|
||||
<label class="form-label text-primary">Highlights</label>
|
||||
<div formArrayName="highlights">
|
||||
<div *ngFor="let highlight of highlights.controls; let i = index" class="card mb-2">
|
||||
<div class="card-body">
|
||||
<input [formControlName]="i" class="form-control" placeholder="Highlight" />
|
||||
<button type="button" class="btn btn-danger btn-sm float-end mt-2" (click)="removeHighlight(i)">
|
||||
<!-- <i class="bi bi-trash"></i> -->X
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<button type="button" class="btn btn-primary" (click)="addHighlight()">
|
||||
<i class="bi bi-plus"></i> Add Highlight
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group mb-3">
|
||||
<label class="form-label text-primary">Organisers</label>
|
||||
<div formArrayName="organisers">
|
||||
<div *ngFor="let organiser of organisers.controls; let i = index" class="card mb-2">
|
||||
<div class="card-body">
|
||||
<input [formControlName]="i" class="form-control" placeholder="Organiser" />
|
||||
<button type="button" class="btn btn-danger btn-sm float-end mt-2" (click)="removeOrganiser(i)">
|
||||
<!-- <i class="bi bi-trash"></i> -->X
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<button type="button" class="btn btn-primary" (click)="addOrganiser()">
|
||||
<i class="bi bi-plus"></i> Add Organiser
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group mb-3">
|
||||
<label class="form-label text-primary">Fees</label>
|
||||
<div formArrayName="fees">
|
||||
<div *ngFor="let fee of fees.controls; let i = index" class="card mb-2">
|
||||
<div class="card-body">
|
||||
<div [formGroupName]="i">
|
||||
<div class="row mb-2">
|
||||
<div class="col-8">
|
||||
<input formControlName="desc" placeholder="Description" class="form-control" />
|
||||
</div>
|
||||
<div class="col-4">
|
||||
<input formControlName="cost" placeholder="Cost" type="number" class="form-control" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<button type="button" class="btn btn-danger btn-sm float-end mt-2" (click)="removeFee(i)">
|
||||
<!-- <i class="bi bi-trash"></i> -->X
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<button type="button" class="btn btn-primary" (click)="addFee()">
|
||||
<i class="bi bi-plus"></i> Add Fee
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-success">Submit</button>
|
||||
</form>
|
||||
</div>
|
||||
@ -0,0 +1,25 @@
|
||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { EventFormComponent } from './event-form.component';
|
||||
|
||||
describe('EventFormComponent', () => {
|
||||
let component: EventFormComponent;
|
||||
let fixture: ComponentFixture<EventFormComponent>;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
declarations: [ EventFormComponent ]
|
||||
})
|
||||
.compileComponents();
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(EventFormComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,135 @@
|
||||
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { FormBuilder, FormGroup, FormArray, Validators } from '@angular/forms';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { EventService } from 'src/app/service/event.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-event-form',
|
||||
templateUrl: './event-form.component.html',
|
||||
styleUrls: ['./event-form.component.css']
|
||||
})
|
||||
export class EventFormComponent implements OnInit {
|
||||
|
||||
|
||||
|
||||
|
||||
eventForm: FormGroup;
|
||||
|
||||
constructor(
|
||||
private fb: FormBuilder,
|
||||
private eventService: EventService,
|
||||
private route: ActivatedRoute,
|
||||
private router: Router
|
||||
|
||||
) {}
|
||||
eventId: number | null;
|
||||
|
||||
ngOnInit() {
|
||||
this.eventForm = this.fb.group({
|
||||
code: ['', Validators.required],
|
||||
year: ['', Validators.required],
|
||||
subject: ['', Validators.required],
|
||||
title: ['', Validators.required],
|
||||
subTitle: [''],
|
||||
date: ['', Validators.required],
|
||||
venues: this.fb.array([]),
|
||||
highlights: this.fb.array([]),
|
||||
organisers: this.fb.array([]),
|
||||
fees: this.fb.array([]),
|
||||
phone: ['', Validators.required],
|
||||
email: ['', [Validators.required, Validators.email]],
|
||||
isActive: [true]
|
||||
});
|
||||
|
||||
this.route.paramMap.subscribe(params => {
|
||||
const idParam = params.get('id');
|
||||
this.eventId = idParam ? +idParam : null;
|
||||
|
||||
if (this.eventId !== null) {
|
||||
this.loadEvent(this.eventId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
loadEvent(id: number): void {
|
||||
this.eventService.getEvent(id).subscribe(event => {
|
||||
this.eventForm.patchValue(event);
|
||||
this.setArrayValues('venues', event.venues);
|
||||
this.setArrayValues('highlights', event.highlights);
|
||||
this.setArrayValues('organisers', event.organisers);
|
||||
this.setArrayValues('fees', event.fees);
|
||||
});
|
||||
}
|
||||
|
||||
setArrayValues(controlName: string, values: any[]): void {
|
||||
const array = this.eventForm.get(controlName) as FormArray;
|
||||
values.forEach(value => array.push(this.fb.group(value)));
|
||||
}
|
||||
|
||||
get venues(): FormArray {
|
||||
return this.eventForm.get('venues') as FormArray;
|
||||
}
|
||||
|
||||
get highlights(): FormArray {
|
||||
return this.eventForm.get('highlights') as FormArray;
|
||||
}
|
||||
|
||||
get organisers(): FormArray {
|
||||
return this.eventForm.get('organisers') as FormArray;
|
||||
}
|
||||
|
||||
get fees(): FormArray {
|
||||
return this.eventForm.get('fees') as FormArray;
|
||||
}
|
||||
|
||||
addVenue() {
|
||||
this.venues.push(this.fb.group({
|
||||
title: [''],
|
||||
date: [''],
|
||||
address: [''],
|
||||
info: ['']
|
||||
}));
|
||||
}
|
||||
|
||||
removeVenue(index: number) {
|
||||
this.venues.removeAt(index);
|
||||
}
|
||||
|
||||
addHighlight() {
|
||||
this.highlights.push(this.fb.control(''));
|
||||
}
|
||||
|
||||
removeHighlight(index: number) {
|
||||
this.highlights.removeAt(index);
|
||||
}
|
||||
|
||||
addOrganiser() {
|
||||
this.organisers.push(this.fb.control(''));
|
||||
}
|
||||
|
||||
removeOrganiser(index: number) {
|
||||
this.organisers.removeAt(index);
|
||||
}
|
||||
|
||||
addFee() {
|
||||
this.fees.push(this.fb.group({
|
||||
desc: [''],
|
||||
cost: ['']
|
||||
}));
|
||||
}
|
||||
|
||||
removeFee(index: number) {
|
||||
this.fees.removeAt(index);
|
||||
}
|
||||
|
||||
onSubmit(): void {
|
||||
if (this.eventForm.valid) {
|
||||
if (this.eventId) {
|
||||
this.eventService.updateEvent(this.eventId, this.eventForm.value).subscribe(() => this.router.navigate(['/events']));
|
||||
} else {
|
||||
this.eventService.createEvent(this.eventForm.value).subscribe(() => this.router.navigate(['/events']));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user