Exploring Event
In this section we explore events i.e we route to /events/:id
. This route
renders our EventDetailed.jsx
component.
#
Event Detailed PageWhen we select any event for more details we route to /events/:id
our EventDetailedPage.jsx
component get rendered. Our listenToEventFromFirstore
runs that fetches the selected event
. We then dispatch an action LISTEN_TO_SELECTED_EVENT
that's responsible of storing the fetched selected event
in our redux store. The component finally renders our selected event
to the screen.
selected event
state in our redux store will be over-ridden according to the event we select.
#
Join/Cancel EventWe join event by clicking on join button which fires addUserAttendance
function that updates the attendance of the user.
A user can also cancel his/her attendance by clicking on Cancel My Place button which fires cancelUserAttendance
that filters out the attendees.
#
Event ChatEventDetailedPage.jsx
component renders EventDetailedChat.jsx
component.
The Chat component uses Firebase Realtime Database
Remember when we said we'll be using both the databases, now is the time.
We earlier looked at addding and reading data from Firebase RTDB here.
Let's explain complete workflow of the EventDetailedChat.jsx
component.
#
Adding ChatsEventDetailedChat.jsx
in turn loads EventDetailedChatForm.jsx
.
We type our comment in the form that passes our entered value to addEventChatComment
which then adds our comment to the firebase RTDB.
#
Fetching ChatsEventDetailedChat.jsx
is a subcomponent of EventDetailedPage.jsx
. When the chat component gets rendered, either the chats are present for a particular event or the chat is empty in our database.
Our useEffect
runs in our EventDetailedChat.jsx
component
It then fetches and gets the comments stored in our RTDB,
We call getEventChatRef
from the useEffect
which returns a reference of our chats stored and we then check if the snapshot
exists(i.e chats are present) then we take out the data as snapshot.val()
but this data is of JSON type and we want to store it as an array in our redux store. Thus, we call another function firebaseObjectToArray
which converts from JSON to our required array.
We then perform a reverse()
operation on this array to always maintain the latest comment at top. After the fetching and conversion is done we then store are comments array in our redux store by dispatching listentoEventChat
and render them accordingly.
note
We also need to make sure we clear the comments from our redux store. This is done to not render and mix up the comments from other event. To fix this we dispatch CLEAR_COMMENTS
in our cleanup function of useEffect
and turn off the listener once we have rendered the comments by getEventChatRef().off()
.
#
Final state in Redux StoreBelow is our final event state in our redux store