Skip to content

Commit f5084ba

Browse files
committed
Show profile questions in suggestions
1 parent 0a19845 commit f5084ba

File tree

13 files changed

+157
-58
lines changed

13 files changed

+157
-58
lines changed

src/Client/Im/Asks.purs

Lines changed: 68 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,38 @@ module Client.Im.Asks where
22

33
import 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)
96
import Client.Network (request)
107
import Client.Network as CCN
11-
import Control.Alt ((<|>))
128
import Data.Array ((:))
139
import Data.Array as DA
14-
import Data.Int as DI
1510
import Data.Maybe (Maybe(..))
16-
import Data.Maybe as DM
17-
import Data.String as DS
18-
import Data.Symbol as TDS
1911
import 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(..))
2414
import Shared.Modal (Modal(..), SpecialModal(..))
25-
import Shared.Post (Post)
26-
import Shared.Resource (maxImageSize)
2715
import Shared.Unsafe as SU
2816
import 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

3633
fetchAsks 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

3938
setAsk Maybe String ImModel MoreMessages
4039
setAsk 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+

src/Client/Im/Main.purs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ update st model =
175175
React userId messageId value event → CIH.react userId messageId value event model
176176
DisplayReaction userId messageId reaction → CIH.displayReaction userId messageId reaction model
177177

178+
--asks
179+
SpecialRequest (FetchAsks userId) → CIA.fetchAsks userId model
180+
SetAsk value → CIA.setAsk value model
181+
SendAsk userId → CIA.sendAsk userId model
182+
AfterSendAsk userId allowed → CIA.afterSendAsk userId allowed model
183+
ToggleShowing userId for ShowAsksCIA.toggleShowing userId for model
184+
DisplayAsks userId asks → CIA.displayAsks userId asks model
185+
178186
--posts
179187
DisplayPosts userId posts → CIPS.displayPosts userId posts model
180188
SpecialRequest (FetchPosts userId) → CIPS.fetchPosts userId model
@@ -189,12 +197,6 @@ update st model =
189197
AfterSendPost id → CIPS.afterSendPost id webSocket model
190198
ToggleShowing userId for toggle → CIPS.toggleShowing userId toggle for model
191199

192-
--asks
193-
SpecialRequest (FetchAsks userId) → CIA.fetchAsks userId model
194-
SetAsk value → CIA.setAsk value model
195-
SendAsk userId → CIA.sendAsk userId model
196-
AfterSendAsk userId allowed → CIA.afterSendAsk userId allowed model
197-
198200
--suggestion
199201
FetchMoreSuggestionsCIS.fetchMoreSuggestions model
200202
ResumeSuggestingCIS.resumeSuggesting model

src/Client/Im/Posts.purs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,11 @@ toggleShowingSuggestions userId toggle model =
130130
--we need this bookkeeping for big suggestion cards
131131
, suggesting = Just userId
132132
, modal = Special $ ShowSuggestionCard userId
133-
, asks = model.asks { question = Nothing }
134133
, posts = model.posts { freeToFetch = not shouldFetch }
135134
} /\ effects
136135
where
137136
found = DA.find ((_ == userId) <<< _.id) model.suggestions
138-
shouldFetch = toggle == ShowPosts && Just ShowInfo == (_.showing <$> found) && Just 0 == (DA.length <<< _.posts <$> found)
137+
shouldFetch = toggle == ShowPosts && Just ShowPosts /= (_.showing <$> found) && Just 0 == (DA.length <<< _.posts <$> found)
139138

140139
update suggestion
141140
| suggestion.id == userId = suggestion { showing = toggle }
@@ -150,12 +149,11 @@ toggleShowingContacts userId toggle model =
150149
model
151150
{ contacts = map update model.contacts
152151
, posts = model.posts { freeToFetch = not shouldFetch }
153-
, asks = model.asks { question = Nothing }
154152
, fullContactProfileVisible = true
155153
} /\ effects
156154
where
157155
found = _.user <$> DA.find ((_ == userId) <<< _.id <<< _.user) model.contacts
158-
shouldFetch = toggle == ShowPosts && Just ShowInfo == (_.showing <$> found) && Just 0 == (DA.length <<< _.posts <$> found)
156+
shouldFetch = toggle == ShowPosts && Just ShowPosts /= (_.showing <$> found) && Just 0 == (DA.length <<< _.posts <$> found)
159157

160158
update contact
161159
| contact.user.id == userId = contact { user = contact.user { showing = toggle } }

src/Client/Profile/Update.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ afterSendAnswer id model = model { loading = false, asks = map upd model.asks }
8080
sendAnswer Int ProfileModel ProfileModel /\ Array (Aff (Maybe ProfileMessage))
8181
sendAnswer id model = model { loading = DM.isJust answer } /\ effects
8282
where
83-
answer = DA.find ((_ == id) <<< _.id) model.asks >>= _.typedAnswer
83+
answer = DA.find ((_ == id) <<< _.id) model.asks >>= _.typedAnswer
8484

8585
send value = do
8686
void <<< CN.silentResponse $ request.profile.answer { body: { id, answer: value } }
@@ -104,7 +104,7 @@ refreshAsks model = model { mode = Asked } /\ [ fetch ]
104104
case result of
105105
Right (Response response) → pure <<< Just <<< SetPField $ _ { asks = map extend response.body <> model.asks }
106106
_ → pure Nothing
107-
extend ask = (R.merge (ask :: Ask) { typedAnswer : Nothing :: Maybe String}) :: ProfileAsk
107+
extend ask = (R.merge (ask Ask) { typedAnswer: Nothing Maybe String }) ProfileAsk
108108

109109
refreshPosts ProfileModel ProfileModel /\ Array (Aff (Maybe ProfileMessage))
110110
refreshPosts model = model { mode = OwnPosts } /\ [ fetch ]

src/Client/css/im.css

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2805,9 +2805,17 @@ label {
28052805
border-bottom: 1px dotted var(--light-color);
28062806
}
28072807

2808+
.ask-list {
2809+
display: flex;
2810+
flex-direction: column;
2811+
align-self: center;
2812+
width: 500px;
2813+
}
2814+
28082815
.ask-entry {
28092816
margin: 15px 0;
2810-
border-bottom: 1px dotted var(--light-color);
2817+
position: relative;
2818+
width: fit-content;
28112819
}
28122820

28132821
.posts-input-tab {
@@ -2861,6 +2869,40 @@ label {
28612869
padding-bottom: 15px;
28622870
}
28632871

2872+
.ask-question {
2873+
background-color: var(--im-second-background-color);
2874+
border-radius: 5px;
2875+
padding: 15px 20px;
2876+
color: var(--light-color);
2877+
}
2878+
2879+
.ask-question:after {
2880+
content: "";
2881+
position: absolute;
2882+
top: 0px;
2883+
right: 0px;
2884+
left: 99%;
2885+
width: 0px;
2886+
height: 0px;
2887+
border: 8px solid var(--im-second-background-color);
2888+
border-right-color: transparent;
2889+
border-bottom-color: transparent;
2890+
}
2891+
2892+
.ask-question-itself {
2893+
padding-top: 15px;
2894+
}
2895+
2896+
.ask-header {
2897+
margin-top: 15px;
2898+
margin-bottom: 5px;
2899+
}
2900+
2901+
.ask-answer {
2902+
margin-top: 10px;
2903+
margin-bottom: 20px;
2904+
}
2905+
28642906
.asks-form .chat-input,
28652907
.post-card .chat-input {
28662908
width: 93%;

src/Server/Asks/Action.purs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ import Shared.Resource (Media(..), ResourceType(..))
3030
import Shared.Resource as SP
3131
import Shared.ResponseError (ResponseError(..))
3232

33+
presentAsks :: Int -> ServerEffect (Array Ask)
34+
presentAsks userId = SAD.presentAsks userId
35+
3336
refreshAsks Int Maybe Int ServerEffect (Array Ask)
3437
refreshAsks loggedUserId id = SAD.presentAllAsks loggedUserId id
3538

src/Server/Asks/Database.purs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ import Shared.Changelog (ChangelogAction(..))
2323
import Shared.User (ProfileVisibility(..))
2424
import Type.Proxy (Proxy(..))
2525

26-
presentAllAsks :: Int -> Maybe Int -> ServerEffect (Array Ask)
26+
presentAsks Int ServerEffect (Array Ask)
27+
presentAsks userId = SD.query $ select ((a ... _id # as _id) /\ _asker /\ _answer /\ _name /\ _question) # from (join (asks # as a) (users # as u) # on (_asker .=. u ... _id)) # wher (_answerer .=. userId) # orderBy ((_date # desc)) # limit (Proxy _ 10)
28+
29+
presentAllAsks Int Maybe Int ServerEffect (Array Ask)
2730
presentAllAsks loggedUserId = case _ of
28-
Just id -> SD.query $ select ((a ... _id # as _id) /\ _asker /\ _answer /\ _name /\ _question) # from (join (asks # as a) (users # as u) # on (_asker .=. u ... _id)) # wher (_answerer .=. loggedUserId .&&. (a ... _id) .>. id) # orderBy ((_date # desc)) # limit (Proxy :: _ 10)
29-
Nothing -> SD.query $ select ((a ... _id # as _id) /\ _asker /\ _answer /\ _name /\ _question) # from (join (asks # as a) (users # as u) # on (_asker .=. u ... _id)) # wher (_answerer .=. loggedUserId) # orderBy ((_date # desc)) # limit (Proxy :: _ 10)
31+
Just id SD.query $ select ((a ... _id # as _id) /\ _asker /\ _answer /\ _name /\ _question) # from (join (asks # as a) (users # as u) # on (_asker .=. u ... _id)) # wher (_answerer .=. loggedUserId .&&. (a ... _id) .>. id) # orderBy ((_date # desc)) # limit (Proxy _ 10)
32+
Nothing SD.query $ select ((a ... _id # as _id) /\ _asker /\ _answer /\ _name /\ _question) # from (join (asks # as a) (users # as u) # on (_asker .=. u ... _id)) # wher (_answerer .=. loggedUserId) # orderBy ((_date # desc)) # limit (Proxy _ 10)
3033

3134
isAllowedToAsk Int Int ServerEffect Boolean
3235
isAllowedToAsk loggedUserId userId = do

src/Server/Asks/Handler.purs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ import Server.Im.Template as SIT
1616
import Server.Posts.Action as SPA
1717
import Server.Response as SR
1818
import Shared.Account (EmailPassword)
19+
import Shared.Ask (Ask)
1920
import Shared.Changelog (Changelog)
2021
import Shared.DateTime (DateTimeWrapper(..))
2122
import Shared.Html (Html(..))
2223

24+
asks { guards { loggedUserId Int }, query :: { answerer :: Int } } ServerEffect (Array Ask)
25+
asks request = SAA.presentAsks request.query.answerer
26+
2327
post { guards { loggedUserId Int }, body { userId :: Int, question :: String } } ServerEffect { allowed:: Boolean }
2428
post request = do
2529
allowed <- SAA.sendAsk request.guards.loggedUserId request.body.userId request.body.question

src/Server/Handler.purs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ handlers reading =
5656
, seen: runJson reading SPSH.seen
5757
}
5858
, asks:
59-
{ post: runJson reading SAH.post
59+
{ get: runJson reading SAH.asks
60+
, post: runJson reading SAH.post
6061
}
6162
, im:
6263
{ get: runHtml reading SIH.im

src/Shared/Im/Types.purs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import Foreign.Object (Object)
2626
import Foreign.Object as FO
2727
import Payload.Client.QueryParams (class EncodeQueryParam)
2828
import Payload.Server.QueryParams (class DecodeQueryParam, DecodeError(..))
29+
import Shared.Ask (Ask)
2930
import Shared.Changelog (Changelog, ChangelogAction)
3031
import Shared.Content (Content(..))
3132
import Shared.DateTime (DateTimeWrapper)
@@ -333,6 +334,7 @@ data ImMessage
333334
| SetAsk (Maybe String)
334335
| SendAsk Int
335336
| AfterSendAsk Int Boolean
337+
| DisplayAsks Int (Array Ask)
336338

337339
--main
338340
| ReloadPage

0 commit comments

Comments
 (0)