Guest User

Untitled

a guest
Jun 7th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.37 KB | None | 0 0
  1. import {Component, OnInit} from '@angular/core';
  2. import { RegisterService } from '../../services/register/register.service';
  3. import { User } from '../../../User';
  4. import { Router } from '@angular/router';
  5. import {Http} from "@angular/http";
  6. import {stat} from "fs";
  7.  
  8.  
  9.  
  10. @Component({
  11. moduleId: module.id,
  12. selector: 'register',
  13. templateUrl: 'register.component.html',
  14. providers: [RegisterService]
  15. })
  16.  
  17. export class RegisterComponent implements OnInit {
  18.  
  19. //initialisation de notre classe/modele User/ts
  20. user : User;
  21. email : string;
  22. pseudo : string;
  23. birthday : Date;
  24. password: string;
  25. ConfirmPassword :string;
  26. status:string;
  27. //contactList = [];
  28. file: File;
  29. namePicture = '';
  30. isNewUser : boolean;
  31. isExistResult : boolean;
  32. isEmailExistResult : boolean;
  33. userIsRegistered : boolean;
  34. source='http://localhost:3000/public/profile.jpg';
  35. //password : string;
  36.  
  37. /*ici les petites salopes on passe en parametre notre service qui push dans la DB en appelant
  38. }
  39. filesToUpload: Array<File>;
  40. les routes de notre fichier register.js du serveur , pour le call a chaque fois qu on appelera ce component
  41. */
  42. filesToUpload: Array<File>;
  43.  
  44.  
  45. constructor(private registerService:RegisterService,private http: Http, private router:Router){
  46. this.filesToUpload = [];
  47. }
  48. /*
  49. addPicture(event: EventTarget) {
  50. var xhr = new XMLHttpRequest();
  51. let eventObj: MSInputMethodContext = <MSInputMethodContext> event;
  52. let target: HTMLInputElement = <HTMLInputElement> eventObj.target;
  53. let files: FileList = target.files;
  54. this.file = files[0];
  55. const formData = new FormData();
  56. formData.append('myPicture', files[0]);
  57. console.log(this.file);
  58. console.log(this.file.name);
  59. console.log(formData.get('myPicture'));
  60. //let picturePlace = document.getElementById('picturePlace');
  61. //this.source = this.file.name;
  62. //this.registerService.addPicture(this.file);
  63. xhr.onreadystatechange = function () {
  64. if (xhr.readyState == 4) {
  65. if (xhr.status == 200) {
  66. resolve(JSON.parse(xhr.response));
  67. } else {
  68. reject(xhr.response);
  69. }
  70. }
  71. }
  72. xhr.open("POST", url, true);
  73. xhr.send(formData);
  74. var headers = new Headers();
  75. headers.append('Content-Type', 'application/json');
  76. //xhr.open('POST', 'http://localhost:3000/api/pictureUser', true);
  77. //xhr.send(formData);
  78. this.http.post('http://localhost:3000/api/pictureUser', formData, {headers})
  79. .subscribe(//JSON.stringify(newUser)
  80. data => {
  81. let body = data.json();
  82. console.log(body);
  83. }
  84. );
  85. }*/
  86. //lie avec notre html bien sur ici add user prend en parametre le click sur le bouton. il settera les valeurs
  87. //de notre model User.ts cree dans le dossier client (on ne fait que recuperer la valeur des champs du formulaire)
  88.  
  89. isEmailExist(){
  90. let userByEmail={
  91. email : this.user.email
  92. }
  93. console.log("this.isNewUser dans register component: " + this.isNewUser);
  94.  
  95. this.registerService.getUserByEmail(userByEmail).then(userRes=>{
  96. console.log("userRes.exist: oooo "+userRes.exist);
  97. if(userRes.exist){
  98. this.isEmailExistResult=true;
  99. return true;
  100. }else{
  101. this.isEmailExistResult=false;
  102. return false;
  103. }
  104. });
  105. }
  106.  
  107. isExist(){
  108. let userByPseudo={
  109. pseudo : this.user.pseudo
  110. }
  111. console.log("this.isNewUser dans register component: " + this.isNewUser);
  112.  
  113. this.registerService.getUserByPseudo(userByPseudo).then(userRes=>{
  114. console.log("userRes.exist: oooo "+userRes.exist);
  115. if(userRes.exist){
  116. this.isExistResult=true;
  117. return true;
  118. }else{
  119. this.isExistResult=false;
  120. return false;
  121. }
  122. });
  123. }
  124.  
  125.  
  126. addUser(email,pseudo,password,picture,birthday,secret,contactList,contactInvites){
  127. //event.preventDefault();
  128. //var newUser = {
  129. //this.user = new User();
  130. //var newUser = {
  131. email = this.user.email;
  132. pseudo = this.user.pseudo;
  133. password = this.user.password;
  134. picture = this.namePicture;
  135. birthday = this.user.birthday;
  136. secret = this.user.secret;
  137. contactList = [];
  138. contactInvites = [];
  139.  
  140. let userByPseudo={
  141. email : this.user.email,
  142. password : this.user.password,
  143. pseudo : this.user.pseudo
  144. }
  145.  
  146. console.log(email);
  147. console.log(pseudo);
  148. console.log(picture);
  149.  
  150. //la on passe notre newUser cree lors du click a notre register.service qui lui le donnera a la DB avec sa propre
  151. //methode addUser
  152. //this.registerService.addUser(newUser)
  153.  
  154. //this.registerService.addUser(email,pseudo,password,picture,secret,contactList,contactInvites,birthday);
  155.  
  156.  
  157. this.registerService.getUserByPseudo(userByPseudo).then(userRes=>{
  158. if(userRes.exist){
  159. this.registerService.getUserByEmail(userByPseudo).then(userRes=>{
  160. if(userRes.exist){
  161. this.isExistResult=true;
  162. this.isEmailExistResult=true;
  163. this.router.navigate([]);
  164. }else{
  165. this.isExistResult=true;
  166. this.isEmailExistResult=false;
  167. this.router.navigate([]);
  168. }
  169. });
  170. }else{
  171. this.registerService.getUserByEmail(userByPseudo).then(userRes=>{
  172. if(userRes.exist){
  173. this.isExistResult=false;
  174. this.isEmailExistResult=true;
  175. this.router.navigate([]);
  176. }else{
  177. this.isExistResult=false;
  178. this.isEmailExistResult=false;
  179. this.registerService.addUser(email,pseudo,password,picture,secret,contactList,contactInvites,birthday);
  180. this.userIsRegistered = true;
  181. this.router.navigate(['/']);
  182. }
  183. });
  184. }
  185. });
  186. }
  187.  
  188. uploadPic(){
  189. this.makeFileRequest("http://localhost:3000/api/upload", [], this.filesToUpload).then((result) => {
  190. console.log(result);
  191. this.namePicture = result[0].filename;
  192. var src = this.namePicture;
  193. this.source = 'http://localhost:3000/public/'+src;
  194.  
  195. }, (error) => {
  196. console.error(error);
  197. });
  198. }
  199.  
  200. fileChangeEvent(fileInput: any){
  201. this.filesToUpload = <Array<File>> fileInput.target.files;
  202. }
  203.  
  204. makeFileRequest(url: string, params: Array<string>, files: Array<File>) {
  205. return new Promise((resolve, reject) => {
  206. var formData: any = new FormData();
  207. var xhr = new XMLHttpRequest();
  208. for(var i = 0; i < files.length; i++) {
  209. formData.append("public[]", files[i], files[i].name);
  210. }
  211. xhr.onreadystatechange = function () {
  212. if (xhr.readyState == 4) {
  213. if (xhr.status == 200) {
  214. resolve(JSON.parse(xhr.response));
  215. } else {
  216. reject(xhr.response);
  217. }
  218. }
  219. }
  220.  
  221. //let picturePlace = document.getElementById('picturePlace');
  222. console.log(this.source);
  223. xhr.open("POST", url, true);
  224. xhr.send(formData);
  225. });
  226. }
  227.  
  228. ngOnInit() {
  229.  
  230. var pattern = "^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$";
  231. var pattern2="\d{1,2}/\d{1,2}/\d{4}";
  232. // initialize model here
  233. this.user = {
  234. _id: '',
  235. pseudo: '',
  236. email: '',
  237. password: '',
  238. picture:'',
  239. bio:'',
  240. birthday:new Date(''),
  241. status:'',
  242. exist:true,
  243. secret:''
  244. }
  245.  
  246. this.isNewUser;
  247. }
  248. /*
  249. getUsername(){
  250. this.chatService.getUsername();
  251. }*/
  252. }
Add Comment
Please, Sign In to add comment