4

I'm building a drupal based e-commerce site right now and got stuck. Still new to react and coding in general, but trying to learn. So I've got all my data pulled in, using redux and rest api, and I'm getting my products, variations and attributes. The product page is setting a specific product based on url, and now I need to be able to select the different attributes via a dropdown menu. Currently I have a place holder dropdown that matches the one shown in react-bootstrap documentation. However, I need to be placing options in that dropdown based off of my array holding the attributes.

I'm sure it's simple but I've been searching around and haven't found an answer yet that works. Hopefully you guys can help.

As you look through the code, keep in mind that sizes = [] is the array I'm looking to place data from as the selectable options in the dropdown.

Here's the product page:

import React, { Component} from 'react';
import '../../css/Home.css';
import MenuBar from "../sub-components/MenuBar";
import LeftMenuBar from "../sub-components/LeftMenuBar";
import "../../css/ProductPage.css"
import WomensWear from "../../media/WomensWear.jpg"
import {
    Dropdown,
    DropdownToggle,
    DropdownMenu,
    DropdownItem } from 'reactstrap';

class ProductPage extends Component {
    constructor(props) {
        super(props);

        this.toggle = this.toggle.bind(this);
        this.state = {
            dropdownOpen: false
        };
    }

    toggle() {
        this.setState(prevState => ({
            dropdownOpen: !prevState.dropdownOpen
        }));
    }

    getProduct() {

        let product = null;
        let sizes = [];
        if (this.props.products && this.props.products.items.length) {
            product = this.props.products.items.find(o => o.path[0].alias === this.props.router.match.url);

            if (product && this.props.variations && this.props.attributes) {
                product.something = [];
                for (let i = 0; i < product.variations.length; i++) {
                    let varid = product.variations[i].target_id;
                    let variation = this.props.variations.items.find(o => o.variation_id[0].value === varid);

                    variation.size = this.props.attributes.items.find(o => o.attribute_value_id[0].value === variation.attribute_size[0].target_id);

                    sizes.push({value: variation.size.attribute_value_id[0].value, name: variation.size.name[0].value});
                    product.something.push(variation);

                    console.log(sizes);
                }
            }
        }
        return product;
    }


    render() {
        let style = {
            height: this.props.height - 56,
        };

        let product = this.getProduct();

        let body = product && product.body.length ? product.body[0].value : null;

        return (
            <div className="App" id="default">
                <div className='MenuBar'>
                    <MenuBar/>
                </div>
                <div>
                    <div style={style} className="ProductPage row no-gutters">
                        <div className="col-xs-3 col-md-3">
                            <LeftMenuBar/>
                        </div>
                        <div className="outer col-xs-4 col-md-4">
                            <div>
                                <div id="ProductPlacement">
                                  <img src={WomensWear} alt=""/>
                                    <div id="alternate-pics">
                                        <div id="alt-pic">
                                        </div>
                                        <div id="alt-pic">
                                        </div>
                                        <div id="alt-pic">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div className="col-xs-5 col-md-5">
                            <div id="ImagePlacement">
                                <div className="ProductTitle">
                                    <h1>First Product</h1>
                                </div>
                                <hr/>
                                <div className="ProductDescription">
                                    <div dangerouslySetInnerHTML={{__html: body}} />
                                </div>
                                <div id="options">
                                    <div id="color">
                                    </div>
                                    <div id="color2">
                                    </div>
                                    <div id="color3">
                                    </div>
                                </div>
                                <div id="options">
                                    <div>
                                        <Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>
                                            <DropdownToggle caret id="size-dropdown">
                                                Size
                                            </DropdownToggle>
                                            <DropdownMenu>
                                                <DropdownItem>1</DropdownItem>
                                                <DropdownItem>3</DropdownItem>
                                                <DropdownItem>5</DropdownItem>
                                            </DropdownMenu>
                                        </Dropdown>
                                        <div className="AddToCart">
                                            <button className="AddToCart">Add To Cart</button>
                                            <button className="Price">$34.99</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}

export default ProductPage;
0

1 Answer 1

6

Neat thing about React is that you can use regular JS.

<Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>
   <DropdownToggle caret id="size-dropdown">
     Size
   </DropdownToggle>
   <DropdownMenu>
     {sizes.map(size => (
       <DropdownItem>{size}</DropdownItem>
     ))}
   </DropdownMenu>
</Dropdown>

Sidenote: select seems to be more suitable element for this but that wasn't your question.

7
  • Ah yeah, that's easy, thanks! Although can you run me through how select would be used? If that's more suitable I'd rather use that. I try to stick to best practices when I can... Commented Nov 29, 2018 at 1:42
  • Well, you want user to be able to select a size, don't you?
    – Solo
    Commented Nov 29, 2018 at 1:43
  • Im not familiar with this library but this seems to be the right place to look at. You'd use map with select input same way.
    – Solo
    Commented Nov 29, 2018 at 1:46
  • Correct. I figured you could do that through the use of an onClick function though to set the size. Commented Nov 29, 2018 at 1:47
  • I see where you're going with it but this is the library I'm looking at react-bootstrap.github.io/components/forms Commented Nov 29, 2018 at 1:51

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.