Skip to content

Commit

Permalink
feat(Posts): create comments for posts
Browse files Browse the repository at this point in the history
  • Loading branch information
jkklapp committed Jun 8, 2022
1 parent 287a62e commit 1144d70
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions frontend/src/components/Dashboard/Posts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<div class="flex flex-wrap place-items-end">
<div class="w-100 mr-auto"></div>
<a
v-show="p.comments > 0"
href="#"
class="text-gray-500 dark:text-gray-400"
@click.prevent="() => fetchPosts(p.id)"
Expand Down Expand Up @@ -118,7 +117,8 @@ export default {
return moment(date).fromNow();
},
fetchPosts(parentId) {
this.$store.dispatch('fetchPosts', { ...this.$store.state, parentId });
this.$store.dispatch('setParentId', parentId);
this.$store.dispatch('fetchPosts', this.$store.state);
},
reset() {
this.$store.dispatch('resetPostsPagination');
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/store/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export default {
commit('SET_USER', user);
commit('SET_LOGGED_IN', user.email && user.displayName);
},
setParentId({ commit }, parentId) {
commit('SET_PARENT_ID', parentId);
},
async fetchPosts({ commit }, { startAfter, limit, parentId }) {
commit('IS_LOADING_POSTS', true);
const { data } = await apiRequest('GET', '/posts', {
Expand All @@ -13,7 +16,7 @@ export default {
parentId,
});
const { results, nextPageToken, remainingMessages } = data;
commit('SET_POSTS', { results, parentId });
commit('SET_POSTS', results);
commit('SET_START_AFTER', nextPageToken);
commit('SET_REMAINING_MESSAGES', remainingMessages || 0);
commit('IS_LOADING_POSTS', false);
Expand All @@ -23,15 +26,20 @@ export default {
},
resetPostsPagination({ commit }) {
commit('SET_START_AFTER', null);
commit('SET_PARENT_ID', null);
},
async postMessage({ commit, state }, message) {
commit('IS_CREATING_POST', true);
commit('PUSH_MESSAGE', {
message,
date: new Date().getTime(),
parentId: state.parentId,
});
try {
const { data } = await apiRequest('POST', '/posts', null, { message });
const { data } = await apiRequest('POST', '/posts', null, {
message,
parentId: state.parentId || undefined,
});
commit('POP_MESSAGE');
commit('PUSH_MESSAGE', data);
commit('SET_REMAINING_MESSAGES', state.remainingMessages - 1);
Expand Down
1 change: 1 addition & 0 deletions frontend/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const store = createStore({
userNameExists: false,
isCheckingEmail: false,
isCheckingName: false,
parentId: null,
},
getters,
mutations,
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/store/mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ export default {
SET_LOGGED_IN(state, data) {
state.loggedIn = data;
},
SET_POSTS(state, { results, parentId }) {
if (parentId) {
const parent = state.posts.find((post) => post.id === parentId);
SET_PARENT_ID(state, data) {
state.parentId = data;
},
SET_POSTS(state, results) {
if (state.parentId) {
const parent = state.posts.find((post) => post.id === state.parentId);
state.posts = [parent, ...results];
} else {
state.posts = results;
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/store/mutations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('mutations', () => {
// mock state
const state = { posts: [] };
// apply mutation
SET_POSTS(state, { results: postsFixture });
SET_POSTS(state, postsFixture);
// assert result
expect(state.posts).toEqual(postsFixture);
});
Expand Down

0 comments on commit 1144d70

Please sign in to comment.