/
TicketPreview.elm
326 lines (261 loc) · 10.2 KB
/
TicketPreview.elm
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
port module TicketPreview exposing (Model, Msg(..), init, main, subscriptions, update, view)
import Base64.Encode
import Basics.Extra exposing (flip)
import Browser
import Css exposing (focus, hover)
import Css.Global
import Dict exposing (Dict)
import Escpos exposing (batch, cut, initialize, newline)
import Html
import Html.Parser exposing (Node)
import Html.Parser.Util
import Html.Styled exposing (..)
import Html.Styled.Attributes exposing (css, src)
import Html.Styled.Events exposing (onClick)
import Http
import List.Extra
import LoadingData exposing (LoadingData(..))
import Svg.Styled exposing (svg)
import Svg.Styled.Attributes
import Tailwind.Breakpoints as Bp
import Tailwind.Utilities as Tw
import Tickets exposing (TicketDemo, allTickets)
import Time
-- MAIN
main : Program Flags Model Msg
main =
Browser.document
{ init = init
, view = mainView
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Flags =
{ ticketRendererEndpoint : String
}
type alias Model =
{ ticketRendererEndpoint : String
, ticketsHtml : Dict String (LoadingData ( Escpos.Command, Html Msg ))
, maybeSelectedTicket : Maybe String
}
init : Flags -> ( Model, Cmd Msg )
init flags =
let
dict =
allTickets
|> List.map (\{ id, escposCommand } -> ( id, Loading ))
|> Dict.fromList
maybeSelectedTicket =
if Dict.member "Capabilities" dict then
Just "Capabilities"
else
Nothing
in
( Model flags.ticketRendererEndpoint dict maybeSelectedTicket
, Cmd.none
)
-- PORTS
port connectAndPrint : List Int -> Cmd msg
-- UPDATE
type Msg
= GotTicketDemo String (Maybe ( Escpos.Command, Html Msg ))
| RequestTicketDemo TicketDemo
| ConnectAndPrint Escpos.Command
| SelectTicket String
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
GotTicketDemo id maybeData ->
( { model
| ticketsHtml =
model.ticketsHtml
|> Dict.insert id
(maybeData
|> Maybe.map Success
|> Maybe.withDefault Error
)
}
, Cmd.none
)
RequestTicketDemo ticketDemo ->
( model
, ticketDemoCmd model.ticketRendererEndpoint ticketDemo
)
ConnectAndPrint escposCommand ->
let
printCommand =
batch []
(List.concat
[ [ initialize ]
, [ escposCommand ]
-- These two newlines add some padding so content is not cut in half
, [ newline, newline ]
, [ cut ]
]
)
in
( model
, connectAndPrint (Escpos.encodeToInts printCommand)
)
SelectTicket id ->
( { model
| maybeSelectedTicket = Just id
, ticketsHtml = Dict.insert id Loading model.ticketsHtml
}
, Cmd.none
)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
let
maybeTicketDemo =
model.maybeSelectedTicket
|> Maybe.andThen (\id -> List.Extra.find (\td -> td.id == id) allTickets)
in
case maybeTicketDemo of
Just ticketDemo ->
Time.every 1000 (\_ -> RequestTicketDemo ticketDemo)
Nothing ->
Sub.none
-- VIEW
mainView : Model -> Browser.Document Msg
mainView model =
{ title = "Tickets Preview"
, body =
[ Html.Styled.toUnstyled <|
div []
[ Css.Global.global Tw.globalStyles
, view model
]
]
}
view : Model -> Html Msg
view model =
let
sidebar =
section [ css [ Tw.px_8, Tw.min_w_min ] ]
[ div [ css [ Tw.py_4, Tw.px_2, Tw.text_lg, Tw.font_semibold, Tw.tracking_widest, Tw.text_gray_900, Tw.uppercase, Tw.rounded_lg ] ]
[ text "Tickets" ]
, nav [ css [ Tw.flex, Tw.flex_col, Tw.w_full, Tw.col_span_1, Tw.bg_white, Tw.space_y_2 ] ]
(allTickets
|> List.map
(\{ id } ->
let
selectedAttr =
if model.maybeSelectedTicket |> Maybe.withDefault "" |> (==) id then
Tw.bg_gray_200
else
Tw.bg_white
in
a [ onClick (SelectTicket id), css [ Tw.transition_all, Tw.duration_200, Tw.py_2, Tw.px_2, Tw.text_gray_900, Tw.text_sm, Tw.font_semibold, Tw.cursor_pointer, Tw.rounded_lg, selectedAttr, hover [ Tw.bg_gray_200 ], focus [ Tw.bg_blue_900 ] ] ]
[ text id ]
)
)
]
ticketSection =
let
ticketContent =
case model.maybeSelectedTicket |> Maybe.andThen (flip Dict.get model.ticketsHtml) of
Nothing ->
text "Select a ticket"
Just loadingData ->
case loadingData of
Loading ->
div [ css [ Tw.place_self_center, Tw.animate_spin, Tw.ease_linear, Tw.rounded_full, Tw.border_t_8, Tw.border_gray_300, Tw.h_10, Tw.w_10 ] ] []
Error ->
text "Woops!"
Success ( escposCommand, ticketHtml ) ->
let
showPhoto =
model.maybeSelectedTicket |> Maybe.withDefault "" |> (==) "Capabilities"
capabilitiesPhoto =
if showPhoto then
[ img [ css [ Tw.col_span_1, Tw.py_14 ], src "https://user-images.githubusercontent.com/8648893/129381755-81baa7c4-016b-47bb-8867-ed0bbd54313e.png" ] [] ]
else
[]
ticketColSpan =
if showPhoto then
Tw.col_span_1
else
Tw.col_span_2
in
div
[ css [ Tw.grid, Tw.grid_cols_1, Bp.lg [ Tw.grid_cols_2 ] ] ]
(div [ css [ ticketColSpan, Tw.space_y_5, Tw.flex, Tw.flex_col, Tw.items_center ] ]
[ button [ css [ Tw.space_x_1, Tw.shadow, Tw.bg_white, hover [ Tw.bg_gray_300 ], Tw.text_gray_900, Tw.font_bold, Tw.py_2, Tw.px_4, Tw.rounded, Tw.inline_flex, Tw.items_center ], onClick (ConnectAndPrint escposCommand) ]
[ printerIcon
, span [] [ text "Print" ]
]
, div [ css [ Tw.bg_white, Tw.w_min, Tw.shadow_2xl ] ] [ ticketHtml ]
]
:: capabilitiesPhoto
)
in
section [ css [ Tw.px_6, Tw.py_6, Tw.col_span_4, Tw.bg_gray_200 ] ]
[ div [ css [ Tw.flex, Tw.h_full, Tw.max_w_full, Tw.w_full, Tw.justify_center ] ]
[ ticketContent
]
]
in
main_ [ css [ Tw.h_screen, Tw.grid, Tw.grid_cols_5 ] ]
[ sidebar
, ticketSection
]
-- HELPERS
parseHtml : String -> Maybe (Html msg)
parseHtml =
Html.Parser.runDocument
>> Result.toMaybe
>> Maybe.map (.document >> Tuple.second)
>> Maybe.map Html.Parser.Util.toVirtualDom
>> Maybe.map (Html.div [])
>> Maybe.map fromUnstyled
ticketDemoCmd : String -> TicketDemo -> Cmd Msg
ticketDemoCmd rendererEndpoint ticketDemo =
let
base64 =
ticketDemo.escposCommand
|> Escpos.encodeToBytes
|> Base64.Encode.bytes
|> Base64.Encode.encode
body =
Http.stringBody "multipart/form-data;boundary=boundary"
(String.join "\n"
[ "--boundary"
, "Content-Disposition: form-data; name=\"receipt\""
, ""
, base64
, "--boundary--"
]
)
in
Http.post
{ url = rendererEndpoint
, body = body
, expect =
Http.expectString
(Result.toMaybe
>> Maybe.andThen parseHtml
>> Maybe.map (Tuple.pair ticketDemo.escposCommand)
>> GotTicketDemo ticketDemo.id
)
}
printerIcon =
svg
[ Svg.Styled.Attributes.css
[ Tw.h_6
, Tw.w_6
]
, Svg.Styled.Attributes.fill "none"
, Svg.Styled.Attributes.viewBox "0 0 24 24"
, Svg.Styled.Attributes.stroke "currentColor"
]
[ Svg.Styled.path
[ Svg.Styled.Attributes.strokeLinecap "round"
, Svg.Styled.Attributes.strokeLinejoin "round"
, Svg.Styled.Attributes.strokeWidth "2"
, Svg.Styled.Attributes.d "M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2 4h6a2 2 0 002-2v-4a2 2 0 00-2-2H9a2 2 0 00-2 2v4a2 2 0 002 2zm8-12V5a2 2 0 00-2-2H9a2 2 0 00-2 2v4h10z"
]
[]
]