diff --git a/searchlib/components/AppInfo/AppInfo.jsx b/searchlib/components/AppInfo/AppInfo.jsx index 9b21caa58..7c5181b04 100644 --- a/searchlib/components/AppInfo/AppInfo.jsx +++ b/searchlib/components/AppInfo/AppInfo.jsx @@ -1,12 +1,25 @@ import React from 'react'; import getInfo from '@eeacms/search/lib/getIndexInfo'; import { useAtom } from 'jotai'; -import { DateTime } from 'luxon'; +// import { DateTime } from 'luxon'; import { indexMetadataAtom, hasRequestAtom } from './state'; +const formatDate = (date) => { + return date.toLocaleString('en-US', { + weekday: 'long', + year: 'numeric', + month: 'long', + day: 'numeric', + hour: '2-digit', + minute: '2-digit', + second: '2-digit', + timeZoneName: 'short', + }); +}; + function AppInfo({ appConfig, ...rest }) { const { app_name, app_version } = appConfig; - const hostname = window.runtimeConfig?.HOSTNAME || 'localhost'; + // const hostname = window.runtimeConfig?.HOSTNAME || 'localhost'; const [indexMetadata, setIndexMetadata] = useAtom(indexMetadataAtom); const [hasRequest, setHasRequest] = useAtom(hasRequestAtom); @@ -19,7 +32,7 @@ function AppInfo({ appConfig, ...rest }) { } if (!indexMetadata) { getInfo(appConfig).then((response) => { - setIndexMetadata(response.toLocaleString(DateTime.DATETIME_FULL) || ''); + setIndexMetadata(response ? formatDate(response) : ''); }); } }, [appConfig, indexMetadata, setIndexMetadata, hasRequest, setHasRequest]); diff --git a/searchlib/components/DateTime/DateTime.jsx b/searchlib/components/DateTime/DateTime.jsx index da771964e..34876af07 100644 --- a/searchlib/components/DateTime/DateTime.jsx +++ b/searchlib/components/DateTime/DateTime.jsx @@ -1,20 +1,43 @@ -import { DateTime } from 'luxon'; +// import { DateTime } from 'luxon'; +// +// const FormatDateTime = (props) => { +// const { value, format = 'DATE_SHORT' } = props; +// +// if (value === null){ +// return null; +// } +// const dt = value +// ? value.isLuxonDateTime +// ? value +// : DateTime.fromISO(value) +// : DateTime.local(); +// +// return format === 'DATE_MED' +// ? dt.toFormat('d MMM yyyy') +// : dt.toLocaleString(DateTime[format]); +// }; +// +// export default FormatDateTime; const FormatDateTime = (props) => { const { value, format = 'DATE_SHORT' } = props; - if (value === null){ + if (value === null) { return null; } - const dt = value - ? value.isLuxonDateTime - ? value - : DateTime.fromISO(value) - : DateTime.local(); - return format === 'DATE_MED' - ? dt.toFormat('d MMM yyyy') - : dt.toLocaleString(DateTime[format]); + const dt = value instanceof Date ? value : new Date(value); + + const formatOptions = { + DATE_SHORT: { year: 'numeric', month: 'short', day: 'numeric' }, + DATE_MED: { day: 'numeric', month: 'short', year: 'numeric' }, + }; + + if (format === 'DATE_MED') { + return dt.toLocaleDateString('en-US', formatOptions.DATE_MED); + } else { + return dt.toLocaleDateString('en-US', formatOptions.DATE_SHORT); + } }; export default FormatDateTime; diff --git a/searchlib/lib/getIndexInfo.js b/searchlib/lib/getIndexInfo.js index a86a7a90b..c9166b955 100644 --- a/searchlib/lib/getIndexInfo.js +++ b/searchlib/lib/getIndexInfo.js @@ -1,5 +1,5 @@ import superagent from 'superagent'; -import { DateTime } from 'luxon'; +// import { DateTime } from 'luxon'; function trim_slash(text) { return text.replace(/^\/+|\/+$/g, ''); @@ -51,10 +51,12 @@ export default async function getInfo(appConfig) { update_ts = aliases[0].substring(11); } - const dt = DateTime.fromMillis(parseInt(update_ts)); + // const dt = DateTime.fromMillis(parseInt(update_ts)); + const dt = new Date(parseInt(update_ts)); return dt; } catch { - console.log('info', info); + // eslint-disable-next-line no-console + console.warn('Error in parsing index settings', info); return ''; } } diff --git a/searchlib/lib/models.js b/searchlib/lib/models.js index c505b13ee..f65606730 100644 --- a/searchlib/lib/models.js +++ b/searchlib/lib/models.js @@ -1,5 +1,5 @@ import registry from '@eeacms/search/registry'; -import { DateTime } from 'luxon'; +// import { DateTime } from 'luxon'; function getHighlight(hit, fieldName) { if ( @@ -116,33 +116,61 @@ export class BasicModel { } export class ResultModel extends BasicModel { + // get daysSinceIssued() { + // const raw = this._result['issued']?.raw; + // const issued = raw ? DateTime.fromISO(raw) : DateTime.local(); + // const res = DateTime.local().diff(issued, 'days').as('days'); + // return res; + // } + get daysSinceIssued() { const raw = this._result['issued']?.raw; - const issued = raw ? DateTime.fromISO(raw) : DateTime.local(); - const res = DateTime.local().diff(issued, 'days').as('days'); - return res; + const issuedDate = raw ? new Date(raw) : new Date(); + const currentDate = new Date(); + + const diffInTime = currentDate.getTime() - issuedDate.getTime(); + const diffInDays = diffInTime / (1000 * 3600 * 24); + return diffInDays; } get id() { return this.id?.raw; } + // get isNew() { + // const raw = this._result['issued']?.raw; + // const issued = raw ? DateTime.fromISO(raw) : DateTime.local(); + // const res = DateTime.local().diff(issued, 'days').as('days'); + // + // return res < 30; + // } + get isNew() { const raw = this._result['issued']?.raw; - const issued = raw ? DateTime.fromISO(raw) : DateTime.local(); - const res = DateTime.local().diff(issued, 'days').as('days'); + const issuedDate = raw ? new Date(raw) : new Date(); + const currentDate = new Date(); - return res < 30; + const diffInTime = currentDate.getTime() - issuedDate.getTime(); + const diffInDays = diffInTime / (1000 * 3600 * 24); + return diffInDays < 30; } + // get issued() { + // const raw = this._result['issued']?.raw; + // return raw ? DateTime.fromISO(raw) : null; + // } get issued() { const raw = this._result['issued']?.raw; - return raw ? DateTime.fromISO(raw) : null; + return raw ? new Date(raw) : null; } + // get expires() { + // const raw = this._result['expires']?.raw; + // return raw ? DateTime.fromISO(raw) : null; + // } get expires() { const raw = this._result['expires']?.raw; - return raw ? DateTime.fromISO(raw) : null; + return raw ? new Date(raw) : null; } // get isExpired() {