Skip to content

Commit

Permalink
feat(seeding): add seeding page with adding new seeds to barn
Browse files Browse the repository at this point in the history
artaommahe committed Jun 4, 2024

Verified

This commit was signed with the committer’s verified signature. The key has expired.
danielleadams Danielle Adams
1 parent 987b944 commit c259bee
Showing 6 changed files with 99 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -6,10 +6,9 @@ import { RouterOutlet } from '@angular/router';
standalone: true,
imports: [RouterOutlet],
template: `
<div class="p-5">
<button>smth</button>
<main class="h-dvh p-5">
<router-outlet></router-outlet>
</div>
</main>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
5 changes: 4 additions & 1 deletion src/app/app.routes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { Routes } from '@angular/router';

export const routes: Routes = [];
export const routes: Routes = [
{ path: 'seeding', loadChildren: () => import('./seeding/seeding.routes').then(m => m.seedingRoutes) },
{ path: '', redirectTo: '/seeding', pathMatch: 'full' },
];
29 changes: 29 additions & 0 deletions src/app/barn/barn.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Injectable, signal } from '@angular/core';

interface Barn {
seeds: Record<string, Seed>;
}

interface Seed {
name: string;
count?: number;
}

@Injectable({ providedIn: 'root' })
export class BarnService {
private barn = signal<Barn>({ seeds: {} });

addSeed(name: string) {
this.barn.update(barn => {
const seed = barn.seeds[name];

return {
...barn,
seeds: {
...barn.seeds,
[name]: { ...seed, count: (seed?.count || 0) + 1 },
},
};
});
}
}
40 changes: 40 additions & 0 deletions src/app/seeding/new-seed/new-seed.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
import { BarnService } from '../../barn/barn.service';

@Component({
selector: 'app-new-seed',
template: `
<div class="flex flex-col gap-2">
<input
type="text"
class="p-2 border border-gray-300 text-center"
placeholder="New seed"
[value]="newSeed()"
(input)="updateSeed($event)"
(keydown.enter)="saveSeed()"
/>
</div>
`,
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class NewSeedComponent {
newSeed = signal('');

private barn = inject(BarnService);

updateSeed(event: Event) {
this.newSeed.set((event.target as HTMLInputElement).value);
}

saveSeed() {
const seed = this.newSeed();

if (!seed) {
return;
}

this.barn.addSeed(seed);
this.newSeed.set('');
}
}
16 changes: 16 additions & 0 deletions src/app/seeding/seeding-page.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { NewSeedComponent } from './new-seed/new-seed.component';

@Component({
selector: 'app-seeding-page',
template: `
<div class="flex flex-col gap-2">
<app-new-seed></app-new-seed>
<!-- <app-seed-list></app-seed-list> -->
</div>
`,
imports: [NewSeedComponent],
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SeedingPageComponent {}
8 changes: 8 additions & 0 deletions src/app/seeding/seeding.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Routes } from '@angular/router';

export const seedingRoutes: Routes = [
{
path: '',
loadComponent: () => import('./seeding-page.component').then(m => m.SeedingPageComponent),
},
];

0 comments on commit c259bee

Please sign in to comment.