forked from Systho/ipl-web3-2018
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
solutions to react exercices of week 6
- Loading branch information
Showing
7 changed files
with
137 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,24 @@ | ||
import React from 'react'; | ||
import React from "react"; | ||
|
||
import Navbar from 'react-bootstrap/lib/Navbar'; | ||
import { withAuthentication } from "react/contexts/authentication.js"; | ||
import { Navbar, Nav, Button } from "react-bootstrap"; | ||
import { withAuthentication } from "react/contexts/authentication"; | ||
import { withTheme } from "react/contexts/theme"; | ||
|
||
|
||
const Navigation = ({ jwt }) => { | ||
return ( | ||
<Navbar bg="dark" variant="dark" fixed="bottom" > | ||
<Navbar.Brand>Current JWT : { jwt } </Navbar.Brand> | ||
|
||
</Navbar> | ||
); | ||
} | ||
const Navigation = ({ jwt, theme, toggleTheme }) => { | ||
return ( | ||
<Navbar bg={theme} variant={theme} fixed="bottom"> | ||
<Nav className="mr-auto"> | ||
<Navbar.Brand>Current JWT : {jwt} </Navbar.Brand> | ||
</Nav> | ||
<Button variant="outline-info" onClick={toggleTheme}> | ||
Change Theme | ||
</Button> | ||
</Navbar> | ||
); | ||
}; | ||
|
||
const NavigationWithAuthentication = withAuthentication(Navigation); | ||
export default NavigationWithAuthentication; | ||
const NavigationWithAuthenticationAndTheme = withTheme( | ||
NavigationWithAuthentication | ||
); | ||
export default NavigationWithAuthenticationAndTheme; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,32 @@ | ||
import React from "react"; | ||
import { Route, Redirect, withRouter } from "react-router-dom"; | ||
|
||
import { Route } from "react-router-dom"; | ||
|
||
import { withAuthentication } from "react/contexts/authentication"; | ||
import HelloWorld from "./hello_world/hello_world"; | ||
import HelloFromParams from "./hello_world/hello_from_params"; | ||
import TodoAppContainer from "./todo_app/todo_app_container"; | ||
import MessagesContainer from "./messages/messages_container"; | ||
import MessageContainer from "./message/message_container"; | ||
import LoginContainer from "./login/login_container"; | ||
|
||
function RouterOutlet() { | ||
function RouterOutlet({ isAuthenticated, location: { pathname } }) { | ||
const redirectToLogin = !isAuthenticated && pathname !== "/login"; | ||
|
||
return ( | ||
<React.Fragment> | ||
<Route | ||
exact | ||
path="/" | ||
render={() => <HelloWorld name="bob" />} | ||
/> | ||
<Route path="/hello/:name" component={HelloFromParams} /> | ||
<Route path="/todo" component={TodoAppContainer} /> | ||
<Route path="/messages" component={MessagesContainer} /> | ||
<Route path="/message/:id" component={MessageContainer} /> | ||
<Route path="/login" component={LoginContainer} /> | ||
{ redirectToLogin && <Redirect to="/login" /> } | ||
{ !redirectToLogin && | ||
<React.Fragment> | ||
<Route exact path="/" render={() => <HelloWorld name="bob" />} /> | ||
<Route path="/hello/:name" component={HelloFromParams} /> | ||
<Route path="/todo" component={TodoAppContainer} /> | ||
<Route path="/messages" component={MessagesContainer} /> | ||
<Route path="/message/:id" component={MessageContainer} /> | ||
<Route path="/login" component={LoginContainer} /> | ||
</React.Fragment> | ||
} | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
export default RouterOutlet; | ||
export default withRouter(withAuthentication(RouterOutlet)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import React from "react"; | ||
import withContextConsumer from "react/utils/with_context_consumer.jsx"; | ||
|
||
const ThemeContext = React.createContext({ | ||
theme: "dark", | ||
setThemeLight: () => {}, | ||
setThemeDark: () => {}, | ||
toggleTheme: () => {}, | ||
}); | ||
|
||
const ThemeConsumer = ThemeContext.Consumer; | ||
|
||
class ThemeProvider extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
theme: "dark" | ||
}; | ||
|
||
this.setTheme = this.setTheme.bind(this); | ||
this.toggleTheme = this.toggleTheme.bind(this); | ||
} | ||
|
||
setTheme(value) { | ||
this.setState({ | ||
theme: value | ||
}); | ||
} | ||
|
||
toggleTheme() { | ||
const { theme } = this.state; | ||
|
||
switch(theme){ | ||
case "dark": | ||
this.setTheme("light"); | ||
break; | ||
|
||
default: | ||
case "light": | ||
this.setTheme("dark"); | ||
} | ||
} | ||
|
||
render() { | ||
const { setTheme, toggleTheme } = this; | ||
const { theme } = this.state; | ||
const { children } = this.props; | ||
|
||
const providerValues = { | ||
theme, | ||
setThemeDark: () => setTheme("dark") , | ||
setThemeLight: () => setTheme("light") , | ||
toggleTheme, | ||
}; | ||
return ( | ||
<ThemeContext.Provider value={providerValues}> | ||
{children} | ||
</ThemeContext.Provider> | ||
); | ||
} | ||
} | ||
|
||
const withTheme = withContextConsumer(ThemeConsumer); | ||
|
||
export { ThemeConsumer, ThemeProvider, withTheme }; |