From 1a7d44c121be422624c23a13c30a89754a9bc2fd Mon Sep 17 00:00:00 2001 From: Egor Savkin Date: Thu, 17 Aug 2023 16:41:50 +0800 Subject: [PATCH] Make it update state Signed-off-by: Egor Savkin --- package-lock.json | 8 +++ static/js/shop.jsx | 12 +++- static/js/shop_components.jsx | 110 ++++++++++++++++++++++------------ 3 files changed, 90 insertions(+), 40 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9ebce87..77875ba 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,6 +7,9 @@ "": { "name": "m-labs-zola", "version": "1.0.0", + "dependencies": { + "json-logic-js": "^2.0.2" + }, "devDependencies": { "@babel/cli": "^7.22.6", "@babel/core": "^7.22.8", @@ -3655,6 +3658,11 @@ "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": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz", diff --git a/static/js/shop.jsx b/static/js/shop.jsx index 74b4115..44304e2 100644 --- a/static/js/shop.jsx +++ b/static/js/shop.jsx @@ -706,7 +706,16 @@ class ProductCartItem extends React.PureComponent { style={{'display': (model.showOverlayRemove && options) ? 'flex' : 'none'}} className="overlayVariant"> - {ProcessOptions({options: options, data: options_data, target: options_data})} + { + options_data[outvar] = value; + this.setState(options_data); + })} + /> ) : null } @@ -867,6 +876,7 @@ class Cart extends React.PureComponent { isTouch={isTouch} hovered={item.id === itemHovered} key={item.id} + id={item.id} index={index} data={itemData} onToggleOverlayRemove={onToggleOverlayRemove} diff --git a/static/js/shop_components.jsx b/static/js/shop_components.jsx index d1fc2bc..79625a6 100644 --- a/static/js/shop_components.jsx +++ b/static/js/shop_components.jsx @@ -1,60 +1,92 @@ 'use strict'; -import React, {useState} from "react"; +import React, {Component} from "react"; import jsonLogic from 'json-logic-js'; -import { v4 as uuidv4 } from 'uuid'; +import {v4 as uuidv4} from 'uuid'; // https://stackoverflow.com/a/70511311 const trueTypeOf = (obj) => Object.prototype.toString.call(obj).slice(8, -1).toLowerCase(); -function Radio(target, {title, variants, outvar}) { - console.log(target) - function handleClick(variant) { - console.log(variant) - target[outvar] = variant; - //this.setState() +class Radio extends Component { + constructor(props) { + super(props); + // Initialize the state object with the initial values from the props + this.state = { + 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); } - return ( -
- {title} - {variants.map((variant, _) => ( -
- handleClick(variant)} - onChange={() => handleClick(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 ( +
+ {this.props.title} + {this.props.variants.map((variant, _) => ( +
+ this.handleClick(variant)} + onChange={() => this.handleClick(variant)} + /> + +
+ ))} +
+ ); + } } -function MissingComponent(...args) { - console.error("Missing component with args:", args) - return +function RadioWrapper(target, id, {title, variants, outvar}) { + return ; +} + + +function MissingComponent(type, id) { + //console.error("Missing component with type:", type) + return
MISSING
} const componentsList = { - "Radio": Radio, + "Radio": RadioWrapper, "Default": MissingComponent, }; -export default function ProcessOptions({options, data, target}) { +export default function ProcessOptions({options, data, target, id}) { let options_t = trueTypeOf(options); if (options_t === "array") { 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") { if ( @@ -62,12 +94,12 @@ export default function ProcessOptions({options, data, target}) { trueTypeOf(options.args) === "object" ) { if (options.type in componentsList) { - return componentsList[options.type](target, options.args); - }else { - return componentsList["Default"](target, options.args); + return componentsList[options.type](target, id + options.type, options.args); + } else { + return componentsList["Default"](options.type, id + "missing"); } } 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}); } } } \ No newline at end of file