Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6694 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6493 stevensc 1
import React from 'react'
2
 
3
const jsonToParams = (data = {}) => {
4
  if (!data) {
5
    return false
6
  } else {
7
    const formBody = []
8
    for (const property in data) {
9
      const encodedKey = encodeURIComponent(property)
10
      // @ts-ignore
11
      const encodedValue = encodeURIComponent(data[property])
12
      formBody.push(encodedKey + '=' + encodedValue)
13
    }
14
    const _formBody = formBody.join('&')
15
    return _formBody
16
  }
17
}
18
 
19
const useWindowSize = () => {
20
  const [size, setSize] = React.useState([0, 0])
21
  React.useLayoutEffect(() => {
22
    function updateSize() {
23
      setSize([window.innerWidth, window.innerHeight])
24
    }
25
    window.addEventListener('resize', updateSize)
26
    updateSize()
27
    return () => window.removeEventListener('resize', updateSize)
28
  }, [])
29
  return size
30
}
31
 
32
const filterItems = (query = '', items = []) => {
33
  if (!query) {
34
    return items
35
  }
36
 
37
  return items.filter((conversation) =>
38
    conversation.name.toLowerCase().includes(query.toLowerCase())
39
  )
40
}
41
 
42
const debounce = (func, timeout = 300) => {
43
  let timer
44
  return (...args) => {
45
    clearTimeout(timer)
46
    timer = setTimeout(() => {
47
      func.apply(this, args)
48
    }, timeout)
49
  }
50
}
51
 
52
const scrollToBottom = (element) => {
53
  if (!element.current) {
54
    return false
55
  }
56
 
57
  element.scrollTop = element.scrollHeight * 9
58
}
59
 
60
export { useWindowSize, jsonToParams, debounce, scrollToBottom, filterItems }