Advertisement
bebo231312312321

Untitled

Mar 14th, 2024
641
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { CommonModule } from '@angular/common';
  2. import { Component, OnInit } from '@angular/core';
  3. import { FormArray, FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
  4. import { ApiService } from '../services/api.service';
  5.  
  6. @Component({
  7.   selector: 'app-create-page',
  8.   templateUrl: './create-page.component.html',
  9.   styleUrls: ['./create-page.component.css'],
  10.   standalone: true,
  11.   imports: [ReactiveFormsModule, CommonModule]
  12. })
  13. export class CreatePageComponent implements OnInit {
  14.   createForm!: FormGroup;
  15.   imagePreview: string | ArrayBuffer = '';
  16.   currentFormSection = 1;
  17.   totalFormSections = 4;
  18.   imagesPreview: string[] = [];
  19.   dayImages: { previews: string[] }[] = [];
  20.   currentDayIndex: number = 0;
  21.  
  22.  
  23.   constructor(private fb: FormBuilder, private apiService:ApiService) { }
  24.  
  25.   ngOnInit() {
  26.     this.createForm = this.fb.group({
  27.       image: [null],
  28.       title: ['', Validators.required],
  29.       paragraph: ['', Validators.required],
  30.       "title-desc": ['', Validators.required],
  31.       'info-desc': ['', Validators.required],
  32.       'images': [null] ,
  33.       dateRange: this.fb.group({
  34.         start: ['', Validators.required],
  35.         end: ['', Validators.required]
  36.       }),
  37.       price: ['', [Validators.required, Validators.pattern(/^\d+$/)]],
  38.       conditions: this.fb.group({
  39.         ticketsIncluded: [false],
  40.         allTransportCosts: [false],
  41.         accommodations: [false],
  42.         allMeals: [false],
  43.         practices: [false],
  44.         atvTour: [false],
  45.         spaAccess: [false],
  46.         guidesIncluded: [false],
  47.         medicalInsurance: [false],
  48.         personalExpenses: [false],
  49.         alcoholicBeverages: [false],
  50.         unspecifiedServices: [false],
  51.         additionalActivitiesFee: [false],
  52.         cancellationInsurance: [false]
  53.       }),
  54.       days: this.fb.array([this.initDayForm()]),
  55.      
  56.       });
  57.       this.dayImages.push({ previews: [] });
  58.       console.log('fs',this.createForm.value)
  59.   }
  60.   initDayForm(): FormGroup {
  61.     return this.fb.group({
  62.       dayImage: [null],
  63.       dayTitle: ['', Validators.required],
  64.       dayInfo: ['', Validators.required]
  65.     });
  66.   };
  67.   goToDay(index: number) {
  68.     this.currentDayIndex = index;
  69.   }
  70.  
  71.   addDay() {
  72.     const days = this.createForm.get('days') as FormArray;
  73.     const newDayFormGroup = this.initDayForm();
  74.     days.push(newDayFormGroup);
  75.    
  76.     this.currentDayIndex = days.length - 1;
  77.   };
  78.  
  79.   // Функция за премахване на ден
  80.   removeDay(index: number) {
  81.     const days = this.createForm.get('days') as FormArray;
  82.     if (days.length > 1) {
  83.       days.removeAt(index);
  84.       this.dayImages.splice(index, 1); // Премахваме снимките за деня
  85.     }
  86.   };
  87.   nextDay() {
  88.     if (this.currentDayIndex < this.days.length - 1) {
  89.       this.currentDayIndex++;
  90.     }
  91.   }
  92.  
  93.   previousDay() {
  94.     if (this.currentDayIndex > 0) {
  95.       this.currentDayIndex--;
  96.     }
  97.   }
  98.     // Функция за получаване на дните като FormArray
  99.     get days(): FormArray {
  100.       return this.createForm.get('days') as FormArray;
  101.     }
  102.   onImageChange(event: Event) {
  103.     const file = (event.target as HTMLInputElement).files?.[0];
  104.     if (file) {
  105.       const reader = new FileReader();
  106.       reader.onload = () => {
  107.         this.imagePreview = reader.result!;
  108.       };
  109.       reader.readAsDataURL(file);
  110.     }
  111.   }
  112.   onImagesChange(event: Event) {
  113.     const files = (event.target as HTMLInputElement).files;
  114.     this.imagesPreview = [];
  115.     if (files && files.length <= 4) {
  116.       Array.from(files).forEach(file => {
  117.         const reader = new FileReader();
  118.         reader.onload = () => {
  119.           this.imagesPreview.push(reader.result as string);
  120.         };
  121.         reader.readAsDataURL(file);
  122.       });
  123.     } else {
  124.  
  125.       console.error("Можете да качите максимум 4 снимки.");
  126.     }
  127.   };
  128.   onDayImageChange(event: Event, dayIndex: number) {
  129.     const files = (event.target as HTMLInputElement).files;
  130.     if (files) {
  131.       this.dayImages[dayIndex].previews = [];
  132.       Array.from(files).forEach(file => {
  133.         const reader = new FileReader();
  134.         reader.onload = () => {
  135.          
  136.           this.dayImages[dayIndex].previews.push(reader.result as string);
  137.         };
  138.         reader.readAsDataURL(file);
  139.       });
  140.     }
  141.   }
  142.   onSubmit() {
  143.  
  144.     if (this.createForm.valid) {
  145.       const formData = {
  146.         ...this.createForm.value,
  147.         image: this.imagePreview,
  148.         images: this.imagesPreview,
  149.         days: this.days.value.map((day: any, index: number) => {
  150.      
  151.           const dayImageObj = this.dayImages[index];
  152.           return {
  153.             ...day,
  154.             dayImage: dayImageObj ? dayImageObj.previews : []
  155.           };
  156.         })
  157.       };
  158.  
  159.  
  160.       this.apiService.createDestination(formData).subscribe({
  161.         next: (response) => {
  162.           console.log('Destination created successfully', response);
  163.    
  164.         },
  165.         error: (error) => {
  166.           console.error('Error creating destination', error);
  167.    
  168.         }
  169.       });
  170.     } else {
  171.       console.log('Form is not valid');
  172.  
  173.     }
  174.   }
  175.  
  176.   goToNextSection() {
  177.     if(this.currentFormSection < this.totalFormSections) {
  178.       this.currentFormSection++;
  179.     }
  180.  
  181.   }
  182.  
  183.   goToPreviousSection() {
  184.     if(this.currentFormSection > 1) {
  185.       this.currentFormSection--;
  186.     }
  187.  
  188.   }
  189. }
  190.  
Advertisement
Comments
  • slottgacorrr
    231 days
    # text 2.61 KB | 0 0
    1. https://www.ted.com/profiles/46419224
    2. https://app.roll20.net/users/13104145/slot-gacor
    3. https://independent.academia.edu/SlotGacorOfficial
    4. https://www.behance.net/slotgacofficia
    5. https://www.inkitt.com/SlotGacorMaxwin
    6. https://letterboxd.com/slotgacor99/
    7. https://hubpages.com/@situsslotgacor99
    8. https://unsplash.com/@slotgacorr
    9. https://www.hackerearth.com/@slotgacor.official.com
    10. https://folkd.com/profile/Slotgacorr
    11. https://republic.com/@slot-gacor-8
    12. https://www.intensedebate.com/people/slotgcr
    13. https://loop.frontiersin.org/people/2684518/overview
    14. https://peatix.com/user/21393903/view
    15. https://www.magcloud.com/user/slotgacorr
    16. https://www.longisland.com/profile/slotgacorrr
    17. https://qiita.com/slotgacorrr
    18. https://www.producthunt.com/@slotgacor_official
    19. https://www.slideshare.net/slotgacorofficialcom
    20. https://experiment.com/users/sslotgacor9
    21. https://www.mapleprimes.com/users/slotgacorrr
    22. https://www.atlasobscura.com/users/slotgacorofficial
    23. https://www.pozible.com/profile/slotgacor-official
    24. https://www.deviantart.com/bestslotgacor99
    25. https://gifyu.com/slotgacorofficia
    26. https://www.zazzle.com/mbr/238130768076719234
    27. https://www.scribd.com/user/731917048/slotgacor-official-com
    28. https://www.tripadvisor.com/Profile/situsslotgacorr
    29. https://www.storeboard.com/slotgacoro
    30. https://www.answers.com/u/SlotGacorrr
    31. https://www.coursera.org/user/87eb485b452b968847dbfea7f6ef0963
    32. https://speakerdeck.com/slottgacorr99
    33. http://www.redbubble.com/people/gameslotgacor
    34. https://www.codecademy.com/profiles/script2275984419
    35. https://slides.com/slotgacor-official
    36. https://app.zintro.com/profile/zi0a3bd074
    37. https://pantip.com/profile/8034143#topics
    38. https://www.noteflight.com/profile/77d3995b137e0a28a68f595d945d5d7059d9e7a3
    39. https://worldcosplay.net/member/1736769
    40. https://communities.bentley.com/members/6600c082_2d00_f6ad_2d00_4641_2d00_a21c_2d00_56c31f0a60ec
    41. https://coolors.co/u/slotgacor._official
    42. https://digimac-technologies.mn.co/members/22858073
    43. https://wefunder.com/slotgacorofficial
    44. https://www.slideserve.com/SlotGacor8
    45. https://app.zintro.com/profile/zi0a3bd074?ref=
    46. https://www.mixcloud.com/SlotGacorrr/
    47. https://www.walkscore.com/people/150478797656/walk-score-user
    48. https://doodleordie.com/profile/slotgacorr
    49. https://www.reverbnation.com/slotgacorr3
    50. https://www.yumpu.com/user/SlotGacorrr
    51. https://www.funddreamer.com/users/slotgacor-official
    52. https://www.atlasobscura.com/users/slotgacorofficial
    53. https://rpgmaker.net/users/SlotGacorrr/
    54. https://comidarealkitchen.mn.co/members/22859676
    55. https://data.world/gameslotgacorr
    56. https://www.awwwards.com/slotgacor-official/
    57. https://triberr.com/SlotGacorrr
    58. https://sketchfab.com/slottgacorrr
Add Comment
Please, Sign In to add comment
Advertisement