-
-
Save konung/2fdf4e1f3f7ff7c2e5dea0ffc1c159cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Shopify.Product.Model where | |
type alias Product = | |
{ created_at : String, | |
id : Int, | |
product_type : String, | |
published_at : String, | |
published_scope : String, | |
title : String, | |
updated_at : String, | |
vendor : String, | |
images : List ProductImage } | |
type alias ProductImage = | |
{ created_at : String, | |
id : Int, | |
position : Int, | |
product_id : Int, | |
variant_ids : List Int, | |
src : String, | |
updated_at : String } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Shopify.Products.Actions where | |
import Shopify.Products.Transit as Transit | |
import Shopify.Products.Model as Model | |
import Effects exposing ( Effects ) | |
import Task exposing ( Task ) | |
import Debug | |
type Action = | |
RequestProducts Model.Products | |
effectFromTask : Task x t -> t -> Effects t | |
effectFromTask a d = | |
Task.toMaybe a | |
|> Task.map (Maybe.withDefault d) | |
|> Effects.task | |
requestProducts : Effects Action | |
requestProducts = | |
effectFromTask Transit.requestPages [] | |
|> Effects.map RequestProducts | |
update : Action -> Model.Products -> (Model.Products,Effects Action) | |
update a s = | |
case a of | |
RequestProducts ps -> (ps, Effects.none) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Shopify.Products.Model where | |
import Shopify.Product.Model exposing ( Product ) | |
import Signal | |
import Effects | |
type alias Products = List Product |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Shopify.Products.Transit where | |
import Shopify.Product.Transit as Transit exposing ( decode, encode ) | |
import Json.Decode as D | |
import Shopify.Settings exposing ( path ) | |
import Task exposing ( andThen ) | |
import Http | |
decode = | |
D.list Transit.decode | |
|> D.at ["products"] | |
decodeCount = | |
D.at ["count"] D.int | |
getJson resource page decoder = | |
"admin/" ++ resource ++ ".json" ++ page | |
|> path | |
|> Http.get decoder | |
countPages count = | |
(floor ((toFloat count)/50)) | |
pagination i = | |
"?page=" ++ (toString i) | |
collectPages = | |
countPages | |
>> (\t -> [1..t]) | |
>> (List.map requestPage) | |
>> Task.sequence | |
>> Task.map List.concat | |
countProducts = | |
getJson "products/count" "" decodeCount | |
requestPage p = | |
getJson "products" (pagination p) decode | |
requestPages = | |
countProducts `andThen` collectPages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Shopify.Products.View where | |
import Shopify.Products.Model | |
import Shopify.Product.View | |
import Shopify.Product.Model | |
import Html exposing ( .. ) | |
import Html.Attributes exposing ( .. ) | |
render : Shopify.Products.Model.Products -> Html | |
render ps = div [] (List.map Shopify.Product.View.render ps) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Shopify.Product.Transit where | |
import Shopify.Product.Model exposing ( Product, ProductImage ) | |
import Json.Decode as D exposing ( (:=) ) | |
import Json.Decode.Extra exposing ( (|:) ) | |
import Json.Encode as E | |
decode : D.Decoder Product | |
decode = | |
D.succeed Product | |
|: ("created_at" := D.string) | |
|: ("id" := D.int) | |
|: ("product_type" := D.string) | |
|: ("published_at" := D.string) | |
|: ("published_scope" := D.string) | |
|: ("title" := D.string) | |
|: ("updated_at" := D.string) | |
|: ("vendor" := D.string) | |
|: ("images" := D.list decodeImage) | |
decodeImage : D.Decoder ProductImage | |
decodeImage = | |
D.succeed ProductImage | |
|: ("created_at" := D.string) | |
|: ("id" := D.int) | |
|: ("position" := D.int) | |
|: ("product_id" := D.int) | |
|: ("variant_ids" := D.list D.int) | |
|: ("src" := D.string) | |
|: ("updated_at" := D.string) | |
encode : Product -> E.Value | |
encode p = | |
E.object | |
[ ("created_at" , E.string p.created_at), | |
("id", E.int p.id), | |
("product_type" , E.string p.product_type), | |
("published_at" , E.string p.published_at), | |
("published_scope" , E.string p.published_scope), | |
("title", E.string p.title), | |
("updated_at" , E.string p.updated_at), | |
("vendor" , E.string p.vendor) ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Shopify.Product.View where | |
import Shopify.Product.Model | |
import Html exposing ( .. ) | |
import Html.Attributes exposing ( .. ) | |
render : Shopify.Product.Model.Product -> Html | |
render p = Html.div [] [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment