Newer
Older
import React, { useContext, useEffect, useRef, useState } from 'react';
import styled from 'styled-components';
import FeedbackComponent from './FeedbackComponent';
const MatchingContainer = styled.div`
border: 3px solid #f5f5f7;
margin-bottom: 30px;
padding: 10px;
`;
const QuestionWrapper = styled.div`
display: flex;
flex-direction: row;
const ActionButton = styled.button`
background: transparent;
`;
const StyledIconAction = styled(Icon)`
height: 24px;
width: 24px;
const CreateOptions = styled.div`
display: flex;
flex-direction: column;
padding-right: 7px;
border-radius: 12px;
&:focus {
outline: none;
}
::placeholder {
color: rgb(170, 170, 170);
font-style: italic;
}
}
background: #fff;
cursor: pointer;
background: #535e76;
cursor: pointer;
margin-right: 20px;
padding: 4px 8px 4px 8px;
}
}
export default ({ node, view, getPos }) => {
const context = useContext(WaxContext);
const {
pmViews: { main },
} = context;
const customProps = main.props.customValues;
const isEditable = main.props.editable(editable => {
return editable;
});
const readOnly = !isEditable;
/* TEMP TO SAVE NODE OPTIONS TODO: SAVE IN CONTEXT OPTIONS */
if (!addingOption) return;
allNodes.forEach(singleNode => {
if (singleNode.node.attrs.id === node.attrs.id) {
main.dispatch(
main.state.tr
.setMeta('addToHistory', false)
.setNodeMarkup(getPos(), undefined, {
...singleNode.node.attrs,
options,
}),
const addOption = () => {
if (addOptionRef.current.value.trim() === '') return;
const obj = { label: addOptionRef.current.value, value: uuidv4() };
setOptions(prevOptions => [...prevOptions, obj]);
setAddingOption(true);
setTimeout(() => {
setAddingOption(false);
});
setOptionText('');
};
const updateOptionText = () => {
setOptionText(addOptionRef.current.value);
};
const handleKeyDown = event => {
if (event.key === 'Enter' || event.which === 13) {
addOption();
}
};
const removeOption = value => {
setOptions(options.filter(option => option.value !== value));
setAddingOption(true);
setTimeout(() => {
setAddingOption(false);
});
const saveInChildOptions = allNodes => {
allNodes.forEach(singleNode => {
if (singleNode.node.attrs.id === node.attrs.id) {
singleNode.node.content.content.forEach(parentNodes => {
parentNodes.forEach(optionNode => {
if (optionNode.type.name === 'matching_option')
/* eslint-disable-next-line no-param-reassign */
optionNode.attrs.options = options;
});
});
}
});
};
return (
<MatchingWrapper>
<span>Matching</span>
<MatchingContainer className="matching">
{(!readOnly ||
(readOnly && !customProps.testMode && !customProps.showFeedBack)) && (
{options.length > 0 && (
<ul>
<li>Options: </li>
{options.map((option, index) => {
return (
{!readOnly && (
<ActionButton
onClick={() => removeOption(option.value)}
type="button"
>
<StyledIconAction name="deleteOutlined" />
</ActionButton>
)}
{!readOnly && (
<AddOption>
<input
onChange={updateOptionText}
onKeyPress={handleKeyDown}
placeholder="Type an option ..."
ref={addOptionRef}
type="text"
value={optionText}
/>
<button onClick={addOption} type="button">
Add Option
</button>
</AddOption>
)}
<FeedbackComponent
getPos={getPos}
node={node}
readOnly={readOnly}
view={view}
/>
)}
</MatchingContainer>
</MatchingWrapper>
);
};
const getNodes = view => {
const allNodes = DocumentHelpers.findBlockNodes(view.state.doc);
const matchingContainerNodes = [];
allNodes.forEach(node => {
if (node.node.type.name === 'matching_container') {
matchingContainerNodes.push(node);
}
});
return matchingContainerNodes;
};