Change to login to role

This commit is contained in:
Łukasz Góreczny 2020-12-21 16:54:22 +01:00
parent b49bdae96a
commit f426f5029b
3 changed files with 16 additions and 7 deletions

View File

@ -54,7 +54,7 @@ BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
MatSidenavModule, MatSidenavModule,
MatToolbarModule, MatToolbarModule,
MatListModule, MatListModule,
StoreModule.forRoot({app: AppReducer}), StoreModule.forRoot({appState: AppReducer}),
StoreDevtoolsModule.instrument({ StoreDevtoolsModule.instrument({
logOnly: environment.production logOnly: environment.production
}) })

View File

@ -8,6 +8,7 @@ import {HttpErrorResponse} from '@angular/common/http';
import {AppStoreModel} from '../../store/models/app-store.model'; import {AppStoreModel} from '../../store/models/app-store.model';
import { Store } from '@ngrx/store'; import { Store } from '@ngrx/store';
import {AddUserId} from '../../store/actions/app.actions'; import {AddUserId} from '../../store/actions/app.actions';
import { AppState } from 'src/app/store/models/app-state.model';
@Component({ @Component({
selector: 'app-sign-in', selector: 'app-sign-in',
@ -17,14 +18,14 @@ import {AddUserId} from '../../store/actions/app.actions';
export class SignInComponent implements OnDestroy, OnInit { export class SignInComponent implements OnDestroy, OnInit {
allSubscriptions = new Subscription(); allSubscriptions = new Subscription();
role$: Observable<string>; role$: Observable<AppStoreModel>;
isExpanded = false; isExpanded = false;
apiError = false; apiError = false;
apiErrorMessage = ''; apiErrorMessage = '';
constructor(private router: Router, private formBuilder: FormBuilder, constructor(private router: Router, private formBuilder: FormBuilder,
private userService: UserService, private store: Store<AppStoreModel>) { private userService: UserService, private store: Store<AppState>) {
} }
public signInFormGroup: FormGroup = this.formBuilder.group({ public signInFormGroup: FormGroup = this.formBuilder.group({
@ -37,18 +38,26 @@ export class SignInComponent implements OnDestroy, OnInit {
}); });
ngOnInit() { ngOnInit() {
this.role$ = this.store.select('role'); this.role$ = this.store.select(s => s.appState);
} }
onLoginButtonClick() { onLoginButtonClick() {
console.log(this.role$); let role = '';
this.store.select(s => s.appState.role).subscribe((v)=>{
role = v;
});
this.allSubscriptions.add( this.allSubscriptions.add(
this.userService.tryLogin( this.userService.tryLogin(
this.signInFormGroup.get('signIn').value['username'], this.signInFormGroup.get('signIn').value['username'],
this.signInFormGroup.get('signIn').value['password']).subscribe( this.signInFormGroup.get('signIn').value['password']).subscribe(
(success) => { (success) => {
this.store.dispatch(new AddUserId({userId: success})); this.store.dispatch(new AddUserId({userId: success}));
this.router.navigate([this.role$]); //TODO zmienić na jedna linie
if (role === 'player') {
this.router.navigate([role]);
} else {
this.router.navigate([role]);
}
}, },
(error: ErrorResponse | HttpErrorResponse) => { (error: ErrorResponse | HttpErrorResponse) => {
if (error instanceof HttpErrorResponse) { if (error instanceof HttpErrorResponse) {

View File

@ -1,5 +1,5 @@
import {AppStoreModel} from './app-store.model'; import {AppStoreModel} from './app-store.model';
export interface AppState { export interface AppState {
readonly player: AppStoreModel; appState: AppStoreModel;
} }