import { FC, useEffect, useState } from 'react'; import { LocalizeText, WiredFurniType } from '../../../../api'; import { Slider, Text } from '../../../../common'; import { useWired } from '../../../../hooks'; import { WiredSourcesSelector } from '../WiredSourcesSelector'; import { WiredConditionBaseView } from './WiredConditionBaseView'; const TEAM_OPTIONS = [ 1, 2, 3, 4 ]; const COMPARISON_OPTIONS = [ 0, 1, 2 ]; const MIN_SCORE = 0; const MAX_SCORE = 999; const SCORE_PATTERN = /^\d*$/; const clampScore = (value: number) => { if(isNaN(value)) return MIN_SCORE; return Math.max(MIN_SCORE, Math.min(MAX_SCORE, Math.floor(value))); }; export const WiredConditionTeamHasScoreView: FC<{}> = () => { const { trigger = null, setIntParams = null } = useWired(); const [ team, setTeam ] = useState(1); const [ comparison, setComparison ] = useState(1); const [ score, setScore ] = useState(0); const [ scoreInput, setScoreInput ] = useState('0'); const [ userSource, setUserSource ] = useState(0); const [ quantifier, setQuantifier ] = useState(0); const [ showAdvanced, setShowAdvanced ] = useState(false); useEffect(() => { if(!trigger) return; const nextTeam = (trigger.intData.length > 0) ? trigger.intData[0] : 1; const nextComparison = (trigger.intData.length > 1) ? trigger.intData[1] : 1; const nextScore = clampScore((trigger.intData.length > 2) ? trigger.intData[2] : 0); const nextUserSource = (trigger.intData.length > 3) ? trigger.intData[3] : 0; const nextQuantifier = (trigger.intData.length > 4) ? trigger.intData[4] : 0; setTeam(TEAM_OPTIONS.includes(nextTeam) ? nextTeam : 1); setComparison(COMPARISON_OPTIONS.includes(nextComparison) ? nextComparison : 1); setScore(nextScore); setScoreInput(nextScore.toString()); setUserSource(nextUserSource); setQuantifier((nextQuantifier === 1) ? 1 : 0); setShowAdvanced(nextUserSource !== 0 || nextQuantifier !== 0); }, [ trigger ]); const updateScore = (value: number) => { const nextValue = clampScore(value); setScore(nextValue); setScoreInput(nextValue.toString()); }; const updateScoreInput = (value: string) => { if(!SCORE_PATTERN.test(value)) return; setScoreInput(value); if(!value.length) { setScore(0); return; } updateScore(parseInt(value)); }; const save = () => { setIntParams([ team, comparison, clampScore(score), userSource, quantifier ]); }; return ( { showAdvanced && <>
{ LocalizeText('wiredfurni.params.quantifier_selection') } { [ 0, 1 ].map(value => { return (
setQuantifier(value) } /> { LocalizeText(`wiredfurni.params.quantifier.users.${ value }`) }
); }) }
} }>
{ LocalizeText('wiredfurni.params.team') } { TEAM_OPTIONS.map(value => { return (
setTeam(value) } /> { LocalizeText(`wiredfurni.params.team.${ value }`) }
); }) }
{ LocalizeText('wiredfurni.params.comparison_selection') } { COMPARISON_OPTIONS.map(value => { return (
setComparison(value) } /> { LocalizeText(`wiredfurni.params.comparison.${ value }`) }
); }) }
{ LocalizeText('wiredfurni.params.setscore2') } setScoreInput(clampScore(score).toString()) } onChange={ event => updateScoreInput(event.target.value) } />
updateScore(event as number) } /> { score }
); };