-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ruth.demo add crud #294
base: main
Are you sure you want to change the base?
Ruth.demo add crud #294
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,20 @@ | |
<v-container> | ||
<v-row> | ||
<v-col cols="12"> | ||
<v-text-field dense label="Title*" required></v-text-field> | ||
<v-text-field | ||
v-model="title" | ||
dense | ||
label="Title *" | ||
required | ||
></v-text-field> | ||
</v-col> | ||
<v-col cols="12"> | ||
<v-textarea dense label="Content*" required></v-textarea> | ||
<v-textarea | ||
v-model="content" | ||
dense | ||
label="Content *" | ||
required | ||
></v-textarea> | ||
</v-col> | ||
</v-row> | ||
</v-container> | ||
|
@@ -28,7 +38,7 @@ | |
<v-btn color="red darken-1" text @click="dialog = false"> | ||
Discard | ||
</v-btn> | ||
<v-btn color="green darken-1" text @click="dialog = false"> | ||
<v-btn color="green darken-1" text @click="onSubmit"> | ||
Add Blog | ||
</v-btn> | ||
</v-card-actions> | ||
|
@@ -39,12 +49,36 @@ | |
</template> | ||
|
||
<script> | ||
import { mapActions } from 'vuex' | ||
|
||
export default { | ||
name: 'RuthAddBlog', | ||
data() { | ||
return { | ||
dialog: false, | ||
isLoggedIn: false, | ||
newBlog: {}, | ||
title: '', | ||
content: '', | ||
} | ||
}, | ||
created() { | ||
this.isLoggedIn = this.$auth.$state.user | ||
}, | ||
methods: { | ||
...mapActions('ruth', ['addBlog']), | ||
onSubmit(e) { | ||
e.preventDefault() | ||
this.newBlog = { | ||
title: this.title, | ||
content: this.content, | ||
description: 'description', | ||
} | ||
Comment on lines
+72
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we apply the above recommendation, we won't need this. |
||
this.$store.dispatch('ruth/addBlog', this.newBlog) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
this.blogTitle = '' | ||
this.blogContent = '' | ||
Comment on lines
+78
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clearing the properties can be done by separate method. |
||
this.dialog = false | ||
}, | ||
}, | ||
} | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,102 @@ | ||
<template> | ||
<div> | ||
<v-card class="my-4 mx-12" outlined> | ||
<v-list-item three-line> | ||
<v-list-item-content> | ||
<nuxt-link | ||
style="text-decoration: none; color: inherit" | ||
:to="`/ruth/${blog._id} `" | ||
> | ||
<v-list-item-title class="text-h5 mt-1" | ||
>{{ blog.title }} <small>{{ blog._id }}</small> | ||
</v-list-item-title> | ||
</nuxt-link> | ||
<v-list-item-subtitle>{{ blog.details }}</v-list-item-subtitle> | ||
</v-list-item-content> | ||
<v-card-actions> | ||
<v-btn class="ma-2" outlined x-small fab color="green"> | ||
<v-icon>mdi-pencil</v-icon> | ||
</v-btn> | ||
<v-btn class="ma-2" outlined x-small fab color="red"> | ||
<v-icon>mdi-delete</v-icon> | ||
</v-btn> | ||
</v-card-actions> | ||
</v-list-item> | ||
<v-list-item> | ||
<v-list-item-subtitle>By {{ blog.author }}</v-list-item-subtitle> | ||
</v-list-item> | ||
<v-card-actions> </v-card-actions> | ||
<v-card class="my-4 mx-6" outlined width="330"> | ||
<v-img | ||
class="white--text align-end" | ||
height="200px" | ||
src="https://source.unsplash.com/random/300x200?sig=${Math.random()}" | ||
> | ||
<v-card-title>{{ blog.title }} </v-card-title> | ||
</v-img> | ||
|
||
<v-card-subtitle class="pb-4"> | ||
By {{ blog.authorUserId.fullName }} | ||
</v-card-subtitle> | ||
|
||
<v-card-text class="text--primary"> | ||
<div>{{ blog.content }}</div> | ||
</v-card-text> | ||
|
||
<v-card-actions> | ||
<nuxt-link | ||
style="text-decoration: none; color: inherit" | ||
:to="`/ruth/${blog._id} `" | ||
> | ||
<v-btn color="green" text> View Blog </v-btn> | ||
</nuxt-link> | ||
<v-spacer></v-spacer> | ||
|
||
<v-dialog v-model="dialog" persistent max-width="600px"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's separate UpdateBlog to its own component |
||
<template #activator="{ on, attrs }"> | ||
<v-btn | ||
class="ma-2" | ||
icon | ||
small | ||
v-bind="attrs" | ||
:disabled="!isAuthor" | ||
color="green" | ||
v-on="on" | ||
@click="editArea(blog)" | ||
> | ||
<v-icon>mdi-pencil</v-icon> | ||
</v-btn> | ||
</template> | ||
<v-card> | ||
<v-card-title> | ||
<span class="text-h5">Edit Blog</span> | ||
</v-card-title> | ||
<v-card-text> | ||
<v-container> | ||
<v-row> | ||
<v-col cols="12"> | ||
<v-text-field | ||
v-model="title" | ||
dense | ||
label="Title*" | ||
required | ||
></v-text-field> | ||
</v-col> | ||
<v-col cols="12"> | ||
<v-textarea | ||
v-model="content" | ||
dense | ||
label="Content*" | ||
required | ||
></v-textarea> | ||
</v-col> | ||
</v-row> | ||
</v-container> | ||
</v-card-text> | ||
<v-card-actions class="mx-4 mt-4"> | ||
<v-spacer></v-spacer> | ||
<v-btn color="red darken-1" text @click="dialog = false"> | ||
Discard | ||
</v-btn> | ||
<v-btn color="green darken-1" text @click="updBlog(blog._id)"> | ||
Update Blog | ||
</v-btn> | ||
</v-card-actions> | ||
</v-card> | ||
</v-dialog> | ||
|
||
<v-btn | ||
class="ma-2" | ||
icon | ||
small | ||
:disabled="!isAuthor" | ||
color="red" | ||
@click="deleteBlog(blog._id)" | ||
> | ||
<v-icon>mdi-delete</v-icon> | ||
</v-btn> | ||
</v-card-actions> | ||
</v-card> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { mapActions } from 'vuex' | ||
|
||
export default { | ||
name: 'RuthBlog', | ||
props: { | ||
|
@@ -39,5 +105,40 @@ export default { | |
required: true, | ||
}, | ||
}, | ||
data() { | ||
return { | ||
isAuthor: false, | ||
dialog: false, | ||
title: '', | ||
content: '', | ||
Comment on lines
+112
to
+113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same style can be applied to this.
|
||
current: {}, | ||
} | ||
}, | ||
created() { | ||
if (this.$auth.$state.loggedIn) { | ||
this.isAuthor = this.blog.authorUserId._id === this.$auth.state.user._id | ||
} | ||
|
||
this.title = this.blog.title | ||
this.content = this.blog.content | ||
}, | ||
methods: { | ||
...mapActions('ruth', ['deleteBlog', 'updateBlog', 'fetchBlogs']), | ||
editArea(blog) { | ||
this.dialog = !this.dialog | ||
this.current = blog | ||
}, | ||
updBlog(id) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better naming |
||
this.updatedBlog = { | ||
_id: id, | ||
content: this.content, | ||
title: this.title, | ||
description: 'description', | ||
} | ||
this.$store.dispatch('ruth/updateBlog', this.updatedBlog) | ||
this.dialog = false | ||
this.fetchBlogs() | ||
}, | ||
}, | ||
} | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,36 +8,50 @@ | |
|
||
<v-card class="mx-6 my-6"> | ||
<v-card-title> | ||
<span class="text-h6 font-weight-light" | ||
>{{ title }} <small>{{ id }}</small></span | ||
> | ||
<span class="text-h6 font-weight-light">{{ title }} </span> | ||
</v-card-title> | ||
|
||
<v-card-text class="text-h6"> | ||
{{ content }} | ||
</v-card-text> | ||
|
||
<v-card-actions> | ||
<v-list-item class="grow"> | ||
<v-list-item-content> | ||
<v-list-item-title>By {{ author }}</v-list-item-title> | ||
</v-list-item-content> | ||
</v-list-item> | ||
</v-card-actions> | ||
<v-list-item class="grow"> | ||
<v-list-item-content> | ||
<v-list-item-title>{{ author }}</v-list-item-title> | ||
</v-list-item-content> | ||
</v-list-item> | ||
</v-card> | ||
</v-app> | ||
</template> | ||
|
||
<script> | ||
import axios from 'axios' | ||
|
||
export default { | ||
data() { | ||
return { | ||
id: 1, | ||
title: 'Blog', | ||
content: | ||
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', | ||
author: 'Ruth Getachew', | ||
title: '', | ||
content: '', | ||
author: '', | ||
} | ||
}, | ||
async created() { | ||
const config = { | ||
headers: { | ||
Accept: 'application/json', | ||
}, | ||
} | ||
try { | ||
const res = await axios.get( | ||
`https://blog-app-backend.onrender.com/api/articles/${this.$route.params.id}`, | ||
config | ||
Comment on lines
+47
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's move all network requests to vuex. |
||
) | ||
|
||
this.author = 'By ' + res.data.authorUserId.fullName | ||
this.content = res.data.content | ||
this.title = res.data.title | ||
} catch (err) {} | ||
}, | ||
} | ||
</script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A better approach would be to have an object that represents a blog