Advertisement
Guest User

Untitled

a guest
May 13th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.93 KB | None | 0 0
  1. import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
  2. import {DemoService} from './demo.service';
  3.  
  4. @Component({
  5. selector: 'app-root',
  6. templateUrl: './app.component.html',
  7. styleUrls: ['./app.component.css']
  8. })
  9. export class AppComponent implements OnInit {
  10. public books;
  11. public booksCopy;
  12. public user;
  13. interval: any;
  14. public page = 'bookList';
  15. public loggedUser = '';
  16. error = '';
  17. currentBook;
  18. currentUserStats;
  19. statEdit;
  20. currentBookComments;
  21. currComment;
  22. statsGet;
  23. mostReadBook;
  24. czytanyTyp;
  25. allTime;
  26. allUsers;
  27. horrory;
  28. fantasy;
  29. scifi;
  30. wtrakcie;
  31. przeczytane;
  32. napotem;
  33. pieChartLabels;
  34. pieChartData;
  35. doughnutChartLabels;
  36. doughnutChartData;
  37. barChartLabels;
  38. barChartData;
  39. mezczyzni;
  40. kobiety;
  41. trans;
  42.  
  43. public barChartOptions:any = {
  44. title: {
  45. display: true,
  46. text: 'Rozkład płci naszych użytkowników'
  47. }
  48. };
  49.  
  50. public pieChartOptions:any = {
  51. title: {
  52. display: true,
  53. text: 'Status książek naszych użytkowników'
  54. }
  55. };
  56.  
  57. public doughnutChartOptions:any = {
  58. title: {
  59. display: true,
  60. text: 'Gatunki czytanych książek'
  61. }
  62. };
  63.  
  64. @ViewChild('nameInput') nameInputRef: ElementRef;
  65. @ViewChild('passInput') passInputRef: ElementRef;
  66.  
  67. @ViewChild('nameRegInput') nameRegInputRef: ElementRef;
  68. @ViewChild('passRegInput') passRegInputRef: ElementRef;
  69. @ViewChild('mailInput') mailInputRef: ElementRef;
  70. @ViewChild('genderInput') genderInputRef: ElementRef;
  71. @ViewChild('ageInput') ageInputRef: ElementRef;
  72.  
  73. @ViewChild('searchInput') searchInputRef: ElementRef;
  74.  
  75. @ViewChild('scoreInput') scoreInputRef: ElementRef;
  76. @ViewChild('statusInput') statusInputRef: ElementRef;
  77. @ViewChild('typeInput') typeInputRef: ElementRef;
  78. @ViewChild('timeInput') timeInputRef: ElementRef;
  79. @ViewChild('currentPageInput') currentPageInputRef: ElementRef;
  80.  
  81. @ViewChild('searchUserInput') searchUserInputRef: ElementRef;
  82.  
  83. @ViewChild('nameUpInput') nameUpInputRef: ElementRef;
  84. @ViewChild('passUpInput') passUpInputRef: ElementRef;
  85. @ViewChild('mailUpInput') mailUpInputRef: ElementRef;
  86. @ViewChild('genderUpInput') genderUpInputRef: ElementRef;
  87. @ViewChild('ageUpInput') ageUpInputRef: ElementRef;
  88.  
  89. @ViewChild('scoreEditInput') scoreEditInputRef: ElementRef;
  90. @ViewChild('statusEditInput') statusEditInputRef: ElementRef;
  91. @ViewChild('typeEditInput') typeEditInputRef: ElementRef;
  92. @ViewChild('timeEditInput') timeEditInputRef: ElementRef;
  93. @ViewChild('currentPageEditInput') currentPageEditInputRef: ElementRef;
  94.  
  95. @ViewChild('commentInput') commentInputRef: ElementRef;
  96.  
  97. constructor(private _demoService: DemoService) {
  98. }
  99.  
  100. ngOnInit() {
  101. this.getBooks();
  102. this.getStats();
  103. this.getAllUsers();
  104. }
  105.  
  106. getBooks() {
  107. this._demoService.getBooks().subscribe(
  108. data => {
  109. this.books = data;
  110. },
  111. err => console.error(err),
  112. () => this.booksCopy = this.books.slice()
  113. );
  114. }
  115.  
  116. getUser(name: string) {
  117. this._demoService.getUser(name).subscribe(
  118. data => {
  119. if (data !== null && data !== undefined) {
  120. this.user = data;
  121. }
  122. },
  123. err => console.error(err),
  124. () => this.currentUserStats = this.user.statistics.slice()
  125. );
  126. }
  127.  
  128. postStats(score: number, status: string, type: string, time: number, currentPage: number) {
  129. const stat = {
  130. status: status,
  131. score: score,
  132. type: type,
  133. time: time,
  134. currentPage: currentPage,
  135. user: this.user,
  136. book: this.currentBook
  137. };
  138.  
  139. this._demoService.postStats(stat).subscribe(
  140. (response: Response) => {
  141. console.log(response);
  142. },
  143. err => console.error(err),
  144. () => {
  145. this.getUser(this.user.name);
  146. this.getStats();
  147. }
  148. );
  149. }
  150.  
  151. postComment(comment) {
  152. const comm = {
  153. content: comment,
  154. book: this.currentBookComments
  155. };
  156.  
  157. this._demoService.postComment(comm).subscribe(
  158. (response: Response) => {
  159. console.log(response);
  160. },
  161. err => console.error(err),
  162. () => {
  163. this.getBooks();
  164. }
  165. );
  166. }
  167.  
  168. doLogin() {
  169. const name = this.nameInputRef.nativeElement.value;
  170. const password = this.passInputRef.nativeElement.value;
  171. this.getUser(name);
  172. setTimeout(() => {
  173. if (this.user !== undefined && this.user !== null) {
  174. if (this.user.name === name && this.user.password === password) {
  175. this.loggedUser = this.user.name;
  176. this.page = 'bookList';
  177. this.error = '';
  178. }
  179. } else {
  180. this.error = 'Niepoprawne dane logowania';
  181. }
  182. }, 300);
  183. }
  184.  
  185. saveUser(name, password, mail, gender, age) {
  186. const user = {
  187. name: name,
  188. password: password,
  189. email: mail,
  190. gender: gender,
  191. age: age,
  192. statistics: []
  193. };
  194.  
  195. this._demoService.postUser(user).subscribe(
  196. (response: Response) => {
  197. console.log(response);
  198. },
  199. err => console.error(err),
  200. () => this.getAllUsers()
  201. );
  202. }
  203.  
  204. updateUser(name, password, mail, gender, age) {
  205. this.user.name = name;
  206. this.user.password = password;
  207. this.user.mail = mail;
  208. this.user.gender = gender;
  209. this.user.age = age;
  210.  
  211. this._demoService.putUser(this.user).subscribe(
  212. (response: Response) => {
  213. console.log(response);
  214. },
  215. err => console.error(err),
  216. () => console.log('successfully updated user')
  217. );
  218.  
  219. }
  220.  
  221. putStat(score, status, type, time, currentPage) {
  222. this.statEdit.score = score;
  223. this.statEdit.status = status;
  224. this.statEdit.type = type;
  225. this.statEdit.time = time;
  226. this.statEdit.currentPage = currentPage;
  227.  
  228. this._demoService.putStat(this.statEdit).subscribe(
  229. (response: Response) => {
  230. console.log(response);
  231. },
  232. err => console.error(err),
  233. () => this.getStats()
  234. );
  235. }
  236.  
  237. doRegister() {
  238. const name = this.nameRegInputRef.nativeElement.value;
  239. const password = this.passRegInputRef.nativeElement.value;
  240. const mail = this.mailInputRef.nativeElement.value;
  241. const gender = this.genderInputRef.nativeElement.value;
  242. const age = this.ageInputRef.nativeElement.value;
  243.  
  244. if (name !== undefined && name !== null && name !== '' &&
  245. password !== undefined && password !== null && password !== '' &&
  246. mail !== undefined && mail !== null && mail !== '' &&
  247. gender !== undefined && gender !== null && gender !== '' &&
  248. age !== undefined && age !== null && age !== ''
  249. ) {
  250. this.page = 'bookList';
  251. this.error = '';
  252. this.saveUser(name, password, mail, gender, age);
  253. } else {
  254. this.error = 'Proszę uzupełnić wszystkie pola!';
  255. }
  256. }
  257.  
  258. doUpdateUser() {
  259. const name = this.nameUpInputRef.nativeElement.value;
  260. const password = this.passUpInputRef.nativeElement.value;
  261. const mail = this.mailUpInputRef.nativeElement.value;
  262. const gender = this.genderUpInputRef.nativeElement.value;
  263. const age = this.ageUpInputRef.nativeElement.value;
  264.  
  265. if (name !== undefined && name !== null && name !== '' &&
  266. password !== undefined && password !== null && password !== '' &&
  267. mail !== undefined && mail !== null && mail !== '' &&
  268. gender !== undefined && gender !== null && gender !== '' &&
  269. age !== undefined && age !== null && age !== ''
  270. ) {
  271. this.page = 'profilePage';
  272. this.error = '';
  273. this.updateUser(name, password, mail, gender, age);
  274. } else {
  275. this.error = 'Proszę uzupełnić wszystkie pola!';
  276. }
  277. }
  278.  
  279. doSearch() {
  280. const search = this.searchInputRef.nativeElement.value;
  281. const regex = new RegExp(search, 'i');
  282. this.booksCopy = [];
  283. for (const book of this.books) {
  284. if (regex.test(book.title)) {
  285. this.booksCopy.push(book);
  286. }
  287. }
  288. this.page = 'bookList';
  289. }
  290.  
  291. doUserStatsSearch() {
  292. const search = this.searchUserInputRef.nativeElement.value;
  293. const regex = new RegExp(search, 'i');
  294. this.currentUserStats = [];
  295. for (const stat of this.user.statistics) {
  296. if (regex.test(stat.book.title)) {
  297. this.currentUserStats.push(stat);
  298. }
  299. }
  300. }
  301.  
  302. doWszystkieSort() {
  303. this.currentUserStats = this.user.statistics.slice();
  304. }
  305.  
  306. doPrzeczytaneSort() {
  307. this.currentUserStats = [];
  308. for (const stat of this.user.statistics) {
  309. if (stat.status === 'Przeczytane') {
  310. this.currentUserStats.push(stat);
  311. }
  312. }
  313. }
  314.  
  315. doWtrakcieSort() {
  316. this.currentUserStats = [];
  317. for (const stat of this.user.statistics) {
  318. if (stat.status === 'W trakcie') {
  319. this.currentUserStats.push(stat);
  320. }
  321. }
  322. }
  323.  
  324. doNapotemSort() {
  325. this.currentUserStats = [];
  326. for (const stat of this.user.statistics) {
  327. if (stat.status === 'Na potem') {
  328. this.currentUserStats.push(stat);
  329. }
  330. }
  331. }
  332.  
  333. addBook(index: number) {
  334. let hasbook = false;
  335. for (const statistic of this.user.statistics) {
  336. if (statistic.book.bookID === this.booksCopy[index].bookID) {
  337. hasbook = true;
  338. }
  339. }
  340.  
  341. if (hasbook) {
  342. alert('You already got this book in your collection!!');
  343. } else {
  344. this.page = 'addBookPage';
  345. this.currentBook = this.booksCopy[index];
  346. }
  347. }
  348.  
  349. editStat(index: number) {
  350. this.page = 'EditStat';
  351. this.statEdit = this.currentUserStats[index];
  352. }
  353.  
  354. saveEditStat() {
  355. const score = this.scoreEditInputRef.nativeElement.value;
  356. const status = this.statusEditInputRef.nativeElement.value;
  357. const type = this.typeEditInputRef.nativeElement.value;
  358. const time = this.timeEditInputRef.nativeElement.value;
  359. const currentPage = this.currentPageEditInputRef.nativeElement.value;
  360.  
  361. if (score !== undefined && score !== null && score !== '' &&
  362. status !== undefined && status !== null && status !== '' &&
  363. type !== undefined && type !== null && type !== '' &&
  364. time !== undefined && time !== null && time !== '' &&
  365. currentPage !== undefined && currentPage !== null && currentPage !== ''
  366. ) {
  367. this.page = 'profilePage';
  368. this.error = '';
  369. this.putStat(score, status, type, time, currentPage);
  370. } else {
  371. this.error = 'Proszę uzupełnić wszystkie pola!';
  372. }
  373. }
  374.  
  375. saveBook() {
  376. const score = this.scoreInputRef.nativeElement.value;
  377. const status = this.statusInputRef.nativeElement.value;
  378. const type = this.typeInputRef.nativeElement.value;
  379. const time = this.timeInputRef.nativeElement.value;
  380. const currentPage = this.currentPageInputRef.nativeElement.value;
  381.  
  382. if (score !== undefined && score !== null && score !== '' &&
  383. status !== undefined && status !== null && status !== '' &&
  384. type !== undefined && type !== null && type !== '' &&
  385. time !== undefined && time !== null && time !== '' &&
  386. currentPage !== undefined && currentPage !== null && currentPage !== ''
  387. ) {
  388. this.page = 'bookList';
  389. this.error = '';
  390. this.postStats(score, status, type, time, currentPage);
  391. } else {
  392. this.error = 'Proszę uzupełnić wszystkie pola!';
  393. }
  394.  
  395. }
  396.  
  397. goBookComment(index: number) {
  398. this.page = 'bookComments';
  399. this.currentBookComments = this.booksCopy[index];
  400. }
  401.  
  402. AddComment() {
  403. const comment = this.commentInputRef.nativeElement.value;
  404.  
  405. if (comment !== undefined && comment !== null && comment !== '') {
  406. this.error = '';
  407. this.currComment = comment;
  408. console.log(comment);
  409. this.currentBookComments.comments.push({content: comment});
  410. this.postComment(comment);
  411. this.commentInputRef.nativeElement.value = '';
  412. } else {
  413. this.error = 'Proszę napisać coś w komentarzu!';
  414. }
  415. }
  416.  
  417. // events
  418. public chartClicked(e: any): void {
  419. console.log(e);
  420. }
  421.  
  422. public chartHovered(e: any): void {
  423. console.log(e);
  424. }
  425.  
  426. getStats() {
  427. this._demoService.getStats().subscribe(
  428. data => {
  429. this.statsGet = data;
  430. },
  431. err => console.error(err),
  432. () => this.getAllUsers()
  433. );
  434. }
  435.  
  436. getAllUsers() {
  437. this._demoService.getAllUsers().subscribe(
  438. data => {
  439. this.allUsers = data;
  440. },
  441. err => console.error(err),
  442. () => {
  443. setTimeout(() => {
  444. this.getMostReadBook();
  445. }, 700);
  446. }
  447. );
  448. }
  449.  
  450. getMostReadBook() {
  451. let setBooks = new Set();
  452. let arrayBooks = [];
  453. var ebooki = 0;
  454. var papierowe = 0;
  455. var sum = 0;
  456. var iter = 0;
  457. var h = 0;
  458. var f = 0;
  459. var s = 0;
  460. var n = 0;
  461. var w = 0;
  462. var p = 0;
  463. var t = 0;
  464. var m = 0;
  465. var k = 0;
  466. for (const stat of this.statsGet) {
  467. setBooks.add(stat.book.title);
  468. arrayBooks.push(stat.book.title);
  469. if (stat.type === 'Ebook') {
  470. ebooki++;
  471. } else {
  472. papierowe++;
  473. }
  474. sum += stat.time;
  475. iter++;
  476.  
  477. if (stat.status === 'Na potem') {
  478. n++;
  479. } else if (stat.status === 'W trakcie') {
  480. w++;
  481. } else {
  482. p++;
  483. }
  484.  
  485. if (stat.book.genre === 'Horror') {
  486. h++;
  487. } else if (stat.book.genre === 'Fantasy') {
  488. f++;
  489. } else {
  490. s++;
  491. }
  492.  
  493. }
  494. this.wtrakcie = w;
  495. this.napotem = n;
  496. this.przeczytane = p;
  497.  
  498. this.horrory = h;
  499. this.fantasy = f;
  500. this.scifi = s;
  501.  
  502. this.allTime = (sum / iter).toFixed(1);
  503.  
  504. for (var user of this.allUsers) {
  505. if (user.gender === 'Transgender') {
  506. t++;
  507. } else if (user.gender === 'Kobieta') {
  508. k++;
  509. } else {
  510. m++;
  511. }
  512. }
  513.  
  514. this.mezczyzni = m;
  515. this.kobiety = k;
  516. this.trans = t;
  517.  
  518. if (ebooki > papierowe) {
  519. this.czytanyTyp = 'Ebook';
  520. } else if (ebooki < papierowe) {
  521. this.czytanyTyp = 'Papierowa';
  522. } else {
  523. this.czytanyTyp = 'Bez różnicy';
  524. }
  525.  
  526. const arraySetBooks = Array.from(setBooks);
  527. let arrayFinal = [];
  528.  
  529. for (const booka of arraySetBooks) {
  530. var temp = 0;
  531. for (const bookb of arrayBooks) {
  532. if (booka === bookb) {
  533. temp++;
  534. }
  535. }
  536. arrayFinal.push({title: booka, value: temp});
  537. }
  538.  
  539. var bookSorted = arrayFinal.slice();
  540. bookSorted.sort(function (a, b) {
  541. return b.value - a.value;
  542. });
  543.  
  544. this.mostReadBook = bookSorted[0].title;
  545. console.log(this.mostReadBook);
  546.  
  547. this.pieChartLabels = ['Przeczytane', 'Na potem', 'W trakcie'];
  548. this.pieChartData = [this.przeczytane, this.napotem, this.wtrakcie];
  549.  
  550. this.doughnutChartLabels = ['Horror', 'Fantasy', 'Sci-fi'];
  551. this.doughnutChartData = [this.horrory, this.fantasy, this.scifi];
  552.  
  553. this.barChartLabels = ['Mężczyźni', 'Kobiety', 'Transgender'];
  554. this.barChartData = [
  555. {data: [this.mezczyzni, this.kobiety, this.trans], label: 'Ilość'}
  556. ];
  557. }
  558. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement