Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BC-4623-Creation time and update time of status messages #2786

Merged
merged 4 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions src/components/topbar/StatusAlerts.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import setupStores from "@@/tests/test-utils/setupStores";
import { mockStatusAlerts } from "@@/tests/test-utils/mockStatusAlerts";
import Vue from "vue";
import createComponentMocks from "@@/tests/test-utils/componentMocks";
import dayjs from "dayjs";
import { formatDateForAlerts } from "@/plugins/datetime";

const testProps = {
statusAlerts: mockStatusAlerts,
Expand Down Expand Up @@ -59,18 +61,17 @@ describe("@/components/topbar/StatusAlerts", () => {
});
});

describe("getCreatedDate", () => {
it("should be getCreatedDate function on the template", () => {
const wrapper = getWrapper(testProps);
const expectedDate = "05.05.2023 12:34";
const alertElement = wrapper.find(".alert-date");
expect(alertElement.element.innerHTML).toContain(expectedDate);
describe("formatDate", () => {
const wrapper = getWrapper(testProps);
it("returns 'vor ein paar Sekunden' for seconds difference", () => {
const fewSecondsAgo = dayjs().subtract(30, "seconds");
const expectedOutput = "vor ein paar Sekunden";
const result = formatDateForAlerts(fewSecondsAgo);
expect(result).toBe(expectedOutput);
});

it("should returns expected result", () => {
const wrapper = getWrapper(testProps);
const expectedDate = "05.05.2023 12:34";
const dateTime = "May 5, 2023 12:34 PM";
expect(wrapper.vm.getCreatedDate(dateTime)).toEqual(expectedDate);
it("returns European format after 7 days", () => {
const pastDate = dayjs.utc().subtract(8, "days").toISOString();
const expectedDate = dayjs.utc(pastDate).format("DD.MM.YYYY");
expect(wrapper.vm.formatDate(pastDate)).toEqual(expectedDate);
});
});
20 changes: 9 additions & 11 deletions src/components/topbar/StatusAlerts.vue
bn-pass marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@
class="text-left text-caption d-flex flex-row alert-date text--secondary mt-0 mt-2"
:data-testid="`alert-date-${index}`"
>
{{ $t("common.labels.updateAt") }}
{{ getDate(item.timestamp) }} |
{{ $t("common.labels.createAt") }}
{{ getCreatedDate(item.createdAt) }}
<template v-if="item.timestamp !== item.createdAt">
{{ $t("common.labels.updateAt") }}
{{ formatDate(item.timestamp) }} |
</template>
{{ $t("common.labels.createAt") }} {{ formatDate(item.createdAt) }}
</v-list-item-subtitle>
</v-list-item-content>
</v-list-item>
Expand All @@ -46,7 +47,7 @@

<script lang="ts">
import { defineComponent, PropType } from "vue";
import { printDateTime, fromNow } from "../../plugins/datetime";
import { formatDateForAlerts } from "../../plugins/datetime";
import { mdiAlertCircle, mdiInformation } from "@mdi/js";
import { StatusAlert } from "@/store/types/status-alert";

Expand All @@ -65,14 +66,11 @@ export default defineComponent({
: { icon: mdiInformation, color: "info" };
};

const getDate = (date: string) => {
return fromNow(date, true);
};
const getCreatedDate = (dateTime: string) => {
return printDateTime(dateTime);
const formatDate = (dateTime: any) => {
return formatDateForAlerts(dateTime, true);
};

return { getIconTag, getDate, getCreatedDate };
return { getIconTag, formatDate };
},
});
</script>
Expand Down
26 changes: 24 additions & 2 deletions src/plugins/datetime.js
Michaellinaresxk marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,29 @@ export const fromNow = (date, isLocalTimeZone) => {
return fromUTC(date).fromNow();
};

export const formatDateForAlerts = (date, isLocalTimeZone = false) => {
const time = isLocalTimeZone ? dayjs(date) : dayjs.tz(date, "UTC");
const current = isLocalTimeZone ? dayjs() : dayjs.utc();

setDayjsLocale();
Michaellinaresxk marked this conversation as resolved.
Show resolved Hide resolved

const totalDaysDiff = Math.abs(current.diff(time, "day"));
const MAX_DAYS_BEFORE_SHOWING_FULL_DATE = 7;

if (totalDaysDiff < MAX_DAYS_BEFORE_SHOWING_FULL_DATE) {
// If it is less than 7 days, we use fromNow
return fromNow(date, isLocalTimeZone);
} else {
// If it is 7 days or more, we return in European format.
return time.format("DD.MM.YYYY");
}
};

export const setDayjsLocale = () => {
const locale = authModule?.getLocale || "de";
dayjs.locale(locale);
};

/**
* Returns future date difference to current local time
* @param {String} date
Expand Down Expand Up @@ -289,6 +312,5 @@ export default ({ app, store }) => {
initDefaultTimezone(app, store);
setDefaultFormats(app);

const locale = authModule.getLocale || "de";
dayjs.locale(locale);
setDayjsLocale();
};
8 changes: 8 additions & 0 deletions src/plugins/datetime.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
currentDate,
fromInputDateTime,
fromNow,
formatDateForAlerts,
fromNowToFuture,
createInputDateTime,
inputRangeDate,
Expand Down Expand Up @@ -154,6 +155,13 @@ describe("@/plugins/datetime", () => {
expect(result).toBe("in 7 days");
});

it("formatDateForAlerts", () => {
const sevenDaysAgo = dayjs().subtract(7, "days");
const expectedDate = sevenDaysAgo.format("DD.MM.YYYY");
const result = formatDateForAlerts(sevenDaysAgo);
expect(result).toBe(expectedDate);
});

it("fromNowToFuture", () => {
const past = dateUTC.toISOString();
const future = dateNow.add(230, "minute").toISOString();
Expand Down
Loading