1
0
Fork 0

Make it update state

Signed-off-by: Egor Savkin <es@m-labs.hk>
This commit is contained in:
Egor Savkin 2023-08-17 16:41:50 +08:00
parent d543e54326
commit 1a7d44c121
3 changed files with 90 additions and 40 deletions

8
package-lock.json generated
View File

@ -7,6 +7,9 @@
"": { "": {
"name": "m-labs-zola", "name": "m-labs-zola",
"version": "1.0.0", "version": "1.0.0",
"dependencies": {
"json-logic-js": "^2.0.2"
},
"devDependencies": { "devDependencies": {
"@babel/cli": "^7.22.6", "@babel/cli": "^7.22.6",
"@babel/core": "^7.22.8", "@babel/core": "^7.22.8",
@ -3655,6 +3658,11 @@
"node": ">=4" "node": ">=4"
} }
}, },
"node_modules/json-logic-js": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/json-logic-js/-/json-logic-js-2.0.2.tgz",
"integrity": "sha512-ZBtBdMJieqQcH7IX/LaBsr5pX+Y5JIW+EhejtM3Ffg2jdN9Iwf+Ht6TbHnvAZ/YtwyuhPaCBlnvzrwVeWdvGDQ=="
},
"node_modules/json-parse-even-better-errors": { "node_modules/json-parse-even-better-errors": {
"version": "2.3.1", "version": "2.3.1",
"resolved": "https://registry.npmjs.org/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz", "resolved": "https://registry.npmjs.org/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz",

View File

@ -706,7 +706,16 @@ class ProductCartItem extends React.PureComponent {
style={{'display': (model.showOverlayRemove && options) ? 'flex' : 'none'}} style={{'display': (model.showOverlayRemove && options) ? 'flex' : 'none'}}
className="overlayVariant"> className="overlayVariant">
{ProcessOptions({options: options, data: options_data, target: options_data})} <ProcessOptions
options={options}
data={options_data}
key={"processed_options" + index}
id={"processed_options" + index}
target={((outvar, value) => {
options_data[outvar] = value;
this.setState(options_data);
})}
/>
</div> </div>
) : null } ) : null }
@ -867,6 +876,7 @@ class Cart extends React.PureComponent {
isTouch={isTouch} isTouch={isTouch}
hovered={item.id === itemHovered} hovered={item.id === itemHovered}
key={item.id} key={item.id}
id={item.id}
index={index} index={index}
data={itemData} data={itemData}
onToggleOverlayRemove={onToggleOverlayRemove} onToggleOverlayRemove={onToggleOverlayRemove}

View File

@ -1,35 +1,56 @@
'use strict'; 'use strict';
import React, {useState} from "react"; import React, {Component} from "react";
import jsonLogic from 'json-logic-js'; import jsonLogic from 'json-logic-js';
import {v4 as uuidv4} from 'uuid'; import {v4 as uuidv4} from 'uuid';
// https://stackoverflow.com/a/70511311 // https://stackoverflow.com/a/70511311
const trueTypeOf = (obj) => Object.prototype.toString.call(obj).slice(8, -1).toLowerCase(); const trueTypeOf = (obj) => Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
function Radio(target, {title, variants, outvar}) { class Radio extends Component {
console.log(target) constructor(props) {
function handleClick(variant) { super(props);
console.log(variant) // Initialize the state object with the initial values from the props
target[outvar] = variant; this.state = {
//this.setState() target: props.target,
outvar: props.outvar,
variants: props.variants,
variant: props.variants[0],
id: props.id
};
// Bind the event handler to this
this.handleClick = this.handleClick.bind(this);
this.state.target(this.state.outvar, this.state.variant);
} }
handleClick(variant) {
console.log(variant);
// Update the state object with the new value for outvar
this.setState({
...this.state,
variant: variant
});
this.state.target(this.state.outvar, variant);
}
render() {
let key = this.state.id + this.state.outvar;
return ( return (
<div className="shop-radio" key={uuidv4()}> <div className="shop-radio" key={this.state.id}>
{title} {this.props.title}
{variants.map((variant, _) => ( {this.props.variants.map((variant, _) => (
<div className="form-check" key={outvar + variant}> <div className="form-check" key={key + variant}>
<input <input
className="form-check-input" className="form-check-input"
type="radio" type="radio"
name={outvar} name={key}
id={outvar + variant} id={key + variant}
checked={target[outvar] === variant} checked={this.state.variant === variant}
onClick={() => handleClick(variant)} onClick={() => this.handleClick(variant)}
onChange={() => handleClick(variant)} onChange={() => this.handleClick(variant)}
/> />
<label className="form-check-label" htmlFor={outvar + variant}> <label className="form-check-label" htmlFor={key + variant}>
{variant} {variant}
</label> </label>
</div> </div>
@ -37,24 +58,35 @@ function Radio(target, {title, variants, outvar}) {
</div> </div>
); );
} }
}
function MissingComponent(...args) { function RadioWrapper(target, id, {title, variants, outvar}) {
console.error("Missing component with args:", args) return <Radio target={target} title={title} variants={variants} outvar={outvar} key={id} id={id}/>;
return <label style={{background: "red"}}>MISSING</label> }
function MissingComponent(type, id) {
//console.error("Missing component with type:", type)
return <div key={type + id} style={{background: "red"}}>MISSING</div>
} }
const componentsList = { const componentsList = {
"Radio": Radio, "Radio": RadioWrapper,
"Default": MissingComponent, "Default": MissingComponent,
}; };
export default function ProcessOptions({options, data, target}) { export default function ProcessOptions({options, data, target, id}) {
let options_t = trueTypeOf(options); let options_t = trueTypeOf(options);
if (options_t === "array") { if (options_t === "array") {
return Array.from( return Array.from(
options.map((option_item, _) => ProcessOptions({options: option_item, data: data, target: target})) options.map((option_item, i) => ProcessOptions({
options: option_item,
data: data,
target: target,
id: id + i
}))
); );
} else if (options_t === "object") { } else if (options_t === "object") {
if ( if (
@ -62,12 +94,12 @@ export default function ProcessOptions({options, data, target}) {
trueTypeOf(options.args) === "object" trueTypeOf(options.args) === "object"
) { ) {
if (options.type in componentsList) { if (options.type in componentsList) {
return componentsList[options.type](target, options.args); return componentsList[options.type](target, id + options.type, options.args);
} else { } else {
return componentsList["Default"](target, options.args); return componentsList["Default"](options.type, id + "missing");
} }
} else { } else {
return ProcessOptions({options: jsonLogic.apply(options, data), data: data, target: target}); return ProcessOptions({options: jsonLogic.apply(options, data), data: data, target: target, id: id});
} }
} }
} }