Skip to content

Commit

Permalink
feat: added post deletion, editing, comment deletion and dark mode su…
Browse files Browse the repository at this point in the history
…pport.
  • Loading branch information
tareq1988 committed Mar 29, 2024
1 parent ad28407 commit 5a9687f
Show file tree
Hide file tree
Showing 27 changed files with 510 additions and 143 deletions.
77 changes: 77 additions & 0 deletions app/Helpers/Formatting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace App\Helpers;

class Formatting
{
/**
* Transform the body of the post.
*
* @param string $body
*
* @return string
*/
public static function transformBody($text)
{
$text = self::makeLinks($text);
$text = self::wpautop($text);

return $text;
}

/**
* Makes links clickable in the given text.
*
* @param string $text The text to process.
* @return string The processed text with clickable links.
*/
public static function makeLinks($text)
{
return preg_replace('/(http|https):\/\/[a-zA-Z0-9\.\-\/\?\&\=\_\%\#]+/', '<a href="$0" target="_blank">$0</a>', $text);
}

/**
* Replaces double line breaks with paragraph elements.
*
* @param string $text The text to process.
* @param bool $br Whether to add line breaks.
* @return string The processed text with paragraph elements.
*/
public static function wpautop($text, $br = true)
{
if (trim($text) === '') {
return '';
}

// Just to make things a little easier, pad the end.
$text = $text . "\n";

// Change multiple <br>'s into two line breaks, which will turn into paragraphs.
$text = preg_replace('|<br\s*/?>\s*<br\s*/?>|', "\n\n", $text);

// Add a double line break after hr tags, which are self closing.
$text = preg_replace('!(<hr\s*?/?>)!', "$1\n\n", $text);

// Standardize newline characters to "\n".
$text = str_replace(array( "\r\n", "\r" ), "\n", $text);

// Remove more than two contiguous line breaks.
$text = preg_replace("/\n\n+/", "\n\n", $text);

// Split up the contents into an array of strings, separated by double line breaks.
$paragraphs = preg_split('/\n\s*\n/', $text, -1, PREG_SPLIT_NO_EMPTY);

// Reset $text prior to rebuilding.
$text = '';

// Rebuild the content as a string, wrapping every bit with a <p>.
foreach ($paragraphs as $paragraph) {
$text .= '<p class="mb-3">' . trim($paragraph, "\n") . "</p>\n";
}

// Under certain strange conditions it could create a P of entirely whitespace.
$text = preg_replace('|<p>\s*</p>|', '', $text);

return $text;
}
}
16 changes: 16 additions & 0 deletions app/Http/Controllers/Admin/CommentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Comment;

class CommentController extends Controller
{
public function destroy(Comment $comment)
{
$comment->delete();

return response()->json(['message' => 'Comment deleted']);
}
}
65 changes: 65 additions & 0 deletions app/Http/Controllers/Admin/FeedbackController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Models\Board;
use App\Models\Status;
use App\Models\Comment;
use App\Helpers\Formatting;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Notifications\FeedbackStatusChanged;
Expand Down Expand Up @@ -59,6 +60,9 @@ public function show(Post $post)
$boards = Board::select('id', 'name', 'posts', 'slug')->get();
$statuses = Status::select('id', 'name', 'color')->get();

$post->raw_body = $post->body;
$post->body = Formatting::transformBody($post->body);

return inertia('Admin/Feedbacks/Show', [
'post' => $post,
'boards' => $boards,
Expand All @@ -67,6 +71,60 @@ public function show(Post $post)
]);
}

public function store(Request $request)
{
$rules = [
'title' => 'required',
'body' => 'required',
'status_id' => 'required|exists:statuses,id',
'board_id' => 'required|exists:boards,id',
];

if ($request->has('behalf_id') && $request->behalf_id) {
$rules['behalf_id'] = 'exists:users,id';
}

$request->validate($rules);

$args = [
'title' => strip_tags($request->title),
'body' => strip_tags($request->body),
'status_id' => $request->status_id,
'board_id' => $request->board_id,
];

if ($request->behalf_id) {
$args['created_by'] = $request->behalf_id;
$args['by'] = auth()->id();
}

$post = Post::create($args);

if ($post) {
$post->votes()->create([
'user_id' => $post->created_by,
'board_id' => $post->board_id,
]);
}

return redirect()->route('admin.feedbacks.show', $post)->with('success', 'Post created successfully');
}

