All files / app/pages/trip-register-page/first-step first-step.component.ts

66.66% Statements 116/174
50% Branches 4/8
66.66% Functions 4/6
66.66% Lines 116/174

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 1751x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x           4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x     4x 4x 4x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                   1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { DatePipe, isPlatformBrowser } from '@angular/common';
import { ChangeDetectorRef, Component, OnInit, PLATFORM_ID, inject } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { Meta } from '@angular/platform-browser';
import { ActivatedRoute, Router } from '@angular/router';
import { HeaderElementComponent } from '@elements/header-element/header-element.component';
import { MetaTagService } from '@services/meta-tag.service';
import { BsDatepickerConfig, BsDatepickerModule, BsDatepickerViewMode } from 'ngx-bootstrap/datepicker';
import Swal from 'sweetalert2';
 
@Component({
	selector: 'grt-first-step',
	providers: [DatePipe],
	imports: [HeaderElementComponent, BsDatepickerModule, FormsModule],
	templateUrl: './first-step.component.html',
	styleUrl: './first-step.component.scss',
})
export class FirstStepComponent implements OnInit {
	private cdr = inject(ChangeDetectorRef);
	private router = inject(Router);
	private route = inject(ActivatedRoute);
	private datePipe = inject(DatePipe);
	private platformId = inject(PLATFORM_ID);
	private meta = inject(Meta);
	private metaTag = inject(MetaTagService);
 
	bsConfig?: Partial<BsDatepickerConfig>;
	minMode: BsDatepickerViewMode = 'month';
	wizardData!: any;
	isBrowser = false;
	slug = '';
	startDateMonth = false;
	options: any;
	minDate: Date = new Date();
	maxDate: Date = new Date();
	bsInlineValue: any = null;
	endDate: Date | any = null;
	tripLength = 0;
 
	constructor() {
		this.metaTag.removeMetaTags();
 
		this.meta.updateTag({
			name: 'robots',
			content: 'noindex, nofollow',
		});
		if (this.isBrowser) {
			const robotsTag = document.querySelector('meta[name="robots"]');
			if (robotsTag) {
				robotsTag.setAttribute('data-seo-lock', 'true');
			}
		}
		const platformId = this.platformId;
		this.minDate.setMonth(new Date().getMonth() + 2);
		this.maxDate.setMonth(new Date().getMonth() + 18);
		this.isBrowser = isPlatformBrowser(platformId);
		this.options = {
			placeholder: 'mm/dd/yyyy', // HTML input placeholder attribute (default: '')
			maxDate: this.maxDate,
			minDate: this.minDate, // Minimal selectable date
		};
	}
 
	ngOnInit(): void {
		this.slug = this.route.snapshot.params['slug'];
		this.bsConfig = Object.assign(
			{},
			{
				minMode: this.minMode,
				showWeekNumbers: false,
			},
		);
 
		if (this.isBrowser) {
			const id = this.route.snapshot.params['slug'];
			if (isPlatformBrowser(this.platformId)) {
				const data = localStorage.getItem(`wizard-${id}`);
				if (data) {
					this.wizardData = JSON.parse(data);
				}
			}
		}
	}
 
	goBack() {
		this.router.navigateByUrl('/trips/' + this.slug);
	}
 
	toggleStartDate() {
		this.startDateMonth = !this.startDateMonth;
		this.minMode = this.startDateMonth ? 'day' : 'month';
		this.minDate = new Date();
		this.maxDate = new Date();
		if (this.minMode === 'day') {
			this.minDate.setDate(new Date().getDate() + 14);
			this.maxDate.setMonth(new Date().getMonth() + 18);
			this.options = {
				placeholder: 'mm/dd/yyyy', // HTML input placeholder attribute (default: '')
				maxDate: this.maxDate,
				minDate: this.minDate, // Minimal selectable date
			};
		} else {
			this.minDate.setMonth(new Date().getMonth() + 2);
			this.maxDate.setMonth(new Date().getMonth() + 18);
			this.options = {
				placeholder: 'mm/dd/yyyy', // HTML input placeholder attribute (default: '')
				maxDate: this.maxDate,
				minDate: this.minDate, // Minimal selectable date
			};
		}
 
		this.bsConfig = Object.assign(
			{},
			{
				minMode: this.minMode,
				showWeekNumbers: false,
			},
		);
 
		this.cdr.detectChanges();
		this.setArrivalDefaultDate();
	}
 
	continue() {
		if (this.bsInlineValue === null && this.isBrowser) {
			Swal.fire({
				toast: true,
				position: 'top-end',
				showConfirmButton: false,
				timerProgressBar: true,
				showCloseButton: true,
				didOpen: (toast) => {
					toast.onmouseenter = Swal.stopTimer;
					toast.onmouseleave = Swal.resumeTimer;
				},
				icon: 'success',
				title: 'Start Date Not Selected',
				text: 'Please, select a start date',
				background: '#51a351',
				color: '#fff',
				width: '28em',
				timer: 5000,
			});
			return;
		}

		if (this.minMode === 'month') {
			this.bsInlineValue = new Date(this.bsInlineValue.getFullYear(), this.bsInlineValue.getMonth(), 10);
		}

		this.endDate = new Date(+this.bsInlineValue);
		this.tripLength = 4;
		this.endDate.setDate(this.endDate.getDate() + this.tripLength);
		this.wizardData = {
			...this.wizardData,
			'days-set': this.startDateMonth,
			'arrive-day': this.datePipe.transform(this.bsInlineValue, 'yyyy-MM-dd'),
			'depart-day': this.datePipe.transform(this.endDate, 'yyyy-MM-dd'),
		};

		console.log(this.wizardData);
		window.localStorage.setItem(`wizard-${this.slug}`, JSON.stringify(this.wizardData));
		window.localStorage.setItem(`wizard`, JSON.stringify(this.wizardData));
		this.router.navigateByUrl(`/trips/register/step-2/${this.slug}`);
	}
 
	setArrivalDefaultDate(): void {
		if (!this.bsInlineValue) {
			const date = new Date();
			date.setMonth(date.getMonth() + 2);
			this.bsInlineValue = date;
		}
	}
}