Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: mentor's review fixes for Dictionary #6

Merged
merged 2 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { ICombinedState, IWord } from '../../store/types';
import './Dictionary.scss';
import styles from './styles';

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

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

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

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: ICombinedState) => ({
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;
51 changes: 21 additions & 30 deletions src/store/actions/dictionaryActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,27 @@ import { Dispatch } from 'redux';
import backendUrl from '../../consts';
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[]) => ({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IWord[] как Array вроде хотели писать?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

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 = `${backendUrl}/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) => {
dispatch(fetchDictionaryStart());
try {
const res = await fetch(`${backendUrl}/words`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice if fetchDictionary function will take two value. For example "page" & "group", for this link https://react-learnwords-example.herokuapp.com/words?page=2&group=0

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Будет сделано в будущем ) это пока что "скелет"

const words = await res.json();
dispatch(fetchDictionarySuccess(words));
} catch (e) {
dispatch(fetchDictionaryError(e));
}
};
9 changes: 4 additions & 5 deletions src/store/reducers/dictionaryReducer/dictionaryReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ const initialState: IDictionaryState = {
wordsLearned: [],
};

export default function dictionaryReducer(
state: IDictionaryState = initialState,
action: IDictionaryAction
) {
const dictionaryReducer = (state: IDictionaryState = 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;