functions.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { useState } from "react";
  2. import { screenWidth } from "./metrics";
  3. export const PAGINATE_BY = 5
  4. /*
  5. Executes before Render
  6. */
  7. export const useConstructor = (callBack = () => { }) => {
  8. const [hasBeenCalled, setHasBeenCalled] = useState(false);
  9. if (hasBeenCalled) return;
  10. callBack();
  11. setHasBeenCalled(true);
  12. }
  13. const _MS_PER_SECOND = 1000;
  14. const _MS_PER_MINUTE = _MS_PER_SECOND * 60;
  15. const _MS_PER_HOUR = _MS_PER_MINUTE * 60;
  16. const _MS_PER_DAY = _MS_PER_HOUR * 24;
  17. const _MS_PER_MONTH = _MS_PER_DAY * 31;
  18. const _MS_PER_YEAR = _MS_PER_MONTH * 12;
  19. export const capitalize = (str) => str.charAt(0).toUpperCase() + str.substring(1, str.length).toLowerCase()
  20. export const getDifferenceInDates = (startDate, endDate) => {
  21. var diff = Math.floor(startDate.getTime() - endDate.getTime());
  22. return {
  23. "year": Math.floor(diff / _MS_PER_YEAR),
  24. "month": Math.floor(diff / _MS_PER_MONTH),
  25. "day": Math.floor(diff / _MS_PER_DAY),
  26. "hour": Math.floor(diff / _MS_PER_HOUR),
  27. "minute": Math.floor(diff / _MS_PER_MINUTE),
  28. "second": Math.floor(diff / _MS_PER_SECOND),
  29. }
  30. }
  31. export const getTimestamp = (iso_date_str) => {
  32. let diffObj = getDifferenceInDates(new Date(), new Date(iso_date_str))
  33. var result = "Timestamp Not Found"
  34. for (const key of Object.keys(diffObj)) {
  35. if (diffObj[key] > 0) {
  36. result = `${diffObj[key]} ${capitalize(key)}${ diffObj[key] > 1 ? "s" : ""} ago`
  37. break
  38. }
  39. }
  40. return result
  41. }
  42. const _getMonth = (index) => {
  43. const months = {
  44. 0: "January",
  45. 1: "Febraury",
  46. 2: "March",
  47. 3: "April",
  48. 4: "May",
  49. 5: "June",
  50. 6: "July",
  51. 7: "August",
  52. 8: "September",
  53. 9: "October",
  54. 10: "November",
  55. 11: "December"
  56. }
  57. if (Object.hasOwn(months, index)) {
  58. return months[index]
  59. } else {
  60. return "Month Not Found"
  61. }
  62. }
  63. export const getDate = (iso_date_str) => {
  64. let date = new Date(iso_date_str)
  65. return `${date.getDate()} ${_getMonth(date.getMonth())} ${date.getFullYear()}`
  66. }
  67. const MOBILE_SCREEN_BREAKPOINT = 480;
  68. const TABLET_SCREEN_BREAKPOINT = 768;
  69. const LARGE_SCREEN_BREAKPOINT = 1180;
  70. export const MOBILE_SCREEN = "Mobile"
  71. export const TABLET_SCREEN = "Tablet"
  72. export const DESKTOP_SCREEN = "Desktop"
  73. export const getScreenType = () => {
  74. let screenType;
  75. if (screenWidth <= MOBILE_SCREEN_BREAKPOINT) {
  76. screenType = MOBILE_SCREEN
  77. }else if(screenWidth <= TABLET_SCREEN_BREAKPOINT){
  78. screenType = TABLET_SCREEN
  79. }else {
  80. screenType = DESKTOP_SCREEN
  81. }
  82. return screenType
  83. }
  84. export const validateEmail = (email) => {
  85. const emailRegex = "^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*$"
  86. return String(email).match(emailRegex)
  87. }
  88. export const navigateToArticle = (navigation,article_id,article_slug) => {
  89. navigation.push("NewsDetailPage",{slug: article_slug, id: article_id})
  90. navigation.navigate("NewsDetailPage",{slug: article_slug, id: article_id})
  91. }
  92. export const navigateToListViewPage = (navigation,topic_type,topic) => {
  93. navigation.navigate("MainNav",{ screen : "NewsListPage", params :{type: topic_type, title: topic }})
  94. }