From d267bd991964339df2da2ac764828491c6d98314 Mon Sep 17 00:00:00 2001 From: Wes Copeland Date: Fri, 6 Sep 2024 17:21:44 -0400 Subject: [PATCH] refactor: improve type safety of forum Recent Posts page (#2663) --- .../Controllers/ForumTopicController.php | 9 +++--- .../Data/RecentPostsPagePropsData.php | 18 +++++++++++ app/Community/RouteServiceProvider.php | 4 +-- app/Data/PaginatedData.php | 16 +++------- resources/js/common/hooks/usePageProps.ts | 9 ++++++ resources/js/common/models/index.ts | 1 - .../ForumBreadcrumbs.test.tsx} | 10 +++--- .../ForumBreadcrumbs.tsx} | 11 +++++-- .../components/ForumBreadcrumbs/index.ts | 1 + .../AggregateRecentPostLinks.test.tsx | 2 +- .../AggregateRecentPostLinks.tsx | 4 +-- .../RecentPostsBreadcrumbs/index.ts | 1 - .../RecentPostsCards.test.tsx | 3 +- .../RecentPostsCards/RecentPostsCards.tsx | 31 +++++++++---------- .../RecentPostsMainRoot.tsx | 4 +-- .../RecentPostsPagination.test.tsx | 2 +- .../RecentPostsPagination.tsx | 5 ++- .../RecentPostsTable.test.tsx | 3 +- .../RecentPostsTable/RecentPostsTable.tsx | 25 +++++++-------- resources/js/features/forums/models/index.ts | 2 -- .../forums/models/recent-forum-post.model.ts | 27 ---------------- .../models/recent-posts-page-props.model.ts | 7 ----- .../factories/createPaginatedData.ts} | 21 +++++-------- .../createRecentActiveForumTopic.ts} | 8 ++--- resources/js/test/factories/index.ts | 2 ++ resources/js/types/generated.d.ts | 7 +++-- resources/js/ziggy.d.ts | 2 +- 27 files changed, 108 insertions(+), 127 deletions(-) create mode 100644 app/Community/Data/RecentPostsPagePropsData.php create mode 100644 resources/js/common/hooks/usePageProps.ts rename resources/js/features/forums/components/{RecentPostsMainRoot/RecentPostsBreadcrumbs/RecentPostsBreadcrumbs.test.tsx => ForumBreadcrumbs/ForumBreadcrumbs.test.tsx} (71%) rename resources/js/features/forums/components/{RecentPostsMainRoot/RecentPostsBreadcrumbs/RecentPostsBreadcrumbs.tsx => ForumBreadcrumbs/ForumBreadcrumbs.tsx} (66%) create mode 100644 resources/js/features/forums/components/ForumBreadcrumbs/index.ts delete mode 100644 resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsBreadcrumbs/index.ts delete mode 100644 resources/js/features/forums/models/index.ts delete mode 100644 resources/js/features/forums/models/recent-forum-post.model.ts delete mode 100644 resources/js/features/forums/models/recent-posts-page-props.model.ts rename resources/js/{common/models/paginated-data.model.ts => test/factories/createPaginatedData.ts} (58%) rename resources/js/{features/forums/models/recent-active-forum-topic.model.ts => test/factories/createRecentActiveForumTopic.ts} (68%) diff --git a/app/Community/Controllers/ForumTopicController.php b/app/Community/Controllers/ForumTopicController.php index 18d7d2dc42..75e5ab26c0 100644 --- a/app/Community/Controllers/ForumTopicController.php +++ b/app/Community/Controllers/ForumTopicController.php @@ -4,6 +4,7 @@ namespace App\Community\Controllers; +use App\Community\Data\RecentPostsPagePropsData; use App\Community\Requests\ForumTopicRequest; use App\Data\ForumTopicData; use App\Data\PaginatedData; @@ -120,7 +121,7 @@ private function getTotalRecentForumTopics(int $permissions = Permissions::Unreg ->count(); } - public function recentlyActive(Request $request): InertiaResponse + public function recentPosts(Request $request): InertiaResponse { $offset = $request->input('page', 1) - 1; $count = 25; @@ -160,9 +161,9 @@ public function recentlyActive(Request $request): InertiaResponse $paginatedTopics = PaginatedData::fromLengthAwarePaginator($paginator); - return Inertia::render('forums/recent-posts', [ - 'paginatedTopics' => $paginatedTopics, - ]); + $props = new RecentPostsPagePropsData($paginatedTopics); + + return Inertia::render('forums/recent-posts', $props); } private function getRecentForumTopics(int $page = 1, int $permissions = Permissions::Unregistered): array diff --git a/app/Community/Data/RecentPostsPagePropsData.php b/app/Community/Data/RecentPostsPagePropsData.php new file mode 100644 index 0000000000..d23c9bace4 --- /dev/null +++ b/app/Community/Data/RecentPostsPagePropsData.php @@ -0,0 +1,18 @@ +')] +class RecentPostsPagePropsData extends Data +{ + public function __construct( + public PaginatedData $paginatedTopics + ) { + } +} diff --git a/app/Community/RouteServiceProvider.php b/app/Community/RouteServiceProvider.php index 2532dea046..1558a27d89 100755 --- a/app/Community/RouteServiceProvider.php +++ b/app/Community/RouteServiceProvider.php @@ -39,9 +39,9 @@ protected function mapWebRoutes(): void Route::middleware(['web', 'csp']) ->group(function () { Route::middleware(['inertia'])->group(function () { - Route::get('settings', [UserSettingsController::class, 'show'])->name('settings.show'); + Route::get('forums/recent-posts', [ForumTopicController::class, 'recentPosts'])->name('forum.recent-posts'); - Route::get('forums/recent-posts', [ForumTopicController::class, 'recentlyActive'])->name('forum.recent-posts'); + Route::get('settings', [UserSettingsController::class, 'show'])->name('settings.show'); }); /* diff --git a/app/Data/PaginatedData.php b/app/Data/PaginatedData.php index 3d6577fc0c..9967897264 100644 --- a/app/Data/PaginatedData.php +++ b/app/Data/PaginatedData.php @@ -6,18 +6,11 @@ use Illuminate\Pagination\LengthAwarePaginator; use Spatie\LaravelData\Data; +use Spatie\TypeScriptTransformer\Attributes\LiteralTypeScriptType; use Spatie\TypeScriptTransformer\Attributes\TypeScript; use Spatie\TypeScriptTransformer\Attributes\TypeScriptType; -// The TypeScript transformer is not capable of recognizing that this resource -// should accept a generic. We'll fix the type with an override in the front-end. -// We'll give the output type an "__UNSAFE" prefix as a warning that it shouldn't -// be directly used by developers. - -/** - * @template T - */ -#[TypeScript('__UNSAFE_PaginatedData')] +#[TypeScript('PaginatedData')] class PaginatedData extends Data { public function __construct( @@ -25,6 +18,7 @@ public function __construct( public int $lastPage, public int $perPage, public int $total, + #[LiteralTypeScriptType('TItems[]')] public array $items, #[TypeScriptType([ @@ -38,8 +32,8 @@ public function __construct( } /** - * @param LengthAwarePaginator $paginator - * @return self + * @template TItems + * @param LengthAwarePaginator $paginator */ public static function fromLengthAwarePaginator(LengthAwarePaginator $paginator): self { diff --git a/resources/js/common/hooks/usePageProps.ts b/resources/js/common/hooks/usePageProps.ts new file mode 100644 index 0000000000..ce783a2e5b --- /dev/null +++ b/resources/js/common/hooks/usePageProps.ts @@ -0,0 +1,9 @@ +import { usePage } from '@inertiajs/react'; + +import type { AppGlobalProps } from '@/common/models'; + +export function usePageProps() { + const { props } = usePage(); + + return props; +} diff --git a/resources/js/common/models/index.ts b/resources/js/common/models/index.ts index cc83033849..c7f7cd18f1 100644 --- a/resources/js/common/models/index.ts +++ b/resources/js/common/models/index.ts @@ -2,4 +2,3 @@ export * from './app-global-props.model'; export * from './app-page.model'; export * from './avatar-size.model'; export * from './laravel-validation-error.model'; -export * from './paginated-data.model'; diff --git a/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsBreadcrumbs/RecentPostsBreadcrumbs.test.tsx b/resources/js/features/forums/components/ForumBreadcrumbs/ForumBreadcrumbs.test.tsx similarity index 71% rename from resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsBreadcrumbs/RecentPostsBreadcrumbs.test.tsx rename to resources/js/features/forums/components/ForumBreadcrumbs/ForumBreadcrumbs.test.tsx index ad18ed5190..1a47fa01a2 100644 --- a/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsBreadcrumbs/RecentPostsBreadcrumbs.test.tsx +++ b/resources/js/features/forums/components/ForumBreadcrumbs/ForumBreadcrumbs.test.tsx @@ -1,11 +1,11 @@ import { render, screen } from '@/test'; -import { RecentPostsBreadcrumbs } from './RecentPostsBreadcrumbs'; +import { ForumBreadcrumbs } from './ForumBreadcrumbs'; -describe('Component: RecentPostsBreadcrumbs', () => { +describe('Component: ForumBreadcrumbs', () => { it('renders without crashing', () => { // ARRANGE - const { container } = render(); + const { container } = render(); // ASSERT expect(container).toBeTruthy(); @@ -13,7 +13,7 @@ describe('Component: RecentPostsBreadcrumbs', () => { it('has a link back to the forum index', () => { // ARRANGE - render(); + render(); // ASSERT const forumIndexLinkEl = screen.getByRole('link', { name: /forum index/i }); @@ -23,7 +23,7 @@ describe('Component: RecentPostsBreadcrumbs', () => { it('communicates the active link in an accessible manner', () => { // ARRANGE - render(); + render(); // ASSERT const activeLinkEl = screen.getByRole('link', { name: /recent posts/i }); diff --git a/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsBreadcrumbs/RecentPostsBreadcrumbs.tsx b/resources/js/features/forums/components/ForumBreadcrumbs/ForumBreadcrumbs.tsx similarity index 66% rename from resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsBreadcrumbs/RecentPostsBreadcrumbs.tsx rename to resources/js/features/forums/components/ForumBreadcrumbs/ForumBreadcrumbs.tsx index 276a1485db..3483b7efc7 100644 --- a/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsBreadcrumbs/RecentPostsBreadcrumbs.tsx +++ b/resources/js/features/forums/components/ForumBreadcrumbs/ForumBreadcrumbs.tsx @@ -9,9 +9,14 @@ import { BaseBreadcrumbSeparator, } from '@/common/components/+vendor/BaseBreadcrumb'; -export const RecentPostsBreadcrumbs: FC = () => { +// TODO support ForumCategory and Forum +interface ForumBreadcrumbsProps { + currentPageLabel: string; +} + +export const ForumBreadcrumbs: FC = ({ currentPageLabel }) => { return ( -
+
@@ -21,7 +26,7 @@ export const RecentPostsBreadcrumbs: FC = () => { - Recent Posts + {currentPageLabel} diff --git a/resources/js/features/forums/components/ForumBreadcrumbs/index.ts b/resources/js/features/forums/components/ForumBreadcrumbs/index.ts new file mode 100644 index 0000000000..0d7323726e --- /dev/null +++ b/resources/js/features/forums/components/ForumBreadcrumbs/index.ts @@ -0,0 +1 @@ +export * from './ForumBreadcrumbs'; diff --git a/resources/js/features/forums/components/RecentPostsMainRoot/AggregateRecentPostLinks/AggregateRecentPostLinks.test.tsx b/resources/js/features/forums/components/RecentPostsMainRoot/AggregateRecentPostLinks/AggregateRecentPostLinks.test.tsx index 41f06ca6c0..a958f80cb6 100644 --- a/resources/js/features/forums/components/RecentPostsMainRoot/AggregateRecentPostLinks/AggregateRecentPostLinks.test.tsx +++ b/resources/js/features/forums/components/RecentPostsMainRoot/AggregateRecentPostLinks/AggregateRecentPostLinks.test.tsx @@ -1,5 +1,5 @@ -import { createRecentActiveForumTopic } from '@/features/forums/models'; import { render, screen } from '@/test'; +import { createRecentActiveForumTopic } from '@/test/factories'; import { AggregateRecentPostLinks } from './AggregateRecentPostLinks'; diff --git a/resources/js/features/forums/components/RecentPostsMainRoot/AggregateRecentPostLinks/AggregateRecentPostLinks.tsx b/resources/js/features/forums/components/RecentPostsMainRoot/AggregateRecentPostLinks/AggregateRecentPostLinks.tsx index f960e4930b..2b1998fb87 100644 --- a/resources/js/features/forums/components/RecentPostsMainRoot/AggregateRecentPostLinks/AggregateRecentPostLinks.tsx +++ b/resources/js/features/forums/components/RecentPostsMainRoot/AggregateRecentPostLinks/AggregateRecentPostLinks.tsx @@ -1,9 +1,7 @@ import type { FC } from 'react'; -import type { RecentActiveForumTopic } from '@/features/forums/models'; - interface AggregateRecentPostLinksProps { - topic: RecentActiveForumTopic; + topic: App.Data.ForumTopic; } export const AggregateRecentPostLinks: FC = ({ topic }) => { diff --git a/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsBreadcrumbs/index.ts b/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsBreadcrumbs/index.ts deleted file mode 100644 index 5e5a8e6445..0000000000 --- a/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsBreadcrumbs/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './RecentPostsBreadcrumbs'; diff --git a/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsCards/RecentPostsCards.test.tsx b/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsCards/RecentPostsCards.test.tsx index a286e33a64..a300fcb023 100644 --- a/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsCards/RecentPostsCards.test.tsx +++ b/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsCards/RecentPostsCards.test.tsx @@ -1,6 +1,5 @@ -import { createPaginatedData } from '@/common/models'; -import { createRecentActiveForumTopic } from '@/features/forums/models'; import { render, screen } from '@/test'; +import { createPaginatedData, createRecentActiveForumTopic } from '@/test/factories'; import { RecentPostsCards } from './RecentPostsCards'; diff --git a/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsCards/RecentPostsCards.tsx b/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsCards/RecentPostsCards.tsx index 378540d4fe..b6292f0c78 100644 --- a/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsCards/RecentPostsCards.tsx +++ b/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsCards/RecentPostsCards.tsx @@ -1,31 +1,30 @@ -import { usePage } from '@inertiajs/react'; import type { FC } from 'react'; import { UserAvatar } from '@/common/components/UserAvatar'; -import type { RecentPostsPageProps } from '@/features/forums/models'; +import { usePageProps } from '@/common/hooks/usePageProps'; import { AggregateRecentPostLinks } from '../AggregateRecentPostLinks'; import { PostTimestamp } from '../PostTimestamp'; export const RecentPostsCards: FC = () => { - const { props } = usePage(); - - const { auth, paginatedTopics } = props; + const { auth, paginatedTopics } = usePageProps(); return (
{paginatedTopics.items.map((topic) => ( -
+
- - - - - + + + {topic.latestComment?.createdAt ? ( + + + + ) : null}
@@ -35,13 +34,13 @@ export const RecentPostsCards: FC = () => {

in{' '} {topic.title}

-

{topic.latestComment.body}

+

{topic.latestComment?.body}

))} diff --git a/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsMainRoot.tsx b/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsMainRoot.tsx index c666ed9548..ebf1ae32b0 100644 --- a/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsMainRoot.tsx +++ b/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsMainRoot.tsx @@ -1,6 +1,6 @@ import type { FC } from 'react'; -import { RecentPostsBreadcrumbs } from './RecentPostsBreadcrumbs'; +import { ForumBreadcrumbs } from '../ForumBreadcrumbs'; import { RecentPostsCards } from './RecentPostsCards'; import { RecentPostsPagination } from './RecentPostsPagination'; import { RecentPostsTable } from './RecentPostsTable'; @@ -8,7 +8,7 @@ import { RecentPostsTable } from './RecentPostsTable'; export const RecentPostsMainRoot: FC = () => { return (
- +

Recent Posts

diff --git a/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsPagination/RecentPostsPagination.test.tsx b/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsPagination/RecentPostsPagination.test.tsx index 0030651ba8..ca5fac5eae 100644 --- a/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsPagination/RecentPostsPagination.test.tsx +++ b/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsPagination/RecentPostsPagination.test.tsx @@ -1,7 +1,7 @@ import { faker } from '@faker-js/faker'; -import { createPaginatedData } from '@/common/models'; import { render, screen } from '@/test'; +import { createPaginatedData } from '@/test/factories'; import { RecentPostsPagination } from './RecentPostsPagination'; diff --git a/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsPagination/RecentPostsPagination.tsx b/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsPagination/RecentPostsPagination.tsx index d7b341dbc7..9b8eba2191 100644 --- a/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsPagination/RecentPostsPagination.tsx +++ b/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsPagination/RecentPostsPagination.tsx @@ -1,4 +1,3 @@ -import { usePage } from '@inertiajs/react'; import type { FC } from 'react'; import { @@ -8,10 +7,10 @@ import { BasePaginationNext, BasePaginationPrevious, } from '@/common/components/+vendor/BasePagination'; -import type { RecentPostsPageProps } from '@/features/forums/models'; +import { usePageProps } from '@/common/hooks/usePageProps'; export const RecentPostsPagination: FC = () => { - const { paginatedTopics } = usePage().props; + const { paginatedTopics } = usePageProps(); const { perPage, diff --git a/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsTable/RecentPostsTable.test.tsx b/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsTable/RecentPostsTable.test.tsx index 68ccea9fb3..52f3631572 100644 --- a/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsTable/RecentPostsTable.test.tsx +++ b/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsTable/RecentPostsTable.test.tsx @@ -1,6 +1,5 @@ -import { createPaginatedData } from '@/common/models'; -import { createRecentActiveForumTopic } from '@/features/forums/models'; import { render, screen } from '@/test'; +import { createPaginatedData, createRecentActiveForumTopic } from '@/test/factories'; import { RecentPostsTable } from './RecentPostsTable'; diff --git a/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsTable/RecentPostsTable.tsx b/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsTable/RecentPostsTable.tsx index eb039a3ed9..3144220707 100644 --- a/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsTable/RecentPostsTable.tsx +++ b/resources/js/features/forums/components/RecentPostsMainRoot/RecentPostsTable/RecentPostsTable.tsx @@ -1,16 +1,13 @@ -import { usePage } from '@inertiajs/react'; import type { FC } from 'react'; import { UserAvatar } from '@/common/components/UserAvatar'; -import type { RecentPostsPageProps } from '@/features/forums/models'; +import { usePageProps } from '@/common/hooks/usePageProps'; import { AggregateRecentPostLinks } from '../AggregateRecentPostLinks'; import { PostTimestamp } from '../PostTimestamp'; export const RecentPostsTable: FC = () => { - const { props } = usePage(); - - const { auth, paginatedTopics } = props; + const { auth, paginatedTopics } = usePageProps(); return ( @@ -24,28 +21,30 @@ export const RecentPostsTable: FC = () => { {paginatedTopics.items.map((topic) => ( - + diff --git a/resources/js/features/forums/models/index.ts b/resources/js/features/forums/models/index.ts deleted file mode 100644 index 1e7f849f83..0000000000 --- a/resources/js/features/forums/models/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './recent-active-forum-topic.model'; -export * from './recent-posts-page-props.model'; diff --git a/resources/js/features/forums/models/recent-forum-post.model.ts b/resources/js/features/forums/models/recent-forum-post.model.ts deleted file mode 100644 index 09b84e7d0c..0000000000 --- a/resources/js/features/forums/models/recent-forum-post.model.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { createFactory } from '@/test/createFactory'; - -export interface RecentForumPost { - authorDisplayName: string; - commentCountDay: number | null; - commentCountWeek: number | null; - commentId: number; - commentIdDay: number | null; - commentIdWeek: number | null; - forumTopicId: number; - forumTopicTitle: string; - postedAt: string; - shortMessage: string; -} - -export const createRecentForumPost = createFactory((faker) => ({ - forumTopicId: faker.number.int({ min: 1, max: 30000 }), - forumTopicTitle: faker.word.words(5), - commentId: faker.number.int({ min: 1, max: 30000 }), - postedAt: faker.date.recent().toISOString(), - authorDisplayName: faker.internet.displayName(), - shortMessage: faker.word.words(20), - commentIdDay: faker.number.int({ min: 1, max: 30000 }), - commentCountDay: faker.number.int({ min: 0, max: 5 }), - commentIdWeek: faker.number.int({ min: 1, max: 30000 }), - commentCountWeek: faker.number.int({ min: 0, max: 5 }), -})); diff --git a/resources/js/features/forums/models/recent-posts-page-props.model.ts b/resources/js/features/forums/models/recent-posts-page-props.model.ts deleted file mode 100644 index 6e36b56511..0000000000 --- a/resources/js/features/forums/models/recent-posts-page-props.model.ts +++ /dev/null @@ -1,7 +0,0 @@ -import type { AppGlobalProps, PaginatedData } from '@/common/models'; - -import type { RecentActiveForumTopic } from './recent-active-forum-topic.model'; - -export interface RecentPostsPageProps extends AppGlobalProps { - paginatedTopics: PaginatedData; -} diff --git a/resources/js/common/models/paginated-data.model.ts b/resources/js/test/factories/createPaginatedData.ts similarity index 58% rename from resources/js/common/models/paginated-data.model.ts rename to resources/js/test/factories/createPaginatedData.ts index c5450084cb..901f7f5325 100644 --- a/resources/js/common/models/paginated-data.model.ts +++ b/resources/js/test/factories/createPaginatedData.ts @@ -1,16 +1,9 @@ -// `__UNSAFE_PaginatedData` leaves the `items` array untyped. -// We'll use this wrapper to enforce strong types. - -export interface PaginatedData extends Omit { - items: T[]; -} - /** * Creates a PaginatedData object with the given items. * - * @template T - The type of the items in the paginated data. - * @param {T[]} items - An array of items to include in the paginated data. - * @returns {PaginatedData} The paginated data object containing the provided items. + * @template TItems - The type of the items in the paginated data. + * @param {TItems[]} items - An array of items to include in the paginated data. + * @returns {App.Data.PaginatedData} The paginated data object containing the provided items. * * @example * // Create an array of User items @@ -32,10 +25,10 @@ export interface PaginatedData extends Omit( - items: T[], - overrides?: Partial>, -): PaginatedData => { +export const createPaginatedData = ( + items: TItems[], + overrides?: Partial>, +): App.Data.PaginatedData => { return { currentPage: 1, items, diff --git a/resources/js/features/forums/models/recent-active-forum-topic.model.ts b/resources/js/test/factories/createRecentActiveForumTopic.ts similarity index 68% rename from resources/js/features/forums/models/recent-active-forum-topic.model.ts rename to resources/js/test/factories/createRecentActiveForumTopic.ts index e2a61a892f..04d91e885b 100644 --- a/resources/js/features/forums/models/recent-active-forum-topic.model.ts +++ b/resources/js/test/factories/createRecentActiveForumTopic.ts @@ -1,10 +1,10 @@ import type { SetRequired } from 'type-fest'; -import { createFactory } from '@/test/createFactory'; -import { createUser } from '@/test/factories'; -import { createForumTopicComment } from '@/test/factories/createForumTopicComment'; +import { createFactory } from '../createFactory'; +import { createForumTopicComment } from './createForumTopicComment'; +import { createUser } from './createUser'; -export type RecentActiveForumTopic = SetRequired; +type RecentActiveForumTopic = SetRequired; export const createRecentActiveForumTopic = createFactory((faker) => ({ commentCount24h: faker.number.int({ min: 0, max: 3 }), diff --git a/resources/js/test/factories/index.ts b/resources/js/test/factories/index.ts index d6052d526b..8d1413af15 100644 --- a/resources/js/test/factories/index.ts +++ b/resources/js/test/factories/index.ts @@ -2,6 +2,8 @@ export * from './createForumTopicComment'; export * from './createGame'; export * from './createGameHash'; export * from './createGameHashLabel'; +export * from './createPaginatedData'; export * from './createPlayerResettableGame'; +export * from './createRecentActiveForumTopic'; export * from './createSystem'; export * from './createUser'; diff --git a/resources/js/types/generated.d.ts b/resources/js/types/generated.d.ts index 8aa48fb1c2..97bb5dd84a 100644 --- a/resources/js/types/generated.d.ts +++ b/resources/js/types/generated.d.ts @@ -1,4 +1,7 @@ declare namespace App.Community.Data { + export type RecentPostsPageProps = { + paginatedTopics: App.Data.PaginatedData; + }; export type UserSettingsPageProps = { userSettings: App.Data.User; can: App.Data.UserPermissions; @@ -25,12 +28,12 @@ declare namespace App.Data { oldestComment7dId?: number; user: App.Data.User | null; }; - export type __UNSAFE_PaginatedData = { + export type PaginatedData = { currentPage: number; lastPage: number; perPage: number; total: number; - items: Array; + items: TItems[]; links: { firstPageUrl: string | null; lastPageUrl: string | null; diff --git a/resources/js/ziggy.d.ts b/resources/js/ziggy.d.ts index 9845d0868b..0e3ab825a3 100644 --- a/resources/js/ziggy.d.ts +++ b/resources/js/ziggy.d.ts @@ -299,8 +299,8 @@ declare module 'ziggy-js' { "binding": "ID" } ], - "settings.show": [], "forum.recent-posts": [], + "settings.show": [], "user.comment.destroyAll": [ { "name": "user",
- +

{topic.title} - + {topic.latestComment?.createdAt ? ( + + ) : null}

-

{topic.latestComment.body}

+

{topic.latestComment?.body}