<commit_before>module Page.Widget exposing (Model, Msg(..), init, update, view, subscriptions)

{-| Viewing an individual widget.
-}

import Data.Widget as Widget exposing (Widget, Body)
import Data.Widget.Author as Author exposing (Author)
import Data.Session as Session exposing (Session)
import Data.User as User exposing (User)
import Data.UserPhoto as UserPhoto
import Data.DataSourceMessage as DataSourceMessage exposing (DataSourceMessage, decoder)
import Date exposing (Date)
import Date.Format
import Html exposing (..)
import Html.Attributes exposing (attribute, class, disabled, href, id, placeholder, value, type_)
import Html.Events exposing (onClick, onInput, onSubmit)
import Http
import Page.Errored as Errored exposing (PageLoadError, pageLoadError)
import Request.Widget
import Request.Profile
import Route
import Task exposing (Task)
import Util exposing ((=>), pair, viewIf)
import Views.Widget
import Views.Widget.Favorite as Favorite
import Views.Author
import Views.Errors
import Views.Page as Page
import Views.User.Follow as Follow
import Data.Widget.Table as Table exposing (Data, Cell, decoder)
import Views.Widget.Renderers.Renderer as Renderer
import Json.Encode as JE
import Json.Decode as JD exposing (field)
import Phoenix.Socket
import Phoenix.Channel
import Phoenix.Push
import Dict


-- CONSTANTS
-- TODO Pass in via a Flag


socketServer : String
socketServer =
    "wss://ew-dashboards-staging.herokuapp.com/socket/websocket"



-- "ws://localhost:4000/socket/websocket"


channelName : String
channelName =
    "dataSource:sheets"



-- MODEL --


type alias Model =
    { errors : List String
    , newMessage : String
    , messages : List String
    , phxSocket : Phoenix.Socket.Socket Msg
    , data : Data
    , widget : Widget Body
    }


subscriptions : Model -> Sub Msg
subscriptions model =
    Phoenix.Socket.listen model.phxSocket PhoenixMsg


user : String
user =
    "msp"


userParams : JE.Value
userParams =
    JE.object [ ( "user_id", JE.string user ) ]


init : Session -> Widget.UUID -> Task PageLoadError Model
init session slug =
    let
        maybeAuthToken =
            Maybe.map .token session.user

        loadWidget =
            Request.Widget.get maybeAuthToken slug
                |> Http.toTask

        handleLoadError err =
            pageLoadError Page.Other ("Widget is currently unavailable. " ++ (toString err))
    in
        Task.map (Model [] "" [] initPhxSocket (Data [])) loadWidget
            |> Task.mapError handleLoadError


initPhxSocket : Phoenix.Socket.Socket Msg
initPhxSocket =
    Phoenix.Socket.init socketServer
        |> Phoenix.Socket.withDebug
        |> Phoenix.Socket.on "new:msg" channelName ReceiveChatMessage



-- VIEW --


view : Session -> Model -> Html Msg
view session model =
    let
        widget =
            model.widget

        author =
            widget.author

        buttons =
            viewButtons widget author session.user
    in
        div [ class "article-page" ]
            [ viewBanner model.errors widget author session.user
            , div [ class "container page" ]
                [ div [ class "row article-content" ]
                    [ div [ class "col-md-12" ]
                        [ h3 [] [ text widget.name ]
                        , Renderer.run widget model.data
                        ]
                    ]
                , hr [] []
                  -- , viewDataSource model
                , div [ class "article-actions" ]
                    [ div [ class "article-meta" ] <|
                        [ a [ Route.href (Route.Profile author.username) ]
                            [ img [ UserPhoto.src author.image ] [] ]
                        , div [ class "info" ]
                            [ Views.Author.view author.username
                            , Views.Widget.viewTimestamp widget
                            ]
                        ]
                            ++ buttons
                    ]
                ]
            ]


viewBanner : List String -> Widget a -> Author -> Maybe User -> Html Msg
viewBanner errors widget author maybeUser =
    let
        buttons =
            viewButtons widget author maybeUser
    in
        div [ class "banner" ]
            [ div [ class "container" ]
                [ h1 [] [ text widget.name ]
                , div [ class "article-meta" ] <|
                    [ a [ Route.href (Route.Profile author.username) ]
                        [ img [ UserPhoto.src author.image ] [] ]
                    , div [ class "info" ]
                        [ Views.Author.view author.username
                        , Views.Widget.viewTimestamp widget
                        ]
                    ]
                        ++ buttons
                , Views.Errors.view DismissErrors errors
                ]
            ]
<commit_msg>Remove widget footer meta data<commit_after>module Page.Widget exposing (Model, Msg(..), init, update, view, subscriptions)

{-| Viewing an individual widget.
-}

