Skip to content

Commit 9862854

Browse files
committed
Switch to a context store for server stuff to better support things in the future
1 parent 16e6f3f commit 9862854

21 files changed

+218
-148
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import http from '@/api/http';
2+
3+
export interface ServerDatabase {
4+
id: string;
5+
name: string;
6+
username: string;
7+
connectionString: string;
8+
allowConnectionsFrom: string;
9+
password?: string;
10+
}
11+
12+
export const rawDataToServerDatabase = (data: any): ServerDatabase => ({
13+
id: data.id,
14+
name: data.name,
15+
username: data.username,
16+
connectionString: `${data.host.address}:${data.host.port}`,
17+
allowConnectionsFrom: data.connections_from,
18+
password: data.relationships && data.relationships.password ? data.relationships.password.attributes.password : undefined,
19+
});
20+
21+
export default (uuid: string, includePassword: boolean = true): Promise<ServerDatabase[]> => {
22+
return new Promise((resolve, reject) => {
23+
http.get(`/api/client/servers/${uuid}/databases`, {
24+
params: includePassword ? { include: 'password' } : undefined,
25+
})
26+
.then(response => resolve(
27+
(response.data.data || []).map((item: any) => rawDataToServerDatabase(item.attributes))
28+
))
29+
.catch(reject);
30+
});
31+
};

resources/scripts/components/FlashMessageRender.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import MessageBox from '@/components/MessageBox';
33
import { State, useStoreState } from 'easy-peasy';
4-
import { ApplicationState } from '@/state/types';
4+
import { ApplicationStore } from '@/state';
55

