Skip to content
This repository has been archived by the owner on Apr 30, 2023. It is now read-only.

Commit

Permalink
Add post-launch tweaks (#10)
Browse files Browse the repository at this point in the history
Add post-launch tweaks
  • Loading branch information
markerikson authored Mar 9, 2020
2 parents 1da4c6f + 08fc6ee commit e500809
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 14 deletions.
2 changes: 1 addition & 1 deletion template/src/App.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { render } from '@testing-library/react';
import { Provider } from 'react-redux';
import store from './store';
import store from './app/store';
import App from './App';

test('renders learn react link', () => {
Expand Down
2 changes: 1 addition & 1 deletion template/src/store.js → template/src/app/store.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { configureStore } from '@reduxjs/toolkit';
import counterReducer from './features/counter/counterSlice';
import counterReducer from '../features/counter/counterSlice';

export default configureStore({
reducer: {
Expand Down
13 changes: 9 additions & 4 deletions template/src/features/counter/Counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import {
decrement,
increment,
incrementByAmount,
incrementAsync,
selectCount,
} from './counterSlice';
import styles from './Counter.module.css';

export function Counter() {
const count = useSelector(selectCount);
const dispatch = useDispatch();
const [incrementAmount, setIncrementAmount] = useState(2);
const [incrementAmount, setIncrementAmount] = useState('2');

return (
<div>
Expand Down Expand Up @@ -42,13 +43,17 @@ export function Counter() {
<button
className={styles.button}
onClick={() =>
dispatch(
incrementByAmount({ amount: Number(incrementAmount) || 0 })
)
dispatch(incrementByAmount(Number(incrementAmount) || 0))
}
>
Add Amount
</button>
<button
className={styles.asyncButton}
onClick={() => dispatch(incrementAsync(Number(incrementAmount) || 0))}
>
Add Async
</button>
</div>
</div>
);
Expand Down
27 changes: 26 additions & 1 deletion template/src/features/counter/Counter.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
justify-content: center;
}

.row:first-child {
.row:not(:last-child) {
margin-bottom: 16px;
}

Expand Down Expand Up @@ -48,3 +48,28 @@
.button:active {
background-color: rgba(112, 76, 182, 0.2);
}

.asyncButton {
composes: button;
position: relative;
margin-left: 8px;
}

.asyncButton:after {
content: "";
background-color: rgba(112, 76, 182, 0.15);
display: block;
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
opacity: 0;
transition: width 1s linear, opacity 0.5s ease 1s;
}

.asyncButton:active:after {
width: 0%;
opacity: 1;
transition: 0s
}
26 changes: 20 additions & 6 deletions template/src/features/counter/counterSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,36 @@ export const slice = createSlice({
},
reducers: {
increment: state => {
// Redux Toolkit allows us to 'mutate' the state. It doesn't actually
// mutate the state because it uses the immer library, which detects
// changes to a "draft state" and produces a brand new immutable state
// based off those changes
// Redux Toolkit allows us to write "mutating" logic in reducers. It
// doesn't actually mutate the state because it uses the immer library,
// which detects changes to a "draft state" and produces a brand new
// immutable state based off those changes
state.value += 1;
},
decrement: state => {
state.value -= 1;
},
incrementByAmount: (state, action) => {
state.value += action.payload.amount;
state.value += action.payload;
},
},
});

export const selectCount = state => state.counter.value;
export const { increment, decrement, incrementByAmount } = slice.actions;

// The function below is called a thunk and allows us to perform async logic. It
// can be dispatched like a regular action: `dispatch(incrementAsync(10))`. This
// will call the thunk with the `dispatch` function as the first argument. Async
// code can then be executed and other actions can be dispatched
export const incrementAsync = amount => dispatch => {
setTimeout(() => {
dispatch(incrementByAmount(amount));
}, 1000);
};

// The function below is called a selector and allows us to select a value from
// the state. Selectors can also be defined inline where they're used instead of
// in the slice file. For example: `useSelector((state) => state.counter.value)`
export const selectCount = state => state.counter.value;

export default slice.reducer;
2 changes: 1 addition & 1 deletion template/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import store from './store';
import store from './app/store';
import { Provider } from 'react-redux';
import * as serviceWorker from './serviceWorker';

Expand Down

0 comments on commit e500809

Please sign in to comment.