feat: Adds support for draft in conversation reply box (#4205)
* Add draft support * Fixes issue with draft loading * Adds draft for private notes * Use localstorage helper * .remove instead of .clear * Remove timestamp * clearLocalStorageOnLogout * Fix draft save on refresh * Remove usage of delete operator * Adds autosave for draft messages * Remove setinterval and add debounce * Removes draft redundancy check * Adds test cases for debouncer * Update app/javascript/shared/helpers/specs/TimeHelpers.spec.js Co-authored-by: Nithin David Thomas <1277421+nithindavid@users.noreply.github.com> * Update app/javascript/shared/helpers/specs/TimeHelpers.spec.js Co-authored-by: Nithin David Thomas <1277421+nithindavid@users.noreply.github.com> * Review fixes * Fixes issue with debouncer * FIxes debouncer issue * Fixes issue with draft empty message * Removes empty keys from local storage drafts * Fixes error with empty draft Co-authored-by: Pranav Raj S <pranav@chatwoot.com> Co-authored-by: Fayaz Ahmed <15716057+fayazara@users.noreply.github.com> Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> Co-authored-by: iamsivin <iamsivin@gmail.com>
This commit is contained in:
committed by
GitHub
parent
dfb56f6bb8
commit
5ea0436051
21
app/javascript/shared/helpers/TimeHelpers.js
Normal file
21
app/javascript/shared/helpers/TimeHelpers.js
Normal file
@@ -0,0 +1,21 @@
|
||||
// Returns a function, that, as long as it continues to be invoked, will not
|
||||
// be triggered. The function will be called after it stops being called for
|
||||
// N milliseconds. If `immediate` is passed, trigger the function on the
|
||||
// leading edge, instead of the trailing.
|
||||
|
||||
export const debounce = (func, delay, immediate) => {
|
||||
let timerId;
|
||||
return (...args) => {
|
||||
const boundFunc = func.bind(this, ...args);
|
||||
clearTimeout(timerId);
|
||||
if (immediate && !timerId) {
|
||||
boundFunc();
|
||||
}
|
||||
const calleeFunc = immediate
|
||||
? () => {
|
||||
timerId = null;
|
||||
}
|
||||
: boundFunc;
|
||||
timerId = setTimeout(calleeFunc, delay);
|
||||
};
|
||||
};
|
||||
32
app/javascript/shared/helpers/specs/TimeHelpers.spec.js
Normal file
32
app/javascript/shared/helpers/specs/TimeHelpers.spec.js
Normal file
@@ -0,0 +1,32 @@
|
||||
import { debounce } from '../TimeHelpers';
|
||||
// Tell Jest to mock all timeout functions
|
||||
jest.useFakeTimers('modern');
|
||||
|
||||
describe('debounce', () => {
|
||||
test('execute just once with immediate false', () => {
|
||||
let func = jest.fn();
|
||||
let debouncedFunc = debounce(func, 1000);
|
||||
|
||||
for (let i = 0; i < 100; i += 1) {
|
||||
debouncedFunc();
|
||||
}
|
||||
|
||||
// Fast-forward time
|
||||
jest.runAllTimers();
|
||||
|
||||
expect(func).toBeCalledTimes(1);
|
||||
});
|
||||
|
||||
test('execute just once with immediate true', () => {
|
||||
let func = jest.fn();
|
||||
let debouncedFunc = debounce(func, 1000, true);
|
||||
|
||||
for (let i = 0; i < 100; i += 1) {
|
||||
debouncedFunc();
|
||||
}
|
||||
|
||||
jest.runAllTimers();
|
||||
|
||||
expect(func).toBeCalledTimes(1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user