<commit_before>port module TeamPicker exposing (..)

import Html exposing (Html, Attribute, beginnerProgram, programWithFlags, div, button, a, hr, text, h2, p, label, select, option, span, input,ul, li, nav, form)
import Html.Attributes exposing (class, placeholder, type_, size, value, selected, name, disabled, href, method, action)
import Html.Events exposing (onClick, onInput)
import String
import Debug

main =
  programWithFlags
    { init = init
    , update = update
    , subscriptions = \_ -> Sub.none
    , view = view
    }

-- MODEL
type alias Team =
  { id: String
  , name: String
  }

type alias Model =
  {
    teams: List Team
    ,current_team: Team

    , newTeam: Bool
    , showTeamList: Bool
    , domain: String
    , formAuthenticityToken: String
  }

type alias Flag =
  { formAuthenticityToken: String
  , domain: String
  , teams: List Team
  , current_team: Team
  }

-- UPDATE
type Msg
  = ToggleTeams Bool
  | SwitchTeam Team
  | NewTeam Bool
  | NoOp


init : Flag -> (Model, Cmd Msg)
init flags =
  (Model flags.teams flags.current_team False False flags.domain flags.formAuthenticityToken, Cmd.none)


update: Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    NoOp -> (model, Cmd.none)
    ToggleTeams toggleFlag ->
      ({ model | showTeamList = not toggleFlag } , Cmd.none)
    SwitchTeam team ->
        ({ model | current_team = team }, Cmd.none)
    NewTeam toggleFlag ->
        --Debug.log new_team
        ({model | newTeam = toggleFlag }, Cmd.none)

-- VIEW

view : Model -> Html Msg
view model =
  div [ class "nav-item teampicker" ]
    [ div []
        [
          a  [ class "button", onClick (ToggleTeams model.showTeamList) ] [ 
                text model.current_team.name
                , span  [] [(if model.showTeamList then " ▲" else " ▼" ) |> text]
             ]
        ]
    , (if model.showTeamList then
       div [ class "teampicker__list box" ]
          [
            viewTeamList model
            , viewTeamFooter model
          ]
       else
         text "")
    ]

viewTeamList : Model -> Html Msg
viewTeamList model =
  div [ class "menu" ] [
    p [ class "menu-label" ] [ text "Switch team" ]
    , ul [ class "menu-list" ] (List.map (\(team) -> li [] [
                                    span [] [ a [ href ("//team-" ++ team.id ++ "." ++ model.domain ++ "/dashboard") ] [text team.name] ]
                                   ]) model.teams)
  ]

viewNewTeam:  Model -> Html Msg
viewNewTeam model =
  if not model.newTeam then
   text ""
  else
   li [] [
     form [ method "POST", action "/teams" ] [
        input [ type_ "hidden", name "authenticity_token", value model.formAuthenticityToken] []
        , div [ class "field" ] [
            p [ class "control" ] [ input [ class "input", type_ "text", name "team[name]", value "", placeholder "Your Team"] [] ]
          ]
        , div [ class "field has-text-centered is-grouped" ] [
            p [ class "control" ] [
              button [ name "button", type_ "submit", class "button is-primary" ] [ text "Save" ]
            ]<commit_msg>Default team can just use the root domain<commit_after>port module TeamPicker exposing (..)

import Html exposing (Html, Attribute, beginnerProgram, programWithFlags, div, button, a, hr, text, h2, p, label, select, option, span, input,ul, li, nav, form)
import Html.Attributes exposing (class, placeholder, type_, size, value, selected, name, disabled, href, method, action)
import Html.Events exposing (onClick, onInput)
import String
import Debug

main =
  programWithFlags
    { init = init
    , update = update
    , subscriptions = \_ -> Sub.none
    , view = view
    }

-- MODEL
type alias Team =
  { id: String
  , name: String
  }

type alias Model =
  {
    teams: List Team
    ,current_team: Team

    , newTeam: Bool
    , showTeamList: Bool
    , domain: String
    , formAuthenticityToken: String
  }

type alias Flag =
  { formAuthenticityToken: String
  , domain: String
  , teams: List Team
  , current_team: Team
  }

-- UPDATE
type Msg
  = ToggleTeams Bool
  | SwitchTeam Team
  | NewTeam Bool
  | NoOp


init : Flag -> (Model, Cmd Msg)
init flags =
  (Model flags.teams flags.current_team False False flags.domain flags.formAuthenticityToken, Cmd.none)


update: Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    NoOp -> (model, Cmd.none)
    ToggleTeams toggleFlag ->
      ({ model | showTeamList = not toggleFlag } , Cmd.none)
    SwitchTeam team ->
        ({ model | current_team = team }, Cmd.none)
    NewTeam toggleFlag ->
        --Debug.log new_team
        ({model | newTeam = toggleFlag }, Cmd.none)

-- VIEW

view : Model -> Html Msg
view model =
  div [ class "nav-item teampicker" ]
    [ div []
        [
          a  [ class "button", onClick (ToggleTeams model.showTeamList) ] [ 
                text model.current_team.name
                , span  [] [(if model.showTeamList then " ▲" else " ▼" ) |> text]
             ]
        ]
    , (if model.showTeamList then
       div [ class "teampicker__list box" ]
          [
            viewTeamList model
            , viewTeamFooter model
          ]
       else
         text "")
    ]

viewTeamList : Model -> Html Msg
viewTeamList model =
  div [ class "menu" ] [
    p [ class "menu-label" ] [ text "Switch team" ]
    , ul [ class "menu-list" ] (List.map (\(team) -> li [] [
                                   case List.head model.teams of
                                     Nothing -> text ""
                                     Just defaultTeam ->
                                       if team.id == defaultTeam.id then
                                         span [] [ a [ href ("//" ++ model.domain ++ "/dashboard") ] [ text team.name ] ]
                                       else
                                         span [] [ a [ href ("//team-" ++ team.id ++ "." ++ model.domain ++ "/dashboard") ] [text team.name] ]

--                                   let
--                                       defaultTeam = List.head model.teams
--                                   in
--                                     if team.id == defaultTeam.id then
--                                       span [] [ a [ href (model.domain ++ "/dashboard") ] [ text team.name ] ]
--                                     else
--                                       span [] [ a [ href ("//team-" ++ team.id ++ "." ++ model.domain ++ "/dashboard") ] [text team.name] ]
--
                                   ]) model.teams)
  ]

viewNewTeam:  Model -> Html Msg
viewNewTeam model =
  if not model.newTeam then
   text ""
  else
   li [] [
     form [ method "POST", action "/teams" ] [
        input [ type_ "hidden", name "authenticity_token", value model.formAuthenticityToken] []
        , div [ class "field" ] [
            p [ class "control" ] [ input [ class "input", type_ "text", name "team[name]", value "", placeholder "Your Team"] [] ]
          ]
        , div [ class "field has-text-centered is-grouped" ] [
            p [ class "control" ] [
              button [ name "button", type_ "submit", class "button is-primary" ] [ text "Save" ]
            ]
{'fraction_non_alphanumeric': 0.12162162162162163, 'fraction_numerical': 0.00027862914460852607, 'mean_word_length': 1.789044289044289, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3744214', 'n_tokens_mistral': 2156, 'n_tokens_neox': 2094, 'n_words': 629}