-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
299 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
namespace modules\authorinfo; | ||
|
||
use Craft; | ||
use craft\elements\User; | ||
use craft\events\DefineGqlTypeFieldsEvent; | ||
use craft\gql\TypeManager; | ||
use GraphQL\Type\Definition\Type; | ||
use yii\base\Event; | ||
|
||
class Module extends \yii\base\Module | ||
{ | ||
public static $instance; | ||
|
||
/** | ||
* Cache for the admin user to avoid multiple queries | ||
*/ | ||
private ?User $adminUser = null; | ||
|
||
public function init() | ||
{ | ||
parent::init(); | ||
self::$instance = $this; | ||
|
||
Event::on( | ||
TypeManager::class, | ||
TypeManager::EVENT_DEFINE_GQL_TYPE_FIELDS, | ||
[$this, 'handleDefineGqlTypeFields'] | ||
); | ||
} | ||
|
||
/** | ||
* Get the admin user, caching the result | ||
*/ | ||
private function getAdminUser(): ?User | ||
{ | ||
if ($this->adminUser === null) { | ||
$this->adminUser = User::find() | ||
->admin(true) | ||
->status(null) | ||
->orderBy(['elements.id' => SORT_ASC]) | ||
->one(); | ||
|
||
if ($this->adminUser) { | ||
Craft::info( | ||
"Found admin user: ID: {$this->adminUser->id}, Name: {$this->adminUser->fullName}", | ||
__METHOD__ | ||
); | ||
} else { | ||
Craft::warning('No admin user found', __METHOD__); | ||
} | ||
} | ||
|
||
return $this->adminUser; | ||
} | ||
|
||
/** | ||
* Handle the DefineGqlTypeFields event | ||
*/ | ||
public function handleDefineGqlTypeFields(DefineGqlTypeFieldsEvent $event): void | ||
{ | ||
if ($event->typeName !== 'EntryInterface') { | ||
return; | ||
} | ||
|
||
$event->fields['authorId'] = [ | ||
'name' => 'authorId', | ||
'type' => Type::int(), | ||
'description' => 'The entry author\'s ID', | ||
'resolve' => function($source) { | ||
try { | ||
if ($source->getSection()->type === 'single') { | ||
return $this->getAdminUser()?->id; | ||
} | ||
return $source->authorId; | ||
} catch (\Throwable $e) { | ||
Craft::error("Error resolving authorId: {$e->getMessage()}", __METHOD__); | ||
return null; | ||
} | ||
} | ||
]; | ||
|
||
$event->fields['authorName'] = [ | ||
'name' => 'authorName', | ||
'type' => Type::string(), | ||
'description' => 'The entry author\'s name', | ||
'resolve' => function($source) { | ||
try { | ||
if ($source->getSection()->type === 'single') { | ||
return $this->getAdminUser()?->fullName; | ||
} | ||
return $source->getAuthor()?->fullName; | ||
} catch (\Throwable $e) { | ||
Craft::error("Error resolving authorName: {$e->getMessage()}", __METHOD__); | ||
return null; | ||
} | ||
} | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<script setup> | ||
import { useGraphQL } from '@/composables/useGraphQL' | ||
import { usePaginatedData } from '@/composables/usePaginatedData' | ||
import { GUESTBOOK_POSTS_QUERY } from '@/queries/guestbookPosts.mjs' | ||
const props = defineProps({ | ||
previewToken: { | ||
type: String, | ||
default: null | ||
} | ||
}) | ||
const fetchPosts = async (page, perPage) => { | ||
try { | ||
const result = await useGraphQL().query(GUESTBOOK_POSTS_QUERY, { | ||
limit: perPage, | ||
offset: (page - 1) * perPage | ||
}, { | ||
previewToken: props.previewToken | ||
}) | ||
return { | ||
posts: result?.guestbookPostsEntries || [], | ||
total: result?.entryCount || 0 | ||
} | ||
} catch (err) { | ||
console.error('GraphQL Error:', err) | ||
throw err | ||
} | ||
} | ||
const { | ||
currentPage, | ||
data, | ||
totalPages, | ||
error, | ||
loading, | ||
updateCurrentPage, | ||
refresh | ||
} = usePaginatedData(fetchPosts) | ||
defineExpose({ | ||
refresh | ||
}) | ||
</script> | ||
<template> | ||
<div v-if="loading" class="py-4"> | ||
Loading... | ||
</div> | ||
<div v-else-if="error" class="py-4 text-red-600"> | ||
{{ error.message }} | ||
</div> | ||
<div v-else> | ||
<div v-if="data?.posts?.length > 0"> | ||
<ol class="mb-2 divide-y divide-slate-300"> | ||
<li v-for="post in data.posts" :key="post.id"> | ||
<article class="text-xl py-6"> | ||
<div v-html="post.textBlock"></div> | ||
<p class="text-sm mt-1"> | ||
<time :datetime="post.postDate">{{ post.postDate }}</time> | ||
</p> | ||
</article> | ||
</li> | ||
</ol> | ||
<Pagination | ||
v-if="totalPages > 1" | ||
:currentPage="currentPage" | ||
:totalPages="totalPages" | ||
@update:currentPage="updateCurrentPage" | ||
/> | ||
</div> | ||
<p v-else class="text-2xl">No entries yet. Create one using the form.</p> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.