Skip to content

Commit

Permalink
feat(Posts): improve loading state
Browse files Browse the repository at this point in the history
  • Loading branch information
jkklapp committed Jun 1, 2022
1 parent bd34266 commit 5e3ab61
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 35 deletions.
18 changes: 15 additions & 3 deletions frontend/src/components/Dashboard/Posts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@
:key="p.id"
class="bg-gray-200 dark:bg-gray-500 text-gray-800 dark:text-gray-100 px-4 pt-2 pb-2 mb-2 shadow-xl ring-1 ring-gray-900/5 sm:mx-auto sm:max-w-lg sm:rounded-lg"
>
<div>
<small>{{ p.userName }} - {{ date(p.date) }}</small>
<div class="flex flex-wrap">
<div>
<small>{{ p.userName }} - {{ date(p.date) }}</small>
</div>
<div v-if="!p.id && isPosting" class="w-100 ml-auto place-items-end">
<div class="lds-ring-small align-text-top">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</div>
</div>
<div>
<span>{{ p.message }}</span>
Expand All @@ -31,7 +41,8 @@
</div>
</div>
<div v-if="isLoading" class="flex justify-center items-center">
<div class="lds-facebook">
<div class="lds-ring">
<div></div>
<div></div>
<div></div>
<div></div>
Expand All @@ -50,6 +61,7 @@ export default {
renderBackToTopButton: 'shouldRenderBackToTopButton',
renderLoadMoreButton: 'shouldRenderLoadMoreButton',
isLoading: 'isLoadingPosts',
isPosting: 'isCreatingPost',
}),
},
methods: {
Expand Down
77 changes: 56 additions & 21 deletions frontend/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,74 @@
@tailwind components;
@tailwind utilities;

.lds-facebook {
.lds-ring {
display: inline-block;
position: relative;
width: 80px;
height: 80px;
}
.lds-facebook div {
.lds-ring div {
box-sizing: border-box;
display: block;
position: absolute;
width: 64px;
height: 64px;
margin: 8px;
border: 8px solid #fff;
border-radius: 50%;
animation: lds-ring 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
border-color: #fff transparent transparent transparent;
}
.lds-ring div:nth-child(1) {
animation-delay: -0.45s;
}
.lds-ring div:nth-child(2) {
animation-delay: -0.3s;
}
.lds-ring div:nth-child(3) {
animation-delay: -0.15s;
}
@keyframes lds-ring {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}

.lds-ring-small {
display: inline-block;
position: relative;
width: 10px;
height: 10px;
}
.lds-ring-small div {
box-sizing: border-box;
display: block;
position: absolute;
left: 8px;
width: 16px;
background: #fff;
animation: lds-facebook 1.2s cubic-bezier(0, 0.5, 0.5, 1) infinite;
width: 8px;
height: 8px;
margin: 1px;
border: 1px solid #fff;
border-radius: 50%;
animation: lds-ring-small 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
border-color: #fff transparent transparent transparent;
}
.lds-facebook div:nth-child(1) {
left: 8px;
animation-delay: -0.24s;
.lds-ring-small div:nth-child(1) {
animation-delay: -0.45s;
}
.lds-facebook div:nth-child(2) {
left: 32px;
animation-delay: -0.12s;
.lds-ring-small div:nth-child(2) {
animation-delay: -0.3s;
}
.lds-facebook div:nth-child(3) {
left: 56px;
animation-delay: 0;
.lds-ring-small div:nth-child(3) {
animation-delay: -0.15s;
}
@keyframes lds-facebook {
@keyframes lds-ring-small {
0% {
top: 8px;
height: 64px;
transform: rotate(0deg);
}
50%, 100% {
top: 24px;
height: 32px;
100% {
transform: rotate(360deg);
}
}
11 changes: 7 additions & 4 deletions frontend/src/store/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ export default {
commit('SET_START_AFTER', null);
},
async postMessage({ commit }, message) {
commit('IS_LOADING_POSTS', true);
const { data } = await apiRequest('POST', '/posts', null, { message });
commit('PUSH_MESSAGE', data);
commit('IS_LOADING_POSTS', false);
commit('IS_CREATING_POST', true);
commit('PUSH_MESSAGE', {
message,
date: new Date().getTime(),
});
await apiRequest('POST', '/posts', null, { message });
commit('IS_CREATING_POST', false);
},
};
11 changes: 7 additions & 4 deletions frontend/src/store/actions/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,21 @@ describe('actions', () => {
testAction(
actions.postMessage,
'Hello World',
{ posts: [] },
{ posts: [], creatingPost: false },
[
{
type: 'IS_LOADING_POSTS',
type: 'IS_CREATING_POST',
payload: true,
},
{
type: 'PUSH_MESSAGE',
payload: POSTS_RESPONSE_FIXTURE[0],
payload: {
message: 'Hello World',
date: expect.anything(),
},
},
{
type: 'IS_LOADING_POSTS',
type: 'IS_CREATING_POST',
payload: false,
},
],
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/store/getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ export default {
isLoadingPosts(state) {
return state.loadingPosts;
},
isCreatingPost(state) {
return state.creatingPost;
},
};
1 change: 1 addition & 0 deletions frontend/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const store = createStore({
posts: [],
message: null,
loadingPosts: false,
creatingPost: false,
},
getters,
mutations,
Expand Down
8 changes: 7 additions & 1 deletion frontend/src/store/mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ export default {
state.startAfter = data;
},
PUSH_MESSAGE(state, data) {
state.posts = [data, ...state.posts];
state.posts = [
{ ...data, userName: state.user.displayName },
...state.posts,
];
},
SET_MESSAGE(state, data) {
state.message = data;
},
IS_LOADING_POSTS(state, data) {
state.loadingPosts = data;
},
IS_CREATING_POST(state, data) {
state.creatingPost = data;
},
};
4 changes: 2 additions & 2 deletions frontend/src/store/mutations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ describe('mutations', () => {
message: 'Hello World',
};
// mock state
const state = { posts: [] };
const state = { posts: [], user: { displayName: 'Test' } };
// apply mutation
PUSH_MESSAGE(state, messageFixture);
// assert result
expect(state.posts).toEqual([messageFixture]);
expect(state.posts).toEqual([{ ...messageFixture, userName: 'Test' }]);
});
});
describe('SET_MESSAGE', () => {
Expand Down

0 comments on commit 5e3ab61

Please sign in to comment.