Current section
Files
Jump to
Current section
Files
src/storefront@cart.erl
-module(storefront@cart).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/storefront/cart.gleam").
-export([reshape_cart/1, merchandise_decoder/0, cart_decoder/0, create_cart/1, get_cart/2]).
-export_type([shopify_create_cart_mutation/0, shopify_create_cart_data/0, create_cart/0, shopify_cart_operation/0, shopify_cart_operation_data/0, shopify_cart_operation_variables/0, cart/0, shopify_cart/0, shopify_cart_cost/0, cart_item/0, merchandise/0, cart_product/0]).
-type shopify_create_cart_mutation() :: {shopify_create_cart_mutation,
shopify_create_cart_data()}.
-type shopify_create_cart_data() :: {shopify_create_cart_data, create_cart()}.
-type create_cart() :: {create_cart, shopify_cart()}.
-opaque shopify_cart_operation() :: {shopify_cart_operation,
shopify_cart_operation_data(),
shopify_cart_operation_variables()}.
-type shopify_cart_operation_data() :: {shopify_cart_operation_data,
gleam@option:option(shopify_cart())}.
-type shopify_cart_operation_variables() :: {shopify_cart_operation_variables,
binary()}.
-type cart() :: {cart,
gleam@option:option(binary()),
binary(),
gleam@option:option(shopify_cart_cost()),
integer(),
list(cart_item())}.
-type shopify_cart() :: {shopify_cart,
gleam@option:option(binary()),
binary(),
gleam@option:option(shopify_cart_cost()),
storefront@utils:connection(cart_item()),
integer()}.
-type shopify_cart_cost() :: {shopify_cart_cost,
storefront@utils:money(),
storefront@utils:money(),
storefront@utils:money()}.
-type cart_item() :: {cart_item,
gleam@option:option(binary()),
integer(),
storefront@utils:cost(),
merchandise()}.
-type merchandise() :: {merchandise,
binary(),
binary(),
list(storefront@utils:selected_option()),
cart_product()}.
-type cart_product() :: {cart_product,
binary(),
binary(),
binary(),
storefront@utils:image()}.
-file("src/storefront/cart.gleam", 212).
-spec reshape_cart(shopify_cart()) -> cart().
reshape_cart(Cart) ->
Result = (case erlang:element(4, Cart) of
none ->
none;
{some, Cost} ->
Lines = storefront@utils:remove_edges_and_nodes(
erlang:element(5, Cart)
),
{some,
{cart,
erlang:element(2, Cart),
erlang:element(3, Cart),
{some,
{shopify_cart_cost,
erlang:element(2, Cost),
erlang:element(3, Cost),
erlang:element(4, Cost)}},
erlang:element(6, Cart),
Lines}}
end),
Lines@1 = storefront@utils:remove_edges_and_nodes(erlang:element(5, Cart)),
gleam@option:unwrap(
Result,
{cart,
erlang:element(2, Cart),
erlang:element(3, Cart),
erlang:element(4, Cart),
erlang:element(6, Cart),
Lines@1}
).
-file("src/storefront/cart.gleam", 564).
-spec shopify_cart_operation_variables_decoder() -> gleam@dynamic@decode:decoder(shopify_cart_operation_variables()).
shopify_cart_operation_variables_decoder() ->
gleam@dynamic@decode:field(
<<"cartId"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Cart_id) ->
gleam@dynamic@decode:success(
{shopify_cart_operation_variables, Cart_id}
)
end
).
-file("src/storefront/cart.gleam", 627).
-spec shopify_cart_cost_decoder() -> gleam@dynamic@decode:decoder(shopify_cart_cost()).
shopify_cart_cost_decoder() ->
gleam@dynamic@decode:field(
<<"totalAmount"/utf8>>,
storefront@utils:money_decoder(),
fun(Total_amount) ->
gleam@dynamic@decode:field(
<<"subtotalAmount"/utf8>>,
storefront@utils:money_decoder(),
fun(Subtotal_amount) ->
gleam@dynamic@decode:field(
<<"totalTaxAmount"/utf8>>,
storefront@utils:money_decoder(),
fun(Total_tax_amount) ->
gleam@dynamic@decode:success(
{shopify_cart_cost,
Total_amount,
Subtotal_amount,
Total_tax_amount}
)
end
)
end
)
end
).
-file("src/storefront/cart.gleam", 679).
-spec cart_product_decoder() -> gleam@dynamic@decode:decoder(cart_product()).
cart_product_decoder() ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Id) ->
gleam@dynamic@decode:field(
<<"handle"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Handle) ->
gleam@dynamic@decode:field(
<<"title"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Title) ->
gleam@dynamic@decode:field(
<<"featuredImage"/utf8>>,
storefront@utils:image_decoder(),
fun(Featured_image) ->
gleam@dynamic@decode:success(
{cart_product,
Id,
Handle,
Title,
Featured_image}
)
end
)
end
)
end
)
end
).
-file("src/storefront/cart.gleam", 664).
-spec merchandise_decoder() -> gleam@dynamic@decode:decoder(merchandise()).
merchandise_decoder() ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Id) ->
gleam@dynamic@decode:field(
<<"title"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Title) ->
gleam@dynamic@decode:field(
<<"selectedOptions"/utf8>>,
gleam@dynamic@decode:list(
storefront@utils:selected_option_decoder()
),
fun(Selected_options) ->
gleam@dynamic@decode:field(
<<"product"/utf8>>,
cart_product_decoder(),
fun(Product) ->
gleam@dynamic@decode:success(
{merchandise,
Id,
Title,
Selected_options,
Product}
)
end
)
end
)
end
)
end
).
-file("src/storefront/cart.gleam", 647).
-spec cart_item_decoder() -> gleam@dynamic@decode:decoder(cart_item()).
cart_item_decoder() ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Id) ->
gleam@dynamic@decode:field(
<<"quantity"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Quantity) ->
gleam@dynamic@decode:field(
<<"cost"/utf8>>,
storefront@utils:cost_decoder(),
fun(Cost) ->
gleam@dynamic@decode:field(
<<"merchandise"/utf8>>,
merchandise_decoder(),
fun(Merchandise) ->
gleam@dynamic@decode:success(
{cart_item,
Id,
Quantity,
Cost,
Merchandise}
)
end
)
end
)
end
)
end
).
-file("src/storefront/cart.gleam", 581).
-spec cart_decoder() -> gleam@dynamic@decode:decoder(cart()).
cart_decoder() ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Id) ->
gleam@dynamic@decode:field(
<<"checkoutUrl"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Checkout_url) ->
gleam@dynamic@decode:field(
<<"cost"/utf8>>,
gleam@dynamic@decode:optional(
shopify_cart_cost_decoder()
),
fun(Cost) ->
gleam@dynamic@decode:field(
<<"totalQuantity"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Total_quantity) ->
gleam@dynamic@decode:field(
<<"lines"/utf8>>,
gleam@dynamic@decode:list(
cart_item_decoder()
),
fun(Lines) ->
gleam@dynamic@decode:success(
{cart,
Id,
Checkout_url,
Cost,
Total_quantity,
Lines}
)
end
)
end
)
end
)
end
)
end
).
-file("src/storefront/cart.gleam", 600).
-spec cart_item_edge_decoder() -> gleam@dynamic@decode:decoder(storefront@utils:edge(cart_item())).
cart_item_edge_decoder() ->
gleam@dynamic@decode:field(
<<"node"/utf8>>,
cart_item_decoder(),
fun(Node) -> gleam@dynamic@decode:success({edge, Node}) end
).
-file("src/storefront/cart.gleam", 605).
-spec cart_item_connection_decoder() -> gleam@dynamic@decode:decoder(storefront@utils:connection(cart_item())).
cart_item_connection_decoder() ->
gleam@dynamic@decode:field(
<<"edges"/utf8>>,
gleam@dynamic@decode:list(cart_item_edge_decoder()),
fun(Edges) -> gleam@dynamic@decode:success({connection, Edges}) end
).
-file("src/storefront/cart.gleam", 610).
-spec shopify_cart_decoder() -> gleam@dynamic@decode:decoder(shopify_cart()).
shopify_cart_decoder() ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Id) ->
gleam@dynamic@decode:field(
<<"checkoutUrl"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Checkout_url) ->
gleam@dynamic@decode:field(
<<"cost"/utf8>>,
gleam@dynamic@decode:optional(
shopify_cart_cost_decoder()
),
fun(Cost) ->
gleam@dynamic@decode:field(
<<"lines"/utf8>>,
cart_item_connection_decoder(),
fun(Lines) ->
gleam@dynamic@decode:field(
<<"totalQuantity"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_int/1},
fun(Total_quantity) ->
gleam@dynamic@decode:success(
{shopify_cart,
Id,
Checkout_url,
Cost,
Lines,
Total_quantity}
)
end
)
end
)
end
)
end
)
end
).
-file("src/storefront/cart.gleam", 362).
-spec create_cart_decoder() -> gleam@dynamic@decode:decoder(create_cart()).
create_cart_decoder() ->
gleam@dynamic@decode:field(
<<"cart"/utf8>>,
shopify_cart_decoder(),
fun(Cart) -> gleam@dynamic@decode:success({create_cart, Cart}) end
).
-file("src/storefront/cart.gleam", 353).
-spec shopify_create_cart_data_decoder() -> gleam@dynamic@decode:decoder(shopify_create_cart_data()).
shopify_create_cart_data_decoder() ->
gleam@dynamic@decode:field(
<<"cartCreate"/utf8>>,
create_cart_decoder(),
fun(Cart_create) ->
gleam@dynamic@decode:success(
{shopify_create_cart_data, Cart_create}
)
end
).
-file("src/storefront/cart.gleam", 342).
-spec shopify_create_cart_mutation_decoder() -> gleam@dynamic@decode:decoder(shopify_create_cart_mutation()).
shopify_create_cart_mutation_decoder() ->
gleam@dynamic@decode:field(
<<"data"/utf8>>,
shopify_create_cart_data_decoder(),
fun(Data) ->
gleam@dynamic@decode:success({shopify_create_cart_mutation, Data})
end
).
-file("src/storefront/cart.gleam", 553).
-spec shopify_cart_operation_data_decoder() -> gleam@dynamic@decode:decoder(shopify_cart_operation_data()).
shopify_cart_operation_data_decoder() ->
gleam@dynamic@decode:field(
<<"cart"/utf8>>,
gleam@dynamic@decode:optional(shopify_cart_decoder()),
fun(Cart) ->
gleam@dynamic@decode:success({shopify_cart_operation_data, Cart})
end
).
-file("src/storefront/cart.gleam", 540).
-spec shopify_cart_operation_decoder() -> gleam@dynamic@decode:decoder(shopify_cart_operation()).
shopify_cart_operation_decoder() ->
gleam@dynamic@decode:field(
<<"data"/utf8>>,
shopify_cart_operation_data_decoder(),
fun(Data) ->
gleam@dynamic@decode:field(
<<"variables"/utf8>>,
shopify_cart_operation_variables_decoder(),
fun(Variables) ->
gleam@dynamic@decode:success(
{shopify_cart_operation, Data, Variables}
)
end
)
end
).
-file("src/storefront/cart.gleam", 19).
-spec create_cart(pify:storefront_api_client_config()) -> {ok, cart()} |
{error, pify:shopify_error()}.
create_cart(Config) ->
Handler = pify:handler(Config),
Fetcher = (erlang:element(4, Handler))(
<<"
mutation createCart($lineItems: [CartLineInput!]) {
cartCreate(input: { lines: $lineItems }) {
cart {
...cart
}
}
}
"/utf8,
"
fragment cart on Cart {
id
checkoutUrl
cost {
subtotalAmount {
amount
currencyCode
}
totalAmount {
amount
currencyCode
}
totalTaxAmount {
amount
currencyCode
}
}
lines(first: 100) {
edges {
node {
id
quantity
attributes {
key
value
}
cost {
totalAmount {
amount
currencyCode
}
}
merchandise {
... on ProductVariant {
id
title
compareAtPrice {
amount
currencyCode
}
selectedOptions {
name
value
}
product {
...product
}
}
}
}
}
}
totalQuantity
}
"/utf8,
"
fragment product on Product {
id
handle
availableForSale
title
description
descriptionHtml
vendor
options {
id
name
values
}
priceRange {
maxVariantPrice {
amount
currencyCode
}
minVariantPrice {
amount
currencyCode
}
}
compareAtPriceRange {
maxVariantPrice {
amount
currencyCode
}
minVariantPrice {
amount
currencyCode
}
}
variants(first: 250) {
edges {
node {
id
title
availableForSale
selectedOptions {
name
value
}
price {
amount
currencyCode
}
}
}
}
featuredImage {
...image
}
images(first: 20) {
edges {
node {
...image
}
}
}
seo {
...seo
}
metafields(
{ namespace: \"metafield\", key: \"width\" }
{ namespace: \"metafield\", key: \"height\" }
{ namespace: \"metafield\", key: \"thickness\" }
{ namespace: \"metafield\", key: \"pages\" }
{ namespace: \"metafield\", key: \"categories\" }
{ namespace: \"metafield\", key: \"language\" }
{ namespace: \"metafield\", key: \"format\" }
{ namespace: \"custom\", key: \"author\" }
{ namespace: \"custom\", key: \"publisher\" }
{ namespace: \"custom\", key: \"related-authors\" }
{ namespace: \"custom\", key: \"people-who-worked\" }
{ namespace: \"custom\", key: \"translator\" }
{ namespace: \"custom\", key: \"related-books\" }
{ namespace: \"shopify\", key: \"genre\" }
{ namespace: \"shopify\", key: \"book-cover-type\" }
{ namespace: \"shopify\", key: \"target-audience\" }
]
) {
key
namespace
value
type
references(first: 5) {
nodes {
... on Metaobject {
id
type
fields {
key
value
}
}
}
}
}
tags
updatedAt
}
"/utf8,
"
fragment image on Image {
url
altText
width
height
}
"/utf8,
"
fragment seo on SEO {
description
title
}
"/utf8>>,
none,
shopify_create_cart_mutation_decoder()
),
case Fetcher of
{done, Cart} ->
{ok,
reshape_cart(
erlang:element(
2,
erlang:element(2, erlang:element(2, Cart))
)
)};
Err ->
{error,
{client_error,
begin
_pipe = snag:new(gleam@string:inspect(Err)),
_pipe@1 = snag:layer(
_pipe,
<<"This effect is not handled in this environment"/utf8>>
),
snag:line_print(_pipe@1)
end}}
end.
-file("src/storefront/cart.gleam", 42).
-spec get_cart(
pify:storefront_api_client_config(),
gleam@http@request:request(cart())
) -> {ok, cart()} | {error, pify:shopify_error()}.
get_cart(Config, Req) ->
Client = pify:handler(Config),
Try_cookies = begin
_pipe = gleam@http@request:get_cookies(Req),
gleam@list:key_find(_pipe, <<"cartId"/utf8>>)
end,
case Try_cookies of
{error, Err} ->
{error,
{client_error,
begin
_pipe@1 = snag:new(gleam@string:inspect(Err)),
_pipe@2 = snag:layer(
_pipe@1,
<<"This effect is not handled in this environment"/utf8>>
),
snag:line_print(_pipe@2)
end}};
{ok, Id} ->
Query = <<"
query getCart($cartId: ID!) {
cart(id: $cartId) {
...cart
}
}
"/utf8,
"
fragment cart on Cart {
id
checkoutUrl
cost {
subtotalAmount {
amount
currencyCode
}
totalAmount {
amount
currencyCode
}
totalTaxAmount {
amount
currencyCode
}
}
lines(first: 100) {
edges {
node {
id
quantity
attributes {
key
value
}
cost {
totalAmount {
amount
currencyCode
}
}
merchandise {
... on ProductVariant {
id
title
compareAtPrice {
amount
currencyCode
}
selectedOptions {
name
value
}
product {
...product
}
}
}
}
}
}
totalQuantity
}
"/utf8,
"
fragment product on Product {
id
handle
availableForSale
title
description
descriptionHtml
vendor
options {
id
name
values
}
priceRange {
maxVariantPrice {
amount
currencyCode
}
minVariantPrice {
amount
currencyCode
}
}
compareAtPriceRange {
maxVariantPrice {
amount
currencyCode
}
minVariantPrice {
amount
currencyCode
}
}
variants(first: 250) {
edges {
node {
id
title
availableForSale
selectedOptions {
name
value
}
price {
amount
currencyCode
}
}
}
}
featuredImage {
...image
}
images(first: 20) {
edges {
node {
...image
}
}
}
seo {
...seo
}
metafields(
{ namespace: \"metafield\", key: \"width\" }
{ namespace: \"metafield\", key: \"height\" }
{ namespace: \"metafield\", key: \"thickness\" }
{ namespace: \"metafield\", key: \"pages\" }
{ namespace: \"metafield\", key: \"categories\" }
{ namespace: \"metafield\", key: \"language\" }
{ namespace: \"metafield\", key: \"format\" }
{ namespace: \"custom\", key: \"author\" }
{ namespace: \"custom\", key: \"publisher\" }
{ namespace: \"custom\", key: \"related-authors\" }
{ namespace: \"custom\", key: \"people-who-worked\" }
{ namespace: \"custom\", key: \"translator\" }
{ namespace: \"custom\", key: \"related-books\" }
{ namespace: \"shopify\", key: \"genre\" }
{ namespace: \"shopify\", key: \"book-cover-type\" }
{ namespace: \"shopify\", key: \"target-audience\" }
]
) {
key
namespace
value
type
references(first: 5) {
nodes {
... on Metaobject {
id
type
fields {
key
value
}
}
}
}
}
tags
updatedAt
}
"/utf8,
"
fragment image on Image {
url
altText
width
height
}
"/utf8,
"
fragment seo on SEO {
description
title
}
"/utf8>>,
Variables = gleam@json:object(
[{<<"cartId"/utf8>>, gleam@json:string(Id)}]
),
Fetcher = (erlang:element(4, Client))(
Query,
{some, Variables},
shopify_cart_operation_decoder()
),
case Fetcher of
{done, Cart} ->
case erlang:element(2, erlang:element(2, Cart)) of
{some, Cart@1} ->
{ok, reshape_cart(Cart@1)};
none ->
{error,
{client_error,
begin
_pipe@3 = snag:new(
<<"We were unable to get the cart."/utf8>>
),
_pipe@4 = snag:layer(
_pipe@3,
<<"This effect is not handled in this environment"/utf8>>
),
snag:line_print(_pipe@4)
end}}
end;
Err@1 ->
{error,
{client_error,
begin
_pipe@5 = snag:new(gleam@string:inspect(Err@1)),
_pipe@6 = snag:layer(
_pipe@5,
<<"This effect is not handled in this environment"/utf8>>
),
snag:line_print(_pipe@6)
end}}
end
end.