import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Helmet from 'react-helmet';
import { graphql } from 'gatsby';
import { uniq } from 'lodash';
import { connect } from 'react-redux';
import { createSelector } from 'reselect';
import { bindActionCreators } from 'redux';
import { withTranslation } from 'react-i18next';
import { Grid, Row, Col } from '@freecodecamp/react-bootstrap';
import Login from '../../components/Header/components/Login';
import Map from '../../components/Map';
import CertChallenge from './components/CertChallenge';
import SuperBlockIntro from './components/SuperBlockIntro';
import { dasherize } from '../../../../utils/slugs';
import Block from './components/Block';
import { Spacer, Link } from '../../components/helpers';
import {
currentChallengeIdSelector,
userFetchStateSelector,
isSignedInSelector
} from '../../redux';
import { resetExpansion, toggleBlock } from './redux';
import { MarkdownRemark, AllChallengeNode } from '../../redux/propTypes';
import './intro.css';
const propTypes = {
currentChallengeId: PropTypes.string,
data: PropTypes.shape({
markdownRemark: MarkdownRemark,
allChallengeNode: AllChallengeNode
}),
expandedState: PropTypes.object,
fetchState: PropTypes.shape({
pending: PropTypes.bool,
complete: PropTypes.bool,
errored: PropTypes.bool
}),
isSignedIn: PropTypes.bool,
location: PropTypes.shape({
state: PropTypes.shape({
breadcrumbBlockClick: PropTypes.string
})
}),
resetExpansion: PropTypes.func,
t: PropTypes.func,
toggleBlock: PropTypes.func
};
const mapStateToProps = state => {
return createSelector(
currentChallengeIdSelector,
isSignedInSelector,
userFetchStateSelector,
(currentChallengeId, isSignedIn, fetchState) => ({
currentChallengeId,
isSignedIn,
fetchState
})
)(state);
};
const mapDispatchToProps = dispatch =>
bindActionCreators(
{ resetExpansion, toggleBlock: b => toggleBlock(b) },
dispatch
);
export class SuperBlockIntroductionPage extends Component {
componentDidMount() {
this.initializeExpandedState();
}
getChosenBlock() {
const {
data: {
allChallengeNode: { edges }
},
isSignedIn,
currentChallengeId,
location
} = this.props;
// if coming from breadcrumb click
if (location.state && location.state.breadcrumbBlockClick)
return dasherize(location.state.breadcrumbBlockClick);
let edge = edges[0];
if (isSignedIn) {
// see if currentChallenge is in this superBlock
const currentChallengeEdge = edges.find(
edge => edge.node.id === currentChallengeId
);
return currentChallengeEdge
? currentChallengeEdge.node.block
: edge.node.block;
}
return edge.node.block;
}
initializeExpandedState() {
const { resetExpansion, toggleBlock } = this.props;
resetExpansion();
return toggleBlock(this.getChosenBlock());
}
render() {
const {
data: {
markdownRemark: {
frontmatter: { superBlock }
},
allChallengeNode: { edges }
},
isSignedIn,
t
} = this.props;
const superBlockDashedName = dasherize(superBlock);
const nodesForSuperBlock = edges.map(({ node }) => node);
const blockDashedNames = uniq(nodesForSuperBlock.map(({ block }) => block));
const superBlockIntroObj = t(`intro:${superBlockDashedName}`);
const { title: i18nSuperBlock, isTranslated } = superBlockIntroObj;
const translationBannerText = t(`intro:misc-text.translation-banner`);
const translationBannerHelpText = t(`intro:misc-text.translation-help`);
return (
<>
{translationBannerText} {translationBannerHelpText}.
)}