<commit_before>port module Main exposing (..)

import Ad
import Common
import CreateAd
import Footer
import Home
import Html as H
import Html.Attributes as A
import Html.Events as E
import Json.Decode as Json
import Link exposing (..)
import ListAds
import ListUsers
import LoginNeeded
import Maybe.Extra as Maybe
import Nav exposing (..)
import Navigation
import PreformattedText
import Profile.Main as Profile
import Profile.View
import Settings
import State.Main exposing (..)
import Static
import User

type alias HtmlId = String
port animation : (HtmlId, Bool) -> Cmd msg -- send True on splash screen, False otherwise
port scrollTop : Bool -> Cmd msg


main : Program Never Model Msg
main =
  Navigation.program UrlChange
    { init = init
    , view = view
    , update = update
    , subscriptions = subscriptions
    }

init : Navigation.Location -> ( Model, Cmd Msg )
init location =
  let
    model = initState location

   -- We want to react initially to UrlChange as well
    urlCmd = Navigation.modifyUrl (routeToPath (parseLocation location))
    profileCmd = Cmd.map ProfileMessage Profile.getMe
  in
    model ! [ urlCmd, profileCmd ]


-- UPDATE

type Msg
  = NewUrl Route
  | UrlChange Navigation.Location
  | AllowProfileCreation
  | ToggleAcceptTerms
  | UserMessage User.Msg
  | ProfileMessage Profile.Msg
  | CreateAdMessage CreateAd.Msg
  | ListAdsMessage ListAds.Msg
  | ListUsersMessage ListUsers.Msg
  | AdMessage Ad.Msg
  | HomeMessage Home.Msg
  | SettingsMessage Settings.Msg


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    NewUrl route ->
      { model | scrollTop = True } ! [ Navigation.newUrl (routeToPath route) ]

    UrlChange location ->
      let
        shouldScroll = model.scrollTop
        route = parseLocation location
        modelWithRoute = { model | route = route, scrollTop = False }
        ( newModel, cmd ) =
          case route of
            ShowAd adId ->
              modelWithRoute ! [ Cmd.map AdMessage (Ad.getAd adId) ]

            Profile ->
              modelWithRoute ! [ Cmd.map ProfileMessage Profile.initTasks ]

            ListAds ->
              modelWithRoute ! [ Cmd.map ListAdsMessage ListAds.getAds ]

            Home ->
              modelWithRoute ! [ Cmd.map HomeMessage Home.initTasks
                               , animation ("home-intro-canvas", False)
                               ]

            User userId ->
              if Just userId == Maybe.map .id model.profile.user
              then
                { model | route = Profile } ! [ Navigation.modifyUrl (routeToPath Profile) ]
              else
                (modelWithRoute, Cmd.batch [ User.getUser userId, User.getAds userId ] |> Cmd.map UserMessage)

            ListUsers ->
              modelWithRoute ! [ Cmd.map ListUsersMessage ListUsers.getUsers ]

            LoginNeeded _ ->
              modelWithRoute ! [ animation ("login-needed-canvas", False) ]

            Settings ->
              modelWithRoute ! [ Cmd.map SettingsMessage Settings.initTasks ]

            newRoute ->
              (modelWithRoute, Cmd.none)

        needsLogin =
          case (route, Maybe.isJust model.profile.user, model.initialLoading) of
            (CreateAd, False, False) -> True
            _ -> False

        newRoute =
          if needsLogin
          then
            routeToPath <| LoginNeeded (routeToPath route |> Just)
          else
            routeToPath route

        doConsentNeededAnimation =
          if (not model.initialLoading) && model.needsConsent then
            animation ("consent-needed-canvas", True)
          else
            Cmd.none

      in
        if needsLogin
        then
          model ! [ Navigation.newUrl newRoute ]
        else
          newModel ! [ cmd
                     , scrollTop shouldScroll
                     , doConsentNeededAnimation
                     ]

    UserMessage msg ->
      let
        (userModel, cmd) = User.update msg model.user
      in
        ( { model | user = userModel}, Cmd.map UserMessage cmd )

    AllowProfileCreation ->
      let<commit_msg>Don't add a history item when login needed<commit_after>port module Main exposing (..)

