25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

145 satır
5.0 KiB

  1. export const defaultHomePage = '/dashboard'
  2. // ** Checks if an object is empty (returns boolean)
  3. export const isObjEmpty = obj => {
  4. if (obj === null || obj === undefined) {
  5. return true
  6. }
  7. return Object.keys(obj).length === 0
  8. }
  9. // ** Returns K format from a number
  10. export const kFormatter = num => (num > 999 ? `${(num / 1000).toFixed(1)}k` : num)
  11. // ** Converts HTML to string
  12. export const htmlToString = html => html.replace(/<\/?[^>]+(>|$)/g, '')
  13. // ** Checks if the passed date is today
  14. const isToday = date => {
  15. const today = new Date()
  16. return (
  17. /* eslint-disable operator-linebreak */
  18. date.getDate() === today.getDate() &&
  19. date.getMonth() === today.getMonth() &&
  20. date.getFullYear() === today.getFullYear()
  21. /* eslint-enable */
  22. )
  23. }
  24. /**
  25. ** Format and return date in Humanize format
  26. ** Intl docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/format
  27. ** Intl Constructor: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat
  28. * @param {String} value date to format
  29. * @param {Object} formatting Intl object to format with
  30. */
  31. export const formatDate = (value, formatting = {month: 'short', day: 'numeric', year: 'numeric'}) => {
  32. if (!value) return value
  33. return new Intl.DateTimeFormat('en-US', formatting).format(new Date(value))
  34. }
  35. // ** Returns short month of passed date
  36. export const formatDateToMonthShort = (value, toTimeForCurrentDay = true) => {
  37. const date = new Date(value)
  38. let formatting = {month: 'short', day: 'numeric'}
  39. if (toTimeForCurrentDay && isToday(date)) {
  40. formatting = {hour: 'numeric', minute: 'numeric'}
  41. }
  42. return new Intl.DateTimeFormat('en-US', formatting).format(new Date(value))
  43. }
  44. /**
  45. ** Return if user is logged in
  46. ** This is completely up to you and how you want to store the token in your frontend application
  47. * ? e.g. If you are using cookies to store the application please update this function
  48. */
  49. export const isUserLoggedIn = () => localStorage.getItem('userData') != null
  50. export const getUserData = () => JSON.parse(localStorage.getItem('userData'))
  51. export const isAdminLoggedIn = () =>{
  52. if (localStorage.getItem('userData') != null){
  53. return JSON.parse(localStorage.getItem('userData')).role === 'admin'
  54. }
  55. }
  56. export const isGLDLoggedIn = () =>{
  57. if (localStorage.getItem('userData') != null){
  58. return JSON.parse(localStorage.getItem('userData')).type === 'GLD'
  59. }
  60. }
  61. export const isINDLoggedIn = () =>{
  62. if (localStorage.getItem('userData') != null){
  63. return JSON.parse(localStorage.getItem('userData')).type === 'IND'
  64. }
  65. }
  66. export const isORGLoggedIn = () =>{
  67. if (localStorage.getItem('userData') != null){
  68. return JSON.parse(localStorage.getItem('userData')).type === 'ORG'
  69. }
  70. }
  71. export const isPrimaryLoggedIn = () =>{
  72. if (localStorage.getItem('userData') != null){
  73. return JSON.parse(localStorage.getItem('userData')).role === 'primary'
  74. }
  75. }
  76. export const isCreditorLoggedIn = () =>{
  77. if (localStorage.getItem('userData') != null){
  78. return JSON.parse(localStorage.getItem('userData')).creditor
  79. }
  80. }
  81. /**
  82. ** This function is used for demo purpose route navigation
  83. ** In real app you won't need this function because your app will navigate to same route for each users regardless of ability
  84. ** Please note role field is just for showing purpose it's not used by anything in frontend
  85. ** We are checking role just for ease
  86. * ? NOTE: If you have different pages to navigate based on user ability then this function can be useful. However, you need to update it.
  87. * @param {String} userRole Role of user
  88. */
  89. export const getHomeRouteForLoggedInUser = userRole => {
  90. if (userRole === 'admin') return defaultHomePage
  91. if (userRole === 'user') return defaultHomePage
  92. if (userRole === 'client') return '/access-control'
  93. return '/login'
  94. }
  95. // ** React Select Theme Colors
  96. export const selectThemeColors = theme => ({
  97. ...theme,
  98. colors: {
  99. ...theme.colors,
  100. primary25: '#7367f01a', // for option hover bg-color
  101. primary: '#7367f0', // for selected option bg-color
  102. neutral10: '#008a9a', // for tags bg-color
  103. neutral20: '#ededed', // for input border-color
  104. neutral30: '#ededed' // for input hover border-color
  105. }
  106. })
  107. export const momentMinuteDiff = (date1, date2) => {
  108. return moment.duration(moment(new Date(date1)).diff(moment(new Date(date2)))).asMinutes()
  109. }
  110. export const minuteDiff = (nowDate, createdAtDate) => {
  111. let diff = (nowDate.getTime() - new Date(createdAtDate)) / 1000
  112. diff /= 60
  113. return Math.abs(Math.ceil(diff))
  114. }
  115. export const gazetteLength = (length,noOfPages) => {
  116. let countLength=0;
  117. if (noOfPages!==null){
  118. let pages = noOfPages
  119. countLength = pages*18
  120. }else{
  121. countLength = length
  122. }
  123. return countLength+" cm"
  124. }
  125. export const getUserId = () =>{
  126. if (localStorage.getItem('userData') != null){
  127. return JSON.parse(localStorage.getItem('userData')).id
  128. }
  129. }