Skip to content

Commit

Permalink
Add toggle button to card
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaScorpion committed Jan 31, 2024
1 parent 992eb96 commit 9a2f450
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
30 changes: 26 additions & 4 deletions src/content/add-billability-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { TimeChimpApi, User } from '../TimeChimpApi';
import { toIsoDate } from '../date';
import { endOfWeek, getWeek, startOfWeek, subWeeks } from 'date-fns';
import { calculateTimeStats } from './stats';
import { getSettings } from './settings';
import { getSettings, updateSettings } from './settings';
import { render } from './index';

const api = new TimeChimpApi();

Expand Down Expand Up @@ -35,7 +36,7 @@ async function doAddBillabilityChart(date: Date, user: User) {
// If not, create a new element which can be used as the chart parent.
let chartContainer: HTMLElement | undefined;
if (!addTimePanel.querySelector('#billability-card')) {
chartContainer = addTimePanel.appendChild(createBillabilityCard());
chartContainer = createBillabilityCard(addTimePanel);
}

const [times, company] = await Promise.all([
Expand All @@ -59,11 +60,32 @@ async function doAddBillabilityChart(date: Date, user: User) {
);
}

function createBillabilityCard() {
function createBillabilityCard(addTimePanel: Element) {
const card = document.createElement('div');
card.className = 'card billability-card';
card.id = 'billability-card';
return card;

const chartContainer = document.createElement('div');
chartContainer.className = 'chart';
card.appendChild(chartContainer);

const actions = document.createElement('div');
card.appendChild(actions);
actions.className = 'actions text-align-right';

const toggleViewBtn = document.createElement('button');
actions.appendChild(toggleViewBtn);
toggleViewBtn.className = 'btn btn-timechimp-border';
toggleViewBtn.textContent = 'Toggle!';
toggleViewBtn.addEventListener('click', () => {
updateSettings({
relativeToContractHours: !getSettings().relativeToContractHours,
});
render();
});

addTimePanel.appendChild(card);
return chartContainer;
}

/**
Expand Down
13 changes: 7 additions & 6 deletions src/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ chrome.runtime.onMessage.addListener(async (msg: Message) => {
currentDate = new Date(msg.date);
}

if (
!currentUser ||
(msg.userName && msg.userName !== currentUser.userName)
) {
currentUser = await getUser(msg.userName);
await render(msg.userName);
});

export async function render(userName?: string) {
if (!currentUser || (userName && userName !== currentUser.userName)) {
currentUser = await getUser(userName);
}

await addBillabilityChart(currentDate, currentUser);
});
}

/**
* Get the user info based on a userName.
Expand Down
8 changes: 6 additions & 2 deletions src/content/style.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
.billability-card {
padding-top: 4px;
.billability-card > .chart {
height: 350px;
}

.billability-card > .actions > button {
margin-right: 20px;
margin-bottom: 20px;
}

0 comments on commit 9a2f450

Please sign in to comment.