fix(ui/core_state): move userRole to login domain
This commit is contained in:
parent
794b14c4d4
commit
99607c66bc
11 changed files with 47 additions and 70 deletions
|
@ -16,6 +16,7 @@ describe("Login", () => {
|
||||||
|
|
||||||
const coreState = newWithWorker(mockWorker);
|
const coreState = newWithWorker(mockWorker);
|
||||||
const pane = new AuthPane({
|
const pane = new AuthPane({
|
||||||
|
userRole: coreState.login.userRole,
|
||||||
authed: coreState.login.authed,
|
authed: coreState.login.authed,
|
||||||
captchaID: coreState.login.captchaID,
|
captchaID: coreState.login.captchaID,
|
||||||
update: (updater: (prevState: ICoreState) => ICoreState) => {},
|
update: (updater: (prevState: ICoreState) => ICoreState) => {},
|
||||||
|
@ -32,31 +33,15 @@ describe("Login", () => {
|
||||||
|
|
||||||
// login
|
// login
|
||||||
expect(updater().props.login).toEqual({
|
expect(updater().props.login).toEqual({
|
||||||
|
userRole: "admin",
|
||||||
authed: true,
|
authed: true,
|
||||||
captchaID: "",
|
captchaID: "",
|
||||||
});
|
});
|
||||||
|
|
||||||
// panes
|
// panes
|
||||||
expect(updater().props.panes).toEqual({
|
expect(updater().props.panes).toEqual({
|
||||||
userRole: "admin",
|
|
||||||
displaying: "",
|
displaying: "",
|
||||||
paneNames: Set(["settings", "login", "admin"]),
|
paneNames: Set(["settings", "login", "admin"]),
|
||||||
});
|
});
|
||||||
|
|
||||||
// admin
|
|
||||||
let usersMap = Map({});
|
|
||||||
usersResps.listUsersMockResp.data.users.forEach((user: User) => {
|
|
||||||
usersMap = usersMap.set(user.name, user);
|
|
||||||
});
|
|
||||||
let roles = Set<string>();
|
|
||||||
Object.keys(usersResps.listRolesMockResp.data.roles).forEach(
|
|
||||||
(role: string) => {
|
|
||||||
roles = roles.add(role);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
expect(updater().props.admin).toEqual({
|
|
||||||
users: usersMap,
|
|
||||||
roles: roles,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -23,7 +23,6 @@ describe("Panes", () => {
|
||||||
panes.closePane();
|
panes.closePane();
|
||||||
|
|
||||||
expect(updater().props.panes).toEqual({
|
expect(updater().props.panes).toEqual({
|
||||||
userRole: coreState.panes.userRole,
|
|
||||||
displaying: "",
|
displaying: "",
|
||||||
paneNames: coreState.panes.paneNames,
|
paneNames: coreState.panes.paneNames,
|
||||||
});
|
});
|
||||||
|
|
|
@ -43,14 +43,14 @@ describe("State Manager", () => {
|
||||||
|
|
||||||
// panes
|
// panes
|
||||||
expect(coreState.panes).toEqual({
|
expect(coreState.panes).toEqual({
|
||||||
userRole: "admin",
|
displaying: "",
|
||||||
displaying: "browser",
|
|
||||||
paneNames: Set(["settings", "login", "admin"]),
|
paneNames: Set(["settings", "login", "admin"]),
|
||||||
});
|
});
|
||||||
|
|
||||||
// login
|
// login
|
||||||
expect(coreState.login).toEqual({
|
expect(coreState.login).toEqual({
|
||||||
authed: false,
|
userRole: "admin",
|
||||||
|
authed: true,
|
||||||
captchaID: "mockCaptchaID",
|
captchaID: "mockCaptchaID",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -50,11 +50,11 @@ export function initState(): ICoreState {
|
||||||
uploadFiles: List<File>([]),
|
uploadFiles: List<File>([]),
|
||||||
},
|
},
|
||||||
panes: {
|
panes: {
|
||||||
userRole: "",
|
|
||||||
displaying: "browser",
|
displaying: "browser",
|
||||||
paneNames: Set<string>(["settings", "login", "admin"]),
|
paneNames: Set<string>(["settings", "login", "admin"]),
|
||||||
},
|
},
|
||||||
login: {
|
login: {
|
||||||
|
userRole: "",
|
||||||
authed: false,
|
authed: false,
|
||||||
captchaID: "",
|
captchaID: "",
|
||||||
},
|
},
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { ICoreState } from "./core_state";
|
||||||
import { updater } from "./state_updater";
|
import { updater } from "./state_updater";
|
||||||
|
|
||||||
export interface Props {
|
export interface Props {
|
||||||
|
userRole: string;
|
||||||
authed: boolean;
|
authed: boolean;
|
||||||
captchaID: string;
|
captchaID: string;
|
||||||
update?: (updater: (prevState: ICoreState) => ICoreState) => void;
|
update?: (updater: (prevState: ICoreState) => ICoreState) => void;
|
||||||
|
@ -48,57 +49,38 @@ export class AuthPane extends React.Component<Props, State, {}> {
|
||||||
this.props.captchaID,
|
this.props.captchaID,
|
||||||
this.state.captchaInput
|
this.state.captchaInput
|
||||||
)
|
)
|
||||||
.then((ok: boolean) => {
|
.then((ok: boolean): Promise<any> => {
|
||||||
if (ok) {
|
if (ok) {
|
||||||
this.update(updater().updateLogin);
|
this.update(updater().updateLogin);
|
||||||
this.setState({ user: "", pwd: "" });
|
this.setState({ user: "", pwd: "", captchaInput: "" });
|
||||||
// close all the panes
|
// close all the panes
|
||||||
updater().displayPane("");
|
updater().displayPane("");
|
||||||
this.update(updater().updatePanes);
|
this.update(updater().updatePanes);
|
||||||
|
|
||||||
// refresh
|
// refresh
|
||||||
return updater().setHomeItems();
|
return Promise.all([
|
||||||
|
updater().setHomeItems(),
|
||||||
|
updater().refreshUploadings(),
|
||||||
|
updater().isSharing(updater().props.browser.dirPath.join("/")),
|
||||||
|
updater().listSharings(),
|
||||||
|
updater().self(),
|
||||||
|
]);
|
||||||
} else {
|
} else {
|
||||||
this.setState({ user: "", pwd: "" });
|
this.setState({ user: "", pwd: "", captchaInput: "" });
|
||||||
alert("Failed to login.");
|
alert("Failed to login.");
|
||||||
}
|
|
||||||
|
|
||||||
})
|
return updater().getCaptchaID();
|
||||||
.then(() => {
|
|
||||||
return updater().refreshUploadings();
|
|
||||||
})
|
|
||||||
.then(() => {
|
|
||||||
return updater().isSharing(
|
|
||||||
updater().props.browser.dirPath.join("/")
|
|
||||||
);
|
|
||||||
})
|
|
||||||
.then(() => {
|
|
||||||
return updater().listSharings();
|
|
||||||
})
|
|
||||||
.then(() => {
|
|
||||||
return updater().self();
|
|
||||||
})
|
|
||||||
.then(() => {
|
|
||||||
// TODO: should rely on props to get userRole
|
|
||||||
if (updater().props.panes.userRole === "admin") {
|
|
||||||
// TODO: remove hardcode
|
|
||||||
return updater().listRoles();
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
// TODO: should rely on props to get userRole
|
|
||||||
if (updater().props.panes.userRole === "admin") {
|
|
||||||
// TODO: remove hardcode
|
|
||||||
return updater().listUsers();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then((_: boolean) => {
|
|
||||||
this.update(updater().updateBrowser);
|
this.update(updater().updateBrowser);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
logout = () => {
|
logout = () => {
|
||||||
updater().logout().then((ok: boolean) => {
|
updater()
|
||||||
|
.logout()
|
||||||
|
.then((ok: boolean) => {
|
||||||
if (ok) {
|
if (ok) {
|
||||||
this.update(updater().updateLogin);
|
this.update(updater().updateLogin);
|
||||||
} else {
|
} else {
|
||||||
|
@ -107,6 +89,14 @@ export class AuthPane extends React.Component<Props, State, {}> {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
refreshCaptcha = async () => {
|
||||||
|
return updater()
|
||||||
|
.getCaptchaID()
|
||||||
|
.then(() => {
|
||||||
|
this.props.update(updater().updateLogin);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<span>
|
<span>
|
||||||
|
@ -157,6 +147,7 @@ export class AuthPane extends React.Component<Props, State, {}> {
|
||||||
<img
|
<img
|
||||||
src={`/v1/captchas/imgs?capid=${this.props.captchaID}`}
|
src={`/v1/captchas/imgs?capid=${this.props.captchaID}`}
|
||||||
className="captcha"
|
className="captcha"
|
||||||
|
onClick={this.refreshCaptcha}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex-list-item-l"></div>
|
<div className="flex-list-item-l"></div>
|
||||||
|
|
|
@ -147,6 +147,7 @@ export class PaneSettings extends React.Component<Props, State, {}> {
|
||||||
</div>
|
</div>
|
||||||
<div className="flex-list-item-r">
|
<div className="flex-list-item-r">
|
||||||
<AuthPane
|
<AuthPane
|
||||||
|
userRole={this.props.login.userRole}
|
||||||
authed={this.props.login.authed}
|
authed={this.props.login.authed}
|
||||||
captchaID={this.props.login.captchaID}
|
captchaID={this.props.login.captchaID}
|
||||||
update={this.update}
|
update={this.update}
|
||||||
|
|
|
@ -9,7 +9,6 @@ import { AuthPane, Props as AuthPaneProps } from "./pane_login";
|
||||||
|
|
||||||
export interface PanesProps {
|
export interface PanesProps {
|
||||||
displaying: string;
|
displaying: string;
|
||||||
userRole: string;
|
|
||||||
paneNames: Set<string>;
|
paneNames: Set<string>;
|
||||||
}
|
}
|
||||||
export interface Props {
|
export interface Props {
|
||||||
|
@ -46,6 +45,7 @@ export class Panes extends React.Component<Props, State, {}> {
|
||||||
),
|
),
|
||||||
login: (
|
login: (
|
||||||
<AuthPane
|
<AuthPane
|
||||||
|
userRole={this.props.login.userRole}
|
||||||
authed={this.props.login.authed}
|
authed={this.props.login.authed}
|
||||||
captchaID={this.props.login.captchaID}
|
captchaID={this.props.login.captchaID}
|
||||||
update={this.props.update}
|
update={this.props.update}
|
||||||
|
@ -53,7 +53,7 @@ export class Panes extends React.Component<Props, State, {}> {
|
||||||
),
|
),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (this.props.panes.userRole === "admin") {
|
if (this.props.login.userRole === "admin") {
|
||||||
panesMap = panesMap.set(
|
panesMap = panesMap.set(
|
||||||
"admin",
|
"admin",
|
||||||
<AdminPane
|
<AdminPane
|
||||||
|
|
|
@ -29,7 +29,7 @@ export class RootFrame extends React.Component<Props, State, {}> {
|
||||||
update={this.props.update}
|
update={this.props.update}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<TopBar update={this.props.update}></TopBar>
|
<TopBar login={this.props.panes.login} update={this.props.update}></TopBar>
|
||||||
|
|
||||||
<div className="container-center">
|
<div className="container-center">
|
||||||
<Browser
|
<Browser
|
||||||
|
|
|
@ -88,13 +88,13 @@ export class StateMgr extends React.Component<Props, State, {}> {
|
||||||
return updater().self();
|
return updater().self();
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
if (updater().props.panes.userRole === "admin") {
|
if (updater().props.login.userRole === "admin") {
|
||||||
// TODO: remove hardcode
|
// TODO: remove hardcode
|
||||||
return updater().listRoles();
|
return updater().listRoles();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
if (updater().props.panes.userRole === "admin") {
|
if (updater().props.login.userRole === "admin") {
|
||||||
// TODO: remove hardcode
|
// TODO: remove hardcode
|
||||||
return updater().listUsers();
|
return updater().listUsers();
|
||||||
}
|
}
|
||||||
|
|
|
@ -202,7 +202,7 @@ export class Updater {
|
||||||
self = async (): Promise<boolean> => {
|
self = async (): Promise<boolean> => {
|
||||||
const resp = await this.usersClient.self();
|
const resp = await this.usersClient.self();
|
||||||
if (resp.status === 200) {
|
if (resp.status === 200) {
|
||||||
this.props.panes.userRole = resp.data.role;
|
this.props.login.userRole = resp.data.role;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
|
|
||||||
import { ICoreState } from "./core_state";
|
import { ICoreState } from "./core_state";
|
||||||
|
import { Props as LoginProps } from "./pane_login";
|
||||||
import { updater } from "./state_updater";
|
import { updater } from "./state_updater";
|
||||||
|
|
||||||
export interface State {}
|
export interface State {}
|
||||||
export interface Props {
|
export interface Props {
|
||||||
|
login: LoginProps;
|
||||||
update?: (updater: (prevState: ICoreState) => ICoreState) => void;
|
update?: (updater: (prevState: ICoreState) => ICoreState) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,11 +24,10 @@ export class TopBar extends React.Component<Props, State, {}> {
|
||||||
return updater()
|
return updater()
|
||||||
.self()
|
.self()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
// TODO: use props instead
|
|
||||||
// TODO: remove hardcode role
|
// TODO: remove hardcode role
|
||||||
if (
|
if (
|
||||||
updater().props.login.authed &&
|
this.props.login.authed &&
|
||||||
updater().props.panes.userRole === "admin"
|
this.props.login.userRole === "admin"
|
||||||
) {
|
) {
|
||||||
return Promise.all([updater().listRoles(), updater().listUsers()]);
|
return Promise.all([updater().listRoles(), updater().listUsers()]);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue