From 469c3f05c210d36200fc1269f6fa552e873f5129 Mon Sep 17 00:00:00 2001 From: Valeriy Date: Fri, 30 Aug 2019 20:07:28 +0300 Subject: [PATCH] feat: use static query to get idToNameMap (#36722) --- api-server/common/models/challenge.js | 56 ------------ api-server/common/models/challenge.json | 7 -- .../components/profile/components/TimeLine.js | 86 ++++++++++--------- .../Challenges/redux/id-to-name-map-saga.js | 18 ---- .../src/templates/Challenges/redux/index.js | 20 +---- client/src/utils/ajax.js | 4 - 6 files changed, 46 insertions(+), 145 deletions(-) delete mode 100644 api-server/common/models/challenge.js delete mode 100644 client/src/templates/Challenges/redux/id-to-name-map-saga.js diff --git a/api-server/common/models/challenge.js b/api-server/common/models/challenge.js deleted file mode 100644 index 34aef6ef14..0000000000 --- a/api-server/common/models/challenge.js +++ /dev/null @@ -1,56 +0,0 @@ -import { Observable } from 'rx'; -import { isEmpty } from 'lodash'; -import { dasherize } from '../utils'; - -export default function(Challenge) { - let challengeIdToNameMap; - - Challenge.on('dataSourceAttached', () => { - Challenge.findOne$ = Observable.fromNodeCallback( - Challenge.findOne, - Challenge - ); - Challenge.findById$ = Observable.fromNodeCallback( - Challenge.findById, - Challenge - ); - Challenge.find$ = Observable.fromNodeCallback(Challenge.find, Challenge); - - Challenge.find({ isPrivate: false }, (err, challenges) => { - if (err) { - throw Error(err); - } - challengeIdToNameMap = challenges.reduce((map, challenge) => { - const { id, block, dashedName, title, superBlock } = challenge; - map[id] = { - challengeTitle: title, - challengePath: `${superBlock}/${dasherize(block)}/${dashedName}` - }; - return map; - }, {}); - }); - - function getIdToNameMap(cb) { - if (isEmpty(challengeIdToNameMap)) { - // We are waiting for the find query to resolve - return setTimeout(() => getIdToNameMap(cb), 50); - } - return cb(null, challengeIdToNameMap); - } - Challenge.getIdToNameMap = getIdToNameMap; - }); - - Challenge.remoteMethod('getIdToNameMap', { - returns: [ - { - arg: 'user', - type: 'object', - root: true - } - ], - http: { - path: '/get-id-to-name', - verb: 'GET' - } - }); -} diff --git a/api-server/common/models/challenge.json b/api-server/common/models/challenge.json index 5725223b55..c5a8801036 100644 --- a/api-server/common/models/challenge.json +++ b/api-server/common/models/challenge.json @@ -133,13 +133,6 @@ "principalType": "ROLE", "principalId": "$everyone", "permission": "ALLOW" - }, - { - "accessType": "EXECUTE", - "principalType": "ROLE", - "principalId": "$everyone", - "permission": "ALLOW", - "property": "getIdToNameMap" } ], "methods": {} diff --git a/client/src/components/profile/components/TimeLine.js b/client/src/components/profile/components/TimeLine.js index d017598394..81f1abcf4b 100644 --- a/client/src/components/profile/components/TimeLine.js +++ b/client/src/components/profile/components/TimeLine.js @@ -1,29 +1,13 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; -import { createSelector } from 'reselect'; -import { bindActionCreators } from 'redux'; -import { connect } from 'react-redux'; import format from 'date-fns/format'; -import { find, reverse, sortBy, isEmpty } from 'lodash'; +import { find, reverse, sortBy } from 'lodash'; import { Button, Modal, Table } from '@freecodecamp/react-bootstrap'; -import { Link } from 'gatsby'; +import { Link, useStaticQuery, graphql } from 'gatsby'; -import { - challengeIdToNameMapSelector, - fetchIdToNameMap -} from '../../../templates/Challenges/redux'; import { FullWidthRow } from '../../helpers'; import SolutionViewer from '../../settings/SolutionViewer'; - -const mapStateToProps = createSelector( - challengeIdToNameMapSelector, - idToNameMap => ({ - idToNameMap - }) -); - -const mapDispatchToProps = dispatch => - bindActionCreators({ fetchIdToNameMap }, dispatch); +import { challengeTypes } from '../../../../utils/challengeTypes'; const propTypes = { completedMap: PropTypes.arrayOf( @@ -40,7 +24,6 @@ const propTypes = { ) }) ), - fetchIdToNameMap: PropTypes.func.isRequired, idToNameMap: PropTypes.objectOf( PropTypes.shape({ challengePath: PropTypes.string, @@ -50,7 +33,7 @@ const propTypes = { username: PropTypes.string }; -class Timeline extends Component { +class TimelineInner extends Component { constructor(props) { super(props); @@ -64,21 +47,14 @@ class Timeline extends Component { this.viewSolution = this.viewSolution.bind(this); } - componentDidMount() { - if (isEmpty(this.props.idToNameMap)) { - return this.props.fetchIdToNameMap(); - } - return null; - } - renderCompletion(completed) { const { idToNameMap } = this.props; const { id, completedDate } = completed; - const { challengeTitle, challengePath } = idToNameMap[id]; + const { challengeTitle, challengePath } = idToNameMap.get(id); return ( - {challengeTitle} + {challengeTitle}