aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/Profile.elm
blob: 6575d41e4429649d2d3f24d0fb194bab26a8c98f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
module Profile exposing (..)

import Browser
import Browser.Navigation as Nav
import Css exposing (..)
import Html
import Html.Styled exposing (..)
import Html.Styled.Attributes exposing (..)
import Html.Styled.Events exposing (..)
import Http
import Icons exposing (..)
import Json.Decode as D
import Json.Encode as Encode
import Styles exposing (..)
import Utils exposing (..)


emptyProfile =
    UserProfile "" "" "" Nothing 0 []


type alias Transaction =
    { amount : Float
    , transactionId : Int
    , orderDate : String
    , paymentMode : String
    }


type alias UserProfile =
    { username : String
    , phoneNumber : String
    , emailId : String
    , address : Maybe String
    , ratingsGiven : Int
    , transactions : List Transaction
    }


type alias Model =
    { profile : UserProfile
    , status : Status
    }


type Status
    = Loading
    | Loaded
    | NotLoaded


type Msg
    = ProfileLoaded (Result Http.Error UserProfile)
    | FetchProfile


init : Model
init =
    Model emptyProfile NotLoaded


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        ProfileLoaded res ->
            case res of
                Ok p ->
                    ( { model | profile = p }, Cmd.none )

                Err _ ->
                    ( { model | status = NotLoaded }, Cmd.none )

        FetchProfile ->
            ( { model | status = Loading }, tryFetchProfile )


decodeProfile : D.Decoder UserProfile
decodeProfile =
    D.map6 UserProfile
        (D.field "username" D.string)
        (D.field "phone_number" D.string)
        (D.field "email_id" D.string)
        (D.field "address" (D.nullable D.string))
        (D.field "ratings_given" D.int)
        (D.field "transactions" (D.list decodeTransaction))


decodeTransaction : D.Decoder Transaction
decodeTransaction =
    D.map4 Transaction
        (D.field "amount" D.float)
        (D.field "id" D.int)
        (D.field "order_date" D.string)
        (D.field "payment_type" D.string)


tryFetchProfile : Cmd Msg
tryFetchProfile =
    let
        _ =
            Debug.log "err" <| "fetching user profile"
    in
    Http.riskyRequest
        { method = "GET"
        , headers = []
        , url = "http://127.0.0.1:7878/user/profile"
        , body = Http.emptyBody
        , expect = Http.expectJson ProfileLoaded decodeProfile
        , timeout = Nothing
        , tracker = Nothing
        }


viewStatus : Status -> String
viewStatus s =
    case s of
        Loading ->
            "Loading"

        Loaded ->
            "Ready!"

        NotLoaded ->
            "Not loaded ..."


viewTransactions : List Transaction -> Html Msg
viewTransactions ts =
    let
        headings =
            [ "Order ID", "Date", "Amount (₹)", "Payment Mode" ]
                |> List.map (th [] << List.singleton << text)

        transactionRow t =
            List.map (td [] << List.singleton)
                [ text <| String.fromInt t.transactionId
                , text t.orderDate
                , text <| String.fromFloat t.amount
                , text t.paymentMode
                ]
    in
    div []
        [ div
            [ css [ bigHeading, marginTop (px 20), marginBottom (px 12) ] ]
            [ text "Transactions" ]
        , Html.Styled.table
            [ css
                [ Css.width (pct 100)
                , maxWidth (px 650)
                ]
            ]
            ([ tr [ style "text-align" "right" ] headings
             ]
                ++ List.map (tr [ style "text-align" "right" ] << transactionRow) ts
            )
        ]


profileField : String -> String -> Html Msg
profileField fieldName entry =
    div []
        [ div
            [ css
                [ cardSecondaryText
                , textTransform uppercase
                , paddingBottom (px 3)
                ]
            ]
            [ text fieldName ]
        , div
            [ css
                [ cardPrimaryText
                , paddingBottom (px 12)
                ]
            ]
            [ text entry ]
        ]


viewProfile : UserProfile -> Html Msg
viewProfile u =
    div
        []
        [ div
            [ css [ bigHeading, marginTop (px 20), marginBottom (px 12) ] ]
            [ text "Profile" ]
        , profileField "name" u.username
        , profileField "email" u.emailId
        , profileField "contact number" u.phoneNumber
        , profileField "address" <| Maybe.withDefault "No address provided" u.address
        , profileField "Total Reviews" <| String.fromInt u.ratingsGiven
        , hr [] []
        , viewTransactions u.transactions
        ]


view : Model -> Html Msg
view model =
    case model.status of
        Loading ->
            div [] [ text <| viewStatus Loading ]

        _ ->
            div
                [ css
                    [ margin auto
                    , marginTop (pct 5)
                    , Css.width (pct 40)
                    ]
                ]
                [ viewProfile model.profile ]