Advertisement
Guest User

Untitled

a guest
Jun 1st, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1. import {Component, Injectable, Injector} from '@angular/core';
  2. import { RegisterService } from '../../services/register/register.service';
  3. import { User } from '../../../User';
  4. import {ChatService} from "../../services/chat/chat.service";
  5. import {Http, Headers} from "@angular/http";
  6. import {Observable} from 'rxjs/Rx';
  7. import {isUndefined} from "util";
  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 {
  18.  
  19. //initialisation de notre classe/modele User/ts
  20. user : User;
  21. email : string;
  22. pseudo : string;
  23. password: string;
  24. ConfirmPassword :string;
  25. //contactList = [];
  26. file: File;
  27. namePicture = '';
  28. isNewUser : boolean;
  29. source='http://localhost:3000/public/profile.jpg';
  30. //password : string;
  31.  
  32. /*ici les petites salopes on passe en parametre notre service qui push dans la DB en appelant
  33.  
  34. }
  35. filesToUpload: Array<File>;
  36.  
  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.  
  55. console.log(this.file);
  56. console.log(this.file.name);
  57. console.log(formData.get('myPicture'));
  58. //let picturePlace = document.getElementById('picturePlace');
  59. //this.source = this.file.name;
  60. //this.registerService.addPicture(this.file);
  61. xhr.onreadystatechange = function () {
  62. if (xhr.readyState == 4) {
  63. if (xhr.status == 200) {
  64. resolve(JSON.parse(xhr.response));
  65. } else {
  66. reject(xhr.response);
  67. }
  68. }
  69. }
  70. xhr.open("POST", url, true);
  71. xhr.send(formData);
  72.  
  73. var headers = new Headers();
  74. headers.append('Content-Type', 'application/json');
  75. //xhr.open('POST', 'http://localhost:3000/api/pictureUser', true);
  76. //xhr.send(formData);
  77. this.http.post('http://localhost:3000/api/pictureUser', formData, {headers})
  78. .subscribe(//JSON.stringify(newUser)
  79. data => {
  80. let body = data.json();
  81. console.log(body);
  82. }
  83. );
  84. }*/
  85. //lie avec notre html bien sur ici add user prend en parametre le click sur le bouton. il settera les valeurs
  86. //de notre model User.ts cree dans le dossier client (on ne fait que recuperer la valeur des champs du formulaire)
  87.  
  88. isNew(pseudo){
  89. pseudo = this.user.pseudo;
  90. console.log("this.isNewUser dans register component: " + this.isNewUser);
  91. if(/*pseudo != null &&this.isNewUser != true*/ this.isNewUser == undefined /*|| this.isNewUser == false*/) {
  92. this.registerService.isNew(pseudo).then(isNewUs => {
  93. //console.log("dans register component" + isNewUs);
  94. if (isNewUs) {
  95. this.isNewUser =isNewUs;
  96. return false;
  97. } else {
  98. this.isNewUser =isNewUs;
  99. return true;
  100. }
  101. });
  102. }else {
  103. if(this.isNewUser){
  104. return false;
  105. }else if(this.isNewUser == false){
  106. return true;
  107. }
  108. }
  109. }
  110. addUser(email,pseudo,password,picture,contactList){
  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. contactList = [];
  120. //}
  121. //}
  122. //password : this.password
  123.  
  124. console.log(email);
  125. console.log(pseudo);
  126. console.log(picture);
  127.  
  128. //la on passe notre newUser cree lors du click a notre register.service qui lui le donnera a la DB avec sa propre
  129. //methode addUser
  130. //this.registerService.addUser(newUser)
  131.  
  132. this.registerService.addUser(email,pseudo,password,picture,contactList);
  133. /*
  134. this.registerService.isNew(pseudo).then(isNewUser=>{
  135. console.log("dans register component"+isNewUser);
  136. if(isNewUser){
  137. this.registerService.addUser(email,pseudo,password,picture,contactList);
  138. }else{
  139. console.log("dans register component ton user existe deja!");
  140. }
  141. });
  142. */
  143.  
  144.  
  145.  
  146. //this.registerService.addUser(email,pseudo,password,picture,contactList);
  147. //.suscribe(
  148. }
  149.  
  150. uploadPic(){
  151. this.makeFileRequest("http://localhost:3000/api/upload", [], this.filesToUpload).then((result) => {
  152. console.log(result);
  153. this.namePicture = result[0].filename;
  154. var src = this.namePicture
  155. this.source = 'http://localhost:3000/public/'+src;
  156.  
  157. }, (error) => {
  158. console.error(error);
  159. });
  160. }
  161.  
  162. fileChangeEvent(fileInput: any){
  163. this.filesToUpload = <Array<File>> fileInput.target.files;
  164. }
  165.  
  166. makeFileRequest(url: string, params: Array<string>, files: Array<File>) {
  167. return new Promise((resolve, reject) => {
  168. var formData: any = new FormData();
  169. var xhr = new XMLHttpRequest();
  170. for(var i = 0; i < files.length; i++) {
  171. formData.append("public[]", files[i], files[i].name);
  172. }
  173. xhr.onreadystatechange = function () {
  174. if (xhr.readyState == 4) {
  175. if (xhr.status == 200) {
  176. resolve(JSON.parse(xhr.response));
  177. } else {
  178. reject(xhr.response);
  179. }
  180. }
  181. }
  182.  
  183. //let picturePlace = document.getElementById('picturePlace');
  184. console.log(this.source)
  185. xhr.open("POST", url, true);
  186. xhr.send(formData);
  187. });
  188. }
  189.  
  190. ngOnInit() {
  191.  
  192. var pattern = "^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$";
  193. // initialize model here
  194. this.user = {
  195. _id: '',
  196. pseudo: '',
  197. email: '',
  198. password: '',
  199. picture:''
  200. }
  201.  
  202. this.isNewUser;
  203. }
  204. /*
  205. getUsername(){
  206. this.chatService.getUsername();
  207. }*/
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement