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,
MatToolbarModule,
MatListModule,
StoreModule.forRoot({app: AppReducer}),
StoreModule.forRoot({appState: AppReducer}),
StoreDevtoolsModule.instrument({
logOnly: environment.production
})

View File

@ -8,6 +8,7 @@ import {HttpErrorResponse} from '@angular/common/http';
import {AppStoreModel} from '../../store/models/app-store.model';
import { Store } from '@ngrx/store';
import {AddUserId} from '../../store/actions/app.actions';
import { AppState } from 'src/app/store/models/app-state.model';
@Component({
selector: 'app-sign-in',
@ -17,14 +18,14 @@ import {AddUserId} from '../../store/actions/app.actions';
export class SignInComponent implements OnDestroy, OnInit {
allSubscriptions = new Subscription();
role$: Observable<string>;
role$: Observable<AppStoreModel>;
isExpanded = false;
apiError = false;
apiErrorMessage = '';
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({
@ -37,18 +38,26 @@ export class SignInComponent implements OnDestroy, OnInit {
});
ngOnInit() {
this.role$ = this.store.select('role');
this.role$ = this.store.select(s => s.appState);
}
onLoginButtonClick() {
console.log(this.role$);
let role = '';
this.store.select(s => s.appState.role).subscribe((v)=>{
role = v;
});
this.allSubscriptions.add(
this.userService.tryLogin(
this.signInFormGroup.get('signIn').value['username'],
this.signInFormGroup.get('signIn').value['password']).subscribe(
(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) => {
if (error instanceof HttpErrorResponse) {

View File

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