@@ -2,39 +2,38 @@ module Client.Im.Asks where
22
33import Prelude
44
5- import Client.AppId (imAppId )
6- import Client.File as CCF
7- import Client.Im.Flame (MoreMessages , NoMessages , NextMessage )
8- import Client.Im.WebSocket as CIW
5+ import Client.Im.Flame (MoreMessages , NextMessage , NoMessages )
96import Client.Network (request )
107import Client.Network as CCN
11- import Control.Alt ((<|>))
128import Data.Array ((:))
139import Data.Array as DA
14- import Data.Int as DI
1510import Data.Maybe (Maybe (..))
16- import Data.Maybe as DM
17- import Data.String as DS
18- import Data.Symbol as TDS
1911import Data.Tuple.Nested ((/\))
20- import Debug (spy )
21- import Effect.Class as EC
22- import Shared.Content (Content (..))
23- import Shared.Im.Types (For (..), ImMessage (..), ImModel , PostMode (..), RetryableRequest (..), SelectedImage , WebSocketPayloadServer (..))
12+ import Shared.Ask (Ask )
13+ import Shared.Im.Types (For (..), ImMessage (..), ImModel , RetryableRequest (..))
2414import Shared.Modal (Modal (..), SpecialModal (..))
25- import Shared.Post (Post )
26- import Shared.Resource (maxImageSize )
2715import Shared.Unsafe as SU
2816import Shared.User (ProfileTab (..))
29- import Type.Proxy (Proxy (..))
30- import Web.Event.Event as WEE
31- import Web.Event.Internal.Types (Event )
32- import Web.HTML.HTMLInputElement as WDE
33- import Web.HTML.HTMLInputElement as WHI
34- import Web.Socket.WebSocket (WebSocket )
17+
18+ displayAsks ∷ Int → Array Ask → ImModel → NoMessages
19+ displayAsks userId asks model =
20+ model
21+ { asks = model.asks { freeToFetch = true }
22+ , suggestions = map updateSuggestion model.suggestions
23+ , contacts = map updateContact model.contacts
24+ } /\ []
25+ where
26+ updateSuggestion suggestion
27+ | suggestion.id == userId = suggestion { asks = suggestion.asks <> asks }
28+ | otherwise = suggestion
29+ updateContact contact
30+ | contact.user.id == userId = contact { user = contact.user { asks = contact.user.asks <> asks } }
31+ | otherwise = contact
3532
3633fetchAsks ∷ Int → ImModel → MoreMessages
37- fetchAsks userId model = model /\ []
34+ fetchAsks userId model = model { asks = model.asks { freeToFetch = false } } /\ [ fetch ]
35+ where
36+ fetch = CCN .retryableResponse (FetchAsks userId) (DisplayAsks userId) $ request.asks.get { query: { answerer: userId } }
3837
3938setAsk ∷ Maybe String → ImModel → MoreMessages
4039setAsk value model = model { asks = model.asks { question = value } } /\ []
@@ -62,3 +61,50 @@ afterSendAsk userId allowed model =
6261 (userId : model.asks.sent) /\ model.asks.unallowed
6362 else
6463 model.asks.sent /\ (userId : model.asks.unallowed)
64+
65+ toggleShowing ∷ Int → For → ImModel → MoreMessages
66+ toggleShowing userId for model =
67+ case for of
68+ ForSuggestions → toggleShowingSuggestions userId model
69+ ForContacts → toggleShowingContacts userId model
70+
71+ toggleShowingSuggestions ∷ Int → ImModel → MoreMessages
72+ toggleShowingSuggestions userId model =
73+ model
74+ { suggestions = map update model.suggestions
75+ -- we need this bookkeeping for big suggestion cards
76+ , suggesting = Just userId
77+ , modal = Special $ ShowSuggestionCard userId
78+ , asks = model.asks { question = Nothing , freeToFetch = not shouldFetch }
79+ } /\ effects
80+ where
81+ found = DA .find ((_ == userId) <<< _.id) model.suggestions
82+ shouldFetch = Just ShowAsks /= (_.showing <$> found) && Just 0 == (DA .length <<< _.asks <$> found)
83+
84+ update suggestion
85+ | suggestion.id == userId = suggestion { showing = ShowAsks }
86+ | otherwise = suggestion
87+
88+ effects
89+ | shouldFetch = [ pure <<< Just <<< SpecialRequest $ FetchAsks userId ]
90+ | otherwise = []
91+
92+ toggleShowingContacts ∷ Int → ImModel → MoreMessages
93+ toggleShowingContacts userId model =
94+ model
95+ { contacts = map update model.contacts
96+ , asks = model.asks { question = Nothing , freeToFetch = not shouldFetch }
97+ , fullContactProfileVisible = true
98+ } /\ effects
99+ where
100+ found = _.user <$> DA .find ((_ == userId) <<< _.id <<< _.user) model.contacts
101+ shouldFetch = Just ShowAsks /= (_.showing <$> found) && Just 0 == (DA .length <<< _.asks <$> found)
102+
103+ update contact
104+ | contact.user.id == userId = contact { user = contact.user { showing = ShowAsks } }
105+ | otherwise = contact
106+
107+ effects
108+ | shouldFetch = [ pure <<< Just <<< SpecialRequest $ FetchAsks userId ]
109+ | otherwise = []
110+
0 commit comments