From 141c78ffa487f3a97e7b4e8d868724d16adce663 Mon Sep 17 00:00:00 2001
From: chris <kokosias@yahoo.gr>
Date: Fri, 10 Nov 2023 19:20:15 +0200
Subject: [PATCH] save exact answers

---
 editors/demo/src/HHMI/HHMI.js                 |  5 +--
 .../components/ExactAnswerComponent.js        | 33 +++++++++++++++++--
 .../NumericalAnswerContainerComponent.js      |  6 ++--
 .../components/PreciseAnswerComponent.js      |  2 +-
 .../schema/NumericalAnswerContainerNode.js    |  6 ++--
 5 files changed, 40 insertions(+), 12 deletions(-)

diff --git a/editors/demo/src/HHMI/HHMI.js b/editors/demo/src/HHMI/HHMI.js
index 1b9ce865f..19ab37c7b 100644
--- a/editors/demo/src/HHMI/HHMI.js
+++ b/editors/demo/src/HHMI/HHMI.js
@@ -167,10 +167,11 @@ const Hhmi = () => {
         ref={editorRef}
         customValues={{ showFeedBack: submitted, testMode }}
         fileUpload={file => renderImage(file)}
-        value={content}
+        // value={content}
+        targetFormat="JSON"
         readonly={readOnly}
         layout={HhmiLayout}
-        // onChange={source => console.log(source)}
+        onChange={source => console.log(source)}
       />
     </>
   );
diff --git a/wax-questions-service/src/NumericalAnswerService/components/ExactAnswerComponent.js b/wax-questions-service/src/NumericalAnswerService/components/ExactAnswerComponent.js
index ac9002e36..a9e8452d8 100644
--- a/wax-questions-service/src/NumericalAnswerService/components/ExactAnswerComponent.js
+++ b/wax-questions-service/src/NumericalAnswerService/components/ExactAnswerComponent.js
@@ -1,6 +1,6 @@
-import React, { useRef, useState } from 'react';
+import React, { useRef, useState, useContext } from 'react';
 import styled from 'styled-components';
-import { DocumentHelpers } from 'wax-prosemirror-core';
+import { DocumentHelpers, WaxContext } from 'wax-prosemirror-core';
 
 const AnswerContainer = styled.div`
   display: flex;
@@ -26,7 +26,9 @@ const ValueInnerContainer = styled.div`
   flex-direction: column;
 `;
 
-const ExactAnswerComponent = () => {
+const ExactAnswerComponent = ({ getPos, node }) => {
+  const context = useContext(WaxContext);
+  const { activeView } = context;
   const [exact, setExact] = useState('');
   const [marginError, setMarginError] = useState('');
 
@@ -40,12 +42,37 @@ const ExactAnswerComponent = () => {
       .replace(/^0[^.]/, '0');
   };
 
+  const SaveValuesToNode = () => {
+    const allNodes = getNodes(context.pmViews.main);
+    allNodes.forEach(singleNode => {
+      if (singleNode.node.attrs.id === node.attrs.id) {
+        const obj = {
+          exactAnswer: onlyNumbers(exactRef.current.value),
+          marginError: onlyNumbers(errorRef.current.value),
+        };
+
+        context.pmViews.main.dispatch(
+          context.pmViews.main.state.tr.setNodeMarkup(
+            singleNode.pos,
+            undefined,
+            {
+              ...singleNode.node.attrs,
+              answersExact: obj,
+            },
+          ),
+        );
+      }
+    });
+  };
+
   const onChangeExact = () => {
     setExact(onlyNumbers(exactRef.current.value));
+    SaveValuesToNode();
   };
 
   const onChangeError = () => {
     setMarginError(onlyNumbers(errorRef.current.value));
+    SaveValuesToNode();
   };
 
   return (
diff --git a/wax-questions-service/src/NumericalAnswerService/components/NumericalAnswerContainerComponent.js b/wax-questions-service/src/NumericalAnswerService/components/NumericalAnswerContainerComponent.js
index 8d05991f4..8abd8c6b8 100644
--- a/wax-questions-service/src/NumericalAnswerService/components/NumericalAnswerContainerComponent.js
+++ b/wax-questions-service/src/NumericalAnswerService/components/NumericalAnswerContainerComponent.js
@@ -106,13 +106,13 @@ export default ({ node, view, getPos }) => {
         <NumericalAnswerOption>
           {!options[node.attrs.id] && <>No Type Selected</>}
           {options[node.attrs.id]?.numericalAnswer === 'exactAnswer' && (
-            <ExactAnswerComponent />
+            <ExactAnswerComponent getPos={getPos} node={node} />
           )}
           {options[node.attrs.id]?.numericalAnswer === 'rangeAnswer' && (
-            <RangeAnswerComponent />
+            <RangeAnswerComponent getPos={getPos} node={node} />
           )}
           {options[node.attrs.id]?.numericalAnswer === 'preciseAnswer' && (
-            <PreciseAnswerComponent />
+            <PreciseAnswerComponent getPos={getPos} node={node} />
           )}
         </NumericalAnswerOption>
         {!testMode && !(readOnly && feedback === '') && (
diff --git a/wax-questions-service/src/NumericalAnswerService/components/PreciseAnswerComponent.js b/wax-questions-service/src/NumericalAnswerService/components/PreciseAnswerComponent.js
index 38c34b2ea..506230e5a 100644
--- a/wax-questions-service/src/NumericalAnswerService/components/PreciseAnswerComponent.js
+++ b/wax-questions-service/src/NumericalAnswerService/components/PreciseAnswerComponent.js
@@ -33,7 +33,7 @@ const PreciseAnswerComponent = () => {
 
   const onlyNumbers = value => {
     return value
-      .replace(/[^0-9.]/g, '')
+      .replace(/[^0-9.;]/g, '')
       .replace(/(\..*?)\..*/g, '$1')
       .replace(/^0[^.]/, '0');
   };
diff --git a/wax-questions-service/src/NumericalAnswerService/schema/NumericalAnswerContainerNode.js b/wax-questions-service/src/NumericalAnswerService/schema/NumericalAnswerContainerNode.js
index b110d5f24..e70db45d3 100644
--- a/wax-questions-service/src/NumericalAnswerService/schema/NumericalAnswerContainerNode.js
+++ b/wax-questions-service/src/NumericalAnswerService/schema/NumericalAnswerContainerNode.js
@@ -4,7 +4,7 @@ const NumericalAnswerContainerNode = {
     class: { default: 'numerical-answer' },
     feedback: { default: '' },
     answerType: { default: '' },
-    answers: { default: [] },
+    answersExact: { default: [] },
   },
   group: 'block questions',
   atom: true,
@@ -14,7 +14,7 @@ const NumericalAnswerContainerNode = {
       tag: 'div.numerical-answer',
       getAttrs(dom) {
         return {
-          answers: JSON.parse(dom.getAttribute('answers')),
+          answersExact: JSON.parse(dom.getAttribute('answersExact')),
           id: dom.getAttribute('id'),
           class: dom.getAttribute('class'),
           feedback: dom.getAttribute('feedback'),
@@ -29,7 +29,7 @@ const NumericalAnswerContainerNode = {
       {
         id: node.attrs.id,
         class: node.attrs.class,
-        answers: JSON.stringify(node.attrs.answers),
+        answersExact: JSON.stringify(node.attrs.answersExact),
         feedback: node.attrs.feedback,
         answerType: node.attrs.answerType,
       },
-- 
GitLab