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;
|
return localStorage.getItem("server") !== null;
|
||||||
}
|
}
|
||||||
getServer(): Server {
|
getServer(): Server {
|
||||||
// return JSON.parse(localStorage.getItem("server") || '{"protocol":"http","host":"localhost","port":8080}');
|
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":"https","host":"bullpen-api.palaeomatiker.home64.de","port":443}');
|
||||||
}
|
}
|
||||||
updateServer(server: Server): void {
|
updateServer(server: Server): void {
|
||||||
localStorage.setItem("server", JSON.stringify(server));
|
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 AuthService from '@/services/AuthService'
|
||||||
import User from "@/types/User";
|
import User from "@/types/User";
|
||||||
|
|
||||||
import { Module, ActionContext } from 'vuex';
|
import {ActionContext, Module} from 'vuex';
|
||||||
import { RootState } from './index';
|
import {RootState} from './index';
|
||||||
import TokenService from "@/services/TokenService";
|
import TokenService from "@/services/TokenService";
|
||||||
import UserService from "@/services/UserService";
|
import UserService from "@/services/UserService";
|
||||||
|
|
||||||
|
|
@ -14,8 +14,8 @@ export interface AuthState {
|
||||||
const user = JSON.parse(localStorage.getItem('auth') || '""');
|
const user = JSON.parse(localStorage.getItem('auth') || '""');
|
||||||
|
|
||||||
const initialState = user
|
const initialState = user
|
||||||
? { isAuthenticated: true, user }
|
? {isAuthenticated: true, user}
|
||||||
: { isAuthenticated: false, user: null };
|
: {isAuthenticated: false, user: null};
|
||||||
|
|
||||||
|
|
||||||
type AuthActionContext = ActionContext<AuthState, RootState>;
|
type AuthActionContext = ActionContext<AuthState, RootState>;
|
||||||
|
|
@ -24,34 +24,33 @@ const auth: Module<AuthState, RootState> = {
|
||||||
namespaced: true,
|
namespaced: true,
|
||||||
state: initialState,
|
state: initialState,
|
||||||
actions: {
|
actions: {
|
||||||
login({ commit }: AuthActionContext, user) {
|
login({commit}: AuthActionContext, user) {
|
||||||
return AuthService.login(user.email, user.password).then(
|
return AuthService
|
||||||
auth => {
|
.login(user.email, user.password)
|
||||||
return UserService.fetchById(auth.id).then(
|
.then(auth => {
|
||||||
(user: User) => {
|
return UserService
|
||||||
|
.fetchById(auth.id)
|
||||||
|
.then((user: User) => {
|
||||||
commit('loginSuccess', user);
|
commit('loginSuccess', user);
|
||||||
return Promise.resolve(user);
|
return Promise.resolve(user);
|
||||||
},
|
}, error => {
|
||||||
error => {
|
|
||||||
commit('loginFailure');
|
commit('loginFailure');
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
}, error => {
|
||||||
},
|
|
||||||
error => {
|
|
||||||
commit('loginFailure');
|
commit('loginFailure');
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
logout({ commit }: AuthActionContext) {
|
logout({commit}: AuthActionContext) {
|
||||||
return AuthService.logout().then(() => {
|
return AuthService.logout().then(() => {
|
||||||
commit('logout');
|
commit('logout');
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
register({ commit }: AuthActionContext, user) {
|
register({commit}: AuthActionContext, user) {
|
||||||
return AuthService.register(user.email, user.password).then(
|
return AuthService.register(user.email, user.password).then(
|
||||||
response => {
|
response => {
|
||||||
commit('registerSuccess');
|
commit('registerSuccess');
|
||||||
|
|
@ -63,7 +62,7 @@ const auth: Module<AuthState, RootState> = {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
refreshToken({ commit }: AuthActionContext, accessToken) {
|
refreshToken({commit}: AuthActionContext, accessToken) {
|
||||||
commit('refreshToken', 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', {
|
store.dispatch('auth/login', {
|
||||||
email: values.email,
|
email: values.email,
|
||||||
password: values.password,
|
password: values.password,
|
||||||
}).then(
|
}).then(() => {
|
||||||
() => {
|
resetForm();
|
||||||
resetForm();
|
onLogin();
|
||||||
onLogin();
|
}, error => {
|
||||||
},
|
loading.value = false;
|
||||||
error => {
|
console.log(error);
|
||||||
loading.value = false;
|
});
|
||||||
console.log(error);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}, ({errors}) => {
|
}, ({errors}) => {
|
||||||
console.log(errors);
|
console.log(errors);
|
||||||
loading.value = false;
|
loading.value = false;
|
||||||
|
|
@ -71,6 +68,7 @@ const submit = handleSubmit((values, { resetForm }) => {
|
||||||
const onLogin = () => {
|
const onLogin = () => {
|
||||||
console.log('check if pitch types are available.');
|
console.log('check if pitch types are available.');
|
||||||
|
|
||||||
|
|
||||||
PitchTypeService.fetchAll().then((pitchTypes: PitchType[]) => {
|
PitchTypeService.fetchAll().then((pitchTypes: PitchType[]) => {
|
||||||
store.commit('pitchTypes/initialize', pitchTypes);
|
store.commit('pitchTypes/initialize', pitchTypes);
|
||||||
router.push({path: '/home'});
|
router.push({path: '/home'});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue