Styling comments list, add date format pipe.
This commit is contained in:
parent
0bc434251e
commit
0699c704de
15
src/app/pipe/dateTimeFormat.pipe.ts
Normal file
15
src/app/pipe/dateTimeFormat.pipe.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import { inject, Pipe, PipeTransform } from '@angular/core';
|
||||||
|
import { DateService } from '../service/date.service';
|
||||||
|
|
||||||
|
@Pipe({
|
||||||
|
name: 'dateTimeFormat'
|
||||||
|
})
|
||||||
|
export class DateTimeFormatPipe implements PipeTransform {
|
||||||
|
|
||||||
|
private readonly dateService = inject(DateService);
|
||||||
|
|
||||||
|
public transform(raw: string): string {
|
||||||
|
return this.dateService.format(raw);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,49 @@
|
|||||||
|
#add-comment-form {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: flex-start;
|
||||||
|
row-gap: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#add-comment-form > textarea {
|
||||||
|
width: 80ch;
|
||||||
|
height: 100px;
|
||||||
|
padding: 10px;
|
||||||
|
font-size: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#comments {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
row-gap: 10px;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#comments > li:not(:last-child) {
|
||||||
|
border-bottom: 1px solid var(--off-white-5);
|
||||||
|
}
|
||||||
|
|
||||||
|
.comment {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
.comment-username-time {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
column-gap: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.comment-username {
|
||||||
|
font-weight: bold;
|
||||||
|
font-size: 1.25em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.comment-time {
|
||||||
|
opacity: 0.75;
|
||||||
|
}
|
||||||
|
|
||||||
|
.comment-text {
|
||||||
|
padding-bottom: 10px;
|
||||||
|
}
|
||||||
@ -3,10 +3,8 @@
|
|||||||
@if (isLoggedIn()) {
|
@if (isLoggedIn()) {
|
||||||
<div id="add-comment-container">
|
<div id="add-comment-container">
|
||||||
<h3>Add Comment</h3>
|
<h3>Add Comment</h3>
|
||||||
<form [formGroup]="addCommentForm" (ngSubmit)="onCommentSubmit()">
|
<form id="add-comment-form" [formGroup]="addCommentForm" (ngSubmit)="onCommentSubmit()">
|
||||||
<label for="comment">Comment</label>
|
<textarea id="comment" formControlName="comment" type="text"></textarea>
|
||||||
<input id="comment" formControlName="comment" type="text" />
|
|
||||||
|
|
||||||
<button type="submit" [disabled]="!addCommentForm.valid">Post Comment</button>
|
<button type="submit" [disabled]="!addCommentForm.valid">Post Comment</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
@ -19,18 +17,23 @@
|
|||||||
} @else if (commentsQuery.isError()) {
|
} @else if (commentsQuery.isError()) {
|
||||||
<p>There was an error loading the comments.</p>
|
<p>There was an error loading the comments.</p>
|
||||||
} @else {
|
} @else {
|
||||||
<ul>
|
<ul id="comments">
|
||||||
@if (addCommentMutation.isPending()) {
|
@if (addCommentMutation.isPending()) {
|
||||||
<li style="opacity: 0.5">
|
<li class="comment" style="opacity: 0.5">
|
||||||
<p>{{ username() }}</p>
|
<div class="comment-username-time">
|
||||||
|
<span class="comment-username">{{ username() }}</span>
|
||||||
|
</div>
|
||||||
<div>{{ addCommentMutation.variables() }}</div>
|
<div>{{ addCommentMutation.variables() }}</div>
|
||||||
</li>
|
</li>
|
||||||
}
|
}
|
||||||
@for (recipeComments of commentsQuery.data()?.pages; track $index) {
|
@for (recipeComments of commentsQuery.data()?.pages; track $index) {
|
||||||
@for (recipeComment of recipeComments.content; track recipeComment.id) {
|
@for (recipeComment of recipeComments.content; track recipeComment.id) {
|
||||||
<li>
|
<li class="comment">
|
||||||
<p>{{ recipeComment.owner.username }} at {{ recipeComment.created }}</p>
|
<div class="comment-username-time">
|
||||||
<div [innerHTML]="recipeComment.text"></div>
|
<span class="comment-username">{{ recipeComment.owner.username }}</span>
|
||||||
|
<span class="comment-time">{{ recipeComment.created | dateTimeFormat }}</span>
|
||||||
|
</div>
|
||||||
|
<div class="comment-text" [innerHTML]="recipeComment.text"></div>
|
||||||
</li>
|
</li>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,10 +4,11 @@ import { RecipeService } from '../service/recipe.service';
|
|||||||
import { injectInfiniteQuery, injectMutation } from '@tanstack/angular-query-experimental';
|
import { injectInfiniteQuery, injectMutation } from '@tanstack/angular-query-experimental';
|
||||||
import { AuthService } from '../service/auth.service';
|
import { AuthService } from '../service/auth.service';
|
||||||
import { RecipeComments } from '../model/RecipeComment.model';
|
import { RecipeComments } from '../model/RecipeComment.model';
|
||||||
|
import { DateTimeFormatPipe } from '../pipe/dateTimeFormat.pipe';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-comments-list',
|
selector: 'app-comments-list',
|
||||||
imports: [ReactiveFormsModule],
|
imports: [ReactiveFormsModule, DateTimeFormatPipe],
|
||||||
templateUrl: './recipe-comments-list.component.html',
|
templateUrl: './recipe-comments-list.component.html',
|
||||||
styleUrl: './recipe-comments-list.component.css',
|
styleUrl: './recipe-comments-list.component.css',
|
||||||
})
|
})
|
||||||
@ -29,7 +30,7 @@ export class RecipeCommentsList {
|
|||||||
queryFn: ({ pageParam }) => {
|
queryFn: ({ pageParam }) => {
|
||||||
return this.recipeService.getComments(this.recipeUsername(), this.recipeSlug(), {
|
return this.recipeService.getComments(this.recipeUsername(), this.recipeSlug(), {
|
||||||
page: pageParam,
|
page: pageParam,
|
||||||
size: 1,
|
size: 10,
|
||||||
sort: [
|
sort: [
|
||||||
{
|
{
|
||||||
property: 'created',
|
property: 'created',
|
||||||
@ -46,8 +47,7 @@ export class RecipeCommentsList {
|
|||||||
|
|
||||||
protected readonly addCommentMutation = injectMutation(() => ({
|
protected readonly addCommentMutation = injectMutation(() => ({
|
||||||
mutationFn: (commentText: string) =>
|
mutationFn: (commentText: string) =>
|
||||||
this.recipeService.addComment(this.recipeUsername(), this.recipeSlug(), commentText),
|
this.recipeService.addComment(this.recipeUsername(), this.recipeSlug(), commentText)
|
||||||
onSuccess: () => this.commentsQuery.fetchNextPage(),
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
protected onCommentSubmit() {
|
protected onCommentSubmit() {
|
||||||
|
|||||||
17
src/app/service/date.service.ts
Normal file
17
src/app/service/date.service.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root'
|
||||||
|
})
|
||||||
|
export class DateService {
|
||||||
|
|
||||||
|
private readonly dateTimeFormat = Intl.DateTimeFormat('en-US', {
|
||||||
|
dateStyle: 'long',
|
||||||
|
timeStyle: 'short'
|
||||||
|
});
|
||||||
|
|
||||||
|
public format(raw: string): string {
|
||||||
|
return this.dateTimeFormat.format(new Date(raw));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,6 +1,7 @@
|
|||||||
@import url('https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&display=swap');
|
@import url('https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&display=swap');
|
||||||
|
|
||||||
:root {
|
:root {
|
||||||
|
--primary-font-family: 'Inter';
|
||||||
--primary-white: #ffffff;
|
--primary-white: #ffffff;
|
||||||
--primary-red: #91351d;
|
--primary-red: #91351d;
|
||||||
--primary-yellow: #ffb61d;
|
--primary-yellow: #ffb61d;
|
||||||
@ -15,7 +16,8 @@
|
|||||||
|
|
||||||
body {
|
body {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
font-family: 'Inter', sans-serif;
|
font-family: var(--primary-font-family), sans-serif;
|
||||||
|
font-size: 16px;
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
color: var(--primary-black);
|
color: var(--primary-black);
|
||||||
}
|
}
|
||||||
@ -51,3 +53,7 @@ fa-icon {
|
|||||||
.fa-star {
|
.fa-star {
|
||||||
color: var(--primary-yellow);
|
color: var(--primary-yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
textarea {
|
||||||
|
font-family: var(--primary-font-family), sans-serif;
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user