-
Notifications
You must be signed in to change notification settings - Fork 7
/
FrontEnd.purs
413 lines (382 loc) · 12.5 KB
/
FrontEnd.purs
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
module FrontEnd where
import Prelude
import CSS (backgroundImage, url)
import Control.Monad.Aff (Aff)
import Control.Monad.Aff.AVar (AVAR)
import Control.Monad.Aff.Class (class MonadAff)
import Control.Monad.Aff.Console (CONSOLE, errorShow, log)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (error)
import Control.Monad.Eff.Exception (EXCEPTION)
import Control.Monad.Eff.Now (NOW)
import Control.Monad.Eff.Ref (REF)
import Control.Monad.Eff.Unsafe (unsafePerformEff)
import Control.MonadPlus (guard)
import DOM (DOM)
import Data.Array (drop, filter, head, reverse, sort, sortWith)
import Data.Either (Either(..), either)
import Data.Foreign (ForeignError)
import Data.JSDate as JSDate
import Data.List.NonEmpty (NonEmptyList)
import Data.Maybe (Maybe(Nothing, Just), isJust, isNothing, maybe)
import Data.Monoid (mempty)
import Data.Newtype (unwrap, wrap)
import Data.Set (Set, insert, member)
import Data.String (Pattern(Pattern), Replacement(..), contains, fromCharArray, replace, split, toCharArray, toLower)
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
import Data.Traversable (find)
import Data.Tuple (Tuple(..))
import Data.Validation.Semigroup (V, invalid, unV)
import ECharts.Types as ET
import FrontEnd.Chart as Chart
import FrontEnd.Style as Styles
import Global (encodeURIComponent)
import Halogen as H
import Halogen.Aff as HA
import Halogen.HTML as HH
import Halogen.HTML.CSS (style)
import Halogen.HTML.Events as HE
import Halogen.HTML.Properties as HP
import Halogen.VDom.Driver as D
import Network.HTTP.Affjax (AJAX)
import Network.HTTP.Affjax as AJ
import Network.HTTP.RequestHeader (RequestHeader(..))
import Routes (class GetHTTPMethod, Route, files, getHTTPMethod, getIcons, open, remove, update, watched)
import Simple.JSON (class ReadForeign, class WriteForeign, readJSON, writeJSON)
import Types (FileData(..), GetIconsRequest(..), OpenRequest(..), Path(..), RemoveRequest(..), WatchedData(..))
reverse' :: String -> String
reverse' = fromCharArray <<< reverse <<< toCharArray
extractNameKinda :: Path -> Either String String
extractNameKinda (Path s)
| [_, a] <- split (Pattern "] ") s
, result <- split (Pattern " - ") (reverse' a)
, Just b <- head $ drop 1 result
, c <- replace (Pattern ".") (Replacement "") b = Right (reverse' c)
| otherwise = Left "didn't match expected patterns"
type VE a = V (NonEmptyList ForeignError) a
request :: forall method req res url m eff.
MonadAff
( ajax :: AJAX
| eff
)
m
=> GetHTTPMethod method
=> WriteForeign req
=> ReadForeign res
=> IsSymbol url
=> Route method req res url -> Maybe req -> m (VE res)
request route body =
H.liftAff $ either invalid pure <$> parseResponse <$> action
where
action = AJ.affjax $ AJ.defaultRequest
{ method = Left (getHTTPMethod route)
, url = reflectSymbol (SProxy :: SProxy url)
, content = writeJSON <$> body
, headers = [RequestHeader "Content-Type" "application/json"]
}
parseResponse response = readJSON response.response
unV' :: forall e a m.
MonadAff
( console :: CONSOLE
| e
)
m
=> (a -> m Unit) -> VE a -> m Unit
unV' = unV (H.liftAff <<< errorShow)
data Dir = ASC | DSC
derive instance eqDir :: Eq Dir
data Col = Title | Status
derive instance eqCol :: Eq Col
data Sorting = Sorting Col Dir | NoSorting
newtype ChartSeries = ChartSeries (Array ChartSeriesData)
newtype ChartSeriesData = ChartSeriesData
{ date :: String
, value :: Int
}
type State =
{ files :: Array Path
, watched :: Array WatchedData
, filterWatched :: Boolean
, sorting :: Sorting
, search :: String
, deleteConfirmations :: Set Path
}
data Query a
= Init a
| FetchData a
| GetIcons a
| OpenFile Path a
| SetWatched Path Boolean a
| Filter Path a
| ChangeSorting Col a
| Search String a
| ClearSearch a
| ToggleFilterWatched Boolean a
| ConfirmDeletion Path a
| Delete Path a
type AppEffects eff =
Aff
( ajax :: AJAX
, console :: CONSOLE
, dom :: DOM
, echarts :: ET.ECHARTS
, exception :: EXCEPTION
, now :: NOW
| eff )
data Slot = ChartSlot
derive instance eqSlot :: Eq Slot
derive instance ordSlot :: Ord Slot
ui :: forall eff. H.Component HH.HTML Query Unit Void (AppEffects eff)
ui =
H.lifecycleParentComponent
{ initialState: const initialState
, render
, eval
, receiver: const Nothing
, initializer: Just (H.action Init)
, finalizer: Nothing
}
where
initialState :: State
initialState =
{ files: mempty
, watched: mempty
, filterWatched: false
, sorting: NoSorting
, search: mempty
, deleteConfirmations: mempty
}
render :: State -> H.ParentHTML Query Chart.Query Slot (AppEffects eff)
render state =
HH.div
[ HP.class_ $ Styles.container ]
$ [ HH.h1_ [ HH.text "Vidtracker" ]
, heatmap
, refreshButton
, getIconsButton
, filterCheckbox
, search
, header
] <> files
where
heatmap =
HH.slot ChartSlot Chart.component state.watched absurd
refreshButton =
HH.button
[ HP.classes $
[ Styles.refreshFiles
, wrap "pure-button"
]
, HE.onClick <<< HE.input_ $ FetchData
]
[ HH.text "Refresh" ]
getIconsButton =
HH.button
[ HP.classes $
[ Styles.getIcons
, wrap "pure-button"
]
, HE.onClick <<< HE.input_ $ GetIcons
]
[ HH.text "Run Get Icons" ]
filterCheckbox =
HH.button
[ HP.classes $
[ Styles.filterWatched
, wrap "pure-button"
] <> (guard state.filterWatched $> wrap "pure-button-primary")
, HE.onClick <<< HE.input_ <<< ToggleFilterWatched <<< not $ state.filterWatched
]
[ HH.text "Filter Watched" ]
search =
HH.div_
$ [ HH.h4_ [ HH.text "Search" ]
, HH.input
[ HP.value state.search
, HE.onValueInput (HE.input Search)
]
] <> clear
clear =
guard (state.search /= "") $>
HH.button
[ HE.onClick $ HE.input_ ClearSearch ]
[ HH.text "Clear" ]
header =
HH.div
[ HP.class_ $ Styles.file]
[ HH.h3
[ HP.class_ $ Styles.dot
] []
, HH.h3
[ HP.class_ $ Styles.fileLink
, HE.onClick $ HE.input_ (ChangeSorting Title)
] [ HH.text $ "Title" <> displayTicker Title ]
, HH.h3
[ HP.class_ $ Styles.fileButton
, HE.onClick $ HE.input_ (ChangeSorting Status)
] [ HH.text $ "Status" <> displayTicker Status ]
, HH.h3 [HP.class_ $ Styles.fileNote ] [ HH.text "Date" ]
, HH.h3
[ HP.class_ $ Styles.filterLink
] [ HH.text "" ]
, HH.h3
[ HP.class_ $ Styles.deleteLink
] [ HH.text "" ]
]
files =
file <$> applyTransforms state.files
displayTicker col
| Sorting col' dir <- state.sorting
, asc <- dir == ASC
, col' == col = if asc
then " ASC"
else " DSC"
| otherwise = ""
applyTransforms = applySorting <<< applySearchFiltering <<< applyWatchFiltering
applyWatchFiltering = if state.filterWatched
then filter $ isNothing <<< findWatched
else id
applySearchFiltering = case state.search of
"" -> id
x -> filter $ \(Path path) -> contains (Pattern $ toLower x) (toLower path)
applySorting
| Sorting col dir <- state.sorting
, rev <- if dir == ASC
then id
else reverse
, sort' <- case col of
Title -> sort
Status -> sortWith findWatched
= rev <<< sort'
| otherwise = id
findWatched path = find (\(WatchedData fd) -> fd.path == path) state.watched
file path =
HH.div
[ HP.class_ $ Styles.file ]
[ HH.span
[ HP.class_ $ Styles.dot
, style do
case extractNameKinda path of
Right name ->
backgroundImage (url $ "icons/" <> encodeURIComponent name)
Left e -> pure mempty
-- borderColor $ fromMaybe (rgb 255 105 180) dotColor
] []
, HH.a
[ HP.class_ Styles.fileLink
, HE.onClick $ HE.input_ (OpenFile path) ]
[ HH.text $ unwrap path ]
, HH.button
[ HP.classes $
[ Styles.fileButton
, wrap "pure-button"
, wrap $ maybe "" (const "pure-button-primary") watched
]
, HE.onClick $ HE.input_ (SetWatched path (not $ isJust watched))
]
[ HH.text $ maybe "not watched" (const "watched") watched ]
, HH.span
[ HP.class_ $ Styles.fileNote ]
[ HH.text $ maybe "" id watched ]
, HH.button
[ HP.classes $
[ Styles.filterLink
, wrap "pure-button"
]
, HE.onClick $ HE.input_ (Filter path)
]
[ HH.text "set filter" ]
, HH.button
[ HP.classes $
[ Styles.deleteLink
, wrap "pure-button"
, if deleteConfirmation
then Styles.deleteConfirmation
else wrap ""
]
, HE.onClick $ HE.input_ $
if deleteConfirmation
then (Delete path)
else (ConfirmDeletion path)
]
[ HH.text
if deleteConfirmation
then "Confirm"
else "Delete"
]
]
where
watched = getDate <$> findWatched path
deleteConfirmation = member path state.deleteConfirmations
getDate (WatchedData {created}) =
-- parsing date is UTZ dependent (ergo effectful), but in our case, we really don't care
JSDate.toDateString <<< unsafePerformEff <<< JSDate.parse $ created
error' = H.liftEff <<< error
eval :: Query ~> H.ParentDSL State Query Chart.Query Slot Void (AppEffects eff)
eval (Init next) = do
eval (FetchData next)
eval (FetchData next) = do
getResult >>= unV'
\(Tuple f w) -> do
H.modify _ {files = f, watched = w}
pure next
where
getResult = do
files <- request files Nothing
watched <- request watched Nothing
pure $ Tuple <$> files <*> watched
eval (GetIcons next) = do
_ <- request getIcons $ Just (GetIconsRequest {})
pure next
eval (OpenFile path next) = do
_ <- request open $ Just (OpenRequest {path})
pure next
eval (SetWatched path flag next) = do
request update (Just $ FileData {path, watched: flag})
>>= unV' \w -> H.modify _ {watched = w}
pure next
eval (Filter path next) = do
case extractNameKinda path of
Left e -> error' e *> pure next
Right s -> eval $ Search s next
eval (Search str next) = do
H.modify _ {search = str}
pure next
eval (ClearSearch next) = do
H.modify _ {search = ""}
pure next
eval (ChangeSorting col next)= do
H.modify $ \s -> case s.sorting of
Sorting curr dir | curr == col ->
s {sorting = case dir of
ASC -> Sorting col DSC
DSC -> NoSorting
}
_ -> s {sorting = Sorting col ASC}
pure next
eval (ToggleFilterWatched flag next) = do
H.modify _ {filterWatched = flag}
pure next
eval (ConfirmDeletion path next) = do
H.modify \s -> s {deleteConfirmations = insert path s.deleteConfirmations}
pure next
eval (Delete path next) = do
_ <- request remove $ Just (RemoveRequest {path})
eval (FetchData next)
main :: forall e.
Eff
( avar :: AVAR
, ref :: REF
, exception :: EXCEPTION
, dom :: DOM
, console :: CONSOLE
, ajax :: AJAX
, now :: NOW
, echarts :: ET.ECHARTS
| e
)
Unit
main = HA.runHalogenAff do
body <- HA.awaitBody
io <- D.runUI ui unit body
log "Running"