show first entry
This commit is contained in:
11
frontend/src/app/app.component.html
Normal file
11
frontend/src/app/app.component.html
Normal file
@ -0,0 +1,11 @@
|
||||
<header
|
||||
class="flex flex-row justify-between items-center px-5 bg-gray-300 h-16 w-full drop-shadow-xl/5"
|
||||
>
|
||||
<a routerLink="/">
|
||||
<h1 class="text-2xl">My Blog</h1>
|
||||
</a>
|
||||
<nav></nav>
|
||||
</header>
|
||||
<main class="w-screen h-full px-5 sm:px-20 xl:px-96 pt-5">
|
||||
<router-outlet />
|
||||
</main>
|
29
frontend/src/app/app.component.spec.ts
Normal file
29
frontend/src/app/app.component.spec.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { AppComponent } from './app.component';
|
||||
|
||||
describe('AppComponent', () => {
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [AppComponent],
|
||||
}).compileComponents();
|
||||
});
|
||||
|
||||
it('should create the app', () => {
|
||||
const fixture = TestBed.createComponent(AppComponent);
|
||||
const app = fixture.componentInstance;
|
||||
expect(app).toBeTruthy();
|
||||
});
|
||||
|
||||
it(`should have the 'frontend' title`, () => {
|
||||
const fixture = TestBed.createComponent(AppComponent);
|
||||
const app = fixture.componentInstance;
|
||||
expect(app.title).toEqual('frontend');
|
||||
});
|
||||
|
||||
it('should render title', () => {
|
||||
const fixture = TestBed.createComponent(AppComponent);
|
||||
fixture.detectChanges();
|
||||
const compiled = fixture.nativeElement as HTMLElement;
|
||||
expect(compiled.querySelector('h1')?.textContent).toContain('Hello, frontend');
|
||||
});
|
||||
});
|
11
frontend/src/app/app.component.ts
Normal file
11
frontend/src/app/app.component.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { Component } from '@angular/core';
|
||||
import { RouterLink, RouterOutlet } from '@angular/router';
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
imports: [RouterOutlet, RouterLink],
|
||||
templateUrl: './app.component.html',
|
||||
})
|
||||
export class AppComponent {
|
||||
title = 'frontend';
|
||||
}
|
13
frontend/src/app/app.config.ts
Normal file
13
frontend/src/app/app.config.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
|
||||
import { provideRouter, withComponentInputBinding } from '@angular/router';
|
||||
|
||||
import { routes } from './app.routes';
|
||||
import { provideHttpClient } from '@angular/common/http';
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [
|
||||
provideZoneChangeDetection({ eventCoalescing: true }),
|
||||
provideRouter(routes, withComponentInputBinding()),
|
||||
provideHttpClient(),
|
||||
],
|
||||
};
|
8
frontend/src/app/app.routes.ts
Normal file
8
frontend/src/app/app.routes.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { Routes } from '@angular/router';
|
||||
import { HomeComponent } from './routes/home/home.component';
|
||||
import { PostComponent } from './routes/post/post.component';
|
||||
|
||||
export const routes: Routes = [
|
||||
{ path: '', component: HomeComponent },
|
||||
{ path: 'post', children: [{ path: ':id', component: PostComponent }] },
|
||||
];
|
@ -0,0 +1 @@
|
||||
<div [innerHTML]="innerHTML"></div>
|
@ -0,0 +1,23 @@
|
||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { MarkdownComponent } from './markdown.component';
|
||||
|
||||
describe('MarkdownComponent', () => {
|
||||
let component: MarkdownComponent;
|
||||
let fixture: ComponentFixture<MarkdownComponent>;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [MarkdownComponent]
|
||||
})
|
||||
.compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(MarkdownComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
22
frontend/src/app/components/markdown/markdown.component.ts
Normal file
22
frontend/src/app/components/markdown/markdown.component.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { Component, Input, OnChanges, OnInit } from '@angular/core';
|
||||
import DOMPurify from 'dompurify';
|
||||
import { marked } from 'marked';
|
||||
|
||||
@Component({
|
||||
selector: 'app-markdown',
|
||||
imports: [],
|
||||
standalone: true,
|
||||
templateUrl: './markdown.component.html',
|
||||
})
|
||||
export class MarkdownComponent implements OnChanges {
|
||||
@Input() markdown: string = '';
|
||||
innerHTML: string = '';
|
||||
|
||||
async parseMD() {
|
||||
this.innerHTML = DOMPurify.sanitize(await marked.parse(this.markdown));
|
||||
}
|
||||
|
||||
ngOnChanges(): void {
|
||||
this.parseMD();
|
||||
}
|
||||
}
|
11
frontend/src/app/routes/home/home.component.html
Normal file
11
frontend/src/app/routes/home/home.component.html
Normal file
@ -0,0 +1,11 @@
|
||||
<p>Welcome to by little blog:</p>
|
||||
<section class="grid gap-5 sm:grid-cols-2 2xl:grid-cols-3">
|
||||
<button
|
||||
*ngFor="let post of posts()"
|
||||
class="p-5 flex flex-col items-start rounded-s bg-white drop-shadow-md hover:drop-shadow-xl"
|
||||
[routerLink]="`/post/${post.id}`"
|
||||
>
|
||||
<h2 class="text-xl">{{ post.title }}</h2>
|
||||
<p><strong>TL;DR; </strong>{{ post.tldr }}</p>
|
||||
</button>
|
||||
</section>
|
23
frontend/src/app/routes/home/home.component.spec.ts
Normal file
23
frontend/src/app/routes/home/home.component.spec.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { HomeComponent } from './home.component';
|
||||
|
||||
describe('HomeComponent', () => {
|
||||
let component: HomeComponent;
|
||||
let fixture: ComponentFixture<HomeComponent>;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [HomeComponent]
|
||||
})
|
||||
.compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(HomeComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
19
frontend/src/app/routes/home/home.component.ts
Normal file
19
frontend/src/app/routes/home/home.component.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { Component, inject, OnInit, Signal } from '@angular/core';
|
||||
import { PostsService } from '../../shared/services/posts.service';
|
||||
import { NgForOf } from '@angular/common';
|
||||
import { RouterLink } from '@angular/router';
|
||||
import { Post } from '../../shared/services/interfaces/post';
|
||||
|
||||
@Component({
|
||||
selector: 'app-home',
|
||||
imports: [NgForOf, RouterLink],
|
||||
standalone: true,
|
||||
templateUrl: './home.component.html',
|
||||
})
|
||||
export class HomeComponent {
|
||||
private postsService = inject(PostsService);
|
||||
|
||||
get posts() {
|
||||
return this.postsService.getPosts();
|
||||
}
|
||||
}
|
7
frontend/src/app/routes/post/post.component.html
Normal file
7
frontend/src/app/routes/post/post.component.html
Normal file
@ -0,0 +1,7 @@
|
||||
<h2 class="text-3xl">{{ post()?.title }}</h2>
|
||||
<p class="mb-5 italic text-sm">TL;DR; {{ post()?.tldr }}</p>
|
||||
|
||||
<app-markdown
|
||||
class="todo"
|
||||
[markdown]="post()?.content || 'this post is empty'"
|
||||
/>
|
23
frontend/src/app/routes/post/post.component.spec.ts
Normal file
23
frontend/src/app/routes/post/post.component.spec.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { PostComponent } from './post.component';
|
||||
|
||||
describe('PostComponent', () => {
|
||||
let component: PostComponent;
|
||||
let fixture: ComponentFixture<PostComponent>;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [PostComponent]
|
||||
})
|
||||
.compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(PostComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
19
frontend/src/app/routes/post/post.component.ts
Normal file
19
frontend/src/app/routes/post/post.component.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { Component, inject, Input } from '@angular/core';
|
||||
import { PostsService } from '../../shared/services/posts.service';
|
||||
import { JsonPipe, NgIf } from '@angular/common';
|
||||
import { MarkdownComponent } from '../../components/markdown/markdown.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-post',
|
||||
imports: [MarkdownComponent],
|
||||
standalone: true,
|
||||
templateUrl: './post.component.html',
|
||||
})
|
||||
export class PostComponent {
|
||||
private posts = inject(PostsService);
|
||||
@Input() id!: string;
|
||||
|
||||
get post() {
|
||||
return this.posts.getPost(parseInt(this.id));
|
||||
}
|
||||
}
|
10
frontend/src/app/shared/services/interfaces/post.ts
Normal file
10
frontend/src/app/shared/services/interfaces/post.ts
Normal file
@ -0,0 +1,10 @@
|
||||
export interface Post {
|
||||
id: number;
|
||||
title: string;
|
||||
tldr: string;
|
||||
content: string;
|
||||
}
|
||||
|
||||
export interface Comment {
|
||||
content: string;
|
||||
}
|
16
frontend/src/app/shared/services/posts.service.spec.ts
Normal file
16
frontend/src/app/shared/services/posts.service.spec.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { PostsService } from './posts.service';
|
||||
|
||||
describe('PostsService', () => {
|
||||
let service: PostsService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(PostsService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
40
frontend/src/app/shared/services/posts.service.ts
Normal file
40
frontend/src/app/shared/services/posts.service.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import {
|
||||
computed,
|
||||
inject,
|
||||
Injectable,
|
||||
Signal,
|
||||
signal,
|
||||
WritableSignal,
|
||||
} from '@angular/core';
|
||||
import { Post } from './interfaces/post';
|
||||
import { environment } from '../../../environments/environment';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class PostsService {
|
||||
private http = inject(HttpClient);
|
||||
private posts: WritableSignal<Map<number, Post>> = signal(new Map());
|
||||
|
||||
constructor() {
|
||||
this.updatePosts(); // Pull posts immediately when the service is instantiated
|
||||
}
|
||||
|
||||
updatePosts() {
|
||||
this.http
|
||||
.get<Post[]>(`${environment.apiRoot}/posts`)
|
||||
.subscribe((res) => this.posts.set(new Map(res.map((p) => [p.id, p]))));
|
||||
}
|
||||
getPosts(): Signal<Post[]> {
|
||||
return computed(() => Array.from(this.posts().values()));
|
||||
}
|
||||
|
||||
getPost(id: number): Signal<Post | undefined> {
|
||||
console.log(typeof id);
|
||||
console.log(this.posts().has(id));
|
||||
console.log(this.posts().has(2));
|
||||
|
||||
return computed(() => this.posts().get(id));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user