-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: barcode component * chore: remove program as only 1 quad is selected * chore: add back program + styling * chore: remove logs, add test
- Loading branch information
1 parent
ba183ac
commit 2cbd4d6
Showing
7 changed files
with
313 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/dgt-components/lib/components/barcode/barcode.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { BarcodeComponent } from './barcode.component'; | ||
|
||
describe('BarcodeComponent', () => { | ||
|
||
let component: BarcodeComponent; | ||
const tag = 'barcode-component'; | ||
|
||
beforeEach(() => { | ||
|
||
component = window.document.createElement(tag) as BarcodeComponent; | ||
|
||
}); | ||
|
||
afterEach(() => { | ||
|
||
document.getElementsByTagName('html')[0].innerHTML = ''; | ||
|
||
}); | ||
|
||
it('should be correctly instantiated', () => { | ||
|
||
expect(component).toBeTruthy(); | ||
|
||
}); | ||
|
||
}); |
103 changes: 103 additions & 0 deletions
103
packages/dgt-components/lib/components/barcode/barcode.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* eslint-disable no-console -- is a web component */ | ||
import { Store } from 'n3'; | ||
import { css, CSSResult, html, internalProperty, property, PropertyValues, query, TemplateResult, unsafeCSS } from 'lit-element'; | ||
import { ComponentResponseEvent } from '@digita-ai/semcom-sdk'; | ||
import { Theme } from '@digita-ai/dgt-theme'; | ||
import JsBarcode from 'jsbarcode'; | ||
import { BaseComponent } from '../base/base.component'; | ||
|
||
export class BarcodeComponent extends BaseComponent { | ||
|
||
@internalProperty() cardNumber?: string; | ||
@internalProperty() program?: string; | ||
@property({ type: Boolean }) hideProgram = false; | ||
|
||
@query('#barcode') barcodeSvg?: HTMLOrSVGElement; | ||
|
||
/** | ||
* Handles a response event. | ||
* Expects an event containing a detail object that contains a data object containing quads. Will take the first quad containing a membershipNumber it finds. | ||
* Make sure to filter the quads you send it if you want a specific barcode. | ||
* | ||
* @param event The response event to handle. | ||
*/ | ||
handleResponse(event: ComponentResponseEvent): void { | ||
|
||
console.log('handleResponse'); | ||
|
||
if (!event || !event.detail || !event.detail.data) { | ||
|
||
throw new Error('Arguments event, event.detail, and event.detail.data should be set.'); | ||
|
||
} | ||
|
||
const store = new Store(event.detail.data); | ||
|
||
const quad = store.getQuads(undefined, 'http://schema.org/membershipNumber', undefined, undefined)[0]; | ||
|
||
const program = store.getQuads(quad.subject, 'http://schema.org/programName', undefined, undefined)[0]; | ||
|
||
this.cardNumber = quad.object.value; | ||
this.program = program.object.value; | ||
|
||
} | ||
|
||
updated(changed: PropertyValues): void { | ||
|
||
if (changed.has('entry') && this.entry) this.readData(this.entry); | ||
|
||
if (this.barcodeSvg) { | ||
|
||
JsBarcode(this.barcodeSvg, this.cardNumber, { | ||
background: 'none', | ||
}); | ||
|
||
} | ||
|
||
super.updated(changed); | ||
|
||
} | ||
|
||
render(): TemplateResult { | ||
|
||
return html` | ||
${this.program && !this.hideProgram ? html`<p id="program">${this.program}</p>` : ''} | ||
<svg id="barcode"></svg> | ||
`; | ||
|
||
} | ||
|
||
static get styles(): CSSResult[] { | ||
|
||
return [ | ||
unsafeCSS(Theme), | ||
css` | ||
:host { | ||
display: flex; | ||
flex-direction: column; | ||
background-color: var(--colors-foreground-inverse); | ||
padding: var(--gap-normal) 0; | ||
} | ||
:host > * { | ||
align-self: center; | ||
} | ||
p { | ||
margin: 0; | ||
} | ||
#barcode { | ||
width: 200px; | ||
height: 110px; | ||
} | ||
`, | ||
]; | ||
|
||
} | ||
|
||
} | ||
|
||
export default BarcodeComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.