Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 

173 rindas
5.8 KiB

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