Skip to content

Commit 59f7cc9

Browse files
committed
Refactored fetchForex async function according to changes made to API, fixed a bug about setting market detail for selected search result
1 parent 6532979 commit 59f7cc9

File tree

4 files changed

+31
-26
lines changed

4 files changed

+31
-26
lines changed

.eslintcache

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/components/most-popular/MostPopular.jsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React from "react";
2-
import { useSelector } from "utils/react-redux-hooks";
1+
import React from 'react';
2+
import { useSelector } from 'utils/react-redux-hooks';
33

4-
import NewsPlaceholder from "assets/newsPlaceholder.png";
4+
import NewsPlaceholder from 'assets/newsPlaceholder.png';
55

66
import {
77
MostPopularContainer,
@@ -12,7 +12,7 @@ import {
1212
ImageContainer,
1313
SeeAll,
1414
MostPopularWithHeader,
15-
} from "./MostPopular.styles";
15+
} from './MostPopular.styles';
1616

1717
const MostPopular = () => {
1818
const { popular, width } = useSelector((state) => state.news);
@@ -35,22 +35,23 @@ const MostPopular = () => {
3535
<StoryContainer>
3636
<StoryTitle font="var(--font-header)">
3737
{width > 830
38-
? story.title.split(" ").slice(0, 6).join(" ")
38+
? story.title.split(' ').slice(0, 6).join(' ')
3939
: story.title}
4040
...
4141
</StoryTitle>
4242
<StoryTitle color="var(--gray)">
4343
{width > 830
44-
? story.abstract.split(" ").slice(0, 10).join(" ")
44+
? story.abstract.split(' ').slice(0, 10).join(' ')
4545
: story.abstract}
4646
...
4747
</StoryTitle>
4848
</StoryContainer>
4949
<ImageContainer
5050
background={
51+
(console.log(story.media[0]),
5152
story.media[0]
52-
? story.media[0]["media-metadata"][2].url
53-
: NewsPlaceholder
53+
? story.media[0]['media-metadata'][2].url
54+
: NewsPlaceholder)
5455
}
5556
/>
5657
</PopularStoriesContainer>

src/components/search-results/SearchResults.jsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { useSelector, useDispatch } from "utils/react-redux-hooks";
1+
import { useSelector, useDispatch } from 'utils/react-redux-hooks';
22

33
import {
44
fetchRating,
55
fetchForex,
66
clearSearchResults,
7-
} from "redux/actions/chart";
7+
} from 'redux/actions/chart';
88

9-
import { StoryTopicContainer } from "components/story-topic/StoryTopic.styles.js";
9+
import { StoryTopicContainer } from 'components/story-topic/StoryTopic.styles.js';
1010
import {
1111
UserFavoriteContainer,
1212
FavoriteCards,
13-
} from "components/user-favorites/UserFavorites.styles";
13+
} from 'components/user-favorites/UserFavorites.styles';
1414

1515
const SearchResults = () => {
1616
const { searchResults } = useSelector((state) => state.chart);
@@ -19,23 +19,23 @@ const SearchResults = () => {
1919
return (
2020
<section
2121
style={{
22-
margin: "0 15px",
23-
position: "relative",
24-
minHeight: "calc(100vh - 153px - 87px)",
22+
margin: '0 15px',
23+
position: 'relative',
24+
minHeight: 'calc(100vh - 153px - 87px)',
2525
}}
2626
>
2727
<StoryTopicContainer>
28-
<h1 style={{ marginTop: "25px" }}>Search Results</h1>
28+
<h1 style={{ marginTop: '25px' }}>Search Results</h1>
2929
</StoryTopicContainer>
3030
<UserFavoriteContainer>
3131
{searchResults.length ? (
3232
searchResults.map((stock) => {
3333
return (
3434
<div
3535
style={{
36-
display: "flex",
37-
border: "1px solid black",
38-
padding: "10px 15px",
36+
display: 'flex',
37+
border: '1px solid black',
38+
padding: '10px 15px',
3939
}}
4040
key={stock.symbol}
4141
>
@@ -47,12 +47,12 @@ const SearchResults = () => {
4747
dispatch(clearSearchResults());
4848
}}
4949
>
50-
<h1>{stock.symbol.split("^").join("")}</h1>
50+
<h1>{stock.symbol.split('^').join('')}</h1>
5151

5252
<h1
5353
style={{
54-
fontWeight: "normal",
55-
fontSize: "var(--size-sub-menu)",
54+
fontWeight: 'normal',
55+
fontSize: 'var(--size-sub-menu)',
5656
}}
5757
>
5858
{stock.exchangeShortName}

src/redux/actions/chart.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,8 @@ export const fetchChartError = (error) => ({
110110
export const fetchForex = (markets) => async (dispatch) => {
111111
const marketArray = markets.split(',').sort();
112112
let forexList = [];
113-
114113
// FMP API changed its services. Before I could make a batch request
115-
// for getting quites. Now they dont let free account do it.
114+
// for getting quotes. Now they don't let free account do it.
116115
// So I had to find a workaround for that with looping the free endpoint.
117116
try {
118117
for (let market of marketArray) {
@@ -122,7 +121,12 @@ export const fetchForex = (markets) => async (dispatch) => {
122121
const data = await response.data;
123122
forexList.push(data);
124123
}
125-
return dispatch(fetchForexSuccess(forexList));
124+
125+
return [
126+
forexList.length === 1
127+
? dispatch(setMarketDetail(forexList.flat()[0]))
128+
: dispatch(fetchForexSuccess(forexList)),
129+
];
126130
} catch (error) {
127131
return dispatch(fetchChartError(error));
128132
}

0 commit comments

Comments
 (0)