Client-side: capturing data from "/posts" server-side response?


#1

I have included a field in the server-side response returned when a user creates a new topic. I’d like to update the client-side with this data.

The field is called stripe_txn_balance, and is present in the following response from the POST to /posts:

JSON response
{
      "action": "create_post",
      "post": {
        "id": 30,
        "name": null,
        "username": "chris",
        "avatar_template": "/letter_avatar_proxy/v2/letter/c/958977/{size}.png",
        "created_at": "2018-03-05T13:09:45.534Z",
        "cooked": "\u003cp\u003easdasdasdasdasdasdasdasdad\u003c/p\u003e",
        "post_number": 1,
        "post_type": 1,
        "updated_at": "2018-03-05T13:09:45.534Z",
        "reply_count": 0,
        "reply_to_post_number": null,
        "quote_count": 0,
        "avg_time": null,
        "incoming_link_count": 0,
        "reads": 0,
        "score": 0,
        "yours": true,
        "topic_id": 24,
        "topic_slug": "dasdasd",
        "display_username": null,
        "primary_group_name": null,
        "primary_group_flair_url": null,
        "primary_group_flair_bg_color": null,
        "primary_group_flair_color": null,
        "version": 1,
        "can_edit": true,
        "can_delete": false,
        "can_recover": null,
        "can_wiki": true,
        "user_title": null,
        "actions_summary": [
          {
            "id": 5,
            "hidden": true,
            "can_act": true
          },
          {
            "id": 3,
            "can_act": true
          },
          {
            "id": 4,
            "can_act": true
          },
          {
            "id": 8,
            "can_act": true
          },
          {
            "id": 7,
            "can_act": true
          }
        ],
        "moderator": false,
        "admin": true,
        "staff": true,
        "user_id": 1,
        "draft_sequence": 1,
        "hidden": false,
        "hidden_reason_id": null,
        "trust_level": 4,
        "deleted_at": null,
        "user_deleted": false,
        "edit_reason": null,
        "can_view_edit_history": true,
        "wiki": false,
        "stripe_txn_balance": 0
      },
      "success": true
    }

When I get the response I’ll be doing something like this on the client side:

Discourse.User.current().set("stripe_txn_balance", post.stripe_txn_balance);

What I’d like to know is where’s best to put this code?

I tried doing the following in an initializer but couldn’t find my field in the model:

api.modifyClass('controller:composer', {
    save(force) {
        const promise = this._super(force);
        const model = this;
        if (promise) {
            promise.then(function(value) {
                console.log(model);
            })
        }
    }
});

model.topic evaluates to this, but it doesn’t include my field (and I also couldn’t find my field elsewhere within model:

A nudge in the right direction would be much appreciated.


(Vinoth Kannan) #2

I think message bus is more suitable for your purpose.


#3

Message bus would be good in order to decouple the code.

However, I’m still none the wiser as to how to extract the stripe_txn_balance field from the composer post response from the server?


(Vinoth Kannan) #4

The JSON response is loaded inside result.responseJson. Try like below

promise.then(result => {
  console.log(result.responseJson);
})

#5

Thanks @vinothkannans for your suggestion.

After much fiddling this is the only approach that worked:

api.modifyClass('model:composer', {
    createPost(opts) {
        const result = this._super(opts);
        if (result) {
            result.then(r => {
                if (r.payload && typeof(r.payload.stripe_txn_balance) !== 'undefined') {
                    Discourse.User.current().set("stripe_txn_balance", r.payload.stripe_txn_balance);
                }
            });
        }
        return result;
    }
});