Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { CommonModule } from '@angular/common';
- import { Component, OnInit } from '@angular/core';
- import { FormArray, FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
- import { ApiService } from '../services/api.service';
- @Component({
- selector: 'app-create-page',
- templateUrl: './create-page.component.html',
- styleUrls: ['./create-page.component.css'],
- standalone: true,
- imports: [ReactiveFormsModule, CommonModule]
- })
- export class CreatePageComponent implements OnInit {
- createForm!: FormGroup;
- imagePreview: string | ArrayBuffer = '';
- currentFormSection = 1;
- totalFormSections = 4;
- imagesPreview: string[] = [];
- dayImages: { previews: string[] }[] = [];
- currentDayIndex: number = 0;
- constructor(private fb: FormBuilder, private apiService:ApiService) { }
- ngOnInit() {
- this.createForm = this.fb.group({
- image: [null],
- title: ['', Validators.required],
- paragraph: ['', Validators.required],
- "title-desc": ['', Validators.required],
- 'info-desc': ['', Validators.required],
- 'images': [null] ,
- dateRange: this.fb.group({
- start: ['', Validators.required],
- end: ['', Validators.required]
- }),
- price: ['', [Validators.required, Validators.pattern(/^\d+$/)]],
- conditions: this.fb.group({
- ticketsIncluded: [false],
- allTransportCosts: [false],
- accommodations: [false],
- allMeals: [false],
- practices: [false],
- atvTour: [false],
- spaAccess: [false],
- guidesIncluded: [false],
- medicalInsurance: [false],
- personalExpenses: [false],
- alcoholicBeverages: [false],
- unspecifiedServices: [false],
- additionalActivitiesFee: [false],
- cancellationInsurance: [false]
- }),
- days: this.fb.array([this.initDayForm()]),
- });
- this.dayImages.push({ previews: [] });
- console.log('fs',this.createForm.value)
- }
- initDayForm(): FormGroup {
- return this.fb.group({
- dayImage: [null],
- dayTitle: ['', Validators.required],
- dayInfo: ['', Validators.required]
- });
- };
- goToDay(index: number) {
- this.currentDayIndex = index;
- }
- addDay() {
- const days = this.createForm.get('days') as FormArray;
- const newDayFormGroup = this.initDayForm();
- days.push(newDayFormGroup);
- this.currentDayIndex = days.length - 1;
- };
- // Функция за премахване на ден
- removeDay(index: number) {
- const days = this.createForm.get('days') as FormArray;
- if (days.length > 1) {
- days.removeAt(index);
- this.dayImages.splice(index, 1); // Премахваме снимките за деня
- }
- };
- nextDay() {
- if (this.currentDayIndex < this.days.length - 1) {
- this.currentDayIndex++;
- }
- }
- previousDay() {
- if (this.currentDayIndex > 0) {
- this.currentDayIndex--;
- }
- }
- // Функция за получаване на дните като FormArray
- get days(): FormArray {
- return this.createForm.get('days') as FormArray;
- }
- onImageChange(event: Event) {
- const file = (event.target as HTMLInputElement).files?.[0];
- if (file) {
- const reader = new FileReader();
- reader.onload = () => {
- this.imagePreview = reader.result!;
- };
- reader.readAsDataURL(file);
- }
- }
- onImagesChange(event: Event) {
- const files = (event.target as HTMLInputElement).files;
- this.imagesPreview = [];
- if (files && files.length <= 4) {
- Array.from(files).forEach(file => {
- const reader = new FileReader();
- reader.onload = () => {
- this.imagesPreview.push(reader.result as string);
- };
- reader.readAsDataURL(file);
- });
- } else {
- console.error("Можете да качите максимум 4 снимки.");
- }
- };
- onDayImageChange(event: Event, dayIndex: number) {
- const files = (event.target as HTMLInputElement).files;
- if (files) {
- this.dayImages[dayIndex].previews = [];
- Array.from(files).forEach(file => {
- const reader = new FileReader();
- reader.onload = () => {
- this.dayImages[dayIndex].previews.push(reader.result as string);
- };
- reader.readAsDataURL(file);
- });
- }
- }
- onSubmit() {
- if (this.createForm.valid) {
- const formData = {
- ...this.createForm.value,
- image: this.imagePreview,
- images: this.imagesPreview,
- days: this.days.value.map((day: any, index: number) => {
- const dayImageObj = this.dayImages[index];
- return {
- ...day,
- dayImage: dayImageObj ? dayImageObj.previews : []
- };
- })
- };
- this.apiService.createDestination(formData).subscribe({
- next: (response) => {
- console.log('Destination created successfully', response);
- },
- error: (error) => {
- console.error('Error creating destination', error);
- }
- });
- } else {
- console.log('Form is not valid');
- }
- }
- goToNextSection() {
- if(this.currentFormSection < this.totalFormSections) {
- this.currentFormSection++;
- }
- }
- goToPreviousSection() {
- if(this.currentFormSection > 1) {
- this.currentFormSection--;
- }
- }
- }
Advertisement
Comments
-
- https://www.ted.com/profiles/46419224
- https://app.roll20.net/users/13104145/slot-gacor
- https://independent.academia.edu/SlotGacorOfficial
- https://www.behance.net/slotgacofficia
- https://www.inkitt.com/SlotGacorMaxwin
- https://letterboxd.com/slotgacor99/
- https://hubpages.com/@situsslotgacor99
- https://unsplash.com/@slotgacorr
- https://www.hackerearth.com/@slotgacor.official.com
- https://folkd.com/profile/Slotgacorr
- https://republic.com/@slot-gacor-8
- https://www.intensedebate.com/people/slotgcr
- https://loop.frontiersin.org/people/2684518/overview
- https://peatix.com/user/21393903/view
- https://www.magcloud.com/user/slotgacorr
- https://www.longisland.com/profile/slotgacorrr
- https://qiita.com/slotgacorrr
- https://www.producthunt.com/@slotgacor_official
- https://www.slideshare.net/slotgacorofficialcom
- https://experiment.com/users/sslotgacor9
- https://www.mapleprimes.com/users/slotgacorrr
- https://www.atlasobscura.com/users/slotgacorofficial
- https://www.pozible.com/profile/slotgacor-official
- https://www.deviantart.com/bestslotgacor99
- https://gifyu.com/slotgacorofficia
- https://www.zazzle.com/mbr/238130768076719234
- https://www.scribd.com/user/731917048/slotgacor-official-com
- https://www.tripadvisor.com/Profile/situsslotgacorr
- https://www.storeboard.com/slotgacoro
- https://www.answers.com/u/SlotGacorrr
- https://www.coursera.org/user/87eb485b452b968847dbfea7f6ef0963
- https://speakerdeck.com/slottgacorr99
- http://www.redbubble.com/people/gameslotgacor
- https://www.codecademy.com/profiles/script2275984419
- https://slides.com/slotgacor-official
- https://app.zintro.com/profile/zi0a3bd074
- https://pantip.com/profile/8034143#topics
- https://www.noteflight.com/profile/77d3995b137e0a28a68f595d945d5d7059d9e7a3
- https://worldcosplay.net/member/1736769
- https://communities.bentley.com/members/6600c082_2d00_f6ad_2d00_4641_2d00_a21c_2d00_56c31f0a60ec
- https://coolors.co/u/slotgacor._official
- https://digimac-technologies.mn.co/members/22858073
- https://wefunder.com/slotgacorofficial
- https://www.slideserve.com/SlotGacor8
- https://app.zintro.com/profile/zi0a3bd074?ref=
- https://www.mixcloud.com/SlotGacorrr/
- https://www.walkscore.com/people/150478797656/walk-score-user
- https://doodleordie.com/profile/slotgacorr
- https://www.reverbnation.com/slotgacorr3
- https://www.yumpu.com/user/SlotGacorrr
- https://www.funddreamer.com/users/slotgacor-official
- https://www.atlasobscura.com/users/slotgacorofficial
- https://rpgmaker.net/users/SlotGacorrr/
- https://comidarealkitchen.mn.co/members/22859676
- https://data.world/gameslotgacorr
- https://www.awwwards.com/slotgacor-official/
- https://triberr.com/SlotGacorrr
- https://sketchfab.com/slottgacorrr
Add Comment
Please, Sign In to add comment
Advertisement