Skip to content

Commit

Permalink
search
Browse files Browse the repository at this point in the history
  • Loading branch information
makssein committed Aug 28, 2023
1 parent 3c5d8ca commit d941f53
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 69 deletions.
3 changes: 1 addition & 2 deletions app/Http/Controllers/Web/Follow/FollowsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;

class FollowsController extends Controller
{
Expand All @@ -14,7 +13,7 @@ public function follow(User $user) {
'status' => true
]);
}

auth()->user()->toggleFollow($user);

return response()->json([
Expand Down
22 changes: 22 additions & 0 deletions app/Http/Controllers/Web/Search/SearchController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Controllers\Web\Search;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;

class SearchController extends Controller
{
public function search(Request $request) {
$query = $request->get('q');

if(!$query || strlen($query) < 1 || strlen($query) > 255) return redirect('/');

$data = User::where('username', 'like', "%$query%")
->orWhere('name', 'like', "%$query%")
->limit(30)->get();

return view('pages/other/search', compact('data'), ['query' => $query]);
}
}
49 changes: 25 additions & 24 deletions public/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,31 +375,32 @@ function parsePostDate(date) {
return formatter.format(date);
}

new Tabs(
[
if(window.location.pathname === '/profile') {
new Tabs(
[
{
id: 'main',
triggerEl: document.querySelector('#main_tab-tab'),
targetEl: document.querySelector('#main_tab')
},
{
id: 'follows',
triggerEl: document.querySelector('#follows_tab-tab'),
targetEl: document.querySelector('#follows_tab')
},
{
id: 'followers',
triggerEl: document.querySelector('#followers_tab-tab'),
targetEl: document.querySelector('#followers_tab')
},
],
{
id: 'main',
triggerEl: document.querySelector('#main_tab-tab'),
targetEl: document.querySelector('#main_tab')
},
{
id: 'follows',
triggerEl: document.querySelector('#follows_tab-tab'),
targetEl: document.querySelector('#follows_tab')
},
{
id: 'followers',
triggerEl: document.querySelector('#followers_tab-tab'),
targetEl: document.querySelector('#followers_tab')
},
],
{
defaultTabId: 'follows',
activeClasses: 'border-b-4 dark:border-cyan-300',
inactiveClasses: '_',
}
);

defaultTabId: 'main',
activeClasses: 'border-b-4 dark:border-cyan-300',
inactiveClasses: '_',
}
);
}

function createToast(data) {
let toasts_block = $("#toasts");
Expand Down
49 changes: 25 additions & 24 deletions resources/assets/js/tabs.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
new Tabs(
[
if(window.location.pathname === '/profile') {
new Tabs(
[
{
id: 'main',
triggerEl: document.querySelector('#main_tab-tab'),
targetEl: document.querySelector('#main_tab')
},
{
id: 'follows',
triggerEl: document.querySelector('#follows_tab-tab'),
targetEl: document.querySelector('#follows_tab')
},
{
id: 'followers',
triggerEl: document.querySelector('#followers_tab-tab'),
targetEl: document.querySelector('#followers_tab')
},
],
{
id: 'main',
triggerEl: document.querySelector('#main_tab-tab'),
targetEl: document.querySelector('#main_tab')
},
{
id: 'follows',
triggerEl: document.querySelector('#follows_tab-tab'),
targetEl: document.querySelector('#follows_tab')
},
{
id: 'followers',
triggerEl: document.querySelector('#followers_tab-tab'),
targetEl: document.querySelector('#followers_tab')
},
],
{
defaultTabId: 'follows',
activeClasses: 'border-b-4 dark:border-cyan-300',
inactiveClasses: '_',
}
);

defaultTabId: 'main',
activeClasses: 'border-b-4 dark:border-cyan-300',
inactiveClasses: '_',
}
);
}
24 changes: 13 additions & 11 deletions resources/views/includes/_follows-list.blade.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<div class="flex items-center justify-between w-full mb-6">
<div class="flex items-center">
@if($user->avatar)
<img alt="avatar" class="w-24 h-24 rounded-full mr-4" src="{{$user->getAvatarLink()}}">
@if($u->avatar)
<img alt="avatar" class="w-24 h-24 rounded-full mr-4" src="{{$u->getAvatarLink()}}">
@else
<img alt="avatar" class="w-24 h-24 rounded-full mr-4" src="{{asset('img/default/default-avatar.svg')}}">
@endif
<div class="flex flex-col items-start justify-center">
<h5 class="font-bold text-xl">{{$user->name}}</h5>
<span class="text-slate-500 text-base">{{"@$user->username"}}</span>
<h5 class="font-bold text-xl">{{$u->name}}</h5>
<span class="text-slate-500 text-base">{{"@$u->username"}}</span>
</div>
</div>
<form data-follow-form action="/profile/{{$user->username}}/follow" method="POST" class="justify-self-end">
@if($isFollowing)
<button type="submit" class="px-4 py-2 font-medium text-sm text-white rounded-full shadow-sm bg-transparent border border-slate-500 hover:bg-slate-800">Отписаться</button>
@else
<button type="submit" class="px-4 py-2 font-medium text-sm text-white rounded-full shadow-sm bg-cyan-600 hover:bg-cyan-700">Подписаться</button>
@endif
</form>
@if(auth()->user()->isNot($u))
<form data-follow-form action="/profile/{{$u->username}}/follow" method="POST" class="justify-self-end">
@if($isFollowing)
<button type="submit" class="px-4 py-2 font-medium text-sm text-white rounded-full shadow-sm bg-transparent border border-slate-500 hover:bg-slate-800">Отписаться</button>
@else
<button type="submit" class="px-4 py-2 font-medium text-sm text-white rounded-full shadow-sm bg-cyan-600 hover:bg-cyan-700">Подписаться</button>
@endif
</form>
@endif
</div>
4 changes: 2 additions & 2 deletions resources/views/includes/_nav-links.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</svg>
</span>
<a class="font-bold text-2xl" href="{{route('/')}}">
Home
Главная
</a>
</li>
<li class="flex items-center mb-7">
Expand Down Expand Up @@ -52,7 +52,7 @@
</svg>
</span>
<a class="font-bold text-2xl" href="{{route('profile.profile', auth()->user()->username)}}">
Profile
Профиль
</a>
</li>
</ul>
Expand Down
4 changes: 2 additions & 2 deletions resources/views/layouts/default.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
@yield('content')
</div>
<div class="text-white w-4/12 pt-4 px-8 col-end-1">
<form class="mb-4">
<form class="mb-4" action="{{route('search')}}" method="GET">
<label for="search" class="mb-2 text-sm font-medium sr-only dark:text-white">Поиск</label>
<div class="relative">
<div class="absolute inset-y-0 left-0 flex items-center pl-5 pointer-events-none">
<svg class="w-4 h-4 text-gray-500 dark:text-gray-400" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 20 20">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m19 19-4-4m0-7A7 7 0 1 1 1 8a7 7 0 0 1 14 0Z"/>
</svg>
</div>
<input type="search" id="search" class="block w-full p-3 pl-12 text-sm border
<input type="search" id="search" name="q" minlength="1" maxlength="255" class="block w-full p-3 pl-12 text-sm border
rounded-full bg-gray-50 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="Поиск" required>
</div>
</form>
Expand Down
8 changes: 4 additions & 4 deletions resources/views/pages/account/profile.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
<div id="follows_tab">
<div class="flex flex-col w-full p-8">
@php $follows = auth()->user()->follows @endphp
@forelse($follows as $user)
@forelse($follows as $u)
@include('includes/_follows-list', ['isFollowing' => true]) <!--Пользователь точно подписан на своих подписчиков, поэтому true -->
@empty
<div class="flex justify-center items-center">
Expand All @@ -206,9 +206,9 @@
<div id="followers_tab">
<div class="flex flex-col w-full p-8">
@php $followers = auth()->user()->followers @endphp
@forelse($followers as $user)
@forelse($followers as $u)
@php
$isFollowing = $follows->contains($user);
$isFollowing = $follows->contains($u);
@endphp
@include('includes/_follows-list', ['isFollowing' => $isFollowing])
@empty
Expand All @@ -222,5 +222,5 @@
@endsection

@section('scripts')
<script>getFeed('/posts/'+ {{$user->id}} +'/get');</script>
<script>getFeed('/posts/{{$user->id}}/get');</script>
@endsection
26 changes: 26 additions & 0 deletions resources/views/pages/other/search.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@extends('layouts/default')

@section('title', 'Поиск')

@section('content')
<div class="flex flex-col items-center pt-4">
<div class="px-8 mb-4 self-start">
<h1 class="text-xl font-bold">Результаты поиска: {{$query}}</h1>
</div>
<div class="flex flex-col w-full p-8">
@php $follows = auth()->user()->follows @endphp
@forelse($data as $u)
@php
$isFollowing = $follows->contains($u);
@endphp
@include('includes/_follows-list' , ['isFollowing' => $isFollowing])
@empty
<p class="self-center text-gray-300">Ничего не найдено</p>
@endforelse
</div>
</div>
@endsection

@section('scripts')

@endsection
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Http\Controllers\Web\Follow\FollowsController;
use App\Http\Controllers\Web\IndexController;
use App\Http\Controllers\Web\Posts\PostsController;
use App\Http\Controllers\Web\Search\SearchController;
use Illuminate\Support\Facades\Route;


Expand Down Expand Up @@ -37,6 +38,8 @@
Route::post('newpassword', [SettingsController::class, 'newPassword'])->name('newpassword');
});

Route::get('/search', [SearchController::class, 'search'])->name('search');

Route::get('/logout', [LoginController::class, 'logout'])->name('logout');
});

Expand Down

0 comments on commit d941f53

Please sign in to comment.