From dd05651610a0d298cfb14ed7b81ec46159d4772d Mon Sep 17 00:00:00 2001 From: Reid Efford Date: Thu, 26 Jan 2023 13:36:22 -0500 Subject: [PATCH] move utils to c/calendarUtils --- .../main/default/lwc/calendar/calendar.js | 2 +- force-app/main/default/lwc/calendar/utils.js | 62 ------------------- .../__tests__/calendarUtils.test.js | 25 ++++++++ .../calendarUtils.js} | 5 +- .../lwc/customCalendar/customCalendar.js | 4 +- 5 files changed, 31 insertions(+), 67 deletions(-) delete mode 100644 force-app/main/default/lwc/calendar/utils.js create mode 100644 force-app/main/default/lwc/calendarUtils/__tests__/calendarUtils.test.js rename force-app/main/default/lwc/{customCalendar/utilities.js => calendarUtils/calendarUtils.js} (96%) diff --git a/force-app/main/default/lwc/calendar/calendar.js b/force-app/main/default/lwc/calendar/calendar.js index 7ac590d..9f9065f 100644 --- a/force-app/main/default/lwc/calendar/calendar.js +++ b/force-app/main/default/lwc/calendar/calendar.js @@ -2,7 +2,7 @@ import { api, LightningElement } from 'lwc'; import { ShowToastEvent } from "lightning/platformShowToastEvent"; import fullCalendar from "@salesforce/resourceUrl/fullCalendar"; import { loadStyle, loadScript } from "lightning/platformResourceLoader"; -import { jsToApexDate, invertColor, msToHMS} from './utils' +import { jsToApexDate } from 'c/calendarUtils' export default class Calendar extends LightningElement { calendarLabel = '' diff --git a/force-app/main/default/lwc/calendar/utils.js b/force-app/main/default/lwc/calendar/utils.js deleted file mode 100644 index 99dfd8c..0000000 --- a/force-app/main/default/lwc/calendar/utils.js +++ /dev/null @@ -1,62 +0,0 @@ -const formatShifts = (shifts) => { - const invertColor = (hex = "#FFFFFF", bw) => { - if (hex.indexOf("#") === 0) { - hex = hex.slice(1); - } - // convert 3-digit hex to 6-digits. - if (hex.length === 3) { - hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2]; - } - if (hex.length !== 6) { - throw new Error("Invalid HEX color."); - } - let r = parseInt(hex.slice(0, 2), 16), - g = parseInt(hex.slice(2, 4), 16), - b = parseInt(hex.slice(4, 6), 16); - if (bw) { - return r * 0.299 + g * 0.587 + b * 0.114 > 186 ? "#000000" : "#FFFFFF"; - } - // invert color components - r = (255 - r).toString(16); - g = (255 - g).toString(16); - b = (255 - b).toString(16); - // pad each with zeros and return - return "#" + padZero(r) + padZero(g) + padZero(b); - }; - - const msToHMS = (duration) => { - let seconds = Math.floor((duration / 1000) % 60), - minutes = Math.floor((duration / (1000 * 60)) % 60), - hours = Math.floor((duration / (1000 * 60 * 60)) % 24); - - hours = hours < 10 ? "0" + hours : hours; - minutes = minutes < 10 ? "0" + minutes : minutes; - seconds = seconds < 10 ? "0" + seconds : seconds; - - return hours + ":" + minutes + ":" + seconds; - }; - - return shifts.map((shift) => { - shift.id = shift.Id; - shift.status = shift.Shift_Status__c; - shift.title = `${shift.Facility_1__r?.Name || ""}-${shift?.Name}`; - shift.start = `${shift.Date__c}T${msToHMS(shift.Scheduled_Start__c)}`; - shift.end = `${shift.Date__c}T${msToHMS(shift.Scheduled_End__c)}`; - shift.textColor = invertColor(shift?.Color__c, true); - shift.borderColor = shift.Color__c; - shift.backgroundColor = shift.Color__c; - return shift; - }); -}; - -const jsToApexDate = (date) => { - const year = date.getFullYear(); - const month = - date.getMonth() + 1 < 10 - ? "0" + (date.getMonth() + 1) - : date.getMonth() + 1; - const day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); - return `${year}-${month}-${day}`; -}; - -export { formatShifts, jsToApexDate }; \ No newline at end of file diff --git a/force-app/main/default/lwc/calendarUtils/__tests__/calendarUtils.test.js b/force-app/main/default/lwc/calendarUtils/__tests__/calendarUtils.test.js new file mode 100644 index 0000000..47e43f2 --- /dev/null +++ b/force-app/main/default/lwc/calendarUtils/__tests__/calendarUtils.test.js @@ -0,0 +1,25 @@ +import { createElement } from 'lwc'; +import CalendarUtils from 'c/calendarUtils'; + +describe('c-calendar-utils', () => { + afterEach(() => { + // The jsdom instance is shared across test cases in a single file so reset the DOM + while (document.body.firstChild) { + document.body.removeChild(document.body.firstChild); + } + }); + + it('TODO: test case generated by CLI command, please fill in test logic', () => { + // Arrange + const element = createElement('c-calendar-utils', { + is: CalendarUtils + }); + + // Act + document.body.appendChild(element); + + // Assert + // const div = element.shadowRoot.querySelector('div'); + expect(1).toBe(1); + }); +}); \ No newline at end of file diff --git a/force-app/main/default/lwc/customCalendar/utilities.js b/force-app/main/default/lwc/calendarUtils/calendarUtils.js similarity index 96% rename from force-app/main/default/lwc/customCalendar/utilities.js rename to force-app/main/default/lwc/calendarUtils/calendarUtils.js index fd80da0..0b2816e 100644 --- a/force-app/main/default/lwc/customCalendar/utilities.js +++ b/force-app/main/default/lwc/calendarUtils/calendarUtils.js @@ -40,7 +40,7 @@ const formatEvents = (events, config) => { return events.map(event => { event.id = event.Id // event.status = event.Shift_Status__c - event.title = event?.Name + event.title = event[config.titleField] event.start = event[config.startDatetimeField] event.end = event[config.endDatetimeField] // event.textColor = invertColor(event?.Color__c, true) @@ -57,5 +57,4 @@ const jsToApexDate = (date) => { return `${year}-${month}-${day}` } - -export { formatEvents, jsToApexDate } \ No newline at end of file +export { formatEvents, jsToApexDate } \ No newline at end of file diff --git a/force-app/main/default/lwc/customCalendar/customCalendar.js b/force-app/main/default/lwc/customCalendar/customCalendar.js index 66b3b3f..ffe9390 100644 --- a/force-app/main/default/lwc/customCalendar/customCalendar.js +++ b/force-app/main/default/lwc/customCalendar/customCalendar.js @@ -4,13 +4,14 @@ import { subscribe, unsubscribe, onError, setDebugFlag, isEmpEnabled } from 'lig import { encodeDefaultFieldValues } from 'lightning/pageReferenceUtils' import Id from '@salesforce/user/Id'; import getEvents from "@salesforce/apex/CustomCalendarHelper.getEvents" -import { formatEvents } from "./utilities" +import { formatEvents } from "c/calendarUtils" export default class CustomCalendar extends NavigationMixin(LightningElement) { @api recordId @api childObject @api parentFieldName @api startDatetimeField @api endDatetimeField + @api titleField @api channelName userId = Id @@ -60,6 +61,7 @@ export default class CustomCalendar extends NavigationMixin(LightningElement) { parentFieldName: this.parentFieldName, startDatetimeField: this.startDatetimeField, endDatetimeField: this.endDatetimeField, + titleField: this.titleField, startDate: this.startDate, endDate: this.endDate }