All files / components/quick-order quick-order.tsx

63.04% Statements 29/46
37.83% Branches 14/37
50% Functions 5/10
64.44% Lines 29/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 1691x   1x 1x 1x     1x             3x   3x   3x   3x           3x   1x 1x   2x         2x   2x 2x         2x 2x         2x 2x                                                             5x 5x             5x 3x                       2x 2x 2x 2x   2x                                                                                                                        
import { Component, h, Prop, Host, State, Watch, Element } from "@stencil/core";
import { QuickOrderResponse } from "./quick-order-response";
import api from "../../utils/api";
import { buildUrl } from "../../utils/url";
import { debounce } from "../../utils/debounce";
import { ButtonSize, ButtonVariant } from "@trumpf/ux.ui/dist/types/components";
 
@Component({
    tag: "ux-w-quick-order",
    styleUrl: "quick-order.scss",
    shadow: false
})
export class QuickOrder {
    /** URL where this widget can request the data */
    @Prop() url: string = "";
    /** This holds the response from the API with all Information required to show the widget */
    @State() data: QuickOrderResponse = null;
    /** width of the element - this helps with styling. Media queries can not be used because the width of the element can be independent from width of the window */
    @State() width: number = 400;
    /** data is loading */
    @State() loading: boolean = false;
 
    @Element() domNode: HTMLUxWQuickOrderElement;
 
    @Watch("url")
    componentWillLoad() {
        if (this.url === "") {
            // eslint-disable-next-line no-console
            console.warn("url needs to be supplied for WidgetQuickOrder");
            return;
        }
        window.setTimeout(() => {
            Iif (this.data === null) {
                this.loading = true;
            }
        });
        api<QuickOrderResponse>(this.url)
            .then((data: QuickOrderResponse) => {
                this.data = data;
                this.loading = false;
            })
            // eslint-disable-next-line no-console
            .catch((error) => console.error(error));
 
        this.setWidth();
        window.onresize = debounce(() => this.setWidth(), 300);
    }
 
    private setWidth() {
        const width =
            this.domNode?.parentElement?.getBoundingClientRect().width;
        Iif (width > 0) {
            this.width = width;
        }
    }
 
    private onSubmit(event) {
        event.preventDefault();
        const textInput = this.domNode.querySelector("input[type=text]");
        const numberInput = this.domNode.querySelector("input[type=number]");
        const text = (textInput as HTMLInputElement).value;
        const number = (numberInput as HTMLInputElement).value;
        const { amountParamName, materialNumberParamName, orderUrl } =
            this.data;
        Iif (
            orderUrl?.length < 1 ||
            amountParamName?.length < 1 ||
            materialNumberParamName?.length < 1
        ) {
            // eslint-disable-next-line no-console
            console.error(
                "Widget Quick Order API needs to provide valid orderUrl, amountParamName and materialNumberParamName"
            );
            return;
        }
        window.location.href = buildUrl(orderUrl, {
            [amountParamName]: number,
            [materialNumberParamName]: text
        });
    }
 
    render() {
        const baseClass = "ux-w-quick-order";
        Iif (this.loading) {
            return (
                <Host class={baseClass}>
                    <ux-w-wrapper isLoading />
                </Host>
            );
        }
        if (this.data === null) {
            return null;
        }
        const {
            link,
            linkTitle,
            title,
            inputLabel,
            inputPlaceholder,
            suggestionUrl,
            orderButton,
            noData,
            noDataText
        } = this.data;
        const { width } = this;
        const isSmall = width <= 380;
        const isMedium = width <= 740;
 
        return (
            <Host
                class={{
                    [baseClass]: true,
                    [`${baseClass}--small`]: isSmall,
                    [`${baseClass}--medium`]: isMedium
                }}
            >
                <ux-w-wrapper url={link} linkTitle={linkTitle} headline={title}>
                    <div slot="content">
                        {noData && (
                            <ux-w-empty icon="crane">
                                <div innerHTML={noDataText} />
                            </ux-w-empty>
                        )}
                        {!noData && (
                            <form
                                class={`${baseClass}__form`}
                                onSubmit={(event) => this.onSubmit(event)}
                            >
                                <div class={`${baseClass}__text`}>
                                    <ux-autosuggest-input
                                        label={inputLabel}
                                        placeholder={inputPlaceholder}
                                        url={suggestionUrl}
                                    />
                                </div>
                                <div class={`${baseClass}__number`}>
                                    <ux-number-input
                                        min="1"
                                        max="99"
                                        initialValue="1"
                                    />
                                </div>
                                <div class={`${baseClass}__button`}>
                                    <ux-button
                                        variant={"primary" as ButtonVariant}
                                        size={
                                            isSmall
                                                ? ("icon" as ButtonSize)
                                                : undefined
                                        }
                                        type="submit"
                                        title={orderButton}
                                    >
                                        {isSmall ? (
                                            <ux-icon slot="icon" name="cart" />
                                        ) : (
                                            orderButton
                                        )}
                                    </ux-button>
                                </div>
                            </form>
                        )}
                    </div>
                </ux-w-wrapper>
            </Host>
        );
    }
}