Browse Source

file upload changes

master
Karan 3 years ago
parent
commit
41f195cb8f
  1. 1
      .gitignore
  2. 1
      package.json
  3. 21
      src/app/pages/home/home.component.html
  4. 93
      src/app/pages/home/home.component.ts
  5. 28
      src/app/pages/home/home.service.ts
  6. 44
      src/app/pages/login/login/login.component.ts

1
.gitignore

@ -9,6 +9,7 @@
# dependencies # dependencies
/node_modules /node_modules
/backend
# profiling files # profiling files
chrome-profiler-events*.json chrome-profiler-events*.json

1
package.json

@ -30,6 +30,7 @@
"rxjs": "~6.6.0", "rxjs": "~6.6.0",
"ts-jest": "^27.0.4", "ts-jest": "^27.0.4",
"tslib": "^2.1.0", "tslib": "^2.1.0",
"xlsx": "^0.17.4",
"zone.js": "~0.11.4" "zone.js": "~0.11.4"
}, },
"devDependencies": { "devDependencies": {

21
src/app/pages/home/home.component.html

@ -1,13 +1,15 @@
<div class="page-container" id="home-page"> <div class="page-container" id="home-page">
<div class="header-container"> <div class="header-container">
<app-header [headerInput]="headerProps"></app-header> <app-header [headerInput]="headerProps"></app-header>
</div> </div>
<main class="main-container"> <main class="main-container">
<div class="switch-tab"> <div class="switch-tab">
<p [ngClass]="{active: isSalesForm}" (click)="switchSalesServiceTab()">Sales Upload</p>
<p [ngClass]="{active: !isSalesForm}" (click)="switchSalesServiceTab()">Service Upload</p>
<p [ngClass]="{ active: isSalesForm }" (click)="switchSalesServiceTab()">
Sales Upload
</p>
<p [ngClass]="{ active: !isSalesForm }" (click)="switchSalesServiceTab()">
Service Upload
</p>
</div> </div>
<section id="from-first-section"> <section id="from-first-section">
@ -16,21 +18,20 @@
<br /> <br />
<input <input
type="file" type="file"
formControlName="fileUpload"
class="user-input font-body-small-bold file-input" class="user-input font-body-small-bold file-input"
placeholder="Upload sales file" placeholder="Upload sales file"
(change)="addfile($event)"
/> />
<!-- formControlName="fileUpload" -->
</div> </div>
<button
[disabled]="(!fileUploadForm.valid || isLoading)">
{{isLoading ? 'Sending messages' : 'Submit'}}
<!-- [disabled]="!fileUploadForm.valid || isLoading" -->
<button type="submit">
{{ isLoading ? "Sending messages" : "Submit" }}
</button> </button>
</form> </form>
</section> </section>
<p class="error font-body-small">{{ error }}</p> <p class="error font-body-small">{{ error }}</p>
</main> </main>
</div> </div>

93
src/app/pages/home/home.component.ts

@ -8,15 +8,16 @@ import { LeadscoreService } from 'src/app/shared/services/leadscore.service';
import { first, finalize } from 'rxjs/operators'; import { first, finalize } from 'rxjs/operators';
import { IGoogleFormData } from 'src/app/models/googleFormData'; import { IGoogleFormData } from 'src/app/models/googleFormData';
import { AuthenticationService } from 'src/app/shared/services/authentication.service'; import { AuthenticationService } from 'src/app/shared/services/authentication.service';
import { Subscription } from 'rxjs';
import * as XLSX from 'xlsx';
import { HomeService } from './home.service';
@Component({ @Component({
selector: 'app-home', selector: 'app-home',
templateUrl: './home.component.html', templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
styleUrls: ['./home.component.scss'],
}) })
export class HomeComponent { export class HomeComponent {
headerProps: IHeaderProps = { headerProps: IHeaderProps = {
rightIcon: logOutButton, rightIcon: logOutButton,
rightIconRoute: 'logout', rightIconRoute: 'logout',
@ -28,31 +29,95 @@ export class HomeComponent {
error = ''; error = '';
isSalesForm = true; isSalesForm = true;
file: any;
arrayBuffer: any;
filelist: any = [];
arrayToBeSend: any = [];
subscriptions: Subscription[] = [];
constructor(private routerService: RouterService,
private formBuilder: FormBuilder) {
this.fileUploadForm = this.formBuilder.group(
{
constructor(
private routerService: RouterService,
private formBuilder: FormBuilder,
private homeService: HomeService
) {
this.fileUploadForm = this.formBuilder.group({
fileUpload: [ fileUpload: [
'', '',
[ [
Validators.required, Validators.required,
Validators.minLength(10), Validators.minLength(10),
Validators.pattern('^[0-9]*')
]
]
}
)
Validators.pattern('^[0-9]*'),
],
],
});
} }
get f() { return this.fileUploadForm.controls; }
get f() {
return this.fileUploadForm.controls;
}
switchSalesServiceTab() { switchSalesServiceTab() {
this.arrayToBeSend = [];
this.isSalesForm = !this.isSalesForm; this.isSalesForm = !this.isSalesForm;
} }
submitForm() { submitForm() {
console.log('inside submit form');
let fileReader = new FileReader();
fileReader.readAsArrayBuffer(this.file);
fileReader.onload = (e) => {
this.arrayBuffer = fileReader.result;
var data = new Uint8Array(this.arrayBuffer);
var arr = new Array();
for (var i = 0; i != data.length; ++i)
arr[i] = String.fromCharCode(data[i]);
var bstr = arr.join('');
var workbook = XLSX.read(bstr, { type: 'binary' });
var first_sheet_name = workbook.SheetNames[0];
var worksheet = workbook.Sheets[first_sheet_name];
// console.log(XLSX.utils.sheet_to_json(worksheet, { raw: true }));
// var arraylist = XLSX.utils.sheet_to_json(worksheet, { raw: true });
this.filelist = XLSX.utils.sheet_to_json(worksheet, { raw: true });
console.log(this.filelist);
if (this.isSalesForm) {
console.log(this.isSalesForm);
for (let i = 0; i < this.filelist.length; i++) {
var details = {
'CUSTOMER NAME': this.filelist[i]['CUSTOMER NAME'],
'PHONE NUMBER': this.filelist[i]['PHONE NUMBER'],
'LEAD ID': this.filelist[i]['LEAD ID'],
'DATE OF ENQUIRY': this.filelist[i]['DATE OF ENQUIRY'],
};
this.arrayToBeSend.push(details);
}
} else {
console.log(this.isSalesForm);
for (let i = 0; i < this.filelist.length; i++) {
var details = {
'CUSTOMER NAME': this.filelist[i]['Customer'],
'PHONE NUMBER': this.filelist[i]['Mobile No'],
'LEAD ID': this.filelist[i]['__EMPTY'],
'DATE OF ENQUIRY': this.filelist[i]['Ready For Invoice Date Time'],
};
this.arrayToBeSend.push(details);
}
}
console.log(this.arrayToBeSend);
this.subscriptions.push(
this.homeService.sendWhatsApp(this.arrayToBeSend).subscribe(
(res) => {
console.log(res);
},
(err) => {
console.log(err);
}
)
);
};
} }
addfile(event: any) {
this.file = event.target.files[0];
console.log('insiide addfile function');
}
} }

28
src/app/pages/home/home.service.ts

@ -1,21 +1,41 @@
// Angular imports // Angular imports
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
// Local imports // Local imports
import { IHomeRequest, IHomeResponse } from '../../../app/model/home'; import { IHomeRequest, IHomeResponse } from '../../../app/model/home';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
Authorization:
'Basic 2b8cba9008e1da2f2733ff8300cf1973-dc928670-56f9-4a29-8725-69239d669e74',
}),
};
// Authorization: 'Basic SW50ZXJuYXRpb25hbFdBX1BPQzpFeHBvcnRzITIwMjE=',
const url = 'http://localhost:3000/';
const whatsAppUrl =
'https://mpwyr4.api.infobip.com/whatsapp/1/message/template';
@Injectable({ @Injectable({
providedIn: 'root'
providedIn: 'root',
}) })
export class HomeService { export class HomeService {
private baseUrl = 'http://localhost:8000/home'; private baseUrl = 'http://localhost:8000/home';
constructor(private http: HttpClient) {
}
constructor(private http: HttpClient) {}
post(data: any): Observable<any> { post(data: any): Observable<any> {
return this.http.post<any>(this.baseUrl, data); return this.http.post<any>(this.baseUrl, data);
} }
sendEmail(body: any): Observable<any> {
return this.http.post(url + 'sendEmail', body, httpOptions);
}
sendWhatsApp(body: any): Observable<any> {
return this.http.post(whatsAppUrl, body, httpOptions);
}
} }

44
src/app/pages/login/login/login.component.ts

@ -1,5 +1,10 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import {
FormBuilder,
FormControl,
FormGroup,
Validators,
} from '@angular/forms';
import { home, success, login } from 'src/app/constants/constants'; import { home, success, login } from 'src/app/constants/constants';
import { invalidUsernameAndPassword } from 'src/app/constants/errorConstants'; import { invalidUsernameAndPassword } from 'src/app/constants/errorConstants';
import { RouterService } from 'src/app/shared/services/routerService'; import { RouterService } from 'src/app/shared/services/routerService';
@ -11,35 +16,37 @@ import { first } from 'rxjs/operators';
@Component({ @Component({
selector: 'app-login', selector: 'app-login',
templateUrl: './login.component.html', templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
styleUrls: ['./login.component.scss'],
}) })
export class LoginComponent implements OnInit { export class LoginComponent implements OnInit {
loginForm: FormGroup; loginForm: FormGroup;
username = ''; username = '';
password = ''; password = '';
error = ''; error = '';
loading = false; loading = false;
constructor(private routerService: RouterService,
constructor(
private routerService: RouterService,
private authenticationService: AuthenticationService, private authenticationService: AuthenticationService,
private formBuilder: FormBuilder) {
private formBuilder: FormBuilder
) {
// redirect to home if already logged in // redirect to home if already logged in
if (localStorage.getItem('loggedInUser')) { if (localStorage.getItem('loggedInUser')) {
this.routerService.navigate(home); this.routerService.navigate(home);
} }
this.loginForm = this.formBuilder.group({ this.loginForm = this.formBuilder.group({
username: ['', Validators.required], username: ['', Validators.required],
password: ['', Validators.required]
})
password: ['', Validators.required],
});
this.loginForm.valueChanges.subscribe(() => { this.loginForm.valueChanges.subscribe(() => {
this.error = ''; this.error = '';
}); });
} }
get f() { return this.loginForm.controls; }
get f() {
return this.loginForm.controls;
}
/** /**
* Login button click handler * Login button click handler
@ -48,20 +55,22 @@ export class LoginComponent implements OnInit {
*/ */
loginClickHandler() { loginClickHandler() {
this.loading = true; this.loading = true;
if(this.authenticationService.login(this.f.username.value, this.f.password.value)){
if (
this.authenticationService.login(
this.f.username.value,
this.f.password.value
)
) {
this.routerService.navigate(home); this.routerService.navigate(home);
}
else {
} else {
this.loading = false; this.loading = false;
this.error = 'Login failed, Please try again!'
this.error = 'Login failed, Please try again!';
} }
} }
inputChangeHandler() {
}
inputChangeHandler() {}
ngOnInit(): void { ngOnInit(): void {
if (localStorage.getItem('loggedInUser')) { if (localStorage.getItem('loggedInUser')) {
// logged in so return true // logged in so return true
this.routerService.navigate(home); this.routerService.navigate(home);
@ -69,7 +78,4 @@ export class LoginComponent implements OnInit {
this.routerService.navigate(login); this.routerService.navigate(login);
} }
} }
} }
Loading…
Cancel
Save