The idea is to bring out an useful feature out of the enriched high quality of data provided by api.loklak.org. Through this blog post, I’ll be discussing about the implementation of a news feature similar to Google to provide latest news relevant to the query in loklak.
Creating news Component
First step in implementing news feature would be to create a news component which would be rendered on clicking of news tab.
Creating an action & reducer for news status
This step involves creating an action & reducer function for tracking the status of news component on click of news tab. When the user will click on news tab, corresponding action would be dispatched to display the news component in feed.
Corresponding reducer function for news status to store current news status would be:
I would not be getting into each line of code here, only the important portions would be discussed here.
Creating news-org to store the names of news organizations
A new file news-org would be created inside app/shared/ folder containing list of news organizations in an array newsOrgs from which we would extract and filter results.
Note: This is a dynamic implementation of news service and hence, the list of news organizations can be easily modified/updated here.
We would also need to export the newsOrgs in the index file inside shared/ folder as:
Creating ngrx action, reducer & effect for news success
Now comes the main part. We would need an action to dispatch on successful response of news search and corresponding reducer to keep appending the async news results.
News action would be created following the simple ngrx composition as:
One of the important parts of news implementation is the reducer function corresponding to the news success action. We would actually append the async results provided by news effect into the store.
Now, we would create an effect which would actually use the search service to fetch results for the list of organizations one by one will keep dispatching a corresponding action to append the response into the store to be displayed in news feed. Instead of showing whole code for the effect, only the main logic is being displayed.
Filtering the news response inside news component
The basic idea is to filter the results from news response which contain the query and display them using news template on click of news tab on results page of loklak.
In above configuration, firstly we needed the current query which we extracted from ngrx store and then checking whether the text of news results contain the query string or not. If the query is present inside the text, we would add it into an array which would be used to display news feed using feed-card.
Displaying the news response
Displaying the news response is very easy, we just need to pass the news response items with index inside feed-card component which would display the news feed similar to the normal feed under the news section.
|<div class=“wrapper feed-results”>
<div *ngFor=“let item of newsResponse; let i = index “>
<feed-card [feedItem]=“item” [feedIndex]=“i”></feed-card>
Creating News tab to display news feed
To provide a tab to user to access news feed, we would need to create a News tab along with the other searching tools inside feed-advanced-search component as:
Testing news feature
Search a query on loklak, and then click on news tab to get the latest relevant news matching the query.