Skip to content

Commit

Permalink
Saving data to localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepViolet777 committed Oct 25, 2022
1 parent 89f12d4 commit be0b184
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 11 deletions.
22 changes: 20 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"deploy": "node ./node_modules/vue-gh-pages/index.js"
},
"dependencies": {
"@hennge/vue3-pagination": "^1.0.17",
"@vuelidate/core": "^2.0.0-alpha.44",
"@vuelidate/validators": "^2.0.0-alpha.31",
"axios": "^0.27.2",
Expand Down
42 changes: 34 additions & 8 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<div class="app light-blue lighten-5">
<post-form @create="createPost" />
<post-list :posts="posts" @delete="deletePost" />
<v-pagination v-model="page" :pages="10" :range-size="1" active-color="#DCEDFF" @update:modelValue="updateHandler" />
</div>
</template>

Expand All @@ -13,34 +14,59 @@ import axios from 'axios/dist/axios.min';
export default {
components: {
PostForm,
PostList
PostList,
},
data() {
return {
posts: [],
page: 10
}
},
methods: {
createPost(post) {
this.posts.unshift(post);
this.setLocalPosts();
},
deletePost(post) {
this.posts = this.posts.filter(p => p.id !== post.id);
this.setLocalPosts();
},
async getPosts() {
try {
const response = await axios.get('https://jsonplaceholder.typicode.com/posts?_limit=10');
const response = await axios.get('https://jsonplaceholder.typicode.com/posts');
this.posts = response.data;
} catch (error) {
console.log(error)
}
}
this.setLocalPosts();
},
setLocalPosts() {
localStorage.setItem('posts', JSON.stringify(this.posts));
},
getLocalPosts(){
this.posts = JSON.parse(localStorage.getItem('posts'));
}
},
mounted() {
this.getPosts();
}
if (localStorage.getItem('posts')!= undefined){
this.getLocalPosts();
} else{
this.getPosts();
}
},
// watch: {
// posts(newPost) {
// localStorage.posts = newPost;
// }
// }
}
</script>

Expand Down Expand Up @@ -68,6 +94,7 @@ body {
min-height: 100vh;
}
.btn {
align-self: flex-end;
margin-top: 15px;
Expand All @@ -78,7 +105,6 @@ body {
font-weight: 500;
color: #fff;
cursor: pointer;
}
}
</style>
6 changes: 5 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import 'materialize-css/dist/js/materialize.min'




const app = createApp(App)

app.use(store).use(router).mount('#app')
app
.use(store)
.use(router)
.mount('#app')



0 comments on commit be0b184

Please sign in to comment.