You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

227 lines
7.4 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. export const haveOrgPaymentRecord = () =>{
  88. if (localStorage.getItem('userData') != null){
  89. return JSON.parse(localStorage.getItem('userData')).orgPaymentRecord
  90. }
  91. }
  92. export const checkSysEnv = () =>{
  93. if (localStorage.getItem('sysEnv') != null){
  94. // console.log(localStorage.getItem('sysEnv'))
  95. return localStorage.getItem('sysEnv')
  96. }
  97. }
  98. /**
  99. ** This function is used for demo purpose route navigation
  100. ** In real app you won't need this function because your app will navigate to same route for each users regardless of ability
  101. ** Please note role field is just for showing purpose it's not used by anything in frontend
  102. ** We are checking role just for ease
  103. * ? NOTE: If you have different pages to navigate based on user ability then this function can be useful. However, you need to update it.
  104. * @param {String} userRole Role of user
  105. */
  106. export const getHomeRouteForLoggedInUser = userRole => {
  107. if (userRole === 'admin') return defaultHomePage
  108. if (userRole === 'user') return defaultHomePage
  109. if (userRole === 'client') return '/access-control'
  110. return '/login'
  111. }
  112. // ** React Select Theme Colors
  113. export const selectThemeColors = theme => ({
  114. ...theme,
  115. colors: {
  116. ...theme.colors,
  117. primary25: '#7367f01a', // for option hover bg-color
  118. primary: '#7367f0', // for selected option bg-color
  119. neutral10: '#008a9a', // for tags bg-color
  120. neutral20: '#ededed', // for input border-color
  121. neutral30: '#ededed' // for input hover border-color
  122. }
  123. })
  124. export const momentMinuteDiff = (date1, date2) => {
  125. return moment.duration(moment(new Date(date1)).diff(moment(new Date(date2)))).asMinutes()
  126. }
  127. export const minuteDiff = (nowDate, createdAtDate) => {
  128. let diff = (nowDate.getTime() - new Date(createdAtDate)) / 1000
  129. diff /= 60
  130. return Math.abs(Math.ceil(diff))
  131. }
  132. export const gazetteLength = (length,noOfPages) => {
  133. let countLength=0;
  134. if (noOfPages!==null){
  135. let pages = noOfPages
  136. countLength = pages*18
  137. }else{
  138. countLength = length
  139. }
  140. return countLength+" cm"
  141. }
  142. export const getUserId = () =>{
  143. if (localStorage.getItem('userData') != null){
  144. return JSON.parse(localStorage.getItem('userData')).id
  145. }
  146. }
  147. export const isPasswordExpiry = () =>{
  148. var date;
  149. if (localStorage.getItem('userData') != null){
  150. date = JSON.parse(localStorage.getItem('userData')).passwordExpiryDate
  151. }
  152. if (date != null){
  153. var currentDate = new Date();
  154. var expirationDate = DateUtils.convertToDate(date);
  155. if (expirationDate < currentDate) {
  156. // console.log(true)
  157. return true; // The date has expired
  158. } else {
  159. // console.log(false)
  160. return false; // The date is still valid
  161. }
  162. }else{
  163. return false;
  164. }
  165. }
  166. export const checkIsOnlyOnlinePaymentByIssueDate = (date) => {
  167. const targetDate = new Date(2026, 0, 28, 8, 0, 0) //2026-01-28T08:00:00.000Z
  168. // const targetDate = new Date(2025, 6, 13, 8, 0, 0); //2025-07-13T08:00:00.000Z
  169. const checkDate = DateUtils.convertToDate(date)
  170. if (isDummyLoggedIn()){
  171. return false;
  172. }
  173. // console.log(checkDate)
  174. // console.log(targetDate)
  175. if (checkDate >= targetDate) {
  176. return true
  177. } else {
  178. return false;
  179. }
  180. }
  181. export const checkIsOnlyOnlinePayment = () => {
  182. const targetDate = new Date(2026, 0, 28, 8, 0, 0)
  183. const checkDate = DateUtils.convertToDate(new Date())
  184. if (isDummyLoggedIn()){
  185. return false;
  186. }
  187. // console.log(checkDate)
  188. // console.log(targetDate)
  189. if (checkDate >= targetDate) {
  190. return true
  191. } else {
  192. return false;
  193. }
  194. }
  195. export const checkMarkAsCreditClient = () => {
  196. const targetDate = new Date(2026, 0, 28, 8, 0, 0)
  197. const checkDate = DateUtils.convertToDate(new Date())
  198. // console.log(targetDate)
  199. // console.log(checkDate >= targetDate)
  200. if (checkDate >= targetDate) {
  201. return true
  202. } else {
  203. return false;
  204. }
  205. }