Skip to content

Commit e20a768

Browse files
committed
Plop user data into the store when loading up the base component
1 parent 328347f commit e20a768

File tree

5 files changed

+85
-26
lines changed

5 files changed

+85
-26
lines changed

resources/scripts/components/App.tsx

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,47 @@ import AccountRouter from '@/routers/AccountRouter';
66
import ServerOverviewContainer from '@/components/ServerOverviewContainer';
77
import { StoreProvider } from 'easy-peasy';
88
import { store } from '@/state';
9+
import { UserData } from '@/state/types';
910

10-
class App extends React.PureComponent {
11-
componentDidMount () {
11+
interface WindowWithUser extends Window {
12+
PterodactylUser?: {
13+
uuid: string;
14+
username: string;
15+
email: string;
16+
root_admin: boolean;
17+
use_totp: boolean;
18+
language: string;
19+
updated_at: string;
20+
created_at: string;
21+
};
22+
}
1223

24+
const App = () => {
25+
const data = (window as WindowWithUser).PterodactylUser;
26+
if (data) {
27+
store.getActions().user.setUserData({
28+
uuid: data.uuid,
29+
username: data.username,
30+
email: data.email,
31+
language: data.language,
32+
rootAdmin: data.root_admin,
33+
useTotp: data.use_totp,
34+
createdAt: new Date(data.created_at),
35+
updatedAt: new Date(data.updated_at),
36+
});
1337
}
1438

15-
render () {
16-
return (
17-
<StoreProvider store={store}>
18-
<Router basename={'/'}>
19-
<div className={'mx-auto px-10 w-auto'} style={{ maxWidth: '1000px' }}>
20-
<Route exact path="/" component={ServerOverviewContainer}/>
21-
<Route path="/auth" component={AuthenticationRouter}/>
22-
<Route path="/account" component={AccountRouter}/>
23-
</div>
24-
</Router>
25-
</StoreProvider>
26-
);
27-
}
28-
}
39+
return (
40+
<StoreProvider store={store}>
41+
<Router basename={'/'}>
42+
<div className={'mx-auto px-10 w-auto'} style={{ maxWidth: '1000px' }}>
43+
<Route exact path="/" component={ServerOverviewContainer}/>
44+
<Route path="/auth" component={AuthenticationRouter}/>
45+
<Route path="/account" component={AccountRouter}/>
46+
</div>
47+
</Router>
48+
</StoreProvider>
49+
);
50+
};
2951

3052
export default hot(App);

resources/scripts/state/index.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
import { action, createStore } from 'easy-peasy';
1+
import { createStore } from 'easy-peasy';
22
import { ApplicationState } from '@/state/types';
3+
import flashes from '@/state/models/flashes';
4+
import user from '@/state/models/user';
35

46
const state: ApplicationState = {
5-
flashes: {
6-
items: [],
7-
addFlash: action((state, payload) => {
8-
state.items.push(payload);
9-
}),
10-
clearFlashes: action(state => {
11-
state.items = [];
12-
}),
13-
},
7+
flashes,
8+
user,
149
};
1510

1611
export const store = createStore(state);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { action } from 'easy-peasy';
2+
import { FlashState } from '@/state/types';
3+
4+
const flashes: FlashState = {
5+
items: [],
6+
addFlash: action((state, payload) => {
7+
state.items.push(payload);
8+
}),
9+
clearFlashes: action(state => {
10+
state.items = [];
11+
}),
12+
};
13+
14+
export default flashes;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { UserState } from '@/state/types';
2+
import { action } from 'easy-peasy';
3+
4+
const user: UserState = {
5+
data: undefined,
6+
setUserData: action((state, payload) => {
7+
state.data = payload;
8+
}),
9+
};
10+
11+
export default user;

resources/scripts/state/types.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Action } from 'easy-peasy';
33

44
export interface ApplicationState {
55
flashes: FlashState;
6+
user: UserState;
67
}
78

89
export interface FlashState {
@@ -11,6 +12,22 @@ export interface FlashState {
1112
clearFlashes: Action<FlashState>;
1213
}
1314

15+
export interface UserState {
16+
data?: UserData;
17+
setUserData: Action<UserState, UserData>;
18+
}
19+
20+
export interface UserData {
21+
uuid: string;
22+
username: string;
23+
email: string;
24+
language: string;
25+
rootAdmin: boolean;
26+
useTotp: boolean;
27+
createdAt: Date;
28+
updatedAt: Date;
29+
}
30+
1431
export interface FlashMessage {
1532
id?: string;
1633
type: FlashMessageType;

0 commit comments

Comments
 (0)