Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
askvortsov1 committed Apr 22, 2021
1 parent 3d131ef commit 9157cef
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 178 deletions.
2 changes: 1 addition & 1 deletion extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
->addInclude(['tags', 'tags.state']),

(new Extend\ApiController(FlarumController\ShowForumController::class))
->addInclude(['tags', 'tags.lastPostedDiscussion', 'tags.parent'])
->addInclude(['tags', 'tags.parent'])
->prepareDataForSerialization(LoadForumTagsRelationship::class),

(new Extend\Settings())
Expand Down
20 changes: 12 additions & 8 deletions js/src/admin/components/TagsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import sortable from 'sortablejs';

import ExtensionPage from 'flarum/components/ExtensionPage';
import Button from 'flarum/components/Button';
import LoadingIndicator from 'flarum/components/LoadingIndicator';
import withAttr from 'flarum/utils/withAttr';

import EditTagModal from './EditTagModal';
Expand Down Expand Up @@ -41,33 +42,36 @@ export default class TagsPage extends ExtensionPage {
// makes mithril completely re-render the component on redraw.
this.forcedRefreshKey = 0;

this.tags = [];
this.loading = true;

app.store.find('tags').then(() => {
let tags = app.store.all('tags').filter(tag => !tag.parent());

this.tags = sortTags(tags);
app.store.find('tags', { include: 'parent,lastPostedDiscussion' }).then(() => {
this.loading = false;

m.redraw();
});
}

content() {
if (this.loading) {
return <LoadingIndicator />;
}

const minPrimaryTags = this.setting('flarum-tags.min_primary_tags', 0);
const maxPrimaryTags = this.setting('flarum-tags.max_primary_tags', 0);

const minSecondaryTags = this.setting('flarum-tags.min_secondary_tags', 0);
const maxSecondaryTags = this.setting('flarum-tags.max_secondary_tags', 0);

view() {
const tags = sortTags(app.store.all('tags').filter(tag => !tag.parent()));

return (
<div className="TagsContent">
<div className="TagsContent-list">
<div className="container" key={this.forcedRefreshKey} oncreate={this.onListOnCreate.bind(this)}><div className="SettingsGroups">
<div className="TagGroup">
<label>{app.translator.trans('flarum-tags.admin.tags.primary_heading')}</label>
<ol className="TagList TagList--primary">
{this.tags
{tags
.filter(tag => tag.position() !== null && !tag.isChild())
.map(tagItem)}
</ol>
Expand All @@ -84,7 +88,7 @@ export default class TagsPage extends ExtensionPage {
<div className="TagGroup TagGroup--secondary">
<label>{app.translator.trans('flarum-tags.admin.tags.secondary_heading')}</label>
<ul className="TagList">
{this.tags
{tags
.filter(tag => tag.position() === null)
.sort((a, b) => a.name().localeCompare(b.name()))
.map(tagItem)}
Expand Down
2 changes: 1 addition & 1 deletion js/src/forum/addTagFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function() {
return this.currentActiveTag;
}

const slug = this.params().tags;
const slug = app.search.params().tags;
let tag = null;

if (slug) {
Expand Down
25 changes: 19 additions & 6 deletions js/src/forum/components/TagsPage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Page from 'flarum/components/Page';
import IndexPage from 'flarum/components/IndexPage';
import Link from 'flarum/components/Link';
import LoadingIndicator from 'flarum/components/LoadingIndicator';
import listItems from 'flarum/helpers/listItems';
import humanTime from 'flarum/helpers/humanTime';

Expand All @@ -12,21 +13,33 @@ export default class TagsPage extends Page {
oninit(vnode) {
super.oninit(vnode);

app.history.push('tags', app.translator.trans('flarum-tags.forum.header.back_to_tags_tooltip'));

this.tags = [];

app.store.find('tags').then(() => {
let tags = app.store.all('tags').filter(tag => !tag.parent());
const preloaded = app.preloadedApiDocument();

if (preloaded) {
this.tags = sortTags(preloaded.filter(tag => !tag.parent()));
return;
}

this.loading = true;

this.tags = sortTags(tags);
app.store.find('tags', { include: 'parent,lastPostedDiscussion' }).then(() => {
this.tags = sortTags(app.store.all('tags').filter(tag => !tag.parent()));

this.loading = false;

m.redraw();
});
// this.tags = sortTags(app.store.all('tags').filter(tag => !tag.parent()));

app.history.push('tags', app.translator.trans('flarum-tags.forum.header.back_to_tags_tooltip'));
}

view() {
if (this.loading) {
return <LoadingIndicator />;
}

const pinned = this.tags.filter(tag => tag.position() !== null);
const cloud = this.tags.filter(tag => tag.position() === null);

Expand Down
4 changes: 3 additions & 1 deletion src/Api/Controller/ListTagsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class ListTagsController extends AbstractListController
* {@inheritdoc}
*/
public $optionalInclude = [
'lastPostedDiscussion'
'children',
'lastPostedDiscussion',
'state'
];

/**
Expand Down
11 changes: 9 additions & 2 deletions src/Api/Controller/ShowTagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@
use Flarum\Api\Controller\AbstractShowController;
use Flarum\Tags\Api\Serializer\TagSerializer;
use Flarum\Tags\Tag;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;

class ShowTagController extends AbstractShowController
{
public $serializer = TagSerializer::class;

public $optionalInclude = ['parent', 'children'];
public $optionalInclude = [
'parent',
'children',
'lastPostedDiscussion',
'state'
];

/**
* @var Tag
*/
Expand All @@ -35,7 +42,7 @@ public function __construct(Tag $tags)
*/
protected function data(ServerRequestInterface $request, Document $document)
{
$slug = array_get($request->getQueryParams(), 'slug');
$slug = Arr::get($request->getQueryParams(), 'slug');
$actor = $request->getAttribute('actor');
$include = $this->extractInclude($request);

Expand Down
33 changes: 19 additions & 14 deletions src/Content/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
use Flarum\Api\Controller\ListDiscussionsController;
use Flarum\Frontend\Document;
use Flarum\Http\RequestUtil;
use Flarum\Tags\Api\Controller\ListTagsController;
use Flarum\Tags\Api\Controller\ShowTagController;
use Flarum\Tags\TagRepository;
use Flarum\User\User;
use Illuminate\Contracts\Translation\Translator;
use Symfony\Contracts\Translation\TranslatorInterface;
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface as Request;
Expand All @@ -40,17 +39,17 @@ class Tag
protected $tags;

/**
* @var Translator
* @var TranslatorInterface
*/
protected $translator;

/**
* @param Client $api
* @param Factory $view
* @param TagRepository $tags
* @param Translator $translator
* @param TranslatorInterface $translator
*/
public function __construct(Client $api, Factory $view, TagRepository $tags, Translator $translator)
public function __construct(Client $api, Factory $view, TagRepository $tags, TranslatorInterface $translator)
{
$this->api = $api;
$this->view = $view;
Expand All @@ -76,16 +75,22 @@ public function __invoke(Document $document, Request $request)
$params = [
'sort' => $sort && isset($sortMap[$sort]) ? $sortMap[$sort] : '',
'filter' => [
'q' => "$q tag:$slug"
'tag' => "$slug"
],
'page' => ['offset' => ($page - 1) * 20, 'limit' => 20],
'include' => 'tags'
'page' => ['offset' => ($page - 1) * 20, 'limit' => 20]
];

$apiDocument = array_merge(
(array) $this->getApiDocument($actor, $params),
(array) $this->getTagsDocument($actor, $slug)
);
$apiDocument = $this->getApiDocument($actor, $params);

$tagsDocument = $this->getTagsDocument($actor, $slug);

foreach ($tagsDocument['data'] as $tag) {
// $apiDocument['data'][] = $tag;
}

foreach ($tagsDocument['included'] as $tag) {
// $apiDocument['included'][] = $tag;
}

$document->title = $tag->name;
if ($tag->description) {
Expand Down Expand Up @@ -123,14 +128,14 @@ private function getSortMap()
*/
private function getApiDocument(User $actor, array $params)
{
return json_decode($this->api->send(ListDiscussionsController::class, $actor, $params)->getBody());
return json_decode($this->api->send(ListDiscussionsController::class, $actor, $params)->getBody(), true);
}

private function getTagsDocument(User $actor, string $slug)
{
return json_decode($this->api->send(ShowTagController::class, $actor, [
'slug' => $slug,
'include' => 'children'
])->getBody());
])->getBody(), true);
}
}
17 changes: 10 additions & 7 deletions src/Content/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Flarum\Tags\Api\Controller\ListTagsController;
use Flarum\Tags\TagRepository;
use Flarum\User\User;
use Illuminate\Contracts\Translation\Translator;
use Symfony\Contracts\Translation\TranslatorInterface;
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface as Request;
Expand All @@ -39,7 +39,7 @@ class Tags
protected $tags;

/**
* @var Translator
* @var TranslatorInterface
*/
protected $translator;

Expand All @@ -57,15 +57,15 @@ class Tags
* @param Client $api
* @param Factory $view
* @param TagRepository $tags
* @param Translator $translator
* @param TranslatorInterface $translator
* @param SettingsRepositoryInterface $settings
* @param UrlGenerator $url
*/
public function __construct(
Client $api,
Factory $view,
TagRepository $tags,
Translator $translator,
TranslatorInterface $translator,
SettingsRepositoryInterface $settings,
UrlGenerator $url
) {
Expand All @@ -79,8 +79,8 @@ public function __construct(

public function __invoke(Document $document, Request $request)
{
$tags = Arr::get($this->getTagsDocument($request->getAttribute('actor')), 'data', []);
$tags = collect($tags);
$apiDocument = $this->getTagsDocument($request->getAttribute('actor'));
$tags = collect(Arr::get($apiDocument, 'data', []));

$childTags = $tags->where('attributes.isChild', true);
$primaryTags = $tags->where('attributes.isChild', false)->where('attributes.position', '!==', null)->sortBy('attributes.position');
Expand All @@ -99,12 +99,15 @@ public function __invoke(Document $document, Request $request)
$document->meta['description'] = $this->translator->trans('flarum-tags.forum.all_tags.meta_description_text');
$document->content = $this->view->make('tags::frontend.content.tags', compact('primaryTags', 'secondaryTags', 'children'));
$document->canonicalUrl = $this->url->to('forum')->base().($defaultRoute === '/tags' ? '' : $request->getUri()->getPath());
$document->payload['apiDocument'] = $apiDocument;

return $document;
}

private function getTagsDocument(User $actor)
{
return json_decode($this->api->send(ListTagsController::class, $actor)->getBody(), true);
return json_decode($this->api->send(ListTagsController::class, $actor, [
'include' => 'parent,children,lastPostedDiscussion'
])->getBody(), true);
}
}
63 changes: 0 additions & 63 deletions src/Listener/AddDiscussionTagsRelationship.php

This file was deleted.

Loading

0 comments on commit 9157cef

Please sign in to comment.