Guest User

Untitled

a guest
Jun 5th, 2017
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  1. import {Component, OnInit, OnDestroy} from '@angular/core';
  2. import { RegisterService } from '../../services/register/register.service';
  3. import { User } from '../../../User';
  4.  
  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, OnDestroy {
  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. source='http://localhost:3000/public/profile.jpg';
  32. //password : string;
  33.  
  34. /*ici les petites salopes on passe en parametre notre service qui push dans la DB en appelant
  35. }
  36. filesToUpload: Array<File>;
  37. les routes de notre fichier register.js du serveur , pour le call a chaque fois qu on appelera ce component
  38. */
  39. filesToUpload: Array<File>;
  40.  
  41.  
  42. constructor(private registerService:RegisterService,private http: Http){
  43. this.filesToUpload = [];
  44. }
  45. /*
  46. addPicture(event: EventTarget) {
  47. var xhr = new XMLHttpRequest();
  48. let eventObj: MSInputMethodContext = <MSInputMethodContext> event;
  49. let target: HTMLInputElement = <HTMLInputElement> eventObj.target;
  50. let files: FileList = target.files;
  51. this.file = files[0];
  52. const formData = new FormData();
  53. formData.append('myPicture', files[0]);
  54. console.log(this.file);
  55. console.log(this.file.name);
  56. console.log(formData.get('myPicture'));
  57. //let picturePlace = document.getElementById('picturePlace');
  58. //this.source = this.file.name;
  59. //this.registerService.addPicture(this.file);
  60. xhr.onreadystatechange = function () {
  61. if (xhr.readyState == 4) {
  62. if (xhr.status == 200) {
  63. resolve(JSON.parse(xhr.response));
  64. } else {
  65. reject(xhr.response);
  66. }
  67. }
  68. }
  69. xhr.open("POST", url, true);
  70. xhr.send(formData);
  71. var headers = new Headers();
  72. headers.append('Content-Type', 'application/json');
  73. //xhr.open('POST', 'http://localhost:3000/api/pictureUser', true);
  74. //xhr.send(formData);
  75. this.http.post('http://localhost:3000/api/pictureUser', formData, {headers})
  76. .subscribe(//JSON.stringify(newUser)
  77. data => {
  78. let body = data.json();
  79. console.log(body);
  80. }
  81. );
  82. }*/
  83. //lie avec notre html bien sur ici add user prend en parametre le click sur le bouton. il settera les valeurs
  84. //de notre model User.ts cree dans le dossier client (on ne fait que recuperer la valeur des champs du formulaire)
  85.  
  86. isNew(pseudo){
  87. pseudo = this.user.pseudo;
  88. console.log("this.isNewUser dans register component: " + this.isNewUser);
  89. if(/*pseudo != null &&this.isNewUser != true*/ this.isNewUser == undefined /*|| this.isNewUser == false*/) {
  90. this.registerService.isNew(pseudo).then(isNewUs => {
  91. //console.log("dans register component" + isNewUs);
  92. if (isNewUs) {
  93. this.isNewUser =isNewUs;
  94. return false;
  95. } else {
  96. this.isNewUser =isNewUs;
  97. return true;
  98. }
  99. });
  100. }else {
  101. if(this.isNewUser){
  102. return false;
  103. }else if(this.isNewUser == false){
  104. return true;
  105. }
  106. }
  107. }
  108.  
  109.  
  110. addUser(email,pseudo,password,picture,birthday,contactList,contactInvites){
  111. //event.preventDefault();
  112. //var newUser = {
  113. //this.user = new User();
  114. //var newUser = {
  115. email = this.user.email;
  116. pseudo = this.user.pseudo;
  117. password = this.user.password;
  118. picture = this.namePicture;
  119. birthday = this.user.birthday;
  120. contactList = [];
  121. contactInvites = [];
  122. //}
  123. //}
  124. //password : this.password
  125.  
  126. console.log(email);
  127. console.log(pseudo);
  128. console.log(picture);
  129.  
  130. //la on passe notre newUser cree lors du click a notre register.service qui lui le donnera a la DB avec sa propre
  131. //methode addUser
  132. //this.registerService.addUser(newUser)
  133.  
  134. this.registerService.addUser(email,pseudo,password,picture,contactList,contactInvites,birthday);
  135. /*
  136. this.registerService.isNew(pseudo).then(isNewUser=>{
  137. console.log("dans register component"+isNewUser);
  138. if(isNewUser){
  139. this.registerService.addUser(email,pseudo,password,picture,contactList);
  140. }else{
  141. console.log("dans register component ton user existe deja!");
  142. }
  143. });
  144. */
  145.  
  146.  
  147.  
  148. //this.registerService.addUser(email,pseudo,password,picture,contactList);
  149. //.suscribe(
  150. }
  151.  
  152. uploadPic(){
  153. this.makeFileRequest("http://localhost:3000/api/upload", [], this.filesToUpload).then((result) => {
  154. console.log(result);
  155. this.namePicture = result[0].filename;
  156. var src = this.namePicture;
  157. this.source = 'http://localhost:3000/public/'+src;
  158.  
  159. }, (error) => {
  160. console.error(error);
  161. });
  162. }
  163.  
  164. fileChangeEvent(fileInput: any){
  165. this.filesToUpload = <Array<File>> fileInput.target.files;
  166. }
  167.  
  168. makeFileRequest(url: string, params: Array<string>, files: Array<File>) {
  169. return new Promise((resolve, reject) => {
  170. var formData: any = new FormData();
  171. var xhr = new XMLHttpRequest();
  172. for(var i = 0; i < files.length; i++) {
  173. formData.append("public[]", files[i], files[i].name);
  174. }
  175. xhr.onreadystatechange = function () {
  176. if (xhr.readyState == 4) {
  177. if (xhr.status == 200) {
  178. resolve(JSON.parse(xhr.response));
  179. } else {
  180. reject(xhr.response);
  181. }
  182. }
  183. }
  184.  
  185. //let picturePlace = document.getElementById('picturePlace');
  186. console.log(this.source);
  187. xhr.open("POST", url, true);
  188. xhr.send(formData);
  189. });
  190. }
  191.  
  192. ngOnInit() {
  193.  
  194. var pattern = "^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$";
  195. var pattern2="\d{1,2}/\d{1,2}/\d{4}";
  196. // initialize model here
  197. this.user = {
  198. _id: '',
  199. pseudo: '',
  200. email: '',
  201. password: '',
  202. picture:'',
  203. bio:'',
  204. birthday:new Date(''),
  205. status:''
  206. }
  207.  
  208. this.isNewUser;
  209. }
  210. /*
  211. getUsername(){
  212. this.chatService.getUsername();
  213. }*/
  214. }
Add Comment
Please, Sign In to add comment