Wed Jan 19 2022
Copied to clipboard! Copy reply
  • 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
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
// too many p

import React from "react";
import styled from "styled-components";
import { graphql } from "gatsby";
import { PageTemplateWrapper } from "@resourcehub/resourcehub-components";
import configGenerator from "@resourcehub/resourcehub-configuration";
import beautify from "json-beautify";

const ProductBoxesContainer = styled.div`
    margin-top: 50px;
    display: flex;
    flex-direction: column;
    justify-content: center;
`;

const PriceBox = styled.div`
    border: 1px solid gray;
    margin: 10px;
    padding: 10px;
    height: 400px;
    // width: 300px;
`;

const Page = ({ data }) => {
    const { pagesGQL, productsGQL, site } = data;

    console.log(pagesGQL);
    const page = {};
    const products = [];

    // Create Products array with Objects like { sku: ..., msrp: ...}
    for (const product of productsGQL.edges) {
        for (const language of product.node.data.Languages) {
            const productObject = {
                sku: [product.node.data.Sku],
                msrp: product.node.data.Msrp,
                id: language.data.Id,
                language: language.data.Language,
                lvl: product.node.data.ShortName,
                // TODO - use name_es, name_pt, etc - based on locale
                name: product.node.data.Name,
                metadata: product.node.data.Metadata,
                code: null,
                price: null,
                cart: null,
                languages: product.node.data.Languages
            };
            products.push(productObject);
        }
    }



    // Price and promo loop. In this loop we also reach out to find the related product. The link is that
    // PROMOS column names match the products table ShortName column . We can then use this to find the
    // product out of the products array by looking for a product where product shortname === promos column name
    // the right product in the products array with it where column name === product.lvl
    const filteredProducts = [];
    for (const node of pagesGQL.edges) {
        const pageData = node.node.data;
        const promoNodes = pageData.PromoNode;
        page.id = pageData.ID;
        page.expirationdate = pageData.Expiration_Date;
        page.expirationdatespanish = pageData.Spanish_Expiration_Date;
        page.productfilter = pageData.Product_Filter;
        page.fullprice = pageData.Full_Price;
        //page.xpay = pageData.XPay;
        page.ribbontext = pageData.Ribbon_Text;
        page.mastheadText = pageData.Masthead_Text;
        page.templates = pageData.Templates;
        // We want to filter products that only shown in Produt_Filter, so file isn't 10k lines long
        const filtered = products.filter((obj) => pageData.Product_Filter.includes(obj.lvl));

        // TODO this may be a bug. check for pages that have no value in the PromoNode column
        //if (!promoNodes) break;
        //console.log("filtered -------");
        //console.log(filtered);
        for (const product of products) {
            // Find this product in the result from the Products query, where info for this product is.
            // GraphQL formatted the keys here with _ instead of -, and if key is a number, prefixed with _
            // So if key is like "_6_hs", we make it "6-hs"

            const productLink = filtered.filter((p) => {
                return product.lvl === p.lvl;
            });


           if (productLink.length) {
            for (const product of productLink) {

                // Find this product inside promoNodes. In promoNodes object, the product names
                // have - replaced with _, and if it starts with a number, it's prefixed with _.
                // ie, "6-mo" would be "_6_mo". So we must format the name here to find it in promoNodes
                let promoNodeProductName = product.lvl.replace("-", "_");
                if (Number.isInteger(parseInt(product.lvl[0]))) {
                    promoNodeProductName = promoNodeProductName.replace(/(.)/, "_$1");
                }
                //console.log(promoNodes[0].data[promoNodeProductName]);
                let price;
                let promocode;

                // No promocode assigned to this page. Price is MSRP, and promocode is blank
                if (!promoNodes) {
                    price = product.msrp;
                    promocode = "";
                } else {
                    price = promoNodes[0].data[promoNodeProductName][0].data.Price;
                    promocode = promoNodes[0].data[promoNodeProductName][0].data.Promocode;
                }
                product.price = price;
                product.code =  promocode;
                product.cart = generateCartLink(product.metadata, product.sku, product.id, product.code);
                // // We don't need this anymore. Delete it so it doesn't write to file
                delete product.languages;
                filteredProducts.push(product);
            }
        }
        }
    }

    page.products = filteredProducts;
    console.log(page);

    function generateCartLink(metadata, sku, lang, pc = "") {
        const ENV = "prod";
        let basecart = {
            en: {
                url: `https://secure.${
                    ENV === "staging" ? "stg." : ""
                }rosettastone.com/us_en_store_view/checkout/cart/add/`
            },
            es: {
                url: `https://secure.${
                    ENV === "staging" ? "stg." : ""
                }rosettastone.com/hispanic_store_view/checkout/cart/add/`
            },
            pt: {
                url: `https://secure.${
                    ENV === "staging" ? "stg." : ""
                }rosettastone.com/us_en_store_view/checkout/cart/add/`
            },
            hs: {
                url: `https://secure.${
                    ENV === "staging" ? "stg." : ""
                }rosettastone.com/us_en_store_view/checkout/cart/add/`
            },
            ko: {
                url: `https://secure.${
                    ENV === "staging" ? "stg." : ""
                }rosettastone.com/us_en_store_view/checkout/cart/add/`
            }
        };

        basecart = basecart["en"].url;

        metadata = JSON.parse(metadata);

        // Format ["123, 456"] into [123, 456]
        sku = sku[0].split(",").map((s) => parseInt(s.trim()));

        //lang = lang.toLowerCase();
        let isUnlimited = false;
        if (metadata.hasOwnProperty("languages")) {
            const value = metadata.languages.trim().toLowerCase();
            if (value === "unlimited" || value === "single") {
                isUnlimited = value === "unlimited";
            } else {
                throw new Error(`---- unknown value for product metadata 'languages': ${metadata.languages}`);
            }
        }

        let isPlus = false;
        if (metadata.hasOwnProperty("plus")) {
            if (typeof metadata.plus === "boolean") {
                isPlus = metadata.plus;
                if (isPlus && sku.length !== 2) {
                    throw new Error(sku);
                }
            } else {
                throw new Error(`---- unknown value for product metadata 'plus': ${metadata.plus}`);
            }
        }

        if (!isPlus && sku.length !== 1) {
            throw new Error(`---- non 'plus' products must have 1 sku`);
        }

        pc = pc ? "/?pc=" + pc : "";

        if (isUnlimited) {
            lang = "all";
        }

        let cartLink;

        if (isPlus) {
            cartLink = `${basecart}sku/${sku[0]}/category_id/${lang}/related_sku/${sku[1]}${pc}`;
        } else if (isUnlimited && metadata.type === "coach") {
            // Unlimited Coaching sku has no category_id in the cart link
            cartLink = `${basecart}sku/${sku[0]}/`;
        } else {
            cartLink = `${basecart}sku/${sku[0]}/category_id/${lang}${pc}`;
        }
        return cartLink;
    }

    const config = configGenerator({
        market: site.siteMetadata.market,
        site: site.siteMetadata.site,
        locale: site.siteMetadata.lang,
        page: "test-airtable"
    });
    return (
        <PageTemplateWrapper config={config}>
            <pre>
                {pagesGQL.edges.map((p, index) => (
                    <div style={{ border: "1px solid black", margin: "0 0 15px 0" }} key={`price-${index}`}>
                        {beautify(p.node.data, null, 2, 100)}
                    </div>
                ))}
            </pre>
        </PageTemplateWrapper>
    );
};
export default Page;

