Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter trade area by country #176

Merged
merged 6 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/Http/Controllers/TradeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ public function index(TradingUsersQuery $query)
/** @var User $loggedUser */
$loggedUser = auth()->user();
$users = $query->fetchAll($loggedUser);
$country = request('country');

return view('trade.index', compact('users'));
return view('trade.index', compact('users', 'country'));
}

public function senders(int $elephpantId, TradingUsersQuery $query)
Expand Down
2 changes: 1 addition & 1 deletion app/Providers/ViewServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function boot()
$view->with('countries', $countries);
});

View::composer(['ranking.index', 'trade._user', 'herd.show'], function ($view) {
View::composer(['ranking.index', 'trade._user', 'herd.show', 'trade.index'], function ($view) {
$usersQuery = (new UsersCountryQuery)->fetchAll();
$countries = (new CountriesQuery())->fetchAll()->filter(function ($country) use ($usersQuery) {
return in_array($country['cca3'], $usersQuery->unique('country_code')->pluck('country_code')->toArray());
Expand Down
4 changes: 3 additions & 1 deletion app/Queries/TradingUsersQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ private function fetchTraders(Collection $userElephpants, TradingUsersQueryOptio
'elephpants',
'elephpantsToTrade' => $elephpantsQuery,
])

->when(request()->has('country') && !empty(request()->get('country')), function ($query) {
return $query->where('country_code', request()->get('country'));
})
->whereHas('elephpantsToTrade', $elephpantsQuery)
->has('elephpantsToTrade')
->whereExists(function ($query) use ($authUserId) {
Expand Down
19 changes: 18 additions & 1 deletion resources/views/trade/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@
<div class="container">
<div class="row justify-content-center">
<div class="col-md-7">
<div class="mb-3">
<div class="mb-3 text-center">
<form action="">
<select name="country" id="country" class="custom-select w-50 mr-1">
<option value="">-- All Traders --</option>
@foreach($countries as $code => $current)
<option value="{{ $code }}" {{ $country === $code ? 'selected' : '' }}>{{ $current->get('name') }}</option>
@endforeach
</select>
<button type="submit" class="btn btn-primary">Change</button>
<a href="?country={{ auth()->user()->country_code }}" class="btn btn-secondary">Filter by My Country</a>
</form>
</div>
@if($country && !$countries->has($country))
<div class="alert alert-danger">Ops! This country is not in our records yet.</div>
@endif
</div>
@if(!$users)
<div class="alert alert-info">
You don't have any double elePHPant to trade yet.
Expand All @@ -27,7 +44,7 @@
</div>
@endforeach
<div class="d-flex custom-pagination">
<div class="mx-auto">{{ $users->links() }}</div>
<div class="mx-auto">{{ $users->appends(request()->query())->links() }}</div>
</div>
@else
<div class="alert alert-info">
Expand Down