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

receipts view #15

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
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
90 changes: 90 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,104 @@
</header>
<div class="dev-tools">
<div id="options">
<button id="toggle-receipts">Toggle receipts</button>

<p id="interaction-status-area" style="min-height: 20px;margin: 10px;"></p>
</div>
</div>

<div id="demo">
<style>
iaux-monthly-giving-circle {
display: block;
margin: 0 auto;
max-width: 800px;
}
</style>
<iaux-monthly-giving-circle></iaux-monthly-giving-circle>
</div>

<script type="module" src="../dist/src/monthly-giving-circle.js"></script>
<script type="module">
let updateNotices = [];

const receiptsData = [
Copy link

@jbuckner jbuckner Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a practical example with types instead of classes, this can become

const receiptsData = [
 { amount: 9999.99, date: .... },
 { amount: 100, date: .... }
]

(or if you're in a TS file, const receiptsData: aReceipt[] = [ ... ])

{
amount: 9999.99,
date: '2020-09-01',
donor: 'John Doe',
paymentMethod: 'Credit Card',
status: 'Completed',
id: 'foo-id-1',
},
{
amount: 100,
date: '2023-02-01',
donor: 'John Doe',
paymentMethod: 'Credit Card',
status: 'Completed',
id: 'foo-id-2',
is_test: true,
},
{
amount: 100,
date: '2024-03-01',
donor: 'John Doe',
paymentMethod: 'Credit Card',
status: 'Completed',
id: 'foo-id-3',
is_test: true,
},
];


let showReceipts = true;

const mgcComponent = document.querySelector('iaux-monthly-giving-circle');

// load start data
mgcComponent.receipts = receiptsData;

// event handlers
mgcComponent.addEventListener('EmailReceiptRequest', (e) => {
const uxMessageInfoArea = document.getElementById('interaction-status-area');

const { donation } = e.detail;
const randomizer = Math.floor(Math.random() + 0.5);
const successOrFail = randomizer === 1 ? 'success' : 'fail';
const returnTiming = randomizer === 1 ? 3000 : 8000;

uxMessageInfoArea.innerText = `Email receipt request for donation ${donation.id} will return ${successOrFail} in ${returnTiming} ms.`;

const message = successOrFail === 'success' ? 'Email receipt sent' : 'Email receipt failed';

const update = {
message,
status: successOrFail,
donationId: donation.id
};

updateNotices = [update, ...updateNotices];

setTimeout(() => {
mgcComponent.updateReceived(update);
console.log('EmailReceiptRequest index.html ----', update);

Check warning on line 121 in demo/index.html

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
uxMessageInfoArea.innerText = '';
}, returnTiming);
});

// options hooks
document.getElementById('toggle-receipts').addEventListener('click', async () => {
if (showReceipts) {
mgcComponent.receipts = [];
showReceipts = false;
return;
}
mgcComponent.receipts = receiptsData;
await mgcComponent.updateComplete;

showReceipts = true;
});
</script>
</body>
</html>
3 changes: 3 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export { MonthlyGivingCircle } from './src/monthly-giving-circle';
export type { anUpdate } from './src/monthly-giving-circle';
export type { aReceipt } from './src/models/receipt';
export { Receipt } from './src/models/receipt';
17 changes: 14 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@internetarchive/donation-monthly-portal",
"version": "1.0.0",
"version": "0.0.0-receipts10",
"description": "The Internet Archive Monthly Portal",
"license": "AGPL-3.0-only",
"main": "dist/index.js",
Expand All @@ -27,6 +27,7 @@
"ghpages:generate": "gh-pages -t -d ghpages -m \"Build for $(git log --pretty=format:\"%h %an %ai %s\" -n1) [skip ci]\""
},
"dependencies": {
"@internetarchive/iaux-notification-toast": "^0.0.0-alpha2",
"@internetarchive/icon-donate": "^1.3.4",
"lit": "^2.8.0"
},
Expand All @@ -52,7 +53,7 @@
"madge": "^6.0.0",
"prettier": "^2.7.1",
"rimraf": "^5.0.0",
"sinon": "^17.0.0",
"sinon": "^17.0.1",
"ts-lit-plugin": "^2.0.0",
"tslib": "^2.7.0",
"typescript": "^4.7.4",
Expand Down
26 changes: 26 additions & 0 deletions src/models/receipt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export type aReceipt = {
id: string;
amount: number;
date: string;
isTest: boolean;
token: string;
};
Copy link

@jbuckner jbuckner Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at your demo data, it looks like you're missing donor, paymentMethod, and status here

export class Receipt {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason you have the separate type aReceipt and the class Receipt? It seems like you should be able to just have the type since the class is just a container for the data and doesn't have any operations on it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, in the constructor of Receipt class, TS is asking to remove implicit any. what's the best way to remove this?

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok, leaving it as internal since that is where it really is only used. other references point to the class - commit here

Copy link

@jbuckner jbuckner Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I mean, do you need the class at all since it's just defining the shape of the data?

Where you're doing

const receipt = new Receipt({ 
  amount: 100,
  date: new Date().toLocaleString(),
  donor: "boop",
  ...
})

you can just replace it with

const receipt: aReceipt = {
  amount: 100,
  date: new Date().toLocaleString(),
  donor: "boop",
  ...
}

The type is telling typescript everything it needs to know about the receipt. You really only need an extra class if you need extra functionality in the class, which you're not doing in Receipt.

It's also lighter-weight than defining a class. Classes have to be added to the runtime javascript, instantiated and stored in memory, whereas a type is just the shape of the data and typescript can completely handle during development time and not include in the runtime javascript (smaller file size, lower overhead, etc).

id: string;

amount: number;

date: string;

isTest: boolean;

token: string;

constructor(receipt: aReceipt) {
this.id = receipt.id;
this.amount = receipt.amount;
this.date = receipt.date;
this.isTest = receipt.isTest;
this.token = receipt.token;
}
}
89 changes: 87 additions & 2 deletions src/monthly-giving-circle.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,107 @@
/* eslint-disable no-debugger */

import { LitElement, html } from 'lit';
import { LitElement, html, TemplateResult, nothing } from 'lit';
import { customElement, property } from 'lit/decorators.js';

import './welcome-message';
import './presentational/mgc-title';
import './receipts';
import type { IauxMgcReceipts } from './receipts';
import './presentational/button-style';

export type anUpdate = {
message: string;
status: 'success' | 'fail';
donationId: string;
};

@customElement('iaux-monthly-giving-circle')
iisa marked this conversation as resolved.
Show resolved Hide resolved
export class MonthlyGivingCircle extends LitElement {
@property({ type: String }) patronName: string = '';

@property({ type: Array }) receipts = [];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can become

@property({ type: Array }) receipts: aReceipt[] = [];

so TS knows the type of data its working with and help the developer out during development.


@property({ type: Array }) updates: anUpdate[] = [];

@property({ type: String, reflect: true }) viewToDisplay:
| 'welcome'
| 'receipts' = 'welcome';

protected createRenderRoot() {
return this;
}

get receiptListElement(): IauxMgcReceipts {
return this.querySelector('iaux-mgc-receipts') as IauxMgcReceipts;
}
Copy link

@jbuckner jbuckner Jan 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the @query() decorator for this, ie:

import { query } from 'lit/decorators.js';

@query('iaux-mgc-receipts') private receiptListElement?: IauxMgcReceipts;

Also, it's optional since you have that conditional of whether to display it or not.


updateReceived(update: anUpdate) {
this.receiptListElement.emailSent({
id: update.donationId,
emailStatus: update.status,
});
this.updates.unshift(update);
}

get showReceiptsCTA(): TemplateResult {
return html`
<iaux-button-style class="link">
<button
@click=${() => {
this.viewToDisplay = 'receipts';
this.dispatchEvent(new CustomEvent('ShowReceipts'));
}}
>
View recent donation history
</button>
</iaux-button-style>
`;
}

protected render() {
if (this.viewToDisplay === 'receipts') {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar suggestion about breaking up this render method to make it easier to read. I didn't notice there are two different versions here that you can display. ie:

if (this.viewToDisplay === 'receipts') {
  ${this.receiptsView}
} else {
  ${this.welcomeView}
}

return html`
<iaux-mgc-title titleStyle="default">
<span slot="title">Recent donations</span>
<span slot="action">
<iaux-button-style class="link">
<button
class="close-receipts"
@click=${(event: Event) => {
const btn = event.target as HTMLButtonElement;
btn.disabled = true;

this.viewToDisplay = 'welcome';
this.dispatchEvent(new CustomEvent('ShowWelcome'));
this.updates = [];
}}
>
Back to account settings
</button>
</iaux-button-style>
</span>
</iaux-mgc-title>
<iaux-mgc-receipts
.receipts=${this.receipts}
@EmailReceiptRequest=${(event: CustomEvent) => {
console.log('EmailReceiptRequest', event.detail);

Check warning on line 87 in src/monthly-giving-circle.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
this.dispatchEvent(
new CustomEvent('EmailReceiptRequest', {
detail: { ...event.detail },
})
);
}}
></iaux-mgc-receipts>
`;
}

return html`
<iaux-mgc-title titleStyle="heart"></iaux-mgc-title>
<iaux-mgc-title titleStyle="heart">
<span slot="title">Monthly Giving Circle</span>
<span slot="action"
>${this.receipts.length ? this.showReceiptsCTA : nothing}</span
>
</iaux-mgc-title>
<iaux-mgc-welcome .patronName=${this.patronName}></iaux-mgc-welcome>
`;
}
Expand Down
72 changes: 72 additions & 0 deletions src/presentational/button-style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { LitElement, html, css } from 'lit';
import { customElement } from 'lit/decorators.js';

import '@internetarchive/icon-donate/icon-donate.js';

@customElement('iaux-button-style')
export class MonthlyGivingCircle extends LitElement {
iisa marked this conversation as resolved.
Show resolved Hide resolved
render() {
return html`<slot></slot>`;
}

static styles = css`
::slotted(*) {
height: 30px;
border: none;
cursor: pointer;
color: #fff;
line-height: normal;
border-radius: 0.4rem;
text-align: center;
vertical-align: middle;
display: inline-block;
padding: 0.6rem 1.2rem;
border: 1px solid transparent;

white-space: nowrap;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;
}

:host(.transparent) ::slotted(*) {
background-color: transparent;
}

:host(.slim) ::slotted(*) {
padding: 0;
}

:host(.primary) ::slotted(*) {
background-color: #194880;
border-color: #c5d1df;
}

:host(.secondary) ::slotted(*) {
background: #333;
}

:host(.cancel) ::slotted(*) {
background-color: #e51c26;
border-color: #f8c6c8;
}

:host(.link) ::slotted(*) {
color: #4b64ff;
border: none;
background: transparent;
display: flex;
align-items: flex-end;
padding: 0;
height: inherit;
}

:host(.disabled) ::slotted(*) {
cursor: not-allowed;
opacity: 0.5;
color: #222;
}
`;
}
Loading
Loading