export const pageQuery = graphql`
    query {
        site {
            ...SiteFragment
        }
        pagesGQL: allAirtable(filter: { table: { eq: "PAGES" }, data: { ID: { eq: "curso" } } }) {
            edges {
                node {
                    id
                    data {
                        Expiration_Date
                        Spanish_Expiration_Date
                        Full_Price
                        Templates
                        Gift
                        ID
                        Product_Filter
                        Full_Price

                        Ribbon_Text
                        Masthead_Text
                        PromoNode {
                            data {
                                lifetime {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                lifetime_u {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                lifetimeplus_u {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                lifetimeplus_u_lite {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                _36 {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                _30 {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                _27 {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                _24 {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                _24_u {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                _18 {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                _15 {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                _13 {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                _12 {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                _12_u {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                _8 {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                _6 {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                _3 {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                _1 {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                familypack_hs {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                lifetime_hs_s {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                lifetime_hs {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                _24_hs {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                _12_hs {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                _6_hs {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                                _3_hs {
                                    data {
                                        Price
                                        Promocode
                                    }
                                }
                            }
                        }
                        Masthead_Image {
                            filename
                            id
                            url
                        }
                        Masthead_Text
                        Mobile_Masthead_Image {
                            filename
                            id
                            url
                        }
                    }
                    table
                    rowIndex
                    recordId
                }
            }
        }
        productsGQL: allAirtable(filter: { table: { eq: "PRODUCTS" } }) {
            edges {
                node {
                    id
                    data {
                        Sku
                        Msrp
                        ShortName
                        Name
                        Name_es
                        Name_pt
                        Name_ko
                        Type
                        Metadata
                        Languages {
                            data {
                                Id
                                Language
                            }
                        }
                    }
                }
            }
        }
    }
`;