job-form.component.ts 7.21 KB
Newer Older
vtduong0912's avatar
vtduong0912 committed
1
import { Component, EventEmitter, Output } from '@angular/core';
vtduong0912's avatar
vtduong0912 committed
2 3 4 5 6 7
import { FormBuilder, FormGroup, Validators, ReactiveFormsModule } from '@angular/forms';
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzModalModule } from 'ng-zorro-antd/modal';
import { JobManagerService } from '../data-access/service/job-manager.service';
import { ResponseResult } from '../../../../shared/data-access/interface/response.type';
import { JobApi } from '../data-access/model/job-manager.model';
vtduong0912's avatar
vtduong0912 committed
8
import { NzNotificationService } from 'ng-zorro-antd/notification'
vtduong0912's avatar
vtduong0912 committed
9
import { catchError, of, tap } from 'rxjs';
vtduong0912's avatar
vtduong0912 committed
10
import { NzFormModule } from 'ng-zorro-antd/form';
vtduong0912's avatar
vtduong0912 committed
11 12 13 14

@Component({
    selector: 'job-form',
    standalone: true,
vtduong0912's avatar
vtduong0912 committed
15 16 17
    imports: [
        NzButtonModule,
        NzModalModule,
vtduong0912's avatar
vtduong0912 committed
18 19
        ReactiveFormsModule,
        NzFormModule
vtduong0912's avatar
vtduong0912 committed
20
    ],
vtduong0912's avatar
vtduong0912 committed
21
    template: `
vtduong0912's avatar
vtduong0912 committed
22 23
        <div class="tw-w-full tw-grid tw-justify-items-end">
            <button nz-button nzType="primary" (click)="isCreate = true">Create new job</button>
vtduong0912's avatar
vtduong0912 committed
24
        </div>
vtduong0912's avatar
vtduong0912 committed
25
        <nz-modal [(nzVisible)]="isCreate" nzTitle="Create new job" nzCancelText="Cancel" nzOkText="Create" (nzOnCancel)="isCreate = false" (nzOnOk)="onCreateSubmit()">
vtduong0912's avatar
vtduong0912 committed
26
        <ng-container *nzModalContent>
vtduong0912's avatar
vtduong0912 committed
27
            <form nz-form [formGroup]="jobCreatingFormGroup">
vtduong0912's avatar
vtduong0912 committed
28 29 30 31 32 33 34
            <div class="tw-flex tw-flex-col tw-flex-wrap tw-gap-y-1">
            <label>Type</label>
                <select class="tw-border tw-rounded-lg tw-h-8" formControlName="type">
                    <option value="Full Time">Full Time</option>
                    <option value="Part Time">Part Time</option>
                </select>
                <label>Company</label>
vtduong0912's avatar
vtduong0912 committed
35 36 37 38 39 40 41
                <nz-form-item>
                    <nz-form-control nzErrorTip="Please enter company!">
                        <nz-input-group>
                            <input class="tw-border tw-rounded-lg tw-w-full tw-h-8" type="text" formControlName="company">
                        </nz-input-group>
                    </nz-form-control>
                </nz-form-item>
vtduong0912's avatar
vtduong0912 committed
42
                <label>Company Url</label>
vtduong0912's avatar
vtduong0912 committed
43 44 45 46 47 48 49
                <nz-form-item>
                    <nz-form-control nzErrorTip="Please enter company url!">
                        <nz-input-group>
                        <input class="tw-border tw-rounded-lg tw-w-full tw-h-8" type="text" formControlName="company_url">
                        </nz-input-group>
                    </nz-form-control>
                </nz-form-item>
vtduong0912's avatar
vtduong0912 committed
50
                <label>Location</label>
vtduong0912's avatar
vtduong0912 committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
                <nz-form-item>
                    <nz-form-control nzErrorTip="Please enter location!">
                        <nz-input-group>
                        <input class="tw-border tw-rounded-lg tw-w-full tw-h-8" type="text" formControlName="location">
                        </nz-input-group>
                    </nz-form-control>
                </nz-form-item>
                <label>Title</label>
                <nz-form-item>
                    <nz-form-control nzErrorTip="Please enter title!">
                        <nz-input-group>
                        <input class="tw-border tw-rounded-lg tw-w-full tw-h-8" type="text" formControlName="title">
                        </nz-input-group>
                    </nz-form-control>
                </nz-form-item>
vtduong0912's avatar
vtduong0912 committed
66
                <label>Description</label>
vtduong0912's avatar
vtduong0912 committed
67 68 69 70 71 72 73
                <nz-form-item>
                    <nz-form-control nzErrorTip="Please enter description!">
                        <nz-input-group>
                        <textarea class="tw-border tw-rounded-lg tw-w-full tw-h-20" type="text" formControlName="description"></textarea>
                        </nz-input-group>
                    </nz-form-control>
                </nz-form-item>
vtduong0912's avatar
vtduong0912 committed
74
                <label>How to Apply</label>
vtduong0912's avatar
vtduong0912 committed
75 76 77 78 79 80 81
                <nz-form-item>
                    <nz-form-control nzErrorTip="Please enter 'How to Apply'!">
                        <nz-input-group>
                        <input class="tw-border tw-rounded-lg tw-w-full tw-h-8" type="text" formControlName="how_to_apply">
                        </nz-input-group>
                    </nz-form-control>
                </nz-form-item>
vtduong0912's avatar
vtduong0912 committed
82
                <label>Company Logo</label>
vtduong0912's avatar
vtduong0912 committed
83 84 85 86 87 88 89
                <nz-form-item>
                    <nz-form-control nzErrorTip="Please enter Company Logo!">
                        <nz-input-group>
                        <input class="tw-border tw-rounded-lg tw-w-full tw-h-8" type="text" formControlName="company_logo">
                        </nz-input-group>
                    </nz-form-control>
                </nz-form-item>
vtduong0912's avatar
vtduong0912 committed
90
                <label>Url</label>
vtduong0912's avatar
vtduong0912 committed
91 92 93 94 95 96 97
                <nz-form-item>
                    <nz-form-control nzErrorTip="Please enter Url!">
                        <nz-input-group>
                        <input class="tw-border tw-rounded-lg tw-w-full tw-h-8" type="text" formControlName="url">
                        </nz-input-group>
                    </nz-form-control>
                </nz-form-item>
vtduong0912's avatar
vtduong0912 committed
98 99 100 101
            </div>
            </form>
        </ng-container>
        </nz-modal>
vtduong0912's avatar
vtduong0912 committed
102 103 104
    `
})

