This file contains hidden or 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
const simulateNativeLink = (rowData, e) => { | |
if (e.ctrlKey || e.metaKey || e.which === 2 || e.button === 4) { | |
const link = document.createElement('a'); | |
link.target = '_blank'; | |
link.rel = 'noopener noreferrer'; | |
link.href = `/my-orders/${rowData.id}`; | |
link.click(); | |
} else { | |
history.push(`/my-orders/${rowData.id}`); | |
} |
This file contains hidden or 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
/** | |
* Source: https://medium.com/@rajeshnaroth/using-throttle-and-debounce-in-a-react-function-component-5489fc3461b3 | |
*/ | |
const Search = () => { | |
const [userQuery, setUserQuery] = useState(""); | |
const delayedQuery = useCallback(_.debounce(q => sendQuery(q), 500), []); | |
// alternative | |
// const delayedQuery = useRef(_.debounce(q => sendQuery(q), 500)).current; | |
const onChange = e => { | |
setUserQuery(e.target.value); |
This file contains hidden or 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
/** | |
* Source: https://gomakethings.com/how-to-test-if-an-element-is-in-the-viewport-with-vanilla-javascript/ | |
* ! IS BUGGED: In some cases window.innerHeight is bigger than bounding.bottom | |
* even if element is fully visible. | |
* TODO: Needs to be improved. | |
*/ | |
const isInViewport = (elem) => { | |
const bounding = elem.getBoundingClientRect(); | |
return ( | |
bounding.top >= 0 && |
This file contains hidden or 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
class App extends Component { | |
constructor() { | |
super() | |
this.handleButtonPress = this.handleButtonPress.bind(this) | |
this.handleButtonRelease = this.handleButtonRelease.bind(this) | |
} | |
handleButtonPress () { | |
this.buttonPressTimer = setTimeout(() => alert('long press activated'), 1500); | |
} |
This file contains hidden or 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
/** | |
* Decode JWT token | |
* | |
* @param {string} token | |
* @return {object} | |
*/ | |
const parseJwt = (token) => { | |
const base64Url = token.split('.')[1]; | |
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/'); | |
const jsonPayload = decodeURIComponent( |
This file contains hidden or 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
const vw = Math.max(document.documentElement.clientWidth, window.innerWidth || 0); | |
const vh = Math.max(document.documentElement.clientHeight, window.innerHeight || 0); |
This file contains hidden or 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
<!-- Display the countdown timer in an element --> | |
<!--Source: https://www.w3schools.com/howto/howto_js_countdown.asp --> | |
<p id="demo"></p> | |
<script> | |
// Set the date we're counting down to | |
var countDownDate = new Date("Jan 5, 2021 15:37:25").getTime(); | |
// Update the count down every 1 second | |
var x = setInterval(function() { |
This file contains hidden or 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
/** | |
* We'll load jQuery and the Bootstrap jQuery plugin which provides support | |
* for JavaScript based Bootstrap features such as modals and tabs. This | |
* code may be modified to fit the specific needs of your application. | |
*/ | |
try { | |
window.Popper = require('popper.js').default; | |
window.$ = window.jQuery = require('jquery'); | |
require('bootstrap'); |
This file contains hidden or 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
const toBase64 = file => new Promise((resolve, reject) => { | |
const reader = new FileReader(); | |
reader.readAsDataURL(file); | |
reader.onload = () => resolve(reader.result); | |
reader.onerror = error => reject(error); | |
}); | |
// toBase64(file).then(file => console.log(file)); |
This file contains hidden or 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
// Fill array with value, len times | |
// Using concat | |
fillArray = (value, len) => { | |
if (len === 0) return []; | |
let a = [value]; | |
while (a.length * 2 <= len) a = a.concat(a); | |
if (a.length < len) a = a.concat(a.slice(0, len - a.length)); | |
return a; | |
}; |