Skip to content

Commit d759935

Browse files
Merge pull request #61 from SundeepChand/fix-newstory
Added limit to the characters being displayed in stories list
2 parents df48059 + 252c482 commit d759935

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

src/components/StoriesList.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,11 @@ import React from 'react'
22
import { navigate, Link } from '@reach/router'
33

44
import Vote from './Vote'
5+
import { strip } from '../utils/filterText'
56

67
const StoriesList = (props) => {
78
const { stories, state, product } = props
89

9-
const strip = (html) => {
10-
html = html.replace(/<\s*[^>]*>/gi, '')
11-
if (html.length > 80) {
12-
html = `${html.substring(0, 80)}...`
13-
}
14-
return html
15-
}
16-
1710
return (
1811
<div className='flex flex-column' data-cy='stories'>
1912
{stories && stories.length ? (
@@ -28,8 +21,8 @@ const StoriesList = (props) => {
2821
navigate(`/story/${story.id}`)
2922
}}
3023
>
31-
<h3>{story.Title}</h3>
32-
<p>{strip(story.Description)}</p>
24+
<h3>{strip(story.Title, 80)}</h3>
25+
<p>{strip(story.Description, 80)}</p>
3326
</div>
3427
<div className='story-author story-subcontent'>
3528
<div className='user-avatar'>

src/modules/TitleSearch.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import React, { useState, useEffect, useCallback } from 'react'
22
import { debounce } from 'lodash'
33
import userStory from '../services/user_story'
4+
import { strip } from '../utils/filterText'
45

56
const Search = (props) => {
67
const { title } = props
78

89
const [searchResults, setSearchResults] = useState([])
910

10-
const strip = (html) => {
11-
return html.replace(/<\s*[^>]*>/gi, '')
12-
}
13-
1411
const handleTitleChange = useCallback(
1512
debounce(async (title) => {
1613
const response = await userStory.getStoriesByTitle(title)
@@ -20,6 +17,9 @@ const Search = (props) => {
2017
)
2118

2219
useEffect(() => {
20+
if (!title) {
21+
return
22+
}
2323
handleTitleChange(title)
2424
}, [title, handleTitleChange])
2525

@@ -42,8 +42,7 @@ const Search = (props) => {
4242
key={key}
4343
>
4444
<div className='stories-content title-search-result'>
45-
<h4>{result.Title}</h4>
46-
{strip(result.Description)}
45+
<h4>{strip(result.Title, 80)}</h4>
4746
</div>
4847
<div className='icon-display'>
4948
{result.followers.length}

src/utils/filterText.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@ export const filterDescriptionText = (text) => {
33
text = text.replace(/[\r\n]/g, '') // Remove the line endings
44
return text
55
}
6+
7+
export const strip = (html, len) => {
8+
html = html.replace(/<\s*[^>]*>/gi, '')
9+
if (html.length > len) {
10+
html = `${html.substring(0, len)}...`
11+
}
12+
return html
13+
}

0 commit comments

Comments
 (0)