-
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(cards): add add-cards button and dialog
- Loading branch information
1 parent
8811615
commit 39a3822
Showing
5 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
30 changes: 30 additions & 0 deletions
30
src/app/cards/add-cards-button/add-cards-button.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,30 @@ | ||
import { ChangeDetectionStrategy, Component, signal } from '@angular/core'; | ||
|
||
import { DialogComponent } from '../../ui/dialog/dialog.component'; | ||
import { IconComponent } from '../../ui/icon/icon'; | ||
import { AddCardsComponent } from '../add-cards/add-cards.component'; | ||
|
||
@Component({ | ||
selector: 'app-add-cards-button', | ||
template: ` | ||
<button | ||
class="fixed bottom-16 right-6 flex items-center justify-center rounded-full bg-action-primary shadow-md" | ||
title="Add" | ||
(click)="showAddCardsDialog.set(true)" | ||
> | ||
<app-icon class="size-10 text-primary" name="plusInCircle" /> | ||
</button> | ||
<app-dialog [open]="showAddCardsDialog()" (close)="showAddCardsDialog.set(false)"> | ||
<ng-template> | ||
<app-add-cards (close)="showAddCardsDialog.set(false)" /> | ||
</ng-template> | ||
</app-dialog> | ||
`, | ||
imports: [IconComponent, AddCardsComponent, DialogComponent], | ||
standalone: true, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class AddCardsButtonComponent { | ||
showAddCardsDialog = signal(false); | ||
} |
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,71 @@ | ||
import { ChangeDetectionStrategy, Component, inject, output, signal } from '@angular/core'; | ||
import { parse } from 'csv-parse/browser/esm/sync'; | ||
|
||
import { BarnService } from '../../barn/barn.service'; | ||
import { ButtonDirective } from '../../ui/button/button'; | ||
import { InputDirective } from '../../ui/input/input'; | ||
|
||
@Component({ | ||
selector: 'app-add-cards', | ||
template: ` | ||
<div class="flex h-full flex-col gap-8"> | ||
<textarea | ||
class="grow" | ||
appInput | ||
placeholder="term;fullTerm;definition (one per line)" | ||
[value]="newCards()" | ||
(input)="updateNewCards($event)" | ||
></textarea> | ||
@if (parseError()) { | ||
<div class="shrink-0 text-semantic-danger">{{ parseError() }}</div> | ||
} | ||
<div class="mt-auto flex shrink-0 justify-end gap-4"> | ||
<button appButton (click)="close.emit()">Cancel</button> | ||
<button appButton appButtonType="primary" (click)="add()">Add</button> | ||
</div> | ||
</div> | ||
`, | ||
imports: [ButtonDirective, InputDirective], | ||
standalone: true, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class AddCardsComponent { | ||
private barnService = inject(BarnService); | ||
|
||
close = output(); | ||
|
||
newCards = signal(''); | ||
parseError = signal<string | null>(null); | ||
|
||
updateNewCards(event: Event) { | ||
this.newCards.set((event.target as HTMLTextAreaElement).value); | ||
} | ||
|
||
add() { | ||
this.parseError.set(null); | ||
|
||
try { | ||
const newCards = ( | ||
parse(this.newCards(), { | ||
delimiter: ';', | ||
columns: ['term', 'fullTerm', 'definition'], | ||
}) as ParsedCard[] | ||
).filter(card => !!card.term); | ||
|
||
this.barnService.addCards(newCards); | ||
} catch (error) { | ||
this.parseError.set(String(error)); | ||
return; | ||
} | ||
|
||
this.close.emit(); | ||
} | ||
} | ||
|
||
interface ParsedCard { | ||
term: string; | ||
fullTerm: string; | ||
definition: string; | ||
} |
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