Skip to content

Commit

Permalink
feat: view a post (monicahq/chandler#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Oct 10, 2022
1 parent 8fd18fc commit fb4da2a
Show file tree
Hide file tree
Showing 20 changed files with 420 additions and 18 deletions.
15 changes: 15 additions & 0 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -64,4 +65,18 @@ public function postSections(): HasMany
{
return $this->hasMany(PostSection::class);
}

protected function title(): Attribute
{
return Attribute::make(
get: function ($value) {
if ($value) {
return $value;
}

return trans('app.undefined');
},
set: fn ($value) => $value,
);
}
}
25 changes: 23 additions & 2 deletions domains/Vault/ManageJournals/Web/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
use App\Models\PostTemplate;
use App\Models\Vault;
use App\Vault\ManageJournals\Services\CreatePost;
use App\Vault\ManageJournals\Services\DestroyPost;
use App\Vault\ManageJournals\Services\UpdatePost;
use App\Vault\ManageJournals\Web\ViewHelpers\PostCreateViewHelper;
use App\Vault\ManageJournals\Web\ViewHelpers\PostEditViewHelper;
use App\Vault\ManageJournals\Web\ViewHelpers\PostShowViewHelper;
use App\Vault\ManageVault\Web\ViewHelpers\VaultIndexViewHelper;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Inertia\Inertia;
use Redirect;

class PostController extends Controller
{
Expand Down Expand Up @@ -78,11 +81,11 @@ public function store(Request $request, int $vaultId, int $journalId, int $templ
public function show(Request $request, int $vaultId, int $journalId, int $postId)
{
$vault = Vault::findOrFail($vaultId);
$journal = Journal::findOrFail($journalId);
$post = Post::findOrFail($postId);

return Inertia::render('Vault/Journal/Post/Create', [
return Inertia::render('Vault/Journal/Post/Show', [
'layoutData' => VaultIndexViewHelper::layoutData($vault),
'data' => PostShowViewHelper::data($post, Auth::user()),
]);
}

Expand Down Expand Up @@ -117,4 +120,22 @@ public function update(Request $request, int $vaultId, int $journalId, int $post
'data' => PostHelper::statistics($post),
], 200);
}

public function destroy(Request $request, int $vaultId, int $journalId, int $postId)
{
(new DestroyPost())->execute([
'account_id' => Auth::user()->account_id,
'author_id' => Auth::user()->id,
'vault_id' => $vaultId,
'journal_id' => $journalId,
'post_id' => $postId,
]);

$journal = Journal::findOrFail($journalId);

return Redirect::route('journal.show', [
'vault' => $vaultId,
'journal' => $journal,
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public static function data(Journal $journal, User $user): array
'id' => $post->id,
'title' => $post->title,
'written_at' => DateHelper::format($post->written_at, $user),
'url' => [
'show' => route('post.show', [
'vault' => $journal->vault_id,
'journal' => $journal->id,
'post' => $post->id,
]),
],
]);

return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,20 @@ public static function data(Journal $journal, Post $post): array
'journal' => $journal->id,
'post' => $post->id,
]),
'show' => route('post.show', [
'vault' => $journal->vault_id,
'journal' => $journal->id,
'post' => $post->id,
]),
'back' => route('journal.show', [
'vault' => $journal->vault_id,
'journal' => $journal->id,
]),
'destroy' => route('post.destroy', [
'vault' => $journal->vault_id,
'journal' => $journal->id,
'post' => $post->id,
]),
],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,52 @@

namespace App\Vault\ManageJournals\Web\ViewHelpers;

use App\Models\Account;
use App\Helpers\DateHelper;
use App\Models\Post;
use App\Models\PostSection;
use App\Models\User;

class PostShowViewHelper
{
public static function chooseTemplate(Account $account)
public static function data(Post $post, User $user): array
{
$sections = $post->postSections()
->orderBy('position')
->whereNotNull('content')
->get()
->map(fn (PostSection $section) => [
'id' => $section->id,
'label' => $section->label,
'content' => $section->content,
]);

return [
'id' => $post->id,
'title' => $post->title,
'title_exists' => $post->title === trans('app.undefined') ? false : true,
'written_at' => DateHelper::format($post->written_at, $user),
'published' => $post->published,
'sections' => $sections,
'journal' => [
'name' => $post->journal->name,
'url' => [
'show' => route('journal.show', [
'vault' => $post->journal->vault_id,
'journal' => $post->journal->id,
]),
],
],
'url' => [
'edit' => route('post.edit', [
'vault' => $post->journal->vault_id,
'journal' => $post->journal->id,
'post' => $post->id,
]),
'back' => route('journal.show', [
'vault' => $post->journal->vault_id,
'journal' => $post->journal->id,
]),
],
];
}
}
2 changes: 1 addition & 1 deletion lang/en/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@
'view_all' => 'View all',
'previous' => 'Previous',
'next' => 'Next',
'view_all' => 'View all',
'view_map' => 'View on map',
'view_older' => 'Load previous entries',
'download' => 'Download',
'undefined' => 'Undefined',