import Ad
import Common
import CreateAd
import Footer
import Home
import Html as H
import Html.Attributes as A
import Html.Events as E
import Json.Decode as Json
import Link exposing (..)
import ListAds
import ListUsers
import LoginNeeded
import Maybe.Extra as Maybe
import Nav exposing (..)
import Navigation
import PreformattedText
import Profile.Main as Profile
import Profile.View
import Settings
import State.Main exposing (..)
import Static
import User

type alias HtmlId = String
port animation : (HtmlId, Bool) -> Cmd msg -- send True on splash screen, False otherwise
port scrollTop : Bool -> Cmd msg


main : Program Never Model Msg
main =
  Navigation.program UrlChange
    { init = init
    , view = view
    , update = update
    , subscriptions = subscriptions
    }

init : Navigation.Location -> ( Model, Cmd Msg )
init location =
  let
    model = initState location

   -- We want to react initially to UrlChange as well
    urlCmd = Navigation.modifyUrl (routeToPath (parseLocation location))
    profileCmd = Cmd.map ProfileMessage Profile.getMe
  in
    model ! [ urlCmd, profileCmd ]


-- UPDATE

type Msg
  = NewUrl Route
  | UrlChange Navigation.Location
  | AllowProfileCreation
  | ToggleAcceptTerms
  | UserMessage User.Msg
  | ProfileMessage Profile.Msg
  | CreateAdMessage CreateAd.Msg
  | ListAdsMessage ListAds.Msg
  | ListUsersMessage ListUsers.Msg
  | AdMessage Ad.Msg
  | HomeMessage Home.Msg
  | SettingsMessage Settings.Msg


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    NewUrl route ->
      { model | scrollTop = True } ! [ Navigation.newUrl (routeToPath route) ]

    UrlChange location ->
      let
        shouldScroll = model.scrollTop
        route = parseLocation location
        modelWithRoute = { model | route = route, scrollTop = False }
        ( newModel, cmd ) =
          case route of
            ShowAd adId ->
              modelWithRoute ! [ Cmd.map AdMessage (Ad.getAd adId) ]

            Profile ->
              modelWithRoute ! [ Cmd.map ProfileMessage Profile.initTasks ]

            ListAds ->
              modelWithRoute ! [ Cmd.map ListAdsMessage ListAds.getAds ]

            Home ->
              modelWithRoute ! [ Cmd.map HomeMessage Home.initTasks
                               , animation ("home-intro-canvas", False)
                               ]

            User userId ->
              if Just userId == Maybe.map .id model.profile.user
              then
                { model | route = Profile } ! [ Navigation.modifyUrl (routeToPath Profile) ]
              else
                (modelWithRoute, Cmd.batch [ User.getUser userId, User.getAds userId ] |> Cmd.map UserMessage)

            ListUsers ->
              modelWithRoute ! [ Cmd.map ListUsersMessage ListUsers.getUsers ]

            LoginNeeded _ ->
              modelWithRoute ! [ animation ("login-needed-canvas", False) ]

            Settings ->
              modelWithRoute ! [ Cmd.map SettingsMessage Settings.initTasks ]

            newRoute ->
              (modelWithRoute, Cmd.none)

        needsLogin =
          case (route, Maybe.isJust model.profile.user, model.initialLoading) of
            (CreateAd, False, False) -> True
            _ -> False

        newRoute =
          if needsLogin
          then
            routeToPath <| LoginNeeded (routeToPath route |> Just)
          else
            routeToPath route

        doConsentNeededAnimation =
          if (not model.initialLoading) && model.needsConsent then
            animation ("consent-needed-canvas", True)
          else
            Cmd.none

      in
        if needsLogin
        then
          model ! [ Navigation.modifyUrl newRoute ]
        else
          newModel ! [ cmd
                     , scrollTop shouldScroll
                     , doConsentNeededAnimation
                     ]

    UserMessage msg ->
      let
        (userModel, cmd) = User.update msg model.user
      in
        ( { model | user = userModel}, Cmd.map UserMessage cmd )

    AllowProfileCreation ->
      let
{'fraction_non_alphanumeric': 0.07289651837524178, 'fraction_numerical': 0.0, 'mean_word_length': 1.9652329749103943, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2800767', 'n_tokens_mistral': 2255, 'n_tokens_neox': 2165, 'n_words': 670}