2019-01-31 02:54:47 +00:00
|
|
|
module App where
|
|
|
|
|
|
|
|
import Prelude
|
|
|
|
|
2019-11-14 04:03:21 +00:00
|
|
|
import Affjax (Response, Error)
|
2019-01-31 02:54:47 +00:00
|
|
|
import Affjax (defaultRequest) as AX
|
|
|
|
import Affjax as Ax
|
|
|
|
import Affjax.RequestBody as AXReq
|
|
|
|
import Affjax.RequestHeader (RequestHeader(..))
|
|
|
|
import Affjax.ResponseFormat as AXRes
|
|
|
|
import Data.Argonaut (Json)
|
|
|
|
import Data.Array ((:))
|
|
|
|
import Data.Either (Either(..))
|
|
|
|
import Data.FormURLEncoded (FormURLEncoded)
|
|
|
|
import Data.HTTP.Method (Method(..))
|
|
|
|
import Data.Maybe (Maybe(..))
|
|
|
|
import Data.MediaType.Common (applicationFormURLEncoded, applicationJSON)
|
|
|
|
import Effect.Aff (Aff)
|
|
|
|
import Effect.Class (liftEffect)
|
|
|
|
import Globals (app')
|
|
|
|
import Model (Bookmark, Bookmark'(..), Note, Note'(..), AccountSettings, AccountSettings'(..))
|
|
|
|
import Simple.JSON as J
|
|
|
|
import Web.HTML (window)
|
|
|
|
import Web.HTML.Location (reload)
|
|
|
|
import Web.HTML.Window (location)
|
|
|
|
|
|
|
|
data StarAction = Star | UnStar
|
|
|
|
instance showStar :: Show StarAction where
|
|
|
|
show Star = "star"
|
|
|
|
show UnStar = "unstar"
|
|
|
|
|
|
|
|
toggleStar :: Int -> StarAction -> Aff Unit
|
|
|
|
toggleStar bid action = do
|
|
|
|
let path = "bm/" <> show bid <> "/" <> show action
|
|
|
|
void (fetchUrlEnc POST path Nothing AXRes.ignore)
|
|
|
|
|
2019-11-14 04:03:21 +00:00
|
|
|
destroy :: Int -> Aff (Either Error (Response Unit))
|
2019-01-31 02:54:47 +00:00
|
|
|
destroy bid =
|
|
|
|
fetchUrlEnc DELETE ("bm/" <> show bid) Nothing AXRes.ignore
|
|
|
|
|
2019-11-14 04:03:21 +00:00
|
|
|
markRead :: Int -> Aff (Either Error (Response Unit))
|
2019-01-31 02:54:47 +00:00
|
|
|
markRead bid = do
|
|
|
|
let path = "bm/" <> show bid <> "/read"
|
|
|
|
fetchUrlEnc POST path Nothing AXRes.ignore
|
|
|
|
|
2019-11-14 04:03:21 +00:00
|
|
|
editBookmark :: Bookmark -> Aff (Either Error (Response Unit))
|
2019-01-31 02:54:47 +00:00
|
|
|
editBookmark bm = do
|
|
|
|
fetchJson POST "api/add" (Just (Bookmark' bm)) AXRes.ignore
|
|
|
|
|
2019-11-14 04:03:21 +00:00
|
|
|
editNote :: Note -> Aff (Either Error (Response Json))
|
2019-01-31 02:54:47 +00:00
|
|
|
editNote bm = do
|
|
|
|
fetchJson POST "api/note/add" (Just (Note' bm)) AXRes.json
|
|
|
|
|
2019-11-14 04:03:21 +00:00
|
|
|
destroyNote :: Int -> Aff (Either Error (Response Unit))
|
2019-01-31 02:54:47 +00:00
|
|
|
destroyNote nid = do
|
|
|
|
fetchUrlEnc DELETE ("api/note/" <> show nid) Nothing AXRes.ignore
|
|
|
|
|
2019-11-14 04:03:21 +00:00
|
|
|
editAccountSettings :: AccountSettings -> Aff (Either Error (Response Unit))
|
2019-01-31 02:54:47 +00:00
|
|
|
editAccountSettings us = do
|
|
|
|
fetchJson POST "api/accountSettings" (Just (AccountSettings' us)) AXRes.ignore
|
|
|
|
|
|
|
|
logout :: Unit -> Aff Unit
|
|
|
|
logout u = do
|
|
|
|
void (fetchUrl POST app.authRlogoutR [] Nothing AXRes.ignore)
|
|
|
|
liftEffect (window >>= location >>= reload)
|
|
|
|
where
|
|
|
|
app = app' u
|
|
|
|
|
|
|
|
fetchJson
|
|
|
|
:: forall a b.
|
|
|
|
J.WriteForeign b
|
|
|
|
=> Method
|
|
|
|
-> String
|
|
|
|
-> Maybe b
|
|
|
|
-> AXRes.ResponseFormat a
|
2019-11-14 04:03:21 +00:00
|
|
|
-> Aff (Either Error (Response a))
|
2019-01-31 02:54:47 +00:00
|
|
|
fetchJson method path content rt =
|
|
|
|
fetchPath method path [ContentType applicationJSON] (AXReq.string <<< J.writeJSON <$> content) rt
|
|
|
|
|
|
|
|
fetchUrlEnc
|
|
|
|
:: forall a.
|
|
|
|
Method
|
|
|
|
-> String
|
|
|
|
-> Maybe FormURLEncoded
|
|
|
|
-> AXRes.ResponseFormat a
|
2019-11-14 04:03:21 +00:00
|
|
|
-> Aff (Either Error (Response a))
|
2019-01-31 02:54:47 +00:00
|
|
|
fetchUrlEnc method path content rt =
|
|
|
|
fetchPath method path [ContentType applicationFormURLEncoded] (AXReq.FormURLEncoded <$> content) rt
|
|
|
|
|
|
|
|
fetchPath
|
|
|
|
:: forall a.
|
|
|
|
Method
|
|
|
|
-> String
|
|
|
|
-> Array RequestHeader
|
|
|
|
-> Maybe AXReq.RequestBody
|
|
|
|
-> AXRes.ResponseFormat a
|
2019-11-14 04:03:21 +00:00
|
|
|
-> Aff (Either Error (Response a))
|
2019-01-31 02:54:47 +00:00
|
|
|
fetchPath method path headers content rt =
|
|
|
|
fetchUrl method ((app' unit).homeR <> path) headers content rt
|
|
|
|
|
|
|
|
fetchUrl
|
|
|
|
:: forall a.
|
|
|
|
Method
|
|
|
|
-> String
|
|
|
|
-> Array RequestHeader
|
|
|
|
-> Maybe AXReq.RequestBody
|
|
|
|
-> AXRes.ResponseFormat a
|
2019-11-14 04:03:21 +00:00
|
|
|
-> Aff (Either Error (Response a))
|
2019-01-31 02:54:47 +00:00
|
|
|
fetchUrl method url headers content rt =
|
|
|
|
Ax.request
|
|
|
|
AX.defaultRequest
|
|
|
|
{ url = url
|
|
|
|
, method = Left method
|
|
|
|
, headers = RequestHeader app.csrfHeaderName app.csrfToken : headers
|
|
|
|
, content = content
|
|
|
|
, responseFormat = rt
|
|
|
|
}
|
|
|
|
where
|
|
|
|
app = app' unit
|