Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
- import {DemoService} from './demo.service';
- @Component({
- selector: 'app-root',
- templateUrl: './app.component.html',
- styleUrls: ['./app.component.css']
- })
- export class AppComponent implements OnInit {
- public books;
- public booksCopy;
- public user;
- interval: any;
- public page = 'bookList';
- public loggedUser = '';
- error = '';
- currentBook;
- currentUserStats;
- statEdit;
- currentBookComments;
- currComment;
- statsGet;
- mostReadBook;
- czytanyTyp;
- allTime;
- allUsers;
- horrory;
- fantasy;
- scifi;
- wtrakcie;
- przeczytane;
- napotem;
- pieChartLabels;
- pieChartData;
- doughnutChartLabels;
- doughnutChartData;
- barChartLabels;
- barChartData;
- mezczyzni;
- kobiety;
- trans;
- public barChartOptions:any = {
- title: {
- display: true,
- text: 'Rozkład płci naszych użytkowników'
- }
- };
- public pieChartOptions:any = {
- title: {
- display: true,
- text: 'Status książek naszych użytkowników'
- }
- };
- public doughnutChartOptions:any = {
- title: {
- display: true,
- text: 'Gatunki czytanych książek'
- }
- };
- @ViewChild('nameInput') nameInputRef: ElementRef;
- @ViewChild('passInput') passInputRef: ElementRef;
- @ViewChild('nameRegInput') nameRegInputRef: ElementRef;
- @ViewChild('passRegInput') passRegInputRef: ElementRef;
- @ViewChild('mailInput') mailInputRef: ElementRef;
- @ViewChild('genderInput') genderInputRef: ElementRef;
- @ViewChild('ageInput') ageInputRef: ElementRef;
- @ViewChild('searchInput') searchInputRef: ElementRef;
- @ViewChild('scoreInput') scoreInputRef: ElementRef;
- @ViewChild('statusInput') statusInputRef: ElementRef;
- @ViewChild('typeInput') typeInputRef: ElementRef;
- @ViewChild('timeInput') timeInputRef: ElementRef;
- @ViewChild('currentPageInput') currentPageInputRef: ElementRef;
- @ViewChild('searchUserInput') searchUserInputRef: ElementRef;
- @ViewChild('nameUpInput') nameUpInputRef: ElementRef;
- @ViewChild('passUpInput') passUpInputRef: ElementRef;
- @ViewChild('mailUpInput') mailUpInputRef: ElementRef;
- @ViewChild('genderUpInput') genderUpInputRef: ElementRef;
- @ViewChild('ageUpInput') ageUpInputRef: ElementRef;
- @ViewChild('scoreEditInput') scoreEditInputRef: ElementRef;
- @ViewChild('statusEditInput') statusEditInputRef: ElementRef;
- @ViewChild('typeEditInput') typeEditInputRef: ElementRef;
- @ViewChild('timeEditInput') timeEditInputRef: ElementRef;
- @ViewChild('currentPageEditInput') currentPageEditInputRef: ElementRef;
- @ViewChild('commentInput') commentInputRef: ElementRef;
- constructor(private _demoService: DemoService) {
- }
- ngOnInit() {
- this.getBooks();
- this.getStats();
- this.getAllUsers();
- }
- getBooks() {
- this._demoService.getBooks().subscribe(
- data => {
- this.books = data;
- },
- err => console.error(err),
- () => this.booksCopy = this.books.slice()
- );
- }
- getUser(name: string) {
- this._demoService.getUser(name).subscribe(
- data => {
- if (data !== null && data !== undefined) {
- this.user = data;
- }
- },
- err => console.error(err),
- () => this.currentUserStats = this.user.statistics.slice()
- );
- }
- postStats(score: number, status: string, type: string, time: number, currentPage: number) {
- const stat = {
- status: status,
- score: score,
- type: type,
- time: time,
- currentPage: currentPage,
- user: this.user,
- book: this.currentBook
- };
- this._demoService.postStats(stat).subscribe(
- (response: Response) => {
- console.log(response);
- },
- err => console.error(err),
- () => {
- this.getUser(this.user.name);
- this.getStats();
- }
- );
- }
- postComment(comment) {
- const comm = {
- content: comment,
- book: this.currentBookComments
- };
- this._demoService.postComment(comm).subscribe(
- (response: Response) => {
- console.log(response);
- },
- err => console.error(err),
- () => {
- this.getBooks();
- }
- );
- }
- doLogin() {
- const name = this.nameInputRef.nativeElement.value;
- const password = this.passInputRef.nativeElement.value;
- this.getUser(name);
- setTimeout(() => {
- if (this.user !== undefined && this.user !== null) {
- if (this.user.name === name && this.user.password === password) {
- this.loggedUser = this.user.name;
- this.page = 'bookList';
- this.error = '';
- }
- } else {
- this.error = 'Niepoprawne dane logowania';
- }
- }, 300);
- }
- saveUser(name, password, mail, gender, age) {
- const user = {
- name: name,
- password: password,
- email: mail,
- gender: gender,
- age: age,
- statistics: []
- };
- this._demoService.postUser(user).subscribe(
- (response: Response) => {
- console.log(response);
- },
- err => console.error(err),
- () => this.getAllUsers()
- );
- }
- updateUser(name, password, mail, gender, age) {
- this.user.name = name;
- this.user.password = password;
- this.user.mail = mail;
- this.user.gender = gender;
- this.user.age = age;
- this._demoService.putUser(this.user).subscribe(
- (response: Response) => {
- console.log(response);
- },
- err => console.error(err),
- () => console.log('successfully updated user')
- );
- }
- putStat(score, status, type, time, currentPage) {
- this.statEdit.score = score;
- this.statEdit.status = status;
- this.statEdit.type = type;
- this.statEdit.time = time;
- this.statEdit.currentPage = currentPage;
- this._demoService.putStat(this.statEdit).subscribe(
- (response: Response) => {
- console.log(response);
- },
- err => console.error(err),
- () => this.getStats()
- );
- }
- doRegister() {
- const name = this.nameRegInputRef.nativeElement.value;
- const password = this.passRegInputRef.nativeElement.value;
- const mail = this.mailInputRef.nativeElement.value;
- const gender = this.genderInputRef.nativeElement.value;
- const age = this.ageInputRef.nativeElement.value;
- if (name !== undefined && name !== null && name !== '' &&
- password !== undefined && password !== null && password !== '' &&
- mail !== undefined && mail !== null && mail !== '' &&
- gender !== undefined && gender !== null && gender !== '' &&
- age !== undefined && age !== null && age !== ''
- ) {
- this.page = 'bookList';
- this.error = '';
- this.saveUser(name, password, mail, gender, age);
- } else {
- this.error = 'Proszę uzupełnić wszystkie pola!';
- }
- }
- doUpdateUser() {
- const name = this.nameUpInputRef.nativeElement.value;
- const password = this.passUpInputRef.nativeElement.value;
- const mail = this.mailUpInputRef.nativeElement.value;
- const gender = this.genderUpInputRef.nativeElement.value;
- const age = this.ageUpInputRef.nativeElement.value;
- if (name !== undefined && name !== null && name !== '' &&
- password !== undefined && password !== null && password !== '' &&
- mail !== undefined && mail !== null && mail !== '' &&
- gender !== undefined && gender !== null && gender !== '' &&
- age !== undefined && age !== null && age !== ''
- ) {
- this.page = 'profilePage';
- this.error = '';
- this.updateUser(name, password, mail, gender, age);
- } else {
- this.error = 'Proszę uzupełnić wszystkie pola!';
- }
- }
- doSearch() {
- const search = this.searchInputRef.nativeElement.value;
- const regex = new RegExp(search, 'i');
- this.booksCopy = [];
- for (const book of this.books) {
- if (regex.test(book.title)) {
- this.booksCopy.push(book);
- }
- }
- this.page = 'bookList';
- }
- doUserStatsSearch() {
- const search = this.searchUserInputRef.nativeElement.value;
- const regex = new RegExp(search, 'i');
- this.currentUserStats = [];
- for (const stat of this.user.statistics) {
- if (regex.test(stat.book.title)) {
- this.currentUserStats.push(stat);
- }
- }
- }
- doWszystkieSort() {
- this.currentUserStats = this.user.statistics.slice();
- }
- doPrzeczytaneSort() {
- this.currentUserStats = [];
- for (const stat of this.user.statistics) {
- if (stat.status === 'Przeczytane') {
- this.currentUserStats.push(stat);
- }
- }
- }
- doWtrakcieSort() {
- this.currentUserStats = [];
- for (const stat of this.user.statistics) {
- if (stat.status === 'W trakcie') {
- this.currentUserStats.push(stat);
- }
- }
- }
- doNapotemSort() {
- this.currentUserStats = [];
- for (const stat of this.user.statistics) {
- if (stat.status === 'Na potem') {
- this.currentUserStats.push(stat);
- }
- }
- }
- addBook(index: number) {
- let hasbook = false;
- for (const statistic of this.user.statistics) {
- if (statistic.book.bookID === this.booksCopy[index].bookID) {
- hasbook = true;
- }
- }
- if (hasbook) {
- alert('You already got this book in your collection!!');
- } else {
- this.page = 'addBookPage';
- this.currentBook = this.booksCopy[index];
- }
- }
- editStat(index: number) {
- this.page = 'EditStat';
- this.statEdit = this.currentUserStats[index];
- }
- saveEditStat() {
- const score = this.scoreEditInputRef.nativeElement.value;
- const status = this.statusEditInputRef.nativeElement.value;
- const type = this.typeEditInputRef.nativeElement.value;
- const time = this.timeEditInputRef.nativeElement.value;
- const currentPage = this.currentPageEditInputRef.nativeElement.value;
- if (score !== undefined && score !== null && score !== '' &&
- status !== undefined && status !== null && status !== '' &&
- type !== undefined && type !== null && type !== '' &&
- time !== undefined && time !== null && time !== '' &&
- currentPage !== undefined && currentPage !== null && currentPage !== ''
- ) {
- this.page = 'profilePage';
- this.error = '';
- this.putStat(score, status, type, time, currentPage);
- } else {
- this.error = 'Proszę uzupełnić wszystkie pola!';
- }
- }
- saveBook() {
- const score = this.scoreInputRef.nativeElement.value;
- const status = this.statusInputRef.nativeElement.value;
- const type = this.typeInputRef.nativeElement.value;
- const time = this.timeInputRef.nativeElement.value;
- const currentPage = this.currentPageInputRef.nativeElement.value;
- if (score !== undefined && score !== null && score !== '' &&
- status !== undefined && status !== null && status !== '' &&
- type !== undefined && type !== null && type !== '' &&
- time !== undefined && time !== null && time !== '' &&
- currentPage !== undefined && currentPage !== null && currentPage !== ''
- ) {
- this.page = 'bookList';
- this.error = '';
- this.postStats(score, status, type, time, currentPage);
- } else {
- this.error = 'Proszę uzupełnić wszystkie pola!';
- }
- }
- goBookComment(index: number) {
- this.page = 'bookComments';
- this.currentBookComments = this.booksCopy[index];
- }
- AddComment() {
- const comment = this.commentInputRef.nativeElement.value;
- if (comment !== undefined && comment !== null && comment !== '') {
- this.error = '';
- this.currComment = comment;
- console.log(comment);
- this.currentBookComments.comments.push({content: comment});
- this.postComment(comment);
- this.commentInputRef.nativeElement.value = '';
- } else {
- this.error = 'Proszę napisać coś w komentarzu!';
- }
- }
- // events
- public chartClicked(e: any): void {
- console.log(e);
- }
- public chartHovered(e: any): void {
- console.log(e);
- }
- getStats() {
- this._demoService.getStats().subscribe(
- data => {
- this.statsGet = data;
- },
- err => console.error(err),
- () => this.getAllUsers()
- );
- }
- getAllUsers() {
- this._demoService.getAllUsers().subscribe(
- data => {
- this.allUsers = data;
- },
- err => console.error(err),
- () => {
- setTimeout(() => {
- this.getMostReadBook();
- }, 700);
- }
- );
- }
- getMostReadBook() {
- let setBooks = new Set();
- let arrayBooks = [];
- var ebooki = 0;
- var papierowe = 0;
- var sum = 0;
- var iter = 0;
- var h = 0;
- var f = 0;
- var s = 0;
- var n = 0;
- var w = 0;
- var p = 0;
- var t = 0;
- var m = 0;
- var k = 0;
- for (const stat of this.statsGet) {
- setBooks.add(stat.book.title);
- arrayBooks.push(stat.book.title);
- if (stat.type === 'Ebook') {
- ebooki++;
- } else {
- papierowe++;
- }
- sum += stat.time;
- iter++;
- if (stat.status === 'Na potem') {
- n++;
- } else if (stat.status === 'W trakcie') {
- w++;
- } else {
- p++;
- }
- if (stat.book.genre === 'Horror') {
- h++;
- } else if (stat.book.genre === 'Fantasy') {
- f++;
- } else {
- s++;
- }
- }
- this.wtrakcie = w;
- this.napotem = n;
- this.przeczytane = p;
- this.horrory = h;
- this.fantasy = f;
- this.scifi = s;
- this.allTime = (sum / iter).toFixed(1);
- for (var user of this.allUsers) {
- if (user.gender === 'Transgender') {
- t++;
- } else if (user.gender === 'Kobieta') {
- k++;
- } else {
- m++;
- }
- }
- this.mezczyzni = m;
- this.kobiety = k;
- this.trans = t;
- if (ebooki > papierowe) {
- this.czytanyTyp = 'Ebook';
- } else if (ebooki < papierowe) {
- this.czytanyTyp = 'Papierowa';
- } else {
- this.czytanyTyp = 'Bez różnicy';
- }
- const arraySetBooks = Array.from(setBooks);
- let arrayFinal = [];
- for (const booka of arraySetBooks) {
- var temp = 0;
- for (const bookb of arrayBooks) {
- if (booka === bookb) {
- temp++;
- }
- }
- arrayFinal.push({title: booka, value: temp});
- }
- var bookSorted = arrayFinal.slice();
- bookSorted.sort(function (a, b) {
- return b.value - a.value;
- });
- this.mostReadBook = bookSorted[0].title;
- console.log(this.mostReadBook);
- this.pieChartLabels = ['Przeczytane', 'Na potem', 'W trakcie'];
- this.pieChartData = [this.przeczytane, this.napotem, this.wtrakcie];
- this.doughnutChartLabels = ['Horror', 'Fantasy', 'Sci-fi'];
- this.doughnutChartData = [this.horrory, this.fantasy, this.scifi];
- this.barChartLabels = ['Mężczyźni', 'Kobiety', 'Transgender'];
- this.barChartData = [
- {data: [this.mezczyzni, this.kobiety, this.trans], label: 'Ilość'}
- ];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement