Add redux, fix routing, add normalize

This commit is contained in:
Melvin Valster
2019-07-29 13:16:07 +02:00
parent 2fcf238446
commit 0bd3034ea7
13 changed files with 353 additions and 82 deletions
+23 -46
View File
@@ -2,21 +2,18 @@ import './Calculator.scss'
import React from 'react'
import { Map } from 'immutable'
import { TalentTree } from './TalentTree'
import {
modifyTalentPoint,
calcAvailablePoints,
encodeKnownTalents,
} from '../lib/tree'
import { talentsBySpec } from '../data/talents'
import { classByName } from '../data/classes'
import { History } from 'history'
// import { debugPrintKnown } from '../lib/debug'
import { calcAvailablePoints } from '../lib/tree'
import { classById } from '../data/classes'
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';
import { addPoint, removePoint } from '../store/calculator/actions'
import { Points } from '../store/calculator/types'
interface Props {
selectedClass: string
history: History
initialTalents?: Map<number, number>
classId: number
points: Points
addPoint: typeof addPoint
removePoint: typeof removePoint
}
const EMPTY_TALENTS = Map<number, number>()
@@ -28,46 +25,19 @@ export class Calculator extends React.PureComponent<Props> {
knownTalents: EMPTY_TALENTS
}
componentDidMount() {
if (this.props.initialTalents) {
this.setState({ knownTalents: this.props.initialTalents })
this.updateURL(this.props.initialTalents)
}
}
componentDidUpdate(prevProps: Props) {
if (prevProps.selectedClass !== this.props.selectedClass) {
this.setState({
knownTalents: EMPTY_TALENTS
})
}
}
updateURL(knownTalents: Map<number, number>) {
const { selectedClass } = this.props
const pointString = encodeKnownTalents(knownTalents, selectedClass)
this.props.history.replace(`/${selectedClass}` + (pointString ? `/${pointString}` : ''))
}
handleTalentPress = (specId: number, talentId: number, modifier: 1 | -1) => {
const talent = talentsBySpec[specId][talentId]
console.log('Clicked talent: ', talentId)
const newKnownTalents = modifyTalentPoint(this.state.knownTalents, talent, modifier)
if (newKnownTalents !== this.state.knownTalents) {
this.updateURL(newKnownTalents)
if (modifier === 1) {
this.props.addPoint(talentId)
} else {
this.props.removePoint(talentId)
}
this.setState({ knownTalents: newKnownTalents })
// Debug
// debugPrintKnown(newKnownTalents)
}
render() {
const { selectedClass } = this.props
const { knownTalents } = this.state
const { classId } = this.props
const knownTalents = this.props.points
const classData = classByName[selectedClass]
const classData = classById[classId]
const availablePoints = calcAvailablePoints(knownTalents)
return (
@@ -99,3 +69,10 @@ export class Calculator extends React.PureComponent<Props> {
}
}
export default connect(
null,
{
addPoint,
removePoint
}
)(Calculator)