<commit_before>/**
 * Created by Mihail on 9/17/2016.
 */

'use strict';

import { EventsConsts } from '../events/events-consts';
import PubSubService from '../events/pubsub-service';
import LocalStorageService from '../local-storage/local-storage-service';
import ViewManipulator from '../utils/view-manipulator';

class ApiController {
	constructor() {
		if (LocalStorageService.isLoggedIn()) {
			ViewManipulator.updateViewState(undefined, undefined, LocalStorageService.isLoggedIn());
		}

		this.pubsub = new PubSubService();
		this.viewCtrl = new ViewManipulator(this.pubsub);
	}

	static login() {
		const loginUrl = "http://localhost:8888/bingo-api/login";
		const elEmail = document.querySelector('#email');
		const elPass = document.querySelector('#password');

		if (elEmail === null || elEmail.value === undefined ||
			elPass === null || elPass.value === undefined) {
			console.log('Not valid user data.');
			return;
		}

		fetch(loginUrl, {
			method: 'POST',
			body: JSON.stringify({
				email: elEmail.value,
				password: <PASSWORD>
			}),
			mode: 'cors',
			redirect: 'follow',
			headers: new Headers({
				'Content-Type': 'application/json'
			})
		}).then((res) => {
			return res.json();
		}).then((returnedValue) => {
			if (returnedValue.token) {
				ViewManipulator.updateViewState(undefined, undefined, true);
				LocalStorageService.saveToken(returnedValue.token);
				ViewManipulator.showUserInfo();
			} else {
				console.log('Show error message for login failed.');
				ViewManipulator.toggleErrorMessageView(document.querySelector('#alertMsg'),
					 'Wrong login details.', true);
			}
		}).catch(function (err) {
			console.log('>>> Fetching error: ', err);
		});
	}

	static register() {
		const registerUrl = "http://localhost:8888/bingo-api/register";
		const elName = document.querySelector('#registerName');
		const elEmail = document.querySelector('#regEmail');
		const elPass = document.querySelector('#registerPassword');

		if (elName === null || elName.value === undefined ||
			elEmail === null || elEmail.value === undefined ||
			elPass === null || elPass.value === undefined) {
			console.log('Not valid user data.');
			return;
		}

		fetch(registerUrl, {
			method: 'POST',
			body: JSON.stringify({
				name: elName.value,
				email: elEmail.value,
				password: <PASSWORD>.value
			}),
			mode: 'cors',
			redirect: 'follow',
			headers: new Headers({
				'Content-Type': 'application/json'
			})
		}).then((res) => {
			return res.json();
		}).then((returnedValue) => {
			if (returnedValue) {
				if (returnedValue.isExisted) {
					ViewManipulator.toggleErrorMessageView(document.querySelector('#alertMsg'),
						'User already existed.', true);
				} else {
					LocalStorageService.saveToken(returnedValue.token);
					ViewManipulator.updateViewState(undefined, undefined, LocalStorageService.isLoggedIn());
				}
			} else {
				console.log('Show error message for registration failed.');
			}
		}).catch(function (err) {
			console.log('>>> Fetching error: ', err);
		});
	}

	logout() {
		LocalStorageService.logout();
		this.pubsub.publish(EventsConsts.LOGOUT, {
			isLogout: true
		});
	}<commit_msg>Fixed updating the user profile info when register new user and automatically login to show the new user's data.<commit_after>/**
 * Created by Mihail on 9/17/2016.
 */

'use strict';

import { EventsConsts } from '../events/events-consts';
import PubSubService from '../events/pubsub-service';
import LocalStorageService from '../local-storage/local-storage-service';
import ViewManipulator from '../utils/view-manipulator';

class ApiController {
	constructor() {
		if (LocalStorageService.isLoggedIn()) {
			ViewManipulator.updateViewState(undefined, undefined, LocalStorageService.isLoggedIn());
		}

		this.pubsub = new PubSubService();
		this.viewCtrl = new ViewManipulator(this.pubsub);
	}

	static login() {
		const loginUrl = "http://localhost:8888/bingo-api/login";
		const elEmail = document.querySelector('#email');
		const elPass = document.querySelector('#password');

		if (elEmail === null || elEmail.value === undefined ||
			elPass === null || elPass.value === undefined) {
			console.log('Not valid user data.');
			return;
		}

		fetch(loginUrl, {
			method: 'POST',
			body: JSON.stringify({
				email: elEmail.value,
				password: <PASSWORD>
			}),
			mode: 'cors',
			redirect: 'follow',
			headers: new Headers({
				'Content-Type': 'application/json'
			})
		}).then((res) => {
			return res.json();
		}).then((returnedValue) => {
			if (returnedValue.token) {
				ViewManipulator.updateViewState(undefined, undefined, true);
				LocalStorageService.saveToken(returnedValue.token);
				ViewManipulator.showUserInfo();
			} else {
				console.log('Show error message for login failed.');
				ViewManipulator.toggleErrorMessageView(document.querySelector('#alertMsg'),
					 'Wrong login details.', true);
			}
		}).catch(function (err) {
			console.log('>>> Fetching error: ', err);
		});
	}

	static register() {
		const registerUrl = "http://localhost:8888/bingo-api/register";
		const elName = document.querySelector('#registerName');
		const elEmail = document.querySelector('#regEmail');
		const elPass = document.querySelector('#registerPassword');

		if (elName === null || elName.value === undefined ||
			elEmail === null || elEmail.value === undefined ||
			elPass === null || elPass.value === undefined) {
			console.log('Not valid user data.');
			return;
		}

		fetch(registerUrl, {
			method: 'POST',
			body: JSON.stringify({
				name: elName.value,
				email: elEmail.value,
				password: <PASSWORD>
			}),
			mode: 'cors',
			redirect: 'follow',
			headers: new Headers({
				'Content-Type': 'application/json'
			})
		}).then((res) => {
			return res.json();
		}).then((returnedValue) => {
			if (returnedValue) {
				if (returnedValue.isExisted) {
					ViewManipulator.toggleErrorMessageView(document.querySelector('#alertMsg'),
						'User already existed.', true);
				} else {
					LocalStorageService.saveToken(returnedValue.token);
					ViewManipulator.updateViewState(undefined, undefined, LocalStorageService.isLoggedIn());
					ViewManipulator.showUserInfo();
				}
			} else {
				console.log('Show error message for registration failed.');
			}
		}).catch(function (err) {
			console.log('>>> Fetching error: ', err);
		});
	}

	logout() {
		LocalStorageService.logout();
		this.pubsub.publish(EventsConsts.LOGOUT, {
			isLogout: true
		});
	}
{'fraction_non_alphanumeric': 0.1583101207056639, 'fraction_numerical': 0.004642525533890436, 'mean_word_length': 3.911094224924012, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '88251', 'n_tokens_mistral': 2323, 'n_tokens_neox': 1978, 'n_words': 415}