@salesforcedevs/dx-components 1.3.321 → 1.3.322

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforcedevs/dx-components",
3
- "version": "1.3.321",
3
+ "version": "1.3.322",
4
4
  "description": "DX Lightning web components",
5
5
  "license": "MIT",
6
6
  "engines": {
@@ -46,5 +46,5 @@
46
46
  "volta": {
47
47
  "node": "16.19.1"
48
48
  },
49
- "gitHead": "284f6c7c36422426de5d8167dcf1f4d88742c9ff"
49
+ "gitHead": "489fc5c1b1cc01080269de7a82f462e12fca8966"
50
50
  }
@@ -1,12 +1,6 @@
1
1
  import { LightningElement, api } from "lwc";
2
2
 
3
3
  export default class RelativeDateTime extends LightningElement {
4
- @api messageWhenHoursPast: String = "";
5
- @api messageWhenDaysPast: String = "";
6
- @api messageWhenWeeksPast: String = "";
7
- @api messageWhenMonthsPast: String = "";
8
- @api messageWhenYearsPast: String = "";
9
-
10
4
  @api
11
5
  set value(value) {
12
6
  if (!value) {
@@ -14,102 +8,35 @@ export default class RelativeDateTime extends LightningElement {
14
8
  return;
15
9
  }
16
10
 
17
- this._selectedDateTime = new Date(value);
11
+ this._inputDateTime = new Date(value);
18
12
  }
19
13
  get value() {
20
- const relativeDateTimeInHours = this.formatDate(
21
- this._selectedDateTime!
22
- );
23
- this._relativeDateTime = this.getRelativeDateTimeAsString(
24
- this._selectedDateTime!,
25
- relativeDateTimeInHours
14
+ this._relativeDateTime = this.getFormattedDateTime(
15
+ this._inputDateTime!
26
16
  );
27
17
  return this._relativeDateTime;
28
18
  }
29
19
  get original() {
30
- return this._selectedDateTime;
20
+ return this._inputDateTime?.toLocaleString();
31
21
  }
32
22
 
23
+ private _inputDateTime?: Date;
33
24
  private _relativeDateTime?: string;
34
- private _selectedDateTime?: Date;
35
-
36
- formatDate(selectedDateTime: Date) {
37
- const currentDateTime = new Date();
38
- const relativeDateTimeInHours = this.getDifferenceInHours(
39
- selectedDateTime,
40
- currentDateTime
41
- );
42
- return relativeDateTimeInHours;
43
- }
44
-
45
- getDifferenceInHours(selectedDateTime: Date, currentDateTime: Date) {
46
- const milliseconds = Math.abs(+selectedDateTime - +currentDateTime);
47
- const hours = Math.floor(milliseconds / 36e5);
48
- return hours;
49
- }
50
-
51
- getRelativeDateTimeAsString(
52
- selectedDateTime: Date,
53
- relativeDateTimeInHours: number
54
- ) {
55
- const daysPast = Math.floor(+relativeDateTimeInHours / 24);
56
- const weeksPast = Math.floor(+daysPast / 7);
57
- const monthsPast = Math.floor(+daysPast / 30);
58
- const yearsPast = Math.floor(+monthsPast / 12);
59
- if (yearsPast >= 1) {
60
- return this.timeLabel("year", yearsPast);
61
- } else if (monthsPast <= 11 && monthsPast >= 1) {
62
- return this.timeLabel("month", monthsPast);
63
- } else if (monthsPast < 1 && daysPast >= 7) {
64
- return this.timeLabel("week", weeksPast);
65
- } else if (daysPast <= 7 && daysPast >= 1) {
66
- return this.timeLabel("day", daysPast);
67
- } else if (relativeDateTimeInHours < 24) {
68
- return this.timeLabel("hour", relativeDateTimeInHours);
69
- }
70
- return "";
71
- }
72
25
 
73
- timeLabel(type: String, number: Number) {
74
- let suffix: String = "";
75
- switch (type) {
76
- case "hour": {
77
- suffix =
78
- this.messageWhenHoursPast ||
79
- `${this.pluralize(type, number)} ago`;
80
- break;
81
- }
82
- case "day": {
83
- suffix =
84
- this.messageWhenDaysPast ||
85
- `${this.pluralize(type, number)} ago`;
86
- break;
87
- }
88
- case "week": {
89
- suffix =
90
- this.messageWhenWeeksPast ||
91
- `${this.pluralize(type, number)} ago`;
92
- break;
93
- }
94
- case "month": {
95
- suffix =
96
- this.messageWhenMonthsPast ||
97
- `${this.pluralize(type, number)} ago`;
98
- break;
99
- }
100
- case "year": {
101
- suffix =
102
- this.messageWhenYearsPast ||
103
- `${this.pluralize(type, number)} ago`;
104
- break;
105
- }
106
- default:
107
- suffix = "";
26
+ getFormattedDateTime(inputDateTime: Date) {
27
+ const now = new Date();
28
+ const yesterday = new Date(
29
+ now.getTime() - 24 * 60 * 60 * 1000
30
+ ).toLocaleDateString();
31
+ const twoDaysAgo = new Date(
32
+ now.getTime() - 48 * 60 * 60 * 1000
33
+ ).toLocaleDateString();
34
+ const dateStringFromInput = inputDateTime?.toLocaleDateString();
35
+ if (dateStringFromInput === yesterday) {
36
+ return "1 day ago";
37
+ } else if (dateStringFromInput === twoDaysAgo) {
38
+ return "2 days ago";
108
39
  }
109
- return suffix ? `${number} ${suffix}` : "";
110
- }
111
-
112
- pluralize(type: String, number: Number) {
113
- return `${type}${number !== 1 ? "s" : ""}`;
40
+ return dateStringFromInput;
114
41
  }
115
42
  }