public function updateContent(Request $request, Post $post)
{
$request->validate([
'title' => 'required',
'body' => 'required',
]);

$post->update([
'title' => strip_tags($request->title),
'body' => strip_tags($request->body),
]);

return redirect()->route('admin.feedbacks.show', $post)->with('success', 'Content updated successfully.');
}

public function update(Request $request, Post $post)
{
$request->validate([
Expand Down Expand Up @@ -113,6 +171,13 @@ private function notify($post)
});
}

public function destroy(Post $post)
{
$post->delete();

return redirect()->route('admin.feedbacks.index')->with('success', 'Feedback deleted successfully.');
}

public function addVote(Post $post, Request $request)
{
$request->validate([
Expand Down
49 changes: 0 additions & 49 deletions app/Http/Controllers/Admin/PostsController.php

This file was deleted.

4 changes: 2 additions & 2 deletions app/Http/Controllers/Frontend/BoardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public function store(Request $request, Board $board)

try {
$post = Post::create([
'title' => $request->title,
'body' => $request->body,
'title' => strip_tags($request->title),
'body' => strip_tags($request->body),
'board_id' => $board->id,
'status_id' => null,
'created_by' => auth()->user()->id,
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/Frontend/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Frontend;

use App\Helpers\Formatting;
use App\Models\Post;
use App\Models\User;
use App\Models\Comment;
Expand Down Expand Up @@ -29,6 +30,7 @@ public function index(Request $request, Post $post)
if (isset($groupedComments[$parentId])) {
foreach ($groupedComments[$parentId] as $comment) {
$children = $buildCommentTree($comment->id);
$comment->body = Formatting::transformBody($comment->body);
$comment->children = $children;
$result[] = $comment;
}
Expand All @@ -53,7 +55,7 @@ public function store(Request $request, Post $post)
]);

$comment = $post->comments()->create([
'body' => $request->body,
'body' => strip_tags($request->body),
'user_id' => auth()->user()->id,
'parent_id' => $parentId === 0 ? null : $parentId,
]);
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/Frontend/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Http\Controllers\Frontend;

use App\Helpers\Formatting;
use App\Http\Controllers\Admin\FeedbackController;
use App\Models\Post;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
Expand All @@ -15,6 +17,8 @@ public function show(Board $board, $post)
$post = Post::where('slug', $post)->withVote()->firstOrFail();
$post->load('creator');

$post->body = Formatting::transformBody($post->body);

$data = [
'post' => $post,
'board' => $board,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"dependencies": {
"@heroicons/react": "^2.1.1",
"@radix-ui/react-popover": "^1.0.7",
"@wedevs/tail-react": "^0.3.3",
"@wedevs/tail-react": "^0.5.0",
"classnames": "^2.5.1",
"lodash.debounce": "^4.0.8",
"react-color": "^2.19.3"
Expand All @@ -39,4 +39,4 @@
"path": "./node_modules/cz-conventional-changelog"
}
}
}
}
9 changes: 6 additions & 3 deletions resources/js/Components/CommentBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,12 @@ const CommentBox = ({ post, parent, onComment }: Props) => {
};

return (
<form className="border border-gray-300 rounded" onSubmit={onFormSubmit}>
<form
className="border border-gray-300 dark:border-gray-700 rounded"
onSubmit={onFormSubmit}
>
<textarea
className="w-full border-0 text-sm px-3 py-2 mt-1 focus:ring-0"
className="w-full border-0 dark:bg-gray-800 dark:text-gray-300 text-sm px-3 py-2 mt-1 focus:ring-0"
autoComplete="off"
rows={rows}
value={form.body}
Expand All @@ -112,7 +115,7 @@ const CommentBox = ({ post, parent, onComment }: Props) => {
></textarea>

{showButtons && (
<div className="flex border-t border-gray-300 px-3 py-2 justify-between items-center">
<div className="flex border-t border-gray-300 dark:border-gray-700 px-3 py-2 justify-between items-center">
<div className="text-xs text-gray-500">
{parent ? (
<span>&nbsp;</span>
Expand Down
Loading

0 comments on commit 5a9687f

Please sign in to comment.