Plop user data into the store when loading up the base component
This commit is contained in:
parent
328347fab7
commit
e20a768182
5 changed files with 87 additions and 28 deletions
|
@ -6,13 +6,36 @@ import AccountRouter from '@/routers/AccountRouter';
|
||||||
import ServerOverviewContainer from '@/components/ServerOverviewContainer';
|
import ServerOverviewContainer from '@/components/ServerOverviewContainer';
|
||||||
import { StoreProvider } from 'easy-peasy';
|
import { StoreProvider } from 'easy-peasy';
|
||||||
import { store } from '@/state';
|
import { store } from '@/state';
|
||||||
|
import { UserData } from '@/state/types';
|
||||||
|
|
||||||
class App extends React.PureComponent {
|
interface WindowWithUser extends Window {
|
||||||
componentDidMount () {
|
PterodactylUser?: {
|
||||||
|
uuid: string;
|
||||||
|
username: string;
|
||||||
|
email: string;
|
||||||
|
root_admin: boolean;
|
||||||
|
use_totp: boolean;
|
||||||
|
language: string;
|
||||||
|
updated_at: string;
|
||||||
|
created_at: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const App = () => {
|
||||||
|
const data = (window as WindowWithUser).PterodactylUser;
|
||||||
|
if (data) {
|
||||||
|
store.getActions().user.setUserData({
|
||||||
|
uuid: data.uuid,
|
||||||
|
username: data.username,
|
||||||
|
email: data.email,
|
||||||
|
language: data.language,
|
||||||
|
rootAdmin: data.root_admin,
|
||||||
|
useTotp: data.use_totp,
|
||||||
|
createdAt: new Date(data.created_at),
|
||||||
|
updatedAt: new Date(data.updated_at),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
render () {
|
|
||||||
return (
|
return (
|
||||||
<StoreProvider store={store}>
|
<StoreProvider store={store}>
|
||||||
<Router basename={'/'}>
|
<Router basename={'/'}>
|
||||||
|
@ -24,7 +47,6 @@ class App extends React.PureComponent {
|
||||||
</Router>
|
</Router>
|
||||||
</StoreProvider>
|
</StoreProvider>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
|
||||||
export default hot(App);
|
export default hot(App);
|
||||||
|
|
|
@ -1,16 +1,11 @@
|
||||||
import { action, createStore } from 'easy-peasy';
|
import { createStore } from 'easy-peasy';
|
||||||
import { ApplicationState } from '@/state/types';
|
import { ApplicationState } from '@/state/types';
|
||||||
|
import flashes from '@/state/models/flashes';
|
||||||
|
import user from '@/state/models/user';
|
||||||
|
|
||||||
const state: ApplicationState = {
|
const state: ApplicationState = {
|
||||||
flashes: {
|
flashes,
|
||||||
items: [],
|
user,
|
||||||
addFlash: action((state, payload) => {
|
|
||||||
state.items.push(payload);
|
|
||||||
}),
|
|
||||||
clearFlashes: action(state => {
|
|
||||||
state.items = [];
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const store = createStore(state);
|
export const store = createStore(state);
|
||||||
|
|
14
resources/scripts/state/models/flashes.ts
Normal file
14
resources/scripts/state/models/flashes.ts
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
import { action } from 'easy-peasy';
|
||||||
|
import { FlashState } from '@/state/types';
|
||||||
|
|
||||||
|
const flashes: FlashState = {
|
||||||
|
items: [],
|
||||||
|
addFlash: action((state, payload) => {
|
||||||
|
state.items.push(payload);
|
||||||
|
}),
|
||||||
|
clearFlashes: action(state => {
|
||||||
|
state.items = [];
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
|
||||||
|
export default flashes;
|
11
resources/scripts/state/models/user.ts
Normal file
11
resources/scripts/state/models/user.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
import { UserState } from '@/state/types';
|
||||||
|
import { action } from 'easy-peasy';
|
||||||
|
|
||||||
|
const user: UserState = {
|
||||||
|
data: undefined,
|
||||||
|
setUserData: action((state, payload) => {
|
||||||
|
state.data = payload;
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
|
||||||
|
export default user;
|
17
resources/scripts/state/types.d.ts
vendored
17
resources/scripts/state/types.d.ts
vendored
|
@ -3,6 +3,7 @@ import { Action } from 'easy-peasy';
|
||||||
|
|
||||||
export interface ApplicationState {
|
export interface ApplicationState {
|
||||||
flashes: FlashState;
|
flashes: FlashState;
|
||||||
|
user: UserState;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface FlashState {
|
export interface FlashState {
|
||||||
|
@ -11,6 +12,22 @@ export interface FlashState {
|
||||||
clearFlashes: Action<FlashState>;
|
clearFlashes: Action<FlashState>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface UserState {
|
||||||
|
data?: UserData;
|
||||||
|
setUserData: Action<UserState, UserData>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UserData {
|
||||||
|
uuid: string;
|
||||||
|
username: string;
|
||||||
|
email: string;
|
||||||
|
language: string;
|
||||||
|
rootAdmin: boolean;
|
||||||
|
useTotp: boolean;
|
||||||
|
createdAt: Date;
|
||||||
|
updatedAt: Date;
|
||||||
|
}
|
||||||
|
|
||||||
export interface FlashMessage {
|
export interface FlashMessage {
|
||||||
id?: string;
|
id?: string;
|
||||||
type: FlashMessageType;
|
type: FlashMessageType;
|
||||||
|
|
Loading…
Reference in a new issue