Alex Mikhalev
7 years ago
15 changed files with 7855 additions and 30 deletions
@ -1,28 +1,42 @@
@@ -1,28 +1,42 @@
|
||||
import { Location } from "history"; |
||||
import * as History from "history"; |
||||
import * as React from "react"; |
||||
import { withRouter } from "react-router"; |
||||
import { Link } from "react-router-dom"; |
||||
import { Menu } from "semantic-ui-react"; |
||||
|
||||
import { AppState, injectState } from "@app/state"; |
||||
import { observer } from "mobx-react"; |
||||
|
||||
interface NavItemProps { |
||||
to: string; |
||||
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>; |
||||
} |
||||
|
||||
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 ( |
||||
<Menu> |
||||
<NavItem to="/devices/grinklers">Device grinklers</NavItem> |
||||
<NavItem to="/messagesTest">Messages test</NavItem> |
||||
<Menu.Menu position="right"> |
||||
<NavItem to="/login">Login</NavItem> |
||||
{loginMenu} |
||||
</Menu.Menu> |
||||
</Menu> |
||||
); |
||||
} |
||||
|
||||
export default withRouter(NavBar); |
||||
export default observer(injectState(NavBar)); |
||||
|
@ -1,11 +1,26 @@
@@ -1,11 +1,26 @@
|
||||
import { ErrorCode, toHttpStatus } from "@common/sprinklersRpc/ErrorCode"; |
||||
|
||||
export class ApiError extends Error { |
||||
name = "ApiError"; |
||||
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); |
||||
this.statusCode = statusCode; |
||||
this.cause = cause; |
||||
this.statusCode = toHttpStatus(code); |
||||
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