Skip to content

Commit

Permalink
Adapting code to make holidays work even though we use esm
Browse files Browse the repository at this point in the history
  • Loading branch information
araujoarthur0 committed Oct 7, 2023
1 parent 2bbe9fc commit a3b36ca
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 118 deletions.
36 changes: 20 additions & 16 deletions __tests__/__renderer__/workday-waiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,19 @@ window.mainApi = workdayWaiverApi;

// Mocking with the actual access to store that main would have
window.mainApi.getWaiverStoreContents = () => { return new Promise((resolve) => resolve(waiverStore.store)); };
window.mainApi.setWaiver = (key, contents) => {
return new Promise((resolve) => {
window.mainApi.setWaiver = (key, contents) =>
{
return new Promise((resolve) =>
{
waiverStore.set(key, contents);
resolve(true);
});
};
window.mainApi.hasWaiver = (key) => { return new Promise((resolve) => resolve(waiverStore.has(key))); };
window.mainApi.deleteWaiver = (key) =>{
return new Promise((resolve) => {
window.mainApi.deleteWaiver = (key) =>
{
return new Promise((resolve) =>
{
waiverStore.delete(key);
resolve(true);
});
Expand Down Expand Up @@ -94,7 +98,7 @@ describe('Test Workday Waiver Window', function()

describe('Adding new waivers update the db and the page', function()
{
beforeEach(async () =>
beforeEach(async() =>
{
await prepareMockup();
});
Expand Down Expand Up @@ -147,14 +151,14 @@ describe('Test Workday Waiver Window', function()
expect(isSorted).toBe(true);

});
test('Time is not valid', async () =>
test('Time is not valid', async() =>
{
$('#hours').val('not a time');
const waiver = await addWaiver();
expect(waiver).toBeFalsy();
});

test('End date less than start date', async () =>
test('End date less than start date', async() =>
{
setHours('08:00');
$('#start-date').val('2020-07-20');
Expand All @@ -163,14 +167,14 @@ describe('Test Workday Waiver Window', function()
expect(waiver).toBeFalsy();
});

test('Add waiver with the same date', async () =>
test('Add waiver with the same date', async() =>
{
addTestWaiver('2020-07-16', 'some reason');
const waiver = await addTestWaiver('2020-07-16', 'some reason');
expect(waiver).toBeFalsy();
});

test('Range does not contain any working day', async () =>
test('Range does not contain any working day', async() =>
{
const waiver = await addTestWaiver('2020-13-01', 'some reason');
expect(waiver).toBeFalsy();
Expand Down Expand Up @@ -216,7 +220,7 @@ describe('Test Workday Waiver Window', function()

describe('Delete waiver', () =>
{
test('Waiver was deleted', async () =>
test('Waiver was deleted', async() =>
{
await prepareMockup();
addTestWaiver('2020-07-16', 'some reason');
Expand All @@ -235,7 +239,7 @@ describe('Test Workday Waiver Window', function()
{
const hd = new Holidays();

beforeEach(async () =>
beforeEach(async() =>
{
await prepareMockup();
});
Expand Down Expand Up @@ -307,7 +311,7 @@ describe('Test Workday Waiver Window', function()
const state = 'CA';
const city = 'LA';

beforeEach(async () =>
beforeEach(async() =>
{
await prepareMockup();
});
Expand Down Expand Up @@ -356,7 +360,7 @@ describe('Test Workday Waiver Window', function()
const country = 'US';
const state = 'CA';

beforeEach(async () =>
beforeEach(async() =>
{
await prepareMockup();
});
Expand All @@ -372,7 +376,7 @@ describe('Test Workday Waiver Window', function()
expect(mockCallback).toBeCalledTimes(holidaysLength);
});

test('Load holidays table', async () =>
test('Load holidays table', async() =>
{
$('#year').append($('<option selected></option>').val(year).html(year));
$('#country').append($('<option selected></option>').val(country).html(country));
Expand Down Expand Up @@ -400,7 +404,7 @@ describe('Test Workday Waiver Window', function()

describe('Add holiday to list', () =>
{
beforeEach(async () =>
beforeEach(async() =>
{
await prepareMockup();
});
Expand Down Expand Up @@ -429,7 +433,7 @@ describe('Test Workday Waiver Window', function()

describe('Clearing the table', () =>
{
beforeEach(async () =>
beforeEach(async() =>
{
await prepareMockup();
addTestWaiver('2020-07-20', 'some other reason');
Expand Down
2 changes: 0 additions & 2 deletions esm-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const { handleSquirrelEvent } = require('./js/squirrel.js');
const { showAlert, showDialogSync } = require('./js/window-aux.js');

import { appConfig } from './js/app-config.js';
import { setupWorkdayWaiverStore } from './main/workday-waiver-aux.js';

if (appConfig.win32)
{
Expand Down Expand Up @@ -135,7 +134,6 @@ app.on('ready', () =>
}
createWindow();
createMenu();
setupWorkdayWaiverStore();
setLanguageChangedCallback(createMenu);
triggerStartupDialogs();
setInterval(refreshOnDayChange, 60 * 60 * 1000);
Expand Down
5 changes: 5 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
/*eslint-disable no-global-assign*/
'use strict';

// Executing the code from this file.
// It has dependencies that must be run without the esm module
require('./main/workday-waiver-aux.js');

// Using esm module to be able to mix node 'require' and ES6 'import' statements
// while we don't move to a newer electron+node system that has this by default
// See https://github.com/electron/electron/issues/21457.

require = require('esm')(module);
module.exports = require('./esm-main.js');

81 changes: 58 additions & 23 deletions main/workday-waiver-aux.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,75 @@
'use strict';

// This file intentionally uses 'require' and is used without esm
// because 'date-holidays' doesn't play nice with esm

const { ipcMain } = require('electron');

const Store = require('electron-store');
const waiverStore = new Store({name: 'waived-workdays'});

function getWaiverStore()
const Holidays = require('date-holidays');
const hd = new Holidays();

// Waiver Store handlers

ipcMain.handle('GET_WAIVER_STORE_CONTENTS', () =>
{
return waiverStore.store;
}
});

function setupWorkdayWaiverStore()
ipcMain.handle('SET_WAIVER', (_event, key, contents) =>
{
ipcMain.handle('GET_WAIVER_STORE_CONTENTS', () =>
{
return getWaiverStore();
});
waiverStore.set(key, contents);
return true;
});

ipcMain.handle('SET_WAIVER', (event, key, contents) =>
{
waiverStore.set(key, contents);
return true;
});
ipcMain.handle('HAS_WAIVER', (_event, key) =>
{
return waiverStore.has(key);
});

ipcMain.handle('HAS_WAIVER', (event, key) =>
{
return waiverStore.has(key);
});
ipcMain.handle('DELETE_WAIVER', (_event, key) =>
{
waiverStore.delete(key);
return true;
});

// Holiday handlers

ipcMain.handle('DELETE_WAIVER', (event, key) =>
function InitHolidays(country, state, city)
{
if (state !== undefined && city !== undefined)
{
hd.init(country, state, city);
}
else if (state !== undefined && state !== '--' )
{
hd.init(country, state);
}
else
{
waiverStore.delete(key);
return true;
});
hd.init(country);
}
}

export {
setupWorkdayWaiverStore
};
ipcMain.handle('GET_HOLIDAYS', (_event, country, state, city, year) =>
{
InitHolidays(country, state, city);
return hd.getHolidays(year);
});

ipcMain.handle('GET_COUNTRIES', () =>
{
return hd.getCountries();
});

ipcMain.handle('GET_STATES', (_event, country) =>
{
return hd.getStates(country);
});

ipcMain.handle('GET_REGIONS', (_event, country, state) =>
{
return hd.getRegions(country, state);
});
58 changes: 21 additions & 37 deletions renderer/preload-scripts/workday-waiver-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ const { ipcRenderer } = require('electron');
import * as config from '../../src/configs/app.config.js';
import { getUserPreferencesPromise, showDay } from '../../js/user-preferences.js';

const Holidays = require('date-holidays');
const hd = new Holidays();

function getLanguageDataPromise()
function getLanguageData()
{
return ipcRenderer.invoke('GET_LANGUAGE_DATA');
}

function getWaiverDayPromise()
function getWaiverDay()
{
return ipcRenderer.invoke('GET_WAIVER_DAY');
}
Expand All @@ -32,64 +29,51 @@ function showDayByPreferences(year, month, day, preferences)
return showDay(year, month, day, preferences);
}

function getHolidays(country, state, city, year)
function getWaiverStoreContents()
{
if (state !== undefined && city !== undefined)
{
hd.init(country, state, city);
}
else if (state !== undefined && state !== '--' )
{
hd.init(country, state);
}
else
{
hd.init(country);
}

return hd.getHolidays(year);
return ipcRenderer.invoke('GET_WAIVER_STORE_CONTENTS');
}

function getCountries()
function setWaiver(key, contents)
{
return hd.getCountries();
return ipcRenderer.invoke('SET_WAIVER', key, contents);
}

function getStates(country)
function hasWaiver(key)
{
return hd.getStates(country);
return ipcRenderer.invoke('HAS_WAIVER', key);
}

function getRegions(country, state)
function deleteWaiver(key)
{
return hd.getRegions(country, state);
return ipcRenderer.invoke('DELETE_WAIVER', key);
}

function getWaiverStoreContents()
function getHolidays(country, state, city, year)
{
return ipcRenderer.invoke('GET_WAIVER_STORE_CONTENTS');
return ipcRenderer.invoke('GET_HOLIDAYS', country, state, city, year);
}

function setWaiver(key, contents)
function getCountries()
{
return ipcRenderer.invoke('SET_WAIVER', key, contents);
return ipcRenderer.invoke('GET_COUNTRIES');
}

function hasWaiver(key)
function getStates(country)
{
return ipcRenderer.invoke('HAS_WAIVER', key);
return ipcRenderer.invoke('GET_STATES', country);
}

function deleteWaiver(key)
function getRegions(country, state)
{
return ipcRenderer.invoke('DELETE_WAIVER', key);
return ipcRenderer.invoke('GET_REGIONS', country, state);
}

const workdayWaiverApi = {
getLanguageMap: () => config.getLanguageMap(),
getUserPreferencesPromise: () => getUserPreferencesPromise(),
getLanguageDataPromise: () => getLanguageDataPromise(),
getWaiverDayPromise: () => getWaiverDayPromise(),
getUserPreferences: () => getUserPreferencesPromise(),
getLanguageData: () => getLanguageData(),
getWaiverDay: () => getWaiverDay(),
showAlert: (alertMessage) => showAlert(alertMessage),
showDialogSync: (dialogOptions) => showDialogSync(dialogOptions),
showDay: (year, month, day, userPreferences) => showDayByPreferences(year, month, day, userPreferences),
Expand Down
Loading

0 comments on commit a3b36ca

Please sign in to comment.