Skip to content

Commit

Permalink
Working spinner loading state
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaScorpion committed Feb 3, 2024
1 parent cdb00ed commit 133d626
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/EventEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export type EventHandler = () => void | Promise<void>;
export type EventHandler<T> = (value: T) => void | Promise<void>;

export class EventEmitter {
private listeners: EventHandler[] = [];
export class EventEmitter<T = void> {
private listeners: EventHandler<T>[] = [];

public addListener(l: EventHandler) {
public addListener(l: EventHandler<T>) {
this.listeners.push(l);
}

public fire() {
this.listeners.forEach((h) => h());
public fire(value: T) {
this.listeners.forEach((h) => h(value));
}
}
14 changes: 13 additions & 1 deletion src/content/add-billability-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ async function doAddBillabilityChart(date: Date, user: User) {
chartContainer = createBillabilityCard(addTimePanel);
}

updateLoadingState(true);
const [times, company] = await Promise.all([
getTimes(user.id, date, GET_TIMES_WEEKS),
api.getCompany(),
]);
updateLoadingState(false);

const settings = getSettings();

Expand Down Expand Up @@ -77,7 +79,8 @@ function createBillabilityCard(addTimePanel: Element) {
spinner.className = 'title-date-spinner';
const spinnerIcon = document.createElement('i');
spinner.appendChild(spinnerIcon);
spinnerIcon.className = 'fa fa-circle-o-notch fa-spin';
spinnerIcon.id = 'billability-loading';
spinnerIcon.className = 'fa fa-circle-o-notch fa-spin hidden';

const toggleViewBtn = document.createElement('button');

Expand All @@ -103,6 +106,15 @@ function createBillabilityCard(addTimePanel: Element) {
return chartContainer;
}

function updateLoadingState(loading: boolean) {
const spinner = document.getElementById('billability-loading');
if (spinner) {
loading
? spinner.classList.remove('hidden')
: spinner.classList.add('hidden');
}
}

/**
* Gets times from TimeChimp for the given weeks in the past, and filters out the ones for the given user id.
* Optionally a date can be provided, by default it will get times for the current date.
Expand Down
2 changes: 1 addition & 1 deletion src/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { settingsUpdateEvent } from './settings';
setDefaultOptions({
// Weeks start on Monday.
weekStartsOn: 1,
// Week number 1 should contain the 4th on January.
// Week number 1 should contain the 4th of January.
firstWeekContainsDate: 4,
});

Expand Down

0 comments on commit 133d626

Please sign in to comment.