Implementing news feature in loklak
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. ng g component feed/news 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. import { Action } from ‘@ngrx/store’; export const ActionTypes = { NEWS_STATUS: ‘[NEWS] NEWS STATUS’ }; export class NewsStatusAction implements Action { type = ActionTypes.NEWS_STATUS; constructor (public payload: boolean) { } } export type Actions = NewsStatusAction; Corresponding reducer function for news status to store current news status would be: export function reducer(state: State = initialState, action: newsStatusAction.NewsStatusAction): State { switch (action.type) { case newsStatusAction.ActionTypes.NEWS_STATUS: { const newsStatusPayload: boolean = action.payload; return Object.assign({}, state, { newsStatus: newsStatusPayload }); } default: { return state; } } } 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. export const newsOrgs = [ ‘CNN’, ‘nytimes’ ]; 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: export { newsOrgs } from ‘./news-org’; 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: import { Action } from ‘@ngrx/store’; import { ApiResponse } from ‘../models/api-response’; export const ActionTypes = { NEWS_SEARCH_SUCCESS: ‘[NEWS] SEARCH SUCCESS’ }; export class NewsSearchSuccessAction implements Action { type = ActionTypes.NEWS_SEARCH_SUCCESS; constructor (public payload: ApiResponse) { } } export type Actions = NewsSearchSuccessAction; 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. import * as newsSuccessAction from ‘../actions/newsSuccess’; import { ApiResponseResult } from ‘../models’; export interface State { newsResponse: ApiResponseResult[]; } export const initialState: State = { newsResponse: [], }; export function reducer(state: State = initialState, action: newsSuccessAction.NewsSearchSuccessAction): State { switch (action.type) { case newsSuccessAction.ActionTypes.NEWS_SEARCH_SUCCESS: { // Appending the news result return Object.assign({}, state, { newsResponse: […action.payload.statuses] });…