import Data.Widget as Widget exposing (Widget, Body)
import Data.Widget.Author as Author exposing (Author)
import Data.Session as Session exposing (Session)
import Data.User as User exposing (User)
import Data.UserPhoto as UserPhoto
import Data.DataSourceMessage as DataSourceMessage exposing (DataSourceMessage, decoder)
import Date exposing (Date)
import Date.Format
import Html exposing (..)
import Html.Attributes exposing (attribute, class, disabled, href, id, placeholder, value, type_)
import Html.Events exposing (onClick, onInput, onSubmit)
import Http
import Page.Errored as Errored exposing (PageLoadError, pageLoadError)
import Request.Widget
import Request.Profile
import Route
import Task exposing (Task)
import Util exposing ((=>), pair, viewIf)
import Views.Widget
import Views.Widget.Favorite as Favorite
import Views.Author
import Views.Errors
import Views.Page as Page
import Views.User.Follow as Follow
import Data.Widget.Table as Table exposing (Data, Cell, decoder)
import Views.Widget.Renderers.Renderer as Renderer
import Json.Encode as JE
import Json.Decode as JD exposing (field)
import Phoenix.Socket
import Phoenix.Channel
import Phoenix.Push
import Dict


-- CONSTANTS
-- TODO Pass in via a Flag


socketServer : String
socketServer =
    "wss://ew-dashboards-staging.herokuapp.com/socket/websocket"



-- "ws://localhost:4000/socket/websocket"


channelName : String
channelName =
    "dataSource:sheets"



-- MODEL --


type alias Model =
    { errors : List String
    , newMessage : String
    , messages : List String
    , phxSocket : Phoenix.Socket.Socket Msg
    , data : Data
    , widget : Widget Body
    }


subscriptions : Model -> Sub Msg
subscriptions model =
    Phoenix.Socket.listen model.phxSocket PhoenixMsg


user : String
user =
    "msp"


userParams : JE.Value
userParams =
    JE.object [ ( "user_id", JE.string user ) ]


init : Session -> Widget.UUID -> Task PageLoadError Model
init session slug =
    let
        maybeAuthToken =
            Maybe.map .token session.user

        loadWidget =
            Request.Widget.get maybeAuthToken slug
                |> Http.toTask

        handleLoadError err =
            pageLoadError Page.Other ("Widget is currently unavailable. " ++ (toString err))
    in
        Task.map (Model [] "" [] initPhxSocket (Data [])) loadWidget
            |> Task.mapError handleLoadError


initPhxSocket : Phoenix.Socket.Socket Msg
initPhxSocket =
    Phoenix.Socket.init socketServer
        |> Phoenix.Socket.withDebug
        |> Phoenix.Socket.on "new:msg" channelName ReceiveChatMessage



-- VIEW --


view : Session -> Model -> Html Msg
view session model =
    let
        widget =
            model.widget

        author =
            widget.author

        buttons =
            viewButtons widget author session.user
    in
        div [ class "article-page" ]
            [ viewBanner model.errors widget author session.user
            , div [ class "container page" ]
                [ div [ class "row article-content" ]
                    [ div [ class "col-md-12" ]
                        [ h3 [] [ text widget.name ]
                        , Renderer.run widget model.data
                        ]
                    ]
                , hr [] []
                  -- , viewDataSource model
                  -- , div [ class "article-actions" ]
                  --     [ div [ class "article-meta" ] <|
                  --         [ a [ Route.href (Route.Profile author.username) ]
                  --             [ img [ UserPhoto.src author.image ] [] ]
                  --           , div [ class "info" ]
                  --               [ Views.Author.view author.username
                  --               , Views.Widget.viewTimestamp widget
                  --               ]
                  --         ]
                  --             ++ buttons
                  --     ]
                ]
            ]


viewBanner : List String -> Widget a -> Author -> Maybe User -> Html Msg
viewBanner errors widget author maybeUser =
    let
        buttons =
            viewButtons widget author maybeUser
    in
        div [ class "banner" ]
            [ div [ class "container" ]
                [ h1 [] [ text widget.name ]
                , div [ class "article-meta" ] <|
                    [ a [ Route.href (Route.Profile author.username) ]
                        [ img [ UserPhoto.src author.image ] [] ]
                    , div [ class "info" ]
                        [ Views.Author.view author.username
                        , Views.Widget.viewTimestamp widget
                        ]
                    ]
                        ++ buttons
                , Views.Errors.view DismissErrors errors
                ]
            ]

{'fraction_non_alphanumeric': 0.08771929824561403, 'fraction_numerical': 0.0016319869441044472, 'mean_word_length': 1.7650874224478286, 'pattern_counts': {'":': 0, '<': 7, '<?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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2535039', 'n_tokens_mistral': 2637, 'n_tokens_neox': 2454, 'n_words': 748}