66
type Props = Readonly<{
77
byKey?: string;
@@ -10,7 +10,7 @@ type Props = Readonly<{
1010
}>;
1111

1212
export default ({ withBottomSpace, spacerClass, byKey }: Props) => {
13-
const flashes = useStoreState((state: State<ApplicationState>) => state.flashes.items);
13+
const flashes = useStoreState((state: State<ApplicationStore>) => state.flashes.items);
1414

1515
let filtered = flashes;
1616
if (byKey) {

resources/scripts/components/auth/ForgotPasswordContainer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import requestPasswordResetEmail from '@/api/auth/requestPasswordResetEmail';
44
import { httpErrorToHuman } from '@/api/http';
55
import LoginFormContainer from '@/components/auth/LoginFormContainer';
66
import { Actions, useStoreActions } from 'easy-peasy';
7-
import { ApplicationState } from '@/state/types';
87
import FlashMessageRender from '@/components/FlashMessageRender';
8+
import { ApplicationStore } from '@/state';
99

1010
export default () => {
1111
const [ isSubmitting, setSubmitting ] = React.useState(false);
1212
const [ email, setEmail ] = React.useState('');
1313

14-
const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationState>) => actions.flashes);
14+
const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
1515

1616
const handleFieldUpdate = (e: React.ChangeEvent<HTMLInputElement>) => setEmail(e.target.value);
1717

resources/scripts/components/auth/LoginCheckpointContainer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import loginCheckpoint from '@/api/auth/loginCheckpoint';
44
import { httpErrorToHuman } from '@/api/http';
55
import LoginFormContainer from '@/components/auth/LoginFormContainer';
66
import { Actions, useStoreActions } from 'easy-peasy';
7-
import { ApplicationState } from '@/state/types';
87
import { StaticContext } from 'react-router';
98
import FlashMessageRender from '@/components/FlashMessageRender';
9+
import { ApplicationStore } from '@/state';
1010

1111
export default ({ history, location: { state } }: RouteComponentProps<{}, StaticContext, { token?: string }>) => {
1212
const [ code, setCode ] = useState('');
1313
const [ isLoading, setIsLoading ] = useState(false);
1414

15-
const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationState>) => actions.flashes);
15+
const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
1616

1717
if (!state || !state.token) {
1818
history.replace('/auth/login');

resources/scripts/components/auth/LoginContainer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import { httpErrorToHuman } from '@/api/http';
55
import LoginFormContainer from '@/components/auth/LoginFormContainer';
66
import FlashMessageRender from '@/components/FlashMessageRender';
77
import { Actions, useStoreActions } from 'easy-peasy';
8-
import { ApplicationState } from '@/state/types';
8+
import { ApplicationStore } from '@/state';
99

1010
export default ({ history }: RouteComponentProps) => {
1111
const [ username, setUsername ] = useState('');
1212
const [ password, setPassword ] = useState('');
1313
const [ isLoading, setLoading ] = useState(false);
1414

15-
const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationState>) => actions.flashes);
15+
const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
1616

1717
const submit = (e: React.FormEvent<HTMLFormElement>) => {
1818
e.preventDefault();

resources/scripts/components/auth/ResetPasswordContainer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { httpErrorToHuman } from '@/api/http';
77
import LoginFormContainer from '@/components/auth/LoginFormContainer';
88
import FlashMessageRender from '@/components/FlashMessageRender';
99
import { Actions, useStoreActions } from 'easy-peasy';
10-
import { ApplicationState } from '@/state/types';
10+
import { ApplicationStore } from '@/state';
1111

1212
type Props = Readonly<RouteComponentProps<{ token: string }> & {}>;
1313

@@ -17,7 +17,7 @@ export default (props: Props) => {
1717
const [ password, setPassword ] = useState('');
1818
const [ passwordConfirm, setPasswordConfirm ] = useState('');
1919

20-
const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationState>) => actions.flashes);
20+
const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
2121

2222
const parsed = parse(props.location.search);
2323
if (email.length === 0 && parsed.email) {

resources/scripts/components/dashboard/forms/UpdateEmailAddressForm.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React from 'react';
22
import { Actions, State, useStoreActions, useStoreState } from 'easy-peasy';
3-
import { ApplicationState } from '@/state/types';
43
import { Form, Formik, FormikActions } from 'formik';
54
import * as Yup from 'yup';
65
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
76
import Field from '@/components/elements/Field';
87
import { httpErrorToHuman } from '@/api/http';
8+
import { ApplicationStore } from '@/state';
99

1010
interface Values {
1111
email: string;
@@ -18,10 +18,10 @@ const schema = Yup.object().shape({
1818
});
1919

2020
export default () => {
21-
const user = useStoreState((state: State<ApplicationState>) => state.user.data);
22-
const updateEmail = useStoreActions((state: Actions<ApplicationState>) => state.user.updateUserEmail);
21+
const user = useStoreState((state: State<ApplicationStore>) => state.user.data);
22+
const updateEmail = useStoreActions((state: Actions<ApplicationStore>) => state.user.updateUserEmail);
2323

24-
const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationState>) => actions.flashes);
24+
const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
2525

2626
const submit = (values: Values, { resetForm, setSubmitting }: FormikActions<Values>) => {
2727
clearFlashes('account:email');

resources/scripts/components/dashboard/forms/UpdatePasswordForm.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React from 'react';
22
import { Actions, State, useStoreActions, useStoreState } from 'easy-peasy';
3-
import { ApplicationState } from '@/state/types';
43
import { Form, Formik, FormikActions } from 'formik';
54
import Field from '@/components/elements/Field';
65
import * as Yup from 'yup';
76
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
87
import updateAccountPassword from '@/api/account/updateAccountPassword';
98
import { httpErrorToHuman } from '@/api/http';
9+
import { ApplicationStore } from '@/state';
1010

1111
interface Values {
1212
current: string;
@@ -23,8 +23,8 @@ const schema = Yup.object().shape({
2323
});
2424

2525
export default () => {
26-
const user = useStoreState((state: State<ApplicationState>) => state.user.data);
27-
const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationState>) => actions.flashes);
26+
const user = useStoreState((state: State<ApplicationStore>) => state.user.data);
27+
const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
2828

2929
if (!user) {
3030
return null;

resources/scripts/components/server/Console.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import React, { createRef } from 'react';
22
import { Terminal } from 'xterm';
33
import * as TerminalFit from 'xterm/lib/addons/fit/fit';
44
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
5-
import { ApplicationState } from '@/state/types';
65
import { connect } from 'react-redux';
76
import { Websocket } from '@/plugins/Websocket';
7+
import { ServerStore } from '@/state/server';
88

99
const theme = {
1010
background: 'transparent',
@@ -113,8 +113,8 @@ class Console extends React.PureComponent<Readonly<Props>> {
113113
}
114114

115115
export default connect(
116-
(state: ApplicationState) => ({
117-
connected: state.server.socket.connected,
118-
instance: state.server.socket.instance,
116+
(state: ServerStore) => ({
117+
connected: state.socket.connected,
118+
instance: state.socket.instance,
119119
}),
120120
)(Console);

resources/scripts/components/server/ServerConsole.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import React from 'react';
22
import Console from '@/components/server/Console';
3-
import { State, useStoreState } from 'easy-peasy';
4-
import { ApplicationState } from '@/state/types';
3+
import { ServerContext } from '@/state/server';
54

65
export default () => {
7-
const status = useStoreState((state: State<ApplicationState>) => state.server.status);
6+
const status = ServerContext.useStoreState(state => state.status.value);
87

98
return (
109
<div className={'my-10 flex'}>

0 commit comments

Comments
 (0)