Alex Mikhalev
7 years ago
15 changed files with 7855 additions and 30 deletions
@ -1,28 +1,42 @@ |
|||||||
import { Location } from "history"; |
import * as History from "history"; |
||||||
import * as React from "react"; |
import * as React from "react"; |
||||||
import { withRouter } from "react-router"; |
|
||||||
import { Link } from "react-router-dom"; |
import { Link } from "react-router-dom"; |
||||||
import { Menu } from "semantic-ui-react"; |
import { Menu } from "semantic-ui-react"; |
||||||
|
|
||||||
|
import { AppState, injectState } from "@app/state"; |
||||||
|
import { observer } from "mobx-react"; |
||||||
|
|
||||||
interface NavItemProps { |
interface NavItemProps { |
||||||
to: string; |
to: string; |
||||||
children: React.ReactNode; |
children: React.ReactNode; |
||||||
|
location: History.Location; |
||||||
} |
} |
||||||
|
|
||||||
function NavItem({ to, children }: NavItemProps) { |
@observer |
||||||
|
function NavItem({ to, children, location }: NavItemProps) { |
||||||
return <Menu.Item as={Link} to={to} active={location.pathname.startsWith(to)}>{children}</Menu.Item>; |
return <Menu.Item as={Link} to={to} active={location.pathname.startsWith(to)}>{children}</Menu.Item>; |
||||||
} |
} |
||||||
|
|
||||||
function NavBar({ location }: { location: Location }) { |
function NavBar({ appState }: { appState: AppState }) { |
||||||
|
let loginMenu; |
||||||
|
if (appState.isLoggedIn) { |
||||||
|
loginMenu = ( |
||||||
|
<NavItem to="/logout">Logout</NavItem> |
||||||
|
); |
||||||
|
} else { |
||||||
|
loginMenu = ( |
||||||
|
<NavItem to="/login">Login</NavItem> |
||||||
|
); |
||||||
|
} |
||||||
return ( |
return ( |
||||||
<Menu> |
<Menu> |
||||||
<NavItem to="/devices/grinklers">Device grinklers</NavItem> |
<NavItem to="/devices/grinklers">Device grinklers</NavItem> |
||||||
<NavItem to="/messagesTest">Messages test</NavItem> |
<NavItem to="/messagesTest">Messages test</NavItem> |
||||||
<Menu.Menu position="right"> |
<Menu.Menu position="right"> |
||||||
<NavItem to="/login">Login</NavItem> |
{loginMenu} |
||||||
</Menu.Menu> |
</Menu.Menu> |
||||||
</Menu> |
</Menu> |
||||||
); |
); |
||||||
} |
} |
||||||
|
|
||||||
export default withRouter(NavBar); |
export default observer(injectState(NavBar)); |
||||||
|
@ -1,11 +1,26 @@ |
|||||||
|
import { ErrorCode, toHttpStatus } from "@common/sprinklersRpc/ErrorCode"; |
||||||
|
|
||||||
export class ApiError extends Error { |
export class ApiError extends Error { |
||||||
name = "ApiError"; |
name = "ApiError"; |
||||||
statusCode: number; |
statusCode: number; |
||||||
cause?: Error; |
code: ErrorCode; |
||||||
|
data: any; |
||||||
|
|
||||||
constructor(statusCode: number, message: string, cause?: Error) { |
constructor(message: string, code: ErrorCode = ErrorCode.BadRequest, data: any = {}) { |
||||||
super(message); |
super(message); |
||||||
this.statusCode = statusCode; |
this.statusCode = toHttpStatus(code); |
||||||
this.cause = cause; |
this.code = code; |
||||||
|
// tslint:disable-next-line:prefer-conditional-expression
|
||||||
|
if (data instanceof Error) { |
||||||
|
this.data = data.toString(); |
||||||
|
} else { |
||||||
|
this.data = data; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
toJSON() { |
||||||
|
return { |
||||||
|
message: this.message, code: this.code, data: this.data, |
||||||
|
}; |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue