Skip to content

Commit

Permalink
Merge pull request #32 from jorbush/localstorage-form
Browse files Browse the repository at this point in the history
save localstorage
  • Loading branch information
jorbush authored Sep 30, 2024
2 parents 12451c6 + 36887bf commit f4c8db3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion postrify-backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=update
app.jwtSecret=YourJwtSecretKey
app.jwtExpirationInMs=2592000000
app.jwtExpirationInMs=604800000
spring.jpa.open-in-view=false
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { Post } from '../../models/post.model';
import { Router } from '@angular/router';
import { PostService } from '../../services/post.service';
Expand All @@ -24,6 +24,7 @@ import { ToastService } from '../../services/toast.service';
minlength="3"
maxlength="30"
#titleInput="ngModel"
(ngModelChange)="saveFormState()"
/>
<div class="character-count">{{ post.title.length }}/30</div>
@if (titleInput.invalid && (titleInput.dirty || titleInput.touched)) {
Expand All @@ -47,6 +48,7 @@ import { ToastService } from '../../services/toast.service';
minlength="10"
maxlength="1000"
#contentInput="ngModel"
(ngModelChange)="saveFormState()"
></textarea>
<div class="character-count">{{ post.content.length }}/1000</div>
@if (
Expand Down Expand Up @@ -143,7 +145,7 @@ import { ToastService } from '../../services/toast.service';
}
`,
})
export class PostFormComponent {
export class PostFormComponent implements OnInit {
post: Post = {
title: '',
content: '',
Expand All @@ -155,11 +157,32 @@ export class PostFormComponent {
private router: Router,
) {}

ngOnInit(): void {
this.loadFormState();
}

saveFormState(): void {
localStorage.setItem('postFormData', JSON.stringify(this.post));
}

loadFormState(): void {
const savedData = localStorage.getItem('postFormData');
if (savedData) {
this.post = JSON.parse(savedData);
}
}

clearFormState(): void {
localStorage.removeItem('postFormData');
this.post = { title: '', content: '' };
}

onSubmit(): void {
this.postService.createPost(this.post).subscribe({
next: (data) => {
console.log('Post created successfully', data);
this.toastService.show('Post created successfully', 'success');
this.clearFormState();
this.router.navigate(['/']);
},
error: (error) => {
Expand Down

0 comments on commit f4c8db3

Please sign in to comment.