|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState } from "react"; |
| 4 | + |
| 5 | +import { |
| 6 | + decrement, |
| 7 | + increment, |
| 8 | + incrementAsync, |
| 9 | + incrementByAmount, |
| 10 | + incrementIfOdd, |
| 11 | + selectCount, |
| 12 | + selectStatus, |
| 13 | +} from "@/lib/features/counter/counterSlice"; |
| 14 | + |
| 15 | +import { useAppDispatch, useAppSelector } from "@/lib/hooks"; |
| 16 | +import styles from "./Counter.module.css"; |
| 17 | + |
| 18 | +export const Counter = () => { |
| 19 | + const dispatch = useAppDispatch(); |
| 20 | + const count = useAppSelector(selectCount); |
| 21 | + const status = useAppSelector(selectStatus); |
| 22 | + const [incrementAmount, setIncrementAmount] = useState("2"); |
| 23 | + |
| 24 | + const incrementValue = Number(incrementAmount) || 0; |
| 25 | + |
| 26 | + return ( |
| 27 | + <div> |
| 28 | + <div className={styles.row}> |
| 29 | + <button |
| 30 | + className={styles.button} |
| 31 | + aria-label="Decrement value" |
| 32 | + onClick={() => dispatch(decrement())} |
| 33 | + > |
| 34 | + - |
| 35 | + </button> |
| 36 | + <span aria-label="Count" className={styles.value}> |
| 37 | + {count} |
| 38 | + </span> |
| 39 | + <button |
| 40 | + className={styles.button} |
| 41 | + aria-label="Increment value" |
| 42 | + onClick={() => dispatch(increment())} |
| 43 | + > |
| 44 | + + |
| 45 | + </button> |
| 46 | + </div> |
| 47 | + <div className={styles.row}> |
| 48 | + <input |
| 49 | + className={styles.textbox} |
| 50 | + aria-label="Set increment amount" |
| 51 | + value={incrementAmount} |
| 52 | + type="number" |
| 53 | + onChange={(e) => { |
| 54 | + setIncrementAmount(e.target.value); |
| 55 | + }} |
| 56 | + /> |
| 57 | + <button |
| 58 | + className={styles.button} |
| 59 | + onClick={() => dispatch(incrementByAmount(incrementValue))} |
| 60 | + > |
| 61 | + Add Amount |
| 62 | + </button> |
| 63 | + <button |
| 64 | + className={styles.asyncButton} |
| 65 | + disabled={status !== "idle"} |
| 66 | + onClick={() => dispatch(incrementAsync(incrementValue))} |
| 67 | + > |
| 68 | + Add Async |
| 69 | + </button> |
| 70 | + <button |
| 71 | + className={styles.button} |
| 72 | + onClick={() => { |
| 73 | + dispatch(incrementIfOdd(incrementValue)); |
| 74 | + }} |
| 75 | + > |
| 76 | + Add If Odd |
| 77 | + </button> |
| 78 | + </div> |
| 79 | + </div> |
| 80 | + ); |
| 81 | +}; |
0 commit comments