Skip to content

Commit 60c40a6

Browse files
authored
Merge pull request #6 from maxchistt/debounce
Debounce & hooks
2 parents 61d7af8 + d6d4e81 commit 60c40a6

File tree

7 files changed

+55
-45
lines changed

7 files changed

+55
-45
lines changed

client/src/App.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import ModalCardEdit from './Cards/ModalCardEdit'
99
import ModalLogin from './Login/ModalLogin'
1010
import DataService from './Services/DataService'
1111
import Card, { checkCardsArr } from './Cards/cardType/Card'
12+
import useDebouncedEffect from './Shared/useDebouncedEffect.hook'
1213

1314
const { loadData, postData, updDataServLogin } = DataService()
1415

@@ -58,7 +59,7 @@ function App() {
5859
const [updaterVal] = useUpdater()
5960

6061
React.useEffect(loadDataFromServer, [logged, userName, updaterVal]) // eslint-disable-line react-hooks/exhaustive-deps
61-
React.useEffect(loadDataToServer, [cardsArr]) // eslint-disable-line react-hooks/exhaustive-deps
62+
useDebouncedEffect(loadDataToServer, [cardsArr], 1200) // eslint-disable-line react-hooks/exhaustive-deps
6263
React.useEffect(clearOldData, [logged]) // eslint-disable-line react-hooks/exhaustive-deps
6364

6465
///////////

client/src/Cards/AddCard.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
1-
import React, { useState } from 'react'
1+
import React from 'react'
22
import PropTypes from 'prop-types'
33
import TextareaAutosize from 'react-textarea-autosize'
44
import Palette, { colors } from './palette/palette'
5-
6-
function useInputValue(defaultValue) {
7-
const [value, setValue] = useState(defaultValue)
8-
return {
9-
bind: {
10-
value,
11-
onChange: event => setValue(event.target.value)
12-
},
13-
clear: () => setValue(defaultValue),
14-
value: () => value,
15-
addBreak: () => setValue(value + "\n")
16-
}
17-
}
5+
import useInputValue from '../Shared/useInputValue.hook'
186

197
function AddCard({ onCreate, onDeleteAll }) {
208
const input = useInputValue('')
@@ -36,8 +24,8 @@ function AddCard({ onCreate, onDeleteAll }) {
3624
}
3725

3826
function submitHandler() {
39-
if (String(input.value()).trim() && String(color).trim()) {
40-
onCreate({ name: String(input.value()).trim(), text: "", color: String(color) })
27+
if (String(input.value).trim() && String(color).trim()) {
28+
onCreate({ name: String(input.value).trim(), text: "", color: String(color) })
4129
input.clear()
4230
setColor(defColor)
4331
}
@@ -71,7 +59,7 @@ function AddCard({ onCreate, onDeleteAll }) {
7159
</div>
7260

7361
<div className="col-xl-1 col-lg-1 col-md-1 col p-1">
74-
<button disabled={!input.value().trim()} className="btn btn-success btn-block" onClick={submitHandler}>
62+
<button disabled={!input.value.trim()} className="btn btn-success btn-block" onClick={submitHandler}>
7563
<i className="bi bi-clipboard-plus"></i>
7664
</button>
7765
</div>

client/src/Cards/ModalCardEdit.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import PropTypes from 'prop-types'
33
import Context from '../context'
44
import TextareaAutosize from 'react-textarea-autosize'
55
import Modal, { ModalProps } from "../Shared/Modal/Modal"
6-
import debounce from '../Shared/debounce'
76
import Card, { PropTypeCard } from './cardType/Card'
87
import Palette from './palette/palette'
98

@@ -32,7 +31,6 @@ function ModalCardEdit({ card = new Card(), index }) {
3231
modalProps.sideClose = true
3332
modalProps.onSideClick = unsetEditCard
3433

35-
3634
function open() {
3735
setShowForm(true)
3836
}
@@ -47,7 +45,7 @@ function ModalCardEdit({ card = new Card(), index }) {
4745
editCardContent(index, name, text)
4846
}
4947

50-
function onInputCange(e) {
48+
function onInputChange(e) {
5149
let name = card.name
5250
let text = card.text
5351
if (e.target.id === "modal-edit-name") name = e.target.value
@@ -82,7 +80,7 @@ function ModalCardEdit({ card = new Card(), index }) {
8280
maxRows={3}
8381
maxLength="100"
8482
value={card.name}
85-
onChange={debounce(onInputCange, 700)}
83+
onChange={onInputChange}
8684
/>
8785

8886
<p style={{ fontWeight: "500" }} className="mb-2 text-dark">
@@ -99,7 +97,7 @@ function ModalCardEdit({ card = new Card(), index }) {
9997
minRows={3}
10098
maxRows={calcMaxRows()}
10199
value={card.text}
102-
onChange={debounce(onInputCange, 1000)}
100+
onChange={onInputChange}
103101
/>
104102
</React.Fragment>
105103
) : (

client/src/Login/ModalLogin.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from "react"
22
import Modal, { ModalProps } from "../Shared/Modal/Modal"
33
import PropTypes from 'prop-types'
4+
import useInputValue from '../Shared/useInputValue.hook'
45
import LoginService from '../Services/LoginService'
56
const { logIn, logOut } = LoginService()
67

@@ -12,19 +13,6 @@ function validateUsername(str) {
1213
return String(str).replace(/@|;|:|\.|,|\/|\\|\||\$|\?|!|#|%|\*|\^|\+|=|\[|\]| |\\ |«|<|>/gi, "").trim()
1314
}
1415

15-
function useInputValue(defaultValue) {
16-
const [value, setValue] = React.useState(defaultValue)
17-
18-
return {
19-
bind: {
20-
value: value,
21-
onChange: event => setValue(event.target.value)
22-
},
23-
clear: () => setValue(""),
24-
value: value
25-
}
26-
}
27-
2816
function ModalLogin(props) {
2917
const { onLogin, onLogout, logged, userName, isOpen, setOpenState } = props
3018

client/src/Shared/debounce.js

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { useState, useEffect } from 'react';
2+
3+
function useDebounce(value, delay) {
4+
5+
const [debouncedValue, setDebouncedValue] = useState(value);
6+
7+
useEffect(
8+
() => {
9+
10+
const handler = setTimeout(() => {
11+
setDebouncedValue(value);
12+
}, delay);
13+
14+
15+
return () => {
16+
clearTimeout(handler);
17+
};
18+
},
19+
20+
[delay, value]
21+
);
22+
23+
return debouncedValue;
24+
}
25+
26+
export default function useDebouncedEffect(func, deps, delay) {
27+
const debounced = useDebounce(deps, delay || 0)
28+
const debDepsList = Array.isArray(debounced) ? debounced : debounced ? [debounced] : undefined
29+
useEffect(func, debDepsList) // eslint-disable-line react-hooks/exhaustive-deps
30+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { useState } from 'react'
2+
3+
export default function useInputValue(defaultValue) {
4+
const [value, setValue] = useState(defaultValue)
5+
return {
6+
bind: {
7+
value,
8+
onChange: event => setValue(event.target.value)
9+
},
10+
clear: () => setValue(defaultValue),
11+
value: value,
12+
addBreak: () => setValue(value + "\n")
13+
}
14+
}

0 commit comments

Comments
 (0)