vtduong0912's avatar
vtduong0912 committed
105 106
export class JobFormComponent {
    @Output() ReloadData = new EventEmitter();
vtduong0912's avatar
vtduong0912 committed
107

vtduong0912's avatar
vtduong0912 committed
108 109 110
    isCreate: boolean = false;
    jobCreatingFormGroup: FormGroup;

vtduong0912's avatar
vtduong0912 committed
111 112 113
    constructor(private _fb: FormBuilder,
        private _service: JobManagerService,
        private _notification: NzNotificationService) {
vtduong0912's avatar
vtduong0912 committed
114 115 116 117 118 119 120 121 122 123 124 125 126
        this.jobCreatingFormGroup = this._fb.group({
            type: ['Full Time', Validators.required],
            company: ['', Validators.required],
            company_url: ['', Validators.required],
            location: ['', Validators.required],
            title: ['', Validators.required],
            description: ['', Validators.required],
            how_to_apply: ['', Validators.required],
            company_logo: ['', Validators.required],
            url: ['', Validators.required],
        });
    }

vtduong0912's avatar
vtduong0912 committed
127 128 129 130 131 132
    createNotification(type: string, title: string, message: string): void {
        this._notification.create(
            type,
            title,
            message
        );
vtduong0912's avatar
vtduong0912 committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    }

    onCreateSubmit() {
        const data = this.jobCreatingFormGroup.value;
        if (!this.jobCreatingFormGroup.valid) {
            Object.values(this.jobCreatingFormGroup.controls).forEach(control => {
                if (control.invalid) {
                    control.markAsDirty();
                    control.updateValueAndValidity({ onlySelf: true });
                }
            });
            return;
        }
        this.jobCreatingFormGroup.markAllAsTouched();
        this._service.jobsPost(data)
            .pipe(
                tap((response: ResponseResult<JobApi.Request>) => {
vtduong0912's avatar
vtduong0912 committed
150 151 152 153 154 155
                    this.createNotification(
                        'success',
                        'Success!',
                        'You have created a new job successfully!'
                    );
                    this.ReloadData.emit();
vtduong0912's avatar
vtduong0912 committed
156 157
                }),
                catchError((err) => {
vtduong0912's avatar
vtduong0912 committed
158 159 160 161 162
                    this.createNotification(
                        'error',
                        'Error!',
                        'An error has occurred! Please try later!'
                    )
vtduong0912's avatar
vtduong0912 committed
163 164 165 166
                    return of(null);
                })
            )
            .subscribe();
vtduong0912's avatar
vtduong0912 committed
167
            this.isCreate = false;
vtduong0912's avatar
vtduong0912 committed
168
    }
vtduong0912's avatar
vtduong0912 committed
169
}