-
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.
Merge pull request #2 from arturparkhisenko/add-redux-tool-kit-example
Add Redux Tool Kit example
- Loading branch information
Showing
49 changed files
with
790 additions
and
232 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
13 changes: 6 additions & 7 deletions
13
...rks-libraries/react-redux-stack/.eslintrc → ...ibraries/react-redux-stack/.eslintrc.json
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
14 changes: 6 additions & 8 deletions
14
javascript/frameworks-libraries/react-redux-stack/public/index.html
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,16 +1,14 @@ | ||
<!doctype html> | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | ||
<meta name="theme-color" content="#000000"> | ||
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico"> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<meta name="theme-color" content="#000000" /> | ||
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" /> | ||
<title>React App</title> | ||
</head> | ||
<body> | ||
<noscript> | ||
You need to enable JavaScript to run this app. | ||
</noscript> | ||
<noscript>You need to enable JavaScript to run this app.</noscript> | ||
<div id="root"></div> | ||
</body> | ||
</html> |
23 changes: 23 additions & 0 deletions
23
javascript/frameworks-libraries/react-redux-stack/src-6-rtk/App.jsx
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,23 @@ | ||
import React, { Component } from 'react'; | ||
import { Provider } from 'react-redux'; | ||
|
||
import Chat from './containers/Chat'; | ||
// import Button from './components/Button'; | ||
// import ButtonFunctional from './components/ButtonFunctional'; | ||
import { store } from './model/store'; | ||
|
||
window.store = store; | ||
// store.subscribe(() => console.log('subscribe', store.getState())); | ||
// TODO: @see https://redux-toolkit.js.org/api/createListenerMiddleware | ||
|
||
export default class App extends Component { | ||
render() { | ||
return ( | ||
<Provider store={store}> | ||
<Chat /> | ||
{/* <Button text="Usual Button" /> */} | ||
{/* <ButtonFunctional text="ButtonFunctional" /> */} | ||
</Provider> | ||
); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
javascript/frameworks-libraries/react-redux-stack/src-6-rtk/App.test.js
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,8 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import App from './App'; | ||
|
||
test('renders without crashing', () => { | ||
render(<App />); | ||
const linkElement = screen.getByText(/learn react/i); | ||
expect(linkElement).toBeInTheDocument(); | ||
}); |
13 changes: 13 additions & 0 deletions
13
javascript/frameworks-libraries/react-redux-stack/src-6-rtk/components/Button.js
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,13 @@ | ||
import React from 'react'; | ||
|
||
class Button extends React.Component { | ||
render() { | ||
return ( | ||
<button onClick={this.props.onClick}> | ||
{this.props.text} | ||
</button> | ||
); | ||
} | ||
} | ||
|
||
export default Button; |
9 changes: 9 additions & 0 deletions
9
javascript/frameworks-libraries/react-redux-stack/src-6-rtk/components/ButtonFunctional.js
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,9 @@ | ||
import React from 'react'; | ||
|
||
const ButtonFunctional = ({text, onClick}) => ( | ||
<button onClick={onClick}> | ||
{text} | ||
</button> | ||
); | ||
|
||
export default ButtonFunctional; |
5 changes: 5 additions & 0 deletions
5
javascript/frameworks-libraries/react-redux-stack/src-6-rtk/components/Heading5.js
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,5 @@ | ||
import styled from 'styled-components'; | ||
|
||
export default styled.h5` | ||
margin: 0 0 1em; | ||
`; |
54 changes: 54 additions & 0 deletions
54
javascript/frameworks-libraries/react-redux-stack/src-6-rtk/components/Messages.jsx
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,54 @@ | ||
import React, { PureComponent } from 'react'; | ||
// Components --------------------------- | ||
import Ul from './Ul'; | ||
import Heading5 from './Heading5'; | ||
// Styles ------------------------------- | ||
import styled from 'styled-components'; | ||
|
||
const Form = styled.form` | ||
padding: 0.5em; | ||
`; | ||
|
||
const Info = styled.span` | ||
opacity: 0.5; | ||
`; | ||
|
||
class Messages extends PureComponent { | ||
submitForm(event) { | ||
event.preventDefault(); | ||
console.log(this.input.value); | ||
this.props.addMessage(this.input.value, '1'); | ||
this.input.value = ''; | ||
} | ||
|
||
render() { | ||
console.log('Messages props', this.props); // {users} | ||
return ( | ||
<Form onSubmit={this.submitForm.bind(this)}> | ||
<Heading5>Messages:</Heading5> | ||
<Ul> | ||
{this.props.messages.map((message) => { | ||
const time = new Date(message.timeStamp); | ||
const hours = time.getHours(); | ||
const minutes = time.getMinutes(); | ||
return ( | ||
<li key={message.timeStamp}> | ||
<Info>{`${hours}:${minutes} ${message.author} `}</Info> | ||
{message.text} | ||
</li> | ||
); | ||
})} | ||
</Ul> | ||
<hr /> | ||
|
||
<input | ||
ref={(input) => (this.input = input)} | ||
type="text" | ||
placeholder="Enter a message" | ||
/> | ||
</Form> | ||
); | ||
} | ||
} | ||
|
||
export default Messages; |
8 changes: 8 additions & 0 deletions
8
javascript/frameworks-libraries/react-redux-stack/src-6-rtk/components/Ul.js
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,8 @@ | ||
import styled from 'styled-components'; | ||
|
||
export default styled.ul` | ||
padding: 0; | ||
list-style: none; | ||
height: 200px; | ||
overflow: auto; | ||
`; |
32 changes: 32 additions & 0 deletions
32
javascript/frameworks-libraries/react-redux-stack/src-6-rtk/components/UserList.jsx
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,32 @@ | ||
import React, {Component} from 'react'; | ||
// Components --------------------------- | ||
import Ul from './Ul'; | ||
// Styles ------------------------------- | ||
import styled from 'styled-components'; | ||
|
||
const Li = styled.li` | ||
width: 60px; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
`; | ||
|
||
const UlStyled = styled(Ul)` | ||
font-family: monospace; | ||
`; | ||
|
||
class UserList extends Component { | ||
render() { | ||
console.log('UserList props', this.props); // {users} | ||
return ( | ||
<Ul className="UserList"> | ||
<UlStyled className="UserList"> | ||
{this.props.users.map(user => { | ||
return <Li key={user}>{user}</Li>; | ||
})} | ||
</UlStyled> | ||
</Ul> | ||
); | ||
} | ||
} | ||
|
||
export default UserList; |
55 changes: 55 additions & 0 deletions
55
javascript/frameworks-libraries/react-redux-stack/src-6-rtk/containers/Chat.jsx
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,55 @@ | ||
import React, { Component } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import styled from 'styled-components'; | ||
|
||
import Messages from './../components/Messages'; | ||
import Users from './../containers/Users'; | ||
import { chatSelector } from './../model/selectors'; | ||
import { addMessage } from './../model/messagesSlice'; | ||
import { changeUser } from './../model/actions'; | ||
|
||
const Content = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
`; | ||
|
||
const Panels = styled.div` | ||
display: flex; | ||
background-color: #f5f5f5; | ||
padding: 0.5em; | ||
`; | ||
|
||
class Chat extends Component { | ||
render() { | ||
console.log('Chat props', this.props); | ||
return ( | ||
<Content> | ||
<h3>Chat</h3> | ||
|
||
<Panels> | ||
<Users addUser={this.props.addUser} users={this.props.users} /> | ||
<Messages | ||
addMessage={this.props.addMessage} | ||
messages={this.props.messages} | ||
/> | ||
</Panels> | ||
</Content> | ||
); | ||
} | ||
} | ||
|
||
// used a selector below | ||
// const mapStateToProps = state => { | ||
// return { | ||
// users: state.users, | ||
// messages: state.messages | ||
// }; | ||
// }; | ||
|
||
const mapDispatchToProps = { addUser: changeUser, addMessage }; | ||
|
||
// usual | ||
//export default connect(mapStateToProps, mapDispatchToProps)(Chat); | ||
|
||
// with reselect | ||
export default connect(chatSelector, mapDispatchToProps)(Chat); |
28 changes: 28 additions & 0 deletions
28
javascript/frameworks-libraries/react-redux-stack/src-6-rtk/containers/Users.jsx
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,28 @@ | ||
import React, {Component} from 'react'; | ||
// Components --------------------------- | ||
import UserList from './../components/UserList'; | ||
import Heading5 from './../components/Heading5'; | ||
// Styles ------------------------------- | ||
import styled from 'styled-components'; | ||
|
||
const Content = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
padding: 0.5em; | ||
background-color: #cccdff; | ||
`; | ||
|
||
class Users extends Component { | ||
render() { | ||
console.log('Users props', this.props); // {users} | ||
return ( | ||
<Content> | ||
<Heading5>Users:</Heading5> | ||
<button onClick={this.props.addUser}>add</button> | ||
<UserList users={this.props.users}/> | ||
</Content> | ||
); | ||
} | ||
} | ||
|
||
export default Users; |
14 changes: 14 additions & 0 deletions
14
javascript/frameworks-libraries/react-redux-stack/src-6-rtk/index.css
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,14 @@ | ||
body { | ||
font-family: "Century Gothic", Futura, sans-serif; | ||
margin: 0; | ||
padding: 0; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
|
||
#root { | ||
flex-grow: 1; | ||
flex-shrink: 0; | ||
} |
14 changes: 14 additions & 0 deletions
14
javascript/frameworks-libraries/react-redux-stack/src-6-rtk/index.js
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,14 @@ | ||
import React from 'react'; | ||
import { createRoot } from 'react-dom/client'; | ||
// Components --------------------------- | ||
import App from './App'; | ||
// Styles ------------------------------- | ||
import 'normalize.css'; | ||
import './index.css'; | ||
// -------------------------------------- | ||
const root = createRoot(document.getElementById('root')); | ||
root.render( | ||
<React.StrictMode> | ||
<App /> | ||
</React.StrictMode> | ||
); |
Oops, something went wrong.