added player model and service
This commit is contained in:
parent
dec7922727
commit
97883d0f07
|
|
@ -5,8 +5,8 @@ class BackendService {
|
|||
return localStorage.getItem("server") !== null;
|
||||
}
|
||||
getServer(): Server {
|
||||
// return JSON.parse(localStorage.getItem("server") || '{"protocol":"http","host":"localhost","port":8080}');
|
||||
return JSON.parse(localStorage.getItem("server") || '{"protocol":"https","host":"bullpen-api.palaeomatiker.home64.de","port":443}');
|
||||
return JSON.parse(localStorage.getItem("server") || '{"protocol":"http","host":"localhost","port":8080}');
|
||||
// return JSON.parse(localStorage.getItem("server") || '{"protocol":"https","host":"bullpen-api.palaeomatiker.home64.de","port":443}');
|
||||
}
|
||||
updateServer(server: Server): void {
|
||||
localStorage.setItem("server", JSON.stringify(server));
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
import Player from "@/types/Player";
|
||||
import { ApiService } from './ApiService';
|
||||
import api from "@/services/Api";
|
||||
import authHeader from "@/services/AuthHeader";
|
||||
import {AxiosError, AxiosResponse} from "axios";
|
||||
|
||||
class PlayerService extends ApiService<Player> {
|
||||
constructor() {
|
||||
super('players');
|
||||
}
|
||||
|
||||
public fetchByUserId(userId: number): Promise<Player> {
|
||||
return api
|
||||
.get(`/${this.name}/user/${userId}`, { headers: authHeader() })
|
||||
.then((response: AxiosResponse<Player>) => {
|
||||
return response.data;
|
||||
}, (error: AxiosError) => {
|
||||
this.handleExpiredToken(error);
|
||||
return Promise.reject(error);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default new PlayerService();
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
import AuthService from '@/services/AuthService'
|
||||
import User from "@/types/User";
|
||||
|
||||
import { Module, ActionContext } from 'vuex';
|
||||
import { RootState } from './index';
|
||||
import {ActionContext, Module} from 'vuex';
|
||||
import {RootState} from './index';
|
||||
import TokenService from "@/services/TokenService";
|
||||
import UserService from "@/services/UserService";
|
||||
|
||||
|
|
@ -14,8 +14,8 @@ export interface AuthState {
|
|||
const user = JSON.parse(localStorage.getItem('auth') || '""');
|
||||
|
||||
const initialState = user
|
||||
? { isAuthenticated: true, user }
|
||||
: { isAuthenticated: false, user: null };
|
||||
? {isAuthenticated: true, user}
|
||||
: {isAuthenticated: false, user: null};
|
||||
|
||||
|
||||
type AuthActionContext = ActionContext<AuthState, RootState>;
|
||||
|
|
@ -24,34 +24,33 @@ const auth: Module<AuthState, RootState> = {
|
|||
namespaced: true,
|
||||
state: initialState,
|
||||
actions: {
|
||||
login({ commit }: AuthActionContext, user) {
|
||||
return AuthService.login(user.email, user.password).then(
|
||||
auth => {
|
||||
return UserService.fetchById(auth.id).then(
|
||||
(user: User) => {
|
||||
login({commit}: AuthActionContext, user) {
|
||||
return AuthService
|
||||
.login(user.email, user.password)
|
||||
.then(auth => {
|
||||
return UserService
|
||||
.fetchById(auth.id)
|
||||
.then((user: User) => {
|
||||
commit('loginSuccess', user);
|
||||
return Promise.resolve(user);
|
||||
},
|
||||
error => {
|
||||
}, error => {
|
||||
commit('loginFailure');
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
},
|
||||
error => {
|
||||
}, error => {
|
||||
commit('loginFailure');
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
},
|
||||
logout({ commit }: AuthActionContext) {
|
||||
logout({commit}: AuthActionContext) {
|
||||
return AuthService.logout().then(() => {
|
||||
commit('logout');
|
||||
return Promise.resolve();
|
||||
})
|
||||
},
|
||||
register({ commit }: AuthActionContext, user) {
|
||||
register({commit}: AuthActionContext, user) {
|
||||
return AuthService.register(user.email, user.password).then(
|
||||
response => {
|
||||
commit('registerSuccess');
|
||||
|
|
@ -63,7 +62,7 @@ const auth: Module<AuthState, RootState> = {
|
|||
}
|
||||
);
|
||||
},
|
||||
refreshToken({ commit }: AuthActionContext, accessToken) {
|
||||
refreshToken({commit}: AuthActionContext, accessToken) {
|
||||
commit('refreshToken', accessToken);
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
import playerService from '@/services/PlayerService'
|
||||
import Player from "@/types/Player";
|
||||
import UserInfo from "@/types/UserInfo";
|
||||
|
||||
import {ActionContext, Module} from 'vuex';
|
||||
import { RootState } from './index';
|
||||
|
||||
export interface PlayerState {
|
||||
player: Player | null;
|
||||
}
|
||||
|
||||
type PlayerActionContext = ActionContext<PlayerState, RootState>;
|
||||
|
||||
const pitchTypes: Module<PlayerState, RootState> = {
|
||||
namespaced: true,
|
||||
state: {player: null},
|
||||
actions: {
|
||||
determinePlayer({commit}: PlayerActionContext, user: UserInfo) {
|
||||
if (user.roles.includes('ROLE_PLAYER')) {
|
||||
playerService.fetchByUserId(user.id).then(player => {
|
||||
commit('initialize', player);
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
mutations: {
|
||||
initialize(state, player: Player) {
|
||||
state.player = player;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default pitchTypes;
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
export default interface Player {
|
||||
id: number,
|
||||
gender: string,
|
||||
bats: string,
|
||||
throws: string,
|
||||
jerseyNumber: number,
|
||||
weight: number,
|
||||
height: number,
|
||||
state: string,
|
||||
createdAt: Date,
|
||||
updatedAt: Date,
|
||||
userId: number
|
||||
}
|
||||
|
|
@ -53,16 +53,13 @@ const submit = handleSubmit((values, { resetForm }) => {
|
|||
store.dispatch('auth/login', {
|
||||
email: values.email,
|
||||
password: values.password,
|
||||
}).then(
|
||||
() => {
|
||||
}).then(() => {
|
||||
resetForm();
|
||||
onLogin();
|
||||
},
|
||||
error => {
|
||||
}, error => {
|
||||
loading.value = false;
|
||||
console.log(error);
|
||||
}
|
||||
);
|
||||
});
|
||||
}, ({errors}) => {
|
||||
console.log(errors);
|
||||
loading.value = false;
|
||||
|
|
@ -71,6 +68,7 @@ const submit = handleSubmit((values, { resetForm }) => {
|
|||
const onLogin = () => {
|
||||
console.log('check if pitch types are available.');
|
||||
|
||||
|
||||
PitchTypeService.fetchAll().then((pitchTypes: PitchType[]) => {
|
||||
store.commit('pitchTypes/initialize', pitchTypes);
|
||||
router.push({path: '/home'});
|
||||
|
|
|
|||
Loading…
Reference in New Issue