|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { GridStatement } from './utils'; |
| 3 | + |
| 4 | +interface StatementProps { |
| 5 | + correctStatement: GridStatement |
| 6 | + isCorrect: boolean |
| 7 | + setIsCorrect: React.Dispatch<React.SetStateAction<boolean>> |
| 8 | + text: string |
| 9 | + setText: React.Dispatch<React.SetStateAction<string>> |
| 10 | +} |
| 11 | + |
| 12 | +export const StatementInput: React.FC<StatementProps> = ({ |
| 13 | + correctStatement, isCorrect, setIsCorrect, text, setText |
| 14 | +}:StatementProps) => { |
| 15 | + const [feedback, setFeedback] = useState<string>( |
| 16 | + 'ERROR: This feedback should not be visible.') |
| 17 | + const [submitted, setSubmitted] = useState<boolean>(false); |
| 18 | + |
| 19 | + const buttonList = ['∀', '→', '∃', '∧', '≥']; |
| 20 | + |
| 21 | + /** |
| 22 | + * Build an object of the key-value pairs extracted from a given statement. |
| 23 | + * |
| 24 | + * e.g: "Key1(x, value1) ∧ Key2(x, value2)" => {key: value, ..., key2: value2} |
| 25 | + * @param text |
| 26 | + */ |
| 27 | + const pullData = (text:string):Object => { |
| 28 | + const rules = {}; |
| 29 | + const found = text.match(/\w+\(\w+.[\w\s]+\)+?/g); |
| 30 | + if (found) { |
| 31 | + found.forEach((keyValue) => { |
| 32 | + const key = keyValue.match(/\w+(?=\(\w+[^\)])/); |
| 33 | + const value = keyValue.match(/\w[\w\s]*(?=\))/); |
| 34 | + if (key != null && value != null) { |
| 35 | + rules[key.toString().toLowerCase()] = |
| 36 | + value.toString().toLowerCase(); |
| 37 | + } |
| 38 | + }); |
| 39 | + } |
| 40 | + return rules; |
| 41 | + }; |
| 42 | + |
| 43 | + /** |
| 44 | + * Splits the statement into a left and right object around the if |
| 45 | + * statement. |
| 46 | + * @param text |
| 47 | + */ |
| 48 | + const parseStatement = (text:string):Object[] => { |
| 49 | + const sides = text.split('→'); |
| 50 | + if (sides.length == 2) { |
| 51 | + return [pullData(sides[0]), pullData(sides[1])] |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + const directionalRelationships = ['Top(y,x)', 'TopLeftOf(y,x)', |
| 56 | + 'TopRightOf(y,x)', 'LeftOf(y,x)', 'RightOf(y,x)', 'Below(y,x)', |
| 57 | + 'BottomLeftOf(y,x)', 'BottomRightOf(y,x)']; |
| 58 | + |
| 59 | + const objectRelationships = ['Shape(x/y, Circle/Square/Triangle)', |
| 60 | + 'Color(x/y, Blue/Green/Red))', 'Value(x/y, 0 to 9))', 'Prime(Value(x/y))', |
| 61 | + 'Location(x/y, top/bottom/left/right [number of subsets] rows/columns', |
| 62 | + 'MultipleOf(Value(x/y))']; |
| 63 | + |
| 64 | + const mkList = (items:string[], uniqueClass='') => |
| 65 | + <ul className={`list-group-flush ps-2 ${uniqueClass}`}> |
| 66 | + {items.map((item, i) => |
| 67 | + <li key={i} className='list-group-item'>{item}</li> |
| 68 | + )} |
| 69 | + </ul> |
| 70 | + |
| 71 | + const mkBtnList = (items:string[]) => |
| 72 | + <ul className="list-inline row my-2"> |
| 73 | + {items.map((item:string, i:number) => |
| 74 | + <li key={i} className='col-auto'> |
| 75 | + <button className='btn btn-outline-secondary' |
| 76 | + aria-label={`Add a ${item} symbol to the statement.`} |
| 77 | + onClick={mkAddChar(item)} |
| 78 | + > |
| 79 | + {item}</button> |
| 80 | + </li> |
| 81 | + )} |
| 82 | + </ul> |
| 83 | + |
| 84 | + const mkAddChar = (char:string) => () => { |
| 85 | + const el = |
| 86 | + document.getElementById('statement-text') as HTMLInputElement; |
| 87 | + el.value = el.value + char |
| 88 | + el.focus(); |
| 89 | + }; |
| 90 | + |
| 91 | + const evaluate = (check:Object[], evalObj:Object[]) => { |
| 92 | + if (check.length == evalObj.length) { |
| 93 | + for (let i in evalObj) { |
| 94 | + for (let [key, value] of Object.entries(evalObj[i])) { |
| 95 | + if (!check[i][key] || check[i][key] !== value) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + return true; |
| 101 | + } |
| 102 | + return false; |
| 103 | + }; |
| 104 | + |
| 105 | + const handleCheck = (e) => { |
| 106 | + setSubmitted(true); |
| 107 | + const el = |
| 108 | + document.getElementById('statement-text') as HTMLInputElement; |
| 109 | + if (el.value.length > 0) { |
| 110 | + const check = parseStatement(el.value); |
| 111 | + if (el.value.match(/^\∀x\s*\(.*\)\s*$/)) { |
| 112 | + setIsCorrect(evaluate(check, |
| 113 | + parseStatement(correctStatement.formalFOLStatement)) |
| 114 | + ); |
| 115 | + } else { |
| 116 | + setIsCorrect(false); |
| 117 | + } |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + const handleText = (e) => { |
| 122 | + setText(e.value); |
| 123 | + }; |
| 124 | + |
| 125 | + useEffect(() => { |
| 126 | + if (isCorrect) { |
| 127 | + setFeedback('Good! XD'); |
| 128 | + } else { |
| 129 | + setFeedback('Ooops <:O'); |
| 130 | + } |
| 131 | + }, [isCorrect]) |
| 132 | + |
| 133 | + useEffect(() => { |
| 134 | + setSubmitted(false); |
| 135 | + console.log(correctStatement.formalFOLStatement); |
| 136 | + }, []) |
| 137 | + |
| 138 | + return <section className='col-4'> |
| 139 | + <p>Enter the statment that defines the relationship of</p> |
| 140 | + <p>{correctStatement.naturalLanguageStatement}</p> |
| 141 | + {mkBtnList(buttonList)} |
| 142 | + <textarea id='statement-text' className='form-control mb-2' onChange={handleText} |
| 143 | + placeholder='Enter the value here' value={text}></textarea> |
| 144 | + <button type='submit' className='btn btn-primary my-2' |
| 145 | + onClick={handleCheck}>Check Statement</button> |
| 146 | + {submitted && (isCorrect ? <p className='text-success'>{feedback}</p> : |
| 147 | + <p className='text-danger'>{feedback}</p>)} |
| 148 | + <p className='col-12 fs-4 my-2'>Relationships</p> |
| 149 | + <div className="row"> |
| 150 | + <div className="col-6"> |
| 151 | + <strong>Object</strong> |
| 152 | + {mkList(objectRelationships)} |
| 153 | + </div> |
| 154 | + <div className="col-6"> |
| 155 | + <strong>Directional</strong> |
| 156 | + {mkList(directionalRelationships)} |
| 157 | + </div> |
| 158 | + </div> |
| 159 | + </section> |
| 160 | +} |
0 commit comments