Skip to content

Commit

Permalink
refactor: mentor's review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyTeterin committed Mar 23, 2021
1 parent 2fc292f commit 8414e35
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 57 deletions.
3 changes: 0 additions & 3 deletions src/components/Dictionary/Dictionary.scss

This file was deleted.

32 changes: 13 additions & 19 deletions src/components/Dictionary/Dictionary.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import React, { useEffect } from 'react';
import { connect, ConnectedProps } from 'react-redux';
import { Typography } from '@material-ui/core';
import { useDispatch, useSelector } from 'react-redux';
import { makeStyles, Typography } from '@material-ui/core';
import Header from '../Header';
import Footer from '../Footer';
import { fetchDictionary } from '../../store/actions/dictionaryActions';
import { IAppState, IWord } from '../../store/types';
import './Dictionary.scss';
import styles from './styles';

type Props = ConnectedProps<typeof connector>;
const Dictionary: React.FC = () => {
const dictionary = useSelector((state: IAppState) => state.dictionary);
const dispatch = useDispatch();
const classes = makeStyles(() => styles)();

const fetch = () => dispatch(fetchDictionary());

const Dictionary: React.FC<Props> = (props: Props) => {
useEffect(() => {
props.fetchDictionary();
fetch();
}, []);

const renderWords = () =>
props.dictionary.words.map((word: IWord) => (
<Typography className="testClass" key={word.word}>
dictionary.words.map((word: IWord) => (
<Typography className={classes.text} key={word.word}>
{word.textMeaningTranslate}
</Typography>
));
Expand All @@ -33,14 +37,4 @@ const Dictionary: React.FC<Props> = (props: Props) => {
);
};

const mapStateToProps = (state: IAppState) => ({
dictionary: state.dictionary,
});

const mapDispatchToProps = (dispatch: any) => ({
fetchDictionary: () => dispatch(fetchDictionary()),
});

const connector = connect(mapStateToProps, mapDispatchToProps);

export default connector(Dictionary);
export default Dictionary;
7 changes: 7 additions & 0 deletions src/components/Dictionary/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const styles = {
text: {
color: 'rebeccapurple',
},
};

export default styles;
52 changes: 22 additions & 30 deletions src/store/actions/dictionaryActions.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
import { Dispatch } from 'redux';
import { DictionaryActionTypes, IWord } from '../types';

export function fetchDictionaryStart() {
return {
type: DictionaryActionTypes.FETCH_START,
};
}
export const fetchDictionaryStart = () => ({
type: DictionaryActionTypes.FETCH_START,
});

export function fetchDictionarySuccess(words: IWord[]) {
return {
type: DictionaryActionTypes.FETCH_SUCCESS,
payload: { words },
};
}
export const fetchDictionarySuccess = (words: IWord[]) => ({
type: DictionaryActionTypes.FETCH_SUCCESS,
payload: { words },
});

export function fetchDictionaryError(error: Error) {
return {
type: DictionaryActionTypes.FETCH_ERROR,
payload: { error },
};
}
export const fetchDictionaryError = (error: Error) => ({
type: DictionaryActionTypes.FETCH_ERROR,
payload: { error },
});

export function fetchDictionary() {
return async (dispatch: Dispatch) => {
const apiURL = 'https://react-learnwords-example.herokuapp.com/words';
dispatch(fetchDictionaryStart());
try {
const res = await fetch(apiURL);
const words = await res.json();
dispatch(fetchDictionarySuccess(words));
} catch (e) {
dispatch(fetchDictionaryError(e));
}
};
}
export const fetchDictionary = () => async (dispatch: Dispatch) => {
const apiURL = 'https://react-learnwords-example.herokuapp.com/words';
dispatch(fetchDictionaryStart());
try {
const res = await fetch(apiURL);
const words = await res.json();
dispatch(fetchDictionarySuccess(words));
} catch (e) {
dispatch(fetchDictionaryError(e));
}
};
9 changes: 4 additions & 5 deletions src/store/reducers/dictionaryReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ const initialState: IDictionary = {
wordsLearned: [],
};

export default function dictionaryReducer(
state: IDictionary = initialState,
action: IDictionaryAction
) {
const dictionaryReducer = (state: IDictionary = initialState, action: IDictionaryAction) => {
switch (action.type) {
case DictionaryActionTypes.FETCH_START:
return { ...state, isLoading: true };
Expand All @@ -21,4 +18,6 @@ export default function dictionaryReducer(
default:
return state;
}
}
};

export default dictionaryReducer;

0 comments on commit 8414e35

Please sign in to comment.