Skip to content

Commit c31fb67

Browse files
Part 5.1 (#43)
* changed to rest api wip * dates etc added
1 parent db5cf48 commit c31fb67

File tree

13 files changed

+71
-42
lines changed

13 files changed

+71
-42
lines changed

angular.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"browser": "src/main.ts",
2323
"allowedCommonJsDependencies": [
2424
"highlight.js",
25-
"quill-delta",
2625
"whatwg-url",
2726
"@supabase/node-fetch"
2827
],

reset-password.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
* USAGE (bash):
77
* export SUPABASE_URL="https://aqdbdmepncxxuanlymwr.supabase.co"
88
* export SERVICE_ROLE_KEY="YOUR_SERVICE_ROLE_KEY_HERE"
9-
* npx ts-node reset-password.ts \
10-
* a3b3cf93-6956-41c7-bec0-1qwqwd42f26 'N3w-Pa$$w0rd!'
9+
* npx tsx reset-password.ts ID 'lSupa'
1110
*********************************************************************/
1211

1312
import { createClient } from '@supabase/supabase-js';
Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
@defer {
2-
<form class="p-12 flex flex-col gap-2" [formGroup]="blogForm" (ngSubmit)="onSubmit()">
3-
<div class="w-full flex">
4-
<input type="text" formControlName="title" placeholder="Title" class="input input-bordered w-full" required/>
5-
</div>
6-
@if (blogForm.controls.title.invalid && (blogForm.controls.title.dirty || blogForm.controls.title.touched)) {
7-
@if (blogForm.controls.title.errors?.['required']) {
8-
<div>Title is required.</div>
9-
}
10-
}
11-
<<<<<<< HEAD
12-
<button class="btn btn--secondary" type="button" (click)="insertImage()">Insert Image</button>
13-
<quill-editor #quill formControlName="content" class="h-screen w-full"></quill-editor>
14-
<div class="mt-12 btn--group">
15-
<button class="btn btn--primary" type="submit">Submit</button>
16-
<button class="btn btn--secondary" type="button" (click)="onSubmit(true)">Save as a draft</button>
17-
</div>
18-
=======
19-
20-
21-
<button type="submit" [disabled]="blogForm.invalid">Submit</button>
22-
>>>>>>> ab739b9 (nothing special)
23-
</form>
2+
<form class="p-12 flex flex-col gap-2" [formGroup]="blogForm" (ngSubmit)="onSubmit()">
3+
<div class="w-full flex">
4+
<input type="text" formControlName="title" placeholder="Title" class="input input-bordered w-full" required/>
5+
</div>
6+
@if (blogForm.controls.title.invalid && (blogForm.controls.title.dirty || blogForm.controls.title.touched)) {
7+
@if (blogForm.controls.title.errors?.['required']) {
8+
<div>Title is required.</div>
9+
}
10+
}
11+
<button class="btn btn--secondary" type="button" (click)="insertImage()">Insert Image</button>
12+
<quill-editor #quill formControlName="content" class="h-screen w-full"></quill-editor>
13+
<div class="mt-12 btn--group">
14+
<button class="btn btn--primary" type="submit">Submit</button>
15+
<button class="btn btn--secondary" type="button" (click)="onSubmit(true)">Save as a draft</button>
16+
</div>
17+
<button type="submit" [disabled]="blogForm.invalid">Submit</button>
18+
</form>
2419
}

src/app/admin/_components/add-post/add-post.component.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ export class AddPostComponent implements OnInit {
6262
validators: [Validators.required],
6363
nonNullable: true,
6464
}),
65-
date: new FormControl<Date | null>(null),
65+
created_at: new FormControl<Date | null>(null),
6666
description: new FormControl<string | null>(null),
67-
isDraft: new FormControl(false, { nonNullable: true }),
67+
is_draft: new FormControl(false, { nonNullable: true }),
6868
});
6969
range: Range | null = null;
7070

@@ -80,6 +80,7 @@ export class AddPostComponent implements OnInit {
8080
this.apiService.getPostById(this.postId).subscribe((post) => {
8181
if (post) {
8282
this.blogForm.patchValue(post);
83+
console.log(this.blogForm.value);
8384
}
8485
});
8586
}
@@ -95,9 +96,9 @@ export class AddPostComponent implements OnInit {
9596
const rawContent = this.blogForm.controls.content.value as string;
9697
const cleanedContent = rawContent.replace(/(&nbsp;|\u00A0)/g, ' ');
9798
this.blogForm.controls.content.setValue(cleanedContent);
98-
this.blogForm.controls.isDraft.setValue(isDraft);
99-
if (!this.blogForm.controls.date.value) {
100-
this.blogForm.controls.date.setValue(null);
99+
this.blogForm.controls.is_draft.setValue(isDraft);
100+
if (!this.blogForm.controls.created_at.value) {
101+
this.blogForm.controls.created_at.setValue(null);
101102
}
102103
if (this.postId) {
103104
this.apiService.updatePost(this.postId, this.blogForm.value as Post);

src/app/admin/_models/post-from.inteface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { SafeHtml } from '@angular/platform-browser';
44
export interface PostForm {
55
title: FormControl<string>;
66
content: FormControl<string | SafeHtml>;
7-
isDraft: FormControl<boolean>;
8-
date: FormControl<any>;
7+
is_draft: FormControl<boolean>;
8+
created_at: FormControl<Date | null>;
99
description?: FormControl<string | null>;
1010
}

src/app/admin/_services/admin-api.service.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@ import { inject, Injectable } from '@angular/core';
33
import { map, Observable } from 'rxjs';
44
import { Post } from '../../supabase-types';
55
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
6+
import { SupabaseService } from '../../services/supabase.service';
67

78
@Injectable()
89
export class AdminApiService {
910
http = inject(HttpClient);
11+
supabaseService = inject(SupabaseService);
1012
private readonly baseUrl =
1113
'https://aqdbdmepncxxuanlymwr.supabase.co/rest/v1/';
1214
private readonly apiKey =
1315
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImFxZGJkbWVwbmN4eHVhbmx5bXdyIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDUwNTA0MjYsImV4cCI6MjA2MDYyNjQyNn0.RNtZZ4Of4LIP3XuS9lumHYdjRLVUGXARtAxaTJmF7lc';
1416

15-
addPost(post: Post): void {}
17+
async addPost(post: Post): Promise<void> {
18+
const { error } = await this.supabaseService.getClient
19+
.from('posts')
20+
.insert({ ...post });
21+
}
1622

1723
getPostById(id: string): Observable<Post> {
1824
const selectQuery = `
@@ -39,7 +45,13 @@ export class AdminApiService {
3945
.pipe(map((results) => results[0] ?? null));
4046
}
4147

42-
updatePost(id: string, post: Post): Promise<void> {
43-
return Promise.resolve();
48+
async updatePost(id: string, post: Post): Promise<void> {
49+
await this.supabaseService.getClient
50+
.from('posts')
51+
.update({ ...post })
52+
.eq('id', id)
53+
.then((x) => {
54+
console.log(x);
55+
});
4456
}
4557
}

src/app/reader/_components/main-page/post/post.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<div class="flex justify-center -mx-2 xl:-mx-48">
44
<img
55
class="w-full h-80 object-cover mt-10 rounded-2xl"
6-
src="{{ date }}-post.webp"
6+
src="{{ date() }}-post.webp"
77
alt="{{ post()?.title }}"
88
/>
99
</div>

src/app/reader/_components/main-page/post/post.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class PostComponent implements OnInit {
3535
postStore = inject(PostStore);
3636

3737
post: Signal<Post | null> = this.postStore.post;
38-
date: string = '';
38+
date: Signal<string | null> = this.postStore.date;
3939

4040
private dialogService = inject(DynamicDialogService);
4141
private viewContainerRef = inject(ViewContainerRef);

src/app/reader/_components/main-page/post/post.store.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import { inject } from '@angular/core';
2-
import { patchState, signalStore, withMethods, withState } from '@ngrx/signals';
2+
import { computed } from '@angular/core';
3+
import {
4+
patchState,
5+
signalStore,
6+
withMethods,
7+
withState,
8+
withComputed,
9+
} from '@ngrx/signals';
310
import { rxMethod } from '@ngrx/signals/rxjs-interop';
411
import { pipe, switchMap, tap } from 'rxjs';
512
import { tapResponse } from '@ngrx/operators';
613

714
import { Post } from '../../../../types/supabase';
815
import { ReaderApiService } from '../../../_services/reader-api.service';
16+
import { formatDateToDDMMYYYY } from '../../../../utlis/date-utils';
917

1018
type PostState = {
1119
post: Post | null;
20+
date: string | null;
1221
loading: boolean;
1322
error: string | null;
1423
};
@@ -17,12 +26,15 @@ const initialState: PostState = {
1726
post: null,
1827
loading: false,
1928
error: null,
29+
date: null,
2030
};
2131

2232
export const PostStore = signalStore(
2333
{ providedIn: 'root' },
2434
withState(initialState),
25-
35+
withComputed((store) => ({
36+
date: computed(() => formatDateToDDMMYYYY(store.post()?.created_at)),
37+
})),
2638
withMethods((store, postService = inject(ReaderApiService)) => ({
2739
getPost: rxMethod<string>(
2840
pipe(

src/app/shared/about-me/about-me.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ <h2 class="text-sm mt-8 w-full md:w-1/2">
1313
new challenges.
1414
</h2>
1515
</div>
16-
<img class="object-cover object-top hidden sm:block" ngSrc="my/photo.webp" priority width="250" height="100">
16+
<img class="object-cover object-top hidden sm:block" ngSrc="my/photo.webp" priority width="250" height="250">
1717
</div>

0 commit comments

Comments
 (0)