'unknown_name' => 'Unknown name',

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,8 @@
"hooks": {
"pre-commit": "lint-staged"
}
},
"dependencies": {
"@tailwindcss/typography": "^0.5.7"
}
}
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ parameters:
- '#Access to an undefined property App\\Models\\LifeEventCategory.*::\$label\.#'
- '#Access to an undefined property App\\Models\\LifeEventType.*::\$label.#'
- '#Access to an undefined property App\\Models\\Module::\$position\.#'
- '#Access to an undefined property App\\Models\\Post::\$title\.#'
- '#Parameter \$get of static method Illuminate\\Database\\Eloquent\\Casts\\Attribute<mixed,mixed>::make\(\) expects \(callable\(mixed, mixed=\): .*\)\|null, Closure\(mixed, mixed\): .* given\.#'
- '#Access to an undefined property App\\Models\\.*::\$pivot\.#'

Expand Down
4 changes: 2 additions & 2 deletions resources/js/Pages/Vault/Dashboard/Partials/DueTasks.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</h3>

<!-- list of tasks -->
<div v-if="data.tasks.length > 0">
<div v-if="data.tasks">
<ul class="mb-4 rounded-lg border border-gray-200 dark:border-gray-800 dark:bg-gray-900">
<li
v-for="task in data.tasks"
Expand Down Expand Up @@ -80,7 +80,7 @@

<!-- blank state -->
<div
v-if="data.tasks.length == 0"
v-if="!data.tasks"
class="mb-6 rounded-lg border border-gray-200 bg-white dark:border-gray-800 dark:bg-gray-900">
<p class="p-5 text-center">
{{ $t('vault.dashboard_due_tasks_blank') }}
Expand Down
16 changes: 12 additions & 4 deletions resources/js/Pages/Vault/Journal/Post/Edit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ const update = () => {
})
.catch(() => {});
};
const destroy = () => {
if (confirm('Are you sure you want to delete this post?')) {
form.delete(props.data.url.destroy, {
onFinish: () => {},
});
}
};
</script>

<template>
Expand Down Expand Up @@ -119,7 +127,7 @@ const update = () => {
<div class="special-grid grid grid-cols-1 gap-6 sm:grid-cols-3">
<!-- left -->
<div class="">
<form class="bg-form mb-6 rounded-lg border border-gray-200 dark:border-gray-700 dark:bg-gray-900">
<div class="bg-form mb-6 rounded-lg border border-gray-200 dark:border-gray-700 dark:bg-gray-900">
<div class="border-gray-200 p-5 dark:border-gray-700">
<!-- title -->
<text-input
Expand All @@ -143,7 +151,7 @@ const update = () => {
:textarea-class="'block w-full mb-8'" />
</div>
</div>
</form>
</div>
</div>

<!-- right -->
Expand All @@ -153,7 +161,7 @@ const update = () => {
<div class="border-b border-gray-200 p-2 text-sm dark:border-gray-700">Post status: draft</div>

<div class="bg-form rounded-b-lg p-5">
<pretty-link :classes="'mr-8'" :text="'Close'" :icon="'exit'" />
<pretty-link :href="data.url.show" :classes="'mr-8'" :text="'Close'" :icon="'exit'" />

<pretty-button
@click="update()"
Expand Down Expand Up @@ -263,7 +271,7 @@ const update = () => {
</ul>

<!-- delete -->
<div class="cursor-pointer text-red-500 hover:text-red-900">{{ $t('app.delete') }}</div>
<div @click="destroy" class="cursor-pointer text-red-500 hover:text-red-900">{{ $t('app.delete') }}</div>
</div>
</div>
</div>
Expand Down
Loading

0 comments on commit fb4da2a

Please sign in to comment.