dfghgfhplkjbv

src/utils.js

Mar 5th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. import { PROD_URL, DEV_URL } from 'src/constants'
  2.  
  3. const setTrueState = (thisisthis) => {
  4. thisisthis.setState({ isOnline: true })
  5. }
  6.  
  7. const setFalseState = (thisisthis) => {
  8. thisisthis.setState({ isOnline: false })
  9. }
  10.  
  11. export const isOnline = (thisisthis) => {
  12. if (typeof window !== 'undefined') {
  13. window.addEventListener('online', () => setTrueState(thisisthis))
  14.  
  15. window.addEventListener('offline', () => setFalseState(thisisthis))
  16. }
  17. }
  18.  
  19. export const removeListeners = () => {
  20. if (typeof window !== 'undefined') {
  21. window.removeEventListener('online', () => setTrueState())
  22. window.removeEventListener('offline', () => setFalseState())
  23. }
  24. }
  25.  
  26. export const getUrl = (templateKey, slug) => {
  27. switch (templateKey) {
  28. case 'Interview':
  29. return `interview/${slug}`
  30. case 'News':
  31. return `news/${slug}`
  32. case 'Podcast':
  33. return `podcast/${slug}`
  34. case 'Video':
  35. return `video/${slug}`
  36. default:
  37. break
  38. }
  39. }
  40.  
  41. export const getLink = (locale, path = '') => {
  42. return locale === 'ru' ? `/${path}` : `/en/${path}`
  43. }
  44.  
  45. export const handleSortByDuration = (arr) => {
  46. return arr.sort((a, b) => a.sec - b.sec)
  47. }
  48.  
  49. export const handleSortByDate = (arr) => {
  50. return [].concat(arr).sort((a, b) => (a.publishDate > b.publishDate ? 1 : b.publishDate > a.publishDate ? -1 : 0))
  51. }
  52.  
  53. export const handleSortByOldDate = (arr) => {
  54. return arr.sort((a, b) => a.node.dateToTime - b.node.dateToTime)
  55. }
  56.  
  57. export const handleSortByNewDate = (arr) => {
  58. return arr.sort((a, b) => b.node.dateToTime - a.node.dateToTime)
  59. }
  60.  
  61. export const transformDurationInSeconds = (arr) => {
  62. return arr.map((item) => {
  63. let secondsDuration = item.duration
  64. .split(':')
  65. .map((item, index) => (index === 0 ? item * 60 : +item))
  66. .reduce((a, b) => a + b, 0)
  67. return (item.sec = secondsDuration)
  68. })
  69. }
  70.  
  71. export const formateDate = (arr, publishdate) => {
  72. return arr.map((i) => {
  73. const tempArr = i.node[publishdate].split('/')
  74. let temp = tempArr[1]
  75. tempArr[1] = tempArr[0]
  76. tempArr[0] = temp
  77. const tempDate = tempArr.join('/')
  78. let dateToTime = Date.parse(tempDate)
  79. i.node.dateToTime = dateToTime
  80. return i
  81. })
  82. }
  83.  
  84. export const shuffle = (array) => {
  85. let currentIndex = array.length
  86. let temporaryValue
  87. let randomIndex = 0
  88. // While there remain elements to shuffle...
  89. while (0 !== currentIndex) {
  90. // Pick a remaining element...
  91. randomIndex = Math.floor(Math.random() * currentIndex)
  92. currentIndex -= 1
  93.  
  94. // And swap it with the current element.
  95. temporaryValue = array[currentIndex]
  96. array[currentIndex] = array[randomIndex]
  97. array[randomIndex] = temporaryValue
  98. }
  99. return array
  100. }
  101.  
  102. export const getBaseUrl = (locale) => {
  103. switch (locale) {
  104. case 'ru': {
  105. if (process.env.NODE_ENV === 'development') {
  106. return `${DEV_URL}/`
  107. }
  108. return `${PROD_URL}/`
  109. }
  110. case 'en':
  111. if (process.env.NODE_ENV === 'development') {
  112. return `${DEV_URL}/en/`
  113. }
  114. return `${PROD_URL}/en/`
  115. default:
  116. return PROD_URL
  117. }
  118. }
  119.  
  120. let counter = 0
  121.  
  122. export const getStyles = (array, length) => {
  123. if (counter >= length - 1) {
  124. counter = 0
  125. }
  126. counter++
  127. // return array[Math.floor(Math.random() * length)]
  128. return array[counter]
  129. }
  130.  
  131. export const colors = [
  132. { color: '#353533', bg: '#8AD3FB' },
  133. { color: '#353533', bg: '#E06C6C' },
  134. { color: '#353533', bg: '#4AAFB5' },
  135. { color: '#353533', bg: '#ECCB52' },
  136. { color: '#fff', bg: '#3F72AF' },
  137. { color: '#2D4059', bg: '#F07B3F' },
  138. { color: '#353533', bg: '#4AAFB5' },
  139. { color: '#00ADB5', bg: '#393E46' },
  140. ]
  141.  
  142. export const blobPositions = [
  143. { top: '-120px', left: '60%' },
  144. { top: '80px', left: '75%' },
  145. { top: '-100px', left: '80%' },
  146. { top: '-240px', left: '70%' },
  147. { top: '-150px', left: '50%' },
  148. ]
Advertisement
Add Comment
Please, Sign In to add comment