Fork me on GitHub
#fulcro
<
2023-01-19
>
tony.kay00:01:24

Released 3.6.0-RC5. Reverts the attempt to convert to react context, and just leverages a different approach to dyn vars that seems sound.

❤️ 2
Jakub Holý (HolyJak)13:01:38

you mean 3.6.0-RC5, I guess?

Eric Dvorsak10:01:51

Is there a way to make a component query use the current session to get user specific data? eg

{[:user/id the-current-user] [:user/name]}
without creating new resolvers like acccount/name ?

tony.kay17:01:00

Yes. That is the correct query

tony.kay17:01:43

but there’s more to it…someone has to trigger a load to load that user into normalized form…e.g. someplace your code has to actually run a df/load! on that ident/query combo

tony.kay17:01:11

(df/load! app [:user/id id] (rc/nc [:user/id :user/name])) for example

tony.kay17:01:50

but if you actually mean “session” in the cookie sense, then the answer is more speculative.

tony.kay17:01:20

in that case where does the-current-user come from?

Eric Dvorsak17:01:31

yeah I have a query in my root {:root/current-session (comp/get-query session/Session)}

Eric Dvorsak17:01:51

and the current user will be in [:root/current-session :user/id]

tony.kay17:01:07

ok, so if you’re already doing that, and the Session component has normalized the user, then your component query shown will work

tony.kay17:01:55

What is your Session component’s query???

Eric Dvorsak17:01:02

yes that's the path I'm trying to follow

tony.kay17:01:25

is should be more like [{:session/user (comp/get-query User}] , where you have a User component that will normalize the user

Eric Dvorsak17:01:36

(defsc Session
  "Session representation. Used primarily for server queries. On-screen representation happens in Login component."
  [_ _]
  {:query [:user/id
           :user/username
           :session/valid?]
   :ident (fn [] [:component/id :user-session])
   :pre-merge     (fn [{:keys [data-tree]}]
                    (merge {:session/valid? false :user/id nil}
                           data-tree))
   :initial-state {:session/valid? false :user/id nil}})

tony.kay17:01:59

your ident should be :user/id (not a fn)

tony.kay17:01:14

that way the user info gets normalized

tony.kay17:01:50

OR, you can use the link query {[:component/id :user-session] [:user/username]}

tony.kay17:01:07

link queries just point to things in your database, and ident puts them there…

Eric Dvorsak17:01:32

my understanding was that with the fn ident Session would be a singleton

tony.kay17:01:34

so up to you…if you want to do live user-switching, then the “current session” is the value held by :root/current-session…which can be a changing ident pointing to user data, which can be normalized.

tony.kay17:01:24

But the core answer to your question is the link query (ident-based join). You just have to decide how you want to represent that data, and link to it.

Eric Dvorsak17:01:56

I don't really mind if the current user is not normalized, like in your video on sessions https://www.youtube.com/watch?v=oQpmKWBm9HE&amp;list=PLVi9lDx-4C_T7jkihlQflyqGqU4xVtsfi&amp;index=15

tony.kay17:01:59

you can also do {[:root/current-session '_] [:user/username]}

tony.kay17:01:17

I don’t care either 😄

tony.kay17:01:24

It’s your application

tony.kay17:01:54

I’m just telling you how to exactly do your very initial question (literally). OR telling you to modify what you do based on how you actually laid out the data.

tony.kay17:01:15

The answer is “link query”. The details from there depend on your app.

Eric Dvorsak17:01:12

Thanks I'm going to go through the link queries chapter again

tony.kay17:01:53

and remember that queries always auto-denormalize…so even if your session is normalized that last link query works…so it is probably more what you want

tony.kay17:01:00

{[:root/current-session '_] [:user/username]}

Jakub Holý (HolyJak)15:01:41

Hi! In RAD, fo/default-value is only used to display the value in a form, right? But if I do not include the field in form and save it, the property won’t have any value. To have one, I’d need to hook into the save middleware to check for missing properties w/ default values and add them there, I guess. Right?

tony.kay17:01:31

Not exactly. The fo/default-value is used in the UISM of the form, when CREATING a new entity, to pre-fill that value into the form…but the requirement there is that the attribute in question is actually on the form. If you want a value to be auto-filled full-stack, then you would need to augment the save middleware to do so.

tony.kay17:01:31

There are (at least) two concerns when talking about “default values”, and they need to be separate: 1. Convenience: pre-filling a field on a form that is likely (e.g. timezone that you detected from a browser) 2. To ensure that the value exists in the database for schema/consistency purposes (often requires a calculation, and may …e.g. who owns this new entity I’m saving? Answer: the person in my current session that talked to me.) The former is the purpose of fo/default-value, which is why it is a form option and not a database adapter option (or attribute option). The latter is pretty wide-open in terms of what has to happen (how do you calculate the value? Where are your sessions? What are your security rules?), and is left to the save middleware.

tony.kay17:01:37

Of course you can try to leverage fo/default-value in your save middleware if it makes sense to you, but I don’t want to conflate the meanings.

Jakub Holý (HolyJak)18:01:26

That absolutely makes, thanks a lot!