Skip to content

Commit

Permalink
perf: only load user state when showing full user data (#50)
Browse files Browse the repository at this point in the history
* perf: only load user state when showing full user data

* perf: only load user state when showing full user data
  • Loading branch information
SychO9 authored May 20, 2024
1 parent 1ea3327 commit 4be7499
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
29 changes: 28 additions & 1 deletion js/src/forum/components/UserPoliciesStateModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,50 @@ import app from 'flarum/forum/app';
import humanTime from 'flarum/common/helpers/humanTime';
import Modal from 'flarum/common/components/Modal';
import sortByAttribute from '../../common/helpers/sortByAttribute';
import LoadingIndicator from 'flarum/common/components/LoadingIndicator';

/* global m */

export default class UserPoliciesStateModal extends Modal {
user = null;
loading = false;

oninit(vnode) {
super.oninit(vnode);

if (this.attrs.user.fofTermsPoliciesState() === undefined) {
this.loading = true;
app.store.find('users', this.attrs.user.id()).then((user) => {
this.user = user;
this.loading = false;
m.redraw();
});
} else {
this.user = this.attrs.user;
}
}

title() {
return app.translator.trans('fof-terms.forum.state-modal.title', {
username: this.attrs.user.username(),
});
}

content() {
if (this.loading) {
return (
<div className="Modal-body">
<LoadingIndicator />
</div>
);
}

return m(
'.Modal-body',
m(
'ul',
sortByAttribute(app.store.all('fof-terms-policies')).map((policy) => {
const state = this.attrs.user.fofTermsPoliciesState()[policy.id()];
const state = this.user.fofTermsPoliciesState()[policy.id()];

return m('li', [
policy.name() + ': ',
Expand Down
8 changes: 7 additions & 1 deletion src/Extenders/UserPoliciesRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace FoF\Terms\Extenders;

use Flarum\Api\Serializer\BasicUserSerializer;
use Flarum\Api\Serializer\CurrentUserSerializer;
use Flarum\User\User;
use FoF\Terms\Repositories\PolicyRepository;

Expand All @@ -29,8 +30,13 @@ public function __construct(PolicyRepository $policies)

public function __invoke(BasicUserSerializer $serializer, User $user, array $attributes)
{
if ($serializer->getActor()->can('seeFoFTermsPoliciesState', $user)) {
$request = $serializer->getRequest();

if ($request->getAttribute('routeName') === 'users.show' && $serializer->getActor()->can('seeFoFTermsPoliciesState', $user)) {
$attributes['fofTermsPoliciesState'] = $this->policies->state($user);
}

if ($serializer instanceof CurrentUserSerializer) {
$attributes['fofTermsPoliciesHasUpdate'] = $this->policies->hasPoliciesUpdate($user);
$attributes['fofTermsPoliciesMustAccept'] = $this->policies->mustAcceptNewPolicies($user);
}
Expand Down

0 comments on commit 4be7499

Please sign in to comment.