From cf0c971ee3cce3e48c929f244d19589d4aeada5a Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Tue, 17 Dec 2019 19:10:48 -0800 Subject: [PATCH] hook feature submit to reducer dispatch ADD_FEATURE --- src/PhonoChangeApplier.js | 2 +- src/components/Features.js | 112 ++++++++++++++++++++++++-------- src/components/Features.test.js | 2 +- 3 files changed, 87 insertions(+), 29 deletions(-) diff --git a/src/PhonoChangeApplier.js b/src/PhonoChangeApplier.js index fd8b82c..63265ba 100644 --- a/src/PhonoChangeApplier.js +++ b/src/PhonoChangeApplier.js @@ -123,7 +123,7 @@ const PhonoChangeApplier = () => { return (
- + diff --git a/src/components/Features.js b/src/components/Features.js index f33208a..fbd8f65 100644 --- a/src/components/Features.js +++ b/src/components/Features.js @@ -2,28 +2,60 @@ import React, {useState} from 'react'; import './Features.scss'; +import type { featureAction } from '../reducers/stateReducer.features'; + const parseFeaturesFromPhonemeObject = phonesObject => { - let featureMap = Object.keys(phonesObject).reduce((featureObject, phoneName) => { - let phone = phonesObject[phoneName]; - Object.keys(phone.features).forEach(feature => { - if (!featureObject[feature]) featureObject[feature] = {plus: [], minus: []} - if (phone.features[feature]) featureObject[feature].plus.push(phone.grapheme) - else featureObject[feature].minus.push(phone.grapheme) - }); - return featureObject; - }, {}) + const getFeatureMap = (phonesObject) => { + return Object.keys(phonesObject).reduce((featureObject, phoneName) => { + let phone = phonesObject[phoneName]; + Object.keys(phone.features).forEach(feature => { + if (!featureObject[feature]) featureObject[feature] = {plus: [], minus: []} + if (phone.features[feature]) featureObject[feature].plus.push(phone.grapheme) + else featureObject[feature].minus.push(phone.grapheme) + }); + return featureObject; + }, {}) + } - return Object.keys(featureMap).map(feature => { - const plusPhones = featureMap[feature].plus.join('|'); - const minusPhones = featureMap[feature].minus.join('|'); - return ( -
  • - {`[+ ${feature}] = ${plusPhones}`}{'\t\t\t'} - {`[- ${feature}] = ${minusPhones}`} -
  • - ) - }); + const getFeatureMapJSX = (featureMap) => { + return Object.keys(featureMap).map(feature => { + const plusPhones = featureMap[feature].plus.join('|'); + const minusPhones = featureMap[feature].minus.join('|'); + return ( +
  • + {`[+ ${feature}] = ${plusPhones}`} + {`[- ${feature}] = ${minusPhones}`} +
  • + ) + }); + } + + const featureMap = getFeatureMap(phonesObject); + const featureMapJSX = getFeatureMapJSX(featureMap); + return featureMapJSX; +} + +const buildReducerAction = (e, newPositivePhones, newNegativePhones, feature): featureAction => { + e.preventDefault(); + const positivePhones = [] + newPositivePhones !== '' + ? newPositivePhones.split('/').forEach(phone => positivePhones.push(phone.trim())) + : positivePhones.push('') + + const negativePhones = [] + newNegativePhones !== '' + ? newNegativePhones.split('/').forEach(phone => negativePhones.push(phone.trim())) + : negativePhones.push('') + + return { + type: "ADD_FEATURE", + value: { + positivePhones, + negativePhones, + feature + } + } } const getPhonemesFromFeatureSubmission = (props, newPhonemes, feature) => { @@ -38,34 +70,60 @@ const getPhonemesFromFeatureSubmission = (props, newPhonemes, feature) => { } const Features = (props) => { - const [feature, setFeature] = useState('nasal') - const [newPhonemes, setNewPhonemes] = useState('n / m / ŋ') + const [feature, setFeature] = useState('aspirated') + const [ newPositivePhones, setNewPositivePhones ] = useState('tʰ / pʰ / kʰ'); + const [ newNegativePhones, setNewNegativePhones ] = useState('t / p / k'); const newFeaturesSubmit = e => { e.preventDefault(); - let newPhonemeObject = getPhonemesFromFeatureSubmission(props, newPhonemes, feature); - props.setPhonemes(newPhonemeObject); - if (!props.features || !props.features.includes(feature)) props.setFeatures([...props.features, feature]) + // let newPhonemeObject = getPhonemesFromFeatureSubmission(props, newPhonemes, feature); + // props.setPhonemes(newPhonemeObject); + // if (!props.features || !props.features.includes(feature)) props.setFeatures([...props.features, feature]) setFeature(''); - setNewPhonemes(''); + setNewPositivePhones(''); + setNewNegativePhones(''); } return (
    +

    Phonetic Features

    +
      {props.phones ? <>{parseFeaturesFromPhonemeObject(props.phones)} : <>}
    +
    setFeature(e.target.value)} + > + + {/* ! Positive Phones */} + + setNewPositivePhones(e.target.value)} + > + + {/* ! Negative Phones */} + + setNewNegativePhones(e.target.value)} + > + + props.dispatch(buildReducerAction(e, newPositivePhones, newNegativePhones, feature))} + value="Add feature" > - setNewPhonemes(e.target.value)}> -
    +
    ); } diff --git a/src/components/Features.test.js b/src/components/Features.test.js index 02595c9..0ae997a 100644 --- a/src/components/Features.test.js +++ b/src/components/Features.test.js @@ -27,6 +27,6 @@ describe('Features', () => { }/>); expect(getByTestId('Features-list')) - .toContainHTML('
  • [+ nasal] = n
  • [+ occlusive] = n
  • [- vowel] = n
  • '); + .toContainHTML('
    • [+ nasal] = n[- nasal] =
    • [+ occlusive] = n[- occlusive] =
    • [+ vowel] = [- vowel] = n
    '); }); }); \ No newline at end of file