added simple comment theme part

added safe form
added comment submit method
pull/49/head
A1Gard 5 months ago
parent 14cc7005dd
commit 9cd2f7521a

@ -2,7 +2,10 @@
namespace App\Http\Controllers;
use App\Models\Comment;
use App\Models\Customer;
use App\Models\Post;
use App\Models\User;
use Illuminate\Http\Request;
use Spatie\Tags\Tag;
@ -29,4 +32,44 @@ class ClientController extends Controller
$tag = Tag::where('slug->'.config('app.locale'),'like',$slug)->first();
return $tag;
}
public function submitComment(Request $request){
$request->validate([
'commentable_type' => ['required','string','min:5'],
'commentable_id' => ['required','integer'],
'message' => ['required','string','min:5'],
'parent_id' => ['nullable','integer'],
]);
$comment = new Comment();
if (!auth()->check() && !auth('customer')->check()){
$request->validate([
'name' => ['required','string','min:2'],
'email' => ['required','email'],
]);
$comment->name = $request->name;
$comment->email = $request->email;
$comment->status = 0;
}else{
if (auth()->check() ){
$comment->commentator_type = User::class;
$comment->commentator_id = auth()->id();
$comment->status = 1;
}else{
$comment->commentator_type = Customer::class;
$comment->commentator_id = auth('customer')->id();
$comment->status = 0;
}
}
$comment->parent_id = $request->input('parent_id',null);
$comment->body = $request->input('message');
$comment->commentable_type = $request->input('commentable_type');
$comment->commentable_id = $request->input('commentable_id');
$comment->ip = request()->ip();
$comment->save();
return redirect()->back()->with(['message' => __('Your comment has been submitted')]);
}
}

@ -16,7 +16,7 @@ class Comment extends Model
public function children()
{
return $this->hasMany(Comment::class, 'sub_comment_id');
return $this->hasMany(Comment::class, 'parent_id');
}
public function parent()

@ -10,19 +10,23 @@ class Part extends Model
use HasFactory;
public function getBlade(){
$className= ucfirst($this->part);
public function getBlade()
{
$className = ucfirst($this->part);
$handle = "\\Resources\\Views\\Segments\\$className";
$handle::onMount($this);
return 'segments.'.$this->segment.'.'.$this->part.'.'.$this->part;
return 'segments.' . $this->segment . '.' . $this->part . '.' . $this->part;
}
public function getBladeWithData(){
$className= ucfirst($this->part);
public function getBladeWithData($item = null)
{
$className = ucfirst($this->part);
$handle = "\\Resources\\Views\\Segments\\$className";
return ['blade' => 'segments.'.$this->segment.'.'.$this->part.'.'.$this->part, 'data' => $handle::onMount($this)];
return ['blade' => 'segments.' . $this->segment . '.' . $this->part . '.' . $this->part, 'data' => $handle::onMount($this, $item)];
}
public function area(){
public function area()
{
return $this->belongsTo(Area::class);
}
}

@ -40,6 +40,10 @@ return [
'driver' => 'session',
'provider' => 'users',
],
'customer' => [
'driver' => 'session',
'provider' => 'customers',
],
],
/*
@ -65,6 +69,11 @@ return [
'model' => env('AUTH_MODEL', App\Models\User::class),
],
'customers' => [
'driver' => 'eloquent',
'model' => App\Models\Customer::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
@ -97,6 +106,12 @@ return [
'expire' => 60,
'throttle' => 60,
],
'customers' => [
'provider' => 'customers',
'table' => 'password_resets',
'expire' => 999999,
'throttle' => 60,
],
],
/*

@ -0,0 +1,8 @@
window.addEventListener('load',function () {
setTimeout(()=>{
document.querySelectorAll('.safe-from')?.forEach(function (el) {
const url = el.querySelector('.safe-url').getAttribute('data-url');
el.setAttribute('action',url);
})
},1220);
})

@ -6,7 +6,7 @@
@section('content')
<main>
@foreach(getParts($area) as $part)
@php($p = $part->getBladeWithData())
@php($p = $part->getBladeWithData($post))
@include($p['blade'],['data' => $p['data']])
@endforeach
</main>

@ -0,0 +1,65 @@
<section class='SimpleComments'>
<div class="{{gfx()['container']}}">
<h5>
{{__("Comments")}}
</h5>
@foreach($data['comments'] as $comment)
@include('segments.post.SimplePost.inc.comment-detail',$comment)
@endforeach
<h5>
{{__("Post your comment")}}
</h5>
@include('components.err')
<form id="comment-form" class="safe-from" method="post">
<div class="safe-url" data-url="{{route('client.comment.submit')}}"></div>
@csrf
<input type="hidden" name="commentable_type" value="{{$data['commentable_type']}}">
<input type="hidden" name="commentable_id" value="{{$data['commentable_id']}}">
<input type="hidden" name="parent_id" id="parent_id" >
<div class="row">
@if(auth()->check())
<div class="col-12">
<span class="comment-as">
{{auth()->user()->name}}
</span>
</div>
@elseif(auth('customer')->check())
<div class="col-12">
<span class="comment-as">
{{auth('customer')->user()->name}}
</span>
</div>
@else
<div class="col-md-6">
<label for="name">
{{__("Name")}}
</label>
<input type="text" name="name" class="form-control" placeholder="{{__("Name")}}" id="name">
</div>
<div class="col-md-6">
<label for="name">
{{__("Email")}}
</label>
<input type="email" name="email" class="form-control" placeholder="{{__("Email")}}" id="email">
</div>
@endif
<div class="col-12 mt-2">
<label>
{{__("Message")}}
</label>
<textarea name="message" placeholder="{{__("Message...")}}" class="form-control"
rows="3"></textarea>
<div class="text-center">
<button class="btn btn-primary w-25 my-3 ">
<i class="ri-send-plane-2-line"></i>
</button>
</div>
</div>
</div>
</form>
</div>
</section>

@ -0,0 +1,12 @@
document.addEventListener('DOMContentLoaded', function () {
document.querySelectorAll('.comment-reply')?.forEach(function (el) {
el.addEventListener('click', function () {
const id = this.getAttribute('data-id');
document.querySelector('#parent_id').value = id;
document.querySelectorAll('.simple-single-comment')?.forEach(function (el2) {
el2.classList.remove('on-reply');
});
el.closest('.simple-single-comment').classList.add('on-reply')
});
})
});

@ -0,0 +1,10 @@
{
"name": "SimpleComments",
"version": "1.0",
"author": "xStack",
"email": "xshop@xstack.ir",
"license": "GPL-3.0-or-later",
"url": "https:\/\/xstack.ir",
"author_url": "https:\/\/4xmen.ir",
"packages": []
}

@ -0,0 +1,27 @@
<?php
namespace Resources\Views\Segments;
use App\Models\Part;
class SimpleComments
{
public static function onAdd(Part $part = null)
{
}
public static function onRemove(Part $part = null)
{
}
public static function onMount(Part $part = null, $model = null)
{
if ($model == null){
return $part;
}
$part->comments = $model->approvedComments()->whereNull('parent_id')->orderBy('id','desc')->get();
$part->commentable_type = get_class($model);
$part->commentable_id = $model->id;
return $part;
}
}

@ -0,0 +1,50 @@
.SimpleComments {
.simple-single-comment{
margin-bottom: 3px;
padding: 10px 1rem;
p{
padding: 1rem;
margin: 0;
}
.tag{
background: var(--xshop-secondary);
color: var(--xshop-diff);
border-radius: var(--xshop-border-radius);
display: inline-block;
padding: 2px 1rem;
opacity: .2;
transition: 470ms;
}
border: 1px solid rgba($xshop-text,.3);
border-radius: var(--xshop-border-radius);
&:hover{
.tag{
opacity: 1;
}
}
.comment-reply{
margin: -5px -.7rem;
}
}
#comment-form{
border-radius: var(--xshop-border-radius);
margin-top: 1rem;
margin-bottom: 1rem;
background: #ffffff44;
padding: 1rem;
}
.comment-as{
font-weight: 700;
color: var(--xshop-primary);
}
.on-reply{
background: var(--xshop-secondary);
color: var(--xshop-diff);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 137 KiB

@ -35,6 +35,9 @@ document.addEventListener('DOMContentLoaded', function () {
window.addEventListener('scroll', function() {
const container = document.getElementById('CounterGrid');
if (container == null){
return ;
}
if (isElementInViewport(container)) {
if (!isCounterInited){
isCounterInited = true;

@ -1,5 +1,36 @@
<div>
<h1>
comment
</h1>
<div class="simple-single-comment">
<div class="row">
<div class="col-3">
@if($comment->commentator()['url'] == null)
<span class="tag float-end">
{{__("Guest")}}
</span>
{{$comment->commentator()['name']}}
@else
@if($comment->commentator_type == \App\Models\User::class)
<span class="tag float-end">
{{__("Admin")}}
</span>
@else
<span class="tag float-end">
{{__("Customer")}}
</span>
@endif
{{$comment->commentator()['name']}}
@endif
</div>
<div class="col-9">
<button type="button" class="btn btn-primary btn-sm float-end comment-reply" data-id="{{$comment->id}}">
<i class="ri-reply-line"></i>
</button>
<p class="pe-4">
{{$comment->body}}
</p>
</div>
</div>
@if($comment->children->count() > 0)
@foreach($comment->children as $comment)
@include('segments.post.SimplePost.inc.comment-detail',$comment)
@endforeach
@endif
</div>

@ -360,8 +360,20 @@ Route::name('client.')->group(function (){
Route::get('/{post}', [\App\Http\Controllers\ClientController::class,'post'])->name('post');
Route::get('/tag/{post}', [\App\Http\Controllers\ClientController::class,'tag'])->name('tag');
Route::post('/comment/submit', [\App\Http\Controllers\ClientController::class,'submitComment'])->name('comment.submit');
})->middleware([\App\Http\Middleware\VisitorCounter::class]);
// to developer test
Route::get('login/as/{mobile}',function ($mobile){
if (auth()->check() && auth()->user()->hasRole('developer') ){
return \Auth::guard('customer')
->loginUsingId(\App\Models\Customer::where('mobile',$mobile)->first()->id);
} else{
return abort(403);
}
})->name('login.as');
Route::get('test',function (){
// return \Resources\Views\Segments\PreloaderCircle::onAdd();
return getCategoryProductBySetting('index_TreeGridProducts_category');

Loading…
Cancel
Save