<commit_before>module ListAds exposing (..)

import Ad
import Html as H
import Html.Attributes as A
import Html.Events as E
import Http
import Json.Decode exposing (list)
import State.Ad
import State.ListAds exposing (..)

type Msg = NoOp | GetAds | UpdateAds (Result Http.Error (List State.Ad.Ad))


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    NoOp ->
      (model, Cmd.none)
    UpdateAds (Ok ads) ->
      ({ model | ads = ads }, Cmd.none )
    --TODO: show error
    UpdateAds (Err _) ->
      (model, Cmd.none)
    GetAds ->
      (model, getAds)


getAds : Cmd Msg
getAds =
  let
    url = "/api/ads/"
    request = Http.get url (list Ad.adDecoder)
  in
    Http.send UpdateAds request


view : Model -> H.Html Msg
view model =
  H.div []
  [ H.div
    []
    [ H.h3 [ A.class "list-ads__header" ] [ H.text "Selaa hakuilmoituksia" ] ]
  , H.div [A.class "list-ads__list-background"]
    [ H.div
      [ A.class "row list-ads__ad-container" ]
      (List.map
        adListView
        model.ads) ]
  ]



adListView : State.Ad.Ad -> H.Html Msg
adListView ad =
  H.div
    [ A.class "col-xs-12 col-sm-6"]
    [ H.div
      [ A.class "list-ads__ad-preview" ]
      [ H.h3 []
        [ H.a 
          [ A.class "list-ads__ad-preview-heading"
          , A.href ("/ads/" ++ (toString ad.id)) ]
          [ H.text ad.heading ] 
        ]
      , H.p [ A.class "list-ads__ad-preview-content" ] [ H.text ad.content]
      , H.hr [] []
      , H.div
        []
        [ H.span [ A.class "list-ads__ad-preview-profile-pic" ] []
        , H.span
          [ A.class "list-ads__ad-preview-profile-info" ]
          [ H.span [ A.class "list-ads__ad-preview-profile-name"] [ H.text ad.createdBy.name ]
          , H.br [] []
          , H.span [ A.class "list-ads__ad-preview-profile-title"] [ H.text ad.createdBy.primaryPosition ]
          ]
        ]
      ]
    ]<commit_msg>truncate ad content to 200 characters in list view<commit_after>module ListAds exposing (..)

import Ad
import Html as H
import Html.Attributes as A
import Html.Events as E
import Http
import Json.Decode exposing (list)
import State.Ad
import State.ListAds exposing (..)

type Msg = NoOp | GetAds | UpdateAds (Result Http.Error (List State.Ad.Ad))


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    NoOp ->
      (model, Cmd.none)
    UpdateAds (Ok ads) ->
      ({ model | ads = ads }, Cmd.none )
    --TODO: show error
    UpdateAds (Err _) ->
      (model, Cmd.none)
    GetAds ->
      (model, getAds)


getAds : Cmd Msg
getAds =
  let
    url = "/api/ads/"
    request = Http.get url (list Ad.adDecoder)
  in
    Http.send UpdateAds request


view : Model -> H.Html Msg
view model =
  H.div []
  [ H.div
    []
    [ H.h3 [ A.class "list-ads__header" ] [ H.text "Selaa hakuilmoituksia" ] ]
  , H.div [A.class "list-ads__list-background"]
    [ H.div
      [ A.class "row list-ads__ad-container" ]
      (List.map
        adListView
        model.ads) ]
  ]



adListView : State.Ad.Ad -> H.Html Msg
adListView ad =
  H.div
    [ A.class "col-xs-12 col-sm-6"]
    [ H.div
      [ A.class "list-ads__ad-preview" ]
      [ H.h3 []
        [ H.a 
          [ A.class "list-ads__ad-preview-heading"
          , A.href ("/ads/" ++ (toString ad.id)) ]
          [ H.text ad.heading ] 
        ]
      , H.p [ A.class "list-ads__ad-preview-content" ] [ H.text (truncateContent ad.content 200) ]
      , H.hr [] []
      , H.div
        []
        [ H.span [ A.class "list-ads__ad-preview-profile-pic" ] []
        , H.span
          [ A.class "list-ads__ad-preview-profile-info" ]
          [ H.span [ A.class "list-ads__ad-preview-profile-name"] [ H.text ad.createdBy.name ]
          , H.br [] []
          , H.span [ A.class "list-ads__ad-preview-profile-title"] [ H.text ad.createdBy.primaryPosition ]
          ]
        ]
      ]
    ]

truncateContent : String -> Int -> String
truncateContent content numChars =
  if (String.length content) < numChars
    then content
    else
      let
        truncated = List.foldl (takeNChars numChars) "" (String.words content)
      in
        (String.dropRight 1 truncated) ++ "..."

takeNChars : Int -> String -> String -> String
takeNChars n word accumulator =
  let
    totalLength = (String.length accumulator) + (String.length word)
  in
    if totalLength < n
      then accumulator ++ word ++ " "
      else accumulator
{'fraction_non_alphanumeric': 0.14058956916099774, 'fraction_numerical': 0.003854875283446712, 'mean_word_length': 1.9884823848238482, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3177197', 'n_tokens_mistral': 1623, 'n_tokens_neox': 1573, 'n_words': 371}