Skip to content

Conversation

@awesomejerry
Copy link

related to #684, #686

support:

src/content/about.en.jade
src/content/about/en.jade

@langpavel
Copy link
Collaborator

Hi @awesomejerry
This code is build on feature/react-intl which internally uses Redux, but you implement this through context. This is not preferred way of doing fetching in Redux branches. If you do this by redux, you will see that you can entirely drop localeDetect HoC which is also not good example of HoC..

You can look at LanguageSwitcher and try rewrite Content code to use Redux.

I know I should rewrite content fetching too and I probably do this soon but for now I will do not merge this.

Anyway Thanks! And ping me in week if nothing happens here 😃

@awesomejerry
Copy link
Author

The main reason I wrote localeDetect is that I don't want to mess up with the original code as much as possible (which is not meant for HoC demonstration apparently), trying to make things happen just inside a new component (that's why the usage of internal state). Furthermore, the locale content is only used by Component, so I didn't use redux or even create a new reducer. But hey, making things consistent might be great though. I'll make some changes by the end of the week.

FYI, I'm gonna make Component a stateful component in order to detect client side language switch. Dispatch action whenever the locale changes and take the content from state.content.data. Please let me know if there's any recommended ways of doing so.

@awesomejerry
Copy link
Author

Hi @langpavel , I've updated the PR to redux style. Please check it. Thanks!

Copy link
Collaborator

@langpavel langpavel left a comment

Choose a reason for hiding this comment

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

I suggest you some changes but it is great job!
Thanks @awesomejerry

/* eslint-disable import/prefer-default-export */

import {
GET_LOCALE_CONTENT,
Copy link
Collaborator

Choose a reason for hiding this comment

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

FETCH_CONTENT_START


import {
GET_LOCALE_CONTENT,
GET_LOCALE_CONTENT_SUCCESS,
Copy link
Collaborator

Choose a reason for hiding this comment

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

FETCH_CONTENT_SUCCESS

import {
GET_LOCALE_CONTENT,
GET_LOCALE_CONTENT_SUCCESS,
GET_LOCALE_CONTENT_ERROR,
Copy link
Collaborator

Choose a reason for hiding this comment

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

FETCH_CONTENT_ERROR

export function getLocaleContent(path, locale) {
return async (dispatch, getState, { graphqlRequest }) => {
dispatch({
type: GET_LOCALE_CONTENT,
Copy link
Collaborator

Choose a reason for hiding this comment

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

  • FETCH_CONTENT_START
  • add more data to payload, path, locale
  • you can make locale optional witch default value from getState().intl.locale

}
`;

export function getLocaleContent(path, locale) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

use object for passing in arguments

export function getLocaleContent({ path, locale })

export const SET_LOCALE_START = 'SET_LOCALE_START';
export const SET_LOCALE_SUCCESS = 'SET_LOCALE_SUCCESS';
export const SET_LOCALE_ERROR = 'SET_LOCALE_ERROR';
export const GET_LOCALE_CONTENT = 'GET_LOCALE_CONTENT';
Copy link
Collaborator

Choose a reason for hiding this comment

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

FETCH_CONTENT_START note it is very good practice to use suffixes consistently, see for example react-redux-loading-bar

return {
...state,
isFetching: true,
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

const key = `${action.payload.locale}:${action.payload.path}`;
return {
  ...state,
  [key]: {
    ...(state[key]),
    isFetching: true,
  },
}

key should be extracted to standalone function based on action.payload

...state,
isFetching: false,
data: action.payload.data,
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

return {
  ...state,
  [key]: {
    isFetching: false,
    ...action.payload
  },
};

const mapState = (state) => ({
locale: state.intl.locale,
data: state.content.data,
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

const mapState = (state, props) => {
  const locale = state.intl.locale;
  const content = state.content[`${locale}:${props.path}`];
  return {
    locale: state.intl.locale,
    ...content
  };
};

@awesomejerry
Copy link
Author

Thanks for the code reviews and suggestions! They're quite helpful.

@langpavel langpavel merged commit 222ef57 into kriasoft:feature/react-intl Oct 17, 2016
@langpavel
Copy link
Collaborator

Thanks @awesomejerry
I refactored some stuff to agressively cache content on client. Have a look!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants