{"size": 2808, "ext": "js", "max_stars_repo_path": "client/src/store/AuthStore.js", "max_stars_repo_name": "JobDelToro/medical-clinic", "max_stars_repo_stars_event_min_datetime": "2020-03-15T13:26:17.000Z", "max_stars_repo_stars_event_max_datetime": "2020-03-15T13:26:17.000Z", "max_issues_repo_path": "client/src/store/AuthStore.js", "max_issues_repo_name": "JobDelToro/medical-clinic", "max_issues_repo_issues_event_min_datetime": "2021-03-10T10:34:37.000Z", "max_issues_repo_issues_event_max_datetime": "2022-01-22T10:59:14.000Z", "max_forks_repo_path": "client/src/store/AuthStore.js", "max_forks_repo_name": "JobDelToro/medical-clinic", "max_forks_repo_forks_event_min_datetime": "2020-11-11T13:23:59.000Z", "max_forks_repo_forks_event_max_datetime": "2020-11-11T13:23:59.000Z"}
{"max_stars_count": 1, "max_issues_count": 3, "max_forks_count": 1, "avg_line_length": 21.7674418605, "max_line_length": 109, "alphanum_fraction": 0.5968660969}
import { decorate, observable, computed, action } from 'mobx';
import { fromPromise } from 'mobx-utils';

import medicalClinicApi from '../apis/medicalClinicApi';
import history from '../history';

class AuthStore {
    // constructor(store) {
    //     extendObservable(this, {
    //         token: ''
    //     });
    // }

    // Observables
    user;
	userType;
	token;
    sessionStatus;
    isClinicianOrPatient = '0';

    // Computed
    get isLoggedIn() {
        return Boolean(this.token);
    }

    // Actions
    signUp = async params => {
		
        if (this.isClinicianOrPatient === '0') {
            const sessionPromise = medicalClinicApi.post('/auth/register/patient', params).catch(error => {
				
				if(error) {
					
					alert(error.response.data.message);
					history.push('/signup');
				}
            });
			
			const res = await sessionPromise;
			if(res) {
				this.sessionStatus = fromPromise(sessionPromise);
				this.setToken(res.data.token);
				this.setUser(res.data.user);
				this.setUserType(res.data.userType);
				history.push('/');
			}
			
        } else if (this.isClinicianOrPatient === '1') {
            const sessionPromise = medicalClinicApi.post('/auth/register/clinician', params).catch(error => {
				
				if(error) {
					
					alert(error.response.data.message);
					history.push('/signup');
				}
            });
			
			const res = await sessionPromise;
			if(res) {
				
				this.sessionStatus = fromPromise(sessionPromise);
				this.setToken(res.data.token);
				this.setUser(res.data.user);
				this.setUserType(res.data.userType);
				history.push('/');
			}
        }
    };

    login = async params => {
        const sessionPromise = medicalClinicApi.post('/auth/login', params).catch(error => {

			if(error) {

				alert(error.response.data.message);
				history.push('/signin');
			}
        });
		
		const res = await sessionPromise;
		if(res) {
			this.sessionStatus = fromPromise(sessionPromise);
			this.handleAuth(res.data);
			history.push('/');
		}
    };

    handleAuth = data => {
        this.setToken(data.token);
		this.setUser(data.user);
		this.setUserType(data.userType);
    };

    setToken = token => {
        this.token = token;
    };

    setUser = user => {
        this.user = user;
    };

	setUserType = userType => {
		this.userType = userType;
	}

    reset = () => {
        this.setToken(null);
        this.sessionStatus = null;
    };
}

//decorate
decorate(AuthStore, {
	user: observable,
	userType: observable,
    sessionStatus: observable,
	token: observable,
    isClinicianOrPatient: observable,
    isLoggedIn: computed,
    signUp: action,
    login: action,
    handleAuth: action,
    setToken: action,
    setUser: action,
	setUserType: action,
    reset: action
});

export default AuthStore;