Skip to content

Commit

Permalink
Merge pull request #34 from MariaInsyt/arthur
Browse files Browse the repository at this point in the history
Implementation of address, location on map. API for agent districts
  • Loading branch information
kyagie committed Mar 5, 2024
2 parents 02004ff + 28cc40e commit 27ed023
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 119 deletions.
167 changes: 92 additions & 75 deletions app/Filament/Resources/BillboardResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Cheesegrits\FilamentGoogleMaps\Fields\Map;
use App\Models\District;
use App\Models\Agent;
use Filament\Forms\Components\Section;

class BillboardResource extends Resource
{
Expand All @@ -26,81 +27,97 @@ public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\Select::make('status')
->options([
'pending' => 'Pending',
'updated' => 'Updated',
'notupdated' => 'Not Updated',
'rejected' => 'rejected'
])
->default('pending')
->required(),
Forms\Components\Select::make('district_id')
->label('District')
->options(District::active()->get()->pluck('name', 'id')->toArray())
->searchable()
->required(),
Forms\Components\Select::make('agent_id')
->label('Agent')
->options(Agent::active()->get()->pluck('name', 'id')->toArray())
->searchable(),
Forms\Components\Toggle::make('is_active')
->default(true)
->required(),
Map::make('map')
->reactive()
->afterStateUpdated(function ($state, callable $get, callable $set) {
$set('lat', $state['lat']);
$set('lng', $state['lng']);
})
->mapControls([
'mapTypeControl' => false,
'scaleControl' => true,
'streetViewControl' => false,
'rotateControl' => false,
'fullscreenControl' => true,
'searchBoxControl' => false,
'zoomControl' => true,
])
->height(fn () => '400px')
->defaultZoom(15)
->defaultLocation(fn ($record) => [
$record->lat ?? 0.3401327,
$record->lng ?? 32.5864384,
])
->draggable()
->clickable(false)
->autocomplete('location')
->autocompleteReverse()
->geolocate()
->geolocateOnLoad(true, false)
->columnSpanFull(),
Forms\Components\TextInput::make('lat')
->label('Latitude')
->reactive()
->afterStateUpdated(function ($state, callable $get, callable $set) {
$set('map', [
'lat' => floatVal($state),
'lng' => floatVal($get('longitude')),
]);
})
->lazy(),
Forms\Components\TextInput::make('lng')
->label('Longitude')
->reactive()
->afterStateUpdated(function ($state, callable $get, callable $set) {
$set('map', [
'lat' => floatval($get('latitude')),
'lng' => floatVal($state),
]);
})
->lazy(),
// Forms\Components\TextInput::make('location')
// ->maxLength(1024),
]);
Section::make('Billboard Information')->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\Select::make('status')
->options([
'pending' => 'Pending',
'updated' => 'Updated',
'notupdated' => 'Not Updated',
'rejected' => 'rejected'
])
->default('pending')
->required(),
])->columnSpan(2),
Section::make('Billboard Status')->schema([
Forms\Components\Toggle::make('is_active')
->default(true),
Forms\Components\Select::make('agent_id')
->label('Agent')
->options(Agent::active()->get()->pluck('name', 'id')->toArray())
->searchable(),
])->columnSpan(1),
Section::make('Location Information')->schema([
Forms\Components\Select::make('district_id')
->label('District')
->options(District::active()->get()->pluck('name', 'id')->toArray())
->searchable()
->required(),
Forms\Components\Textarea::make('address')
->label('Address')
->autosize()
->maxLength(1024)
->required(),
Forms\Components\TextInput::make('location')
->label('Location')
->placeholder('Start typing to search for a location')
->maxLength(1024)
->required(),
Map::make('map')
->reactive()
->afterStateUpdated(function ($state, callable $get, callable $set) {
$set('lat', $state['lat']);
$set('lng', $state['lng']);
})
->mapControls([
'mapTypeControl' => false,
'scaleControl' => true,
'streetViewControl' => false,
'rotateControl' => false,
'fullscreenControl' => true,
'searchBoxControl' => false,
'zoomControl' => true,
])
->height(fn () => '800px')
->defaultZoom(12)
->defaultLocation(fn ($record) => [
$record->lat ?? 0.3401327,
$record->lng ?? 32.5864384,
])
->draggable()
->clickable(false)
->autocomplete('location', placeField: 'name', types: [
'geocode',
'establishment',
], countries: ['UG'])
->autocompleteReverse()
->geolocate()
->geolocateOnLoad(true, false)
->columnSpanFull(),
Forms\Components\TextInput::make('lat')
->label('Latitude')
->reactive()
->afterStateUpdated(function ($state, callable $get, callable $set) {
$set('map', [
'lat' => floatVal($state),
'lng' => floatVal($get('longitude')),
]);
})
->lazy(),
Forms\Components\TextInput::make('lng')
->label('Longitude')
->reactive()
->afterStateUpdated(function ($state, callable $get, callable $set) {
$set('map', [
'lat' => floatval($get('latitude')),
'lng' => floatVal($state),
]);
})
->lazy(),
])->columns(2)
])->columns(3);
}

public static function table(Table $table): Table
Expand Down
31 changes: 31 additions & 0 deletions app/Http/Controllers/AgentDistrictController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,40 @@

namespace App\Http\Controllers;

use App\Models\Agent;
use App\Models\AgentDistrict;
use App\Models\Billboard;
use Illuminate\Http\Request;

class AgentDistrictController extends Controller
{
//
public function agentDistricts(Request $request)
{
$agent = Agent::select('id', 'name', 'email', 'phone_number', 'status')
->find($request->user()->agent_id);

$agentDistricts = AgentDistrict::where('agent_id', $agent->id)->with(
'district:id,name'
)->get();

foreach ($agentDistricts as $agentDistrict) {
$agentDistrict->billboards =
Billboard::select('id', 'name', 'status', 'updated_at', 'lat', 'lng', 'location')
->active()->where(
'district_id',
$agentDistrict->district_id
)
->where(
'agent_id',
$agent->id
)
->active()
->get();
}

return response()->json([
'agent_districts' => $agentDistricts
], 200);
}
}
45 changes: 1 addition & 44 deletions app/Models/Billboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static function boot()
'lat',
'lng',
'location',
'address',
];

protected $hidden = [
Expand All @@ -44,10 +45,6 @@ public static function boot()
];


protected $appends = [
'location',
];

//Scope Active
public function scopeActive($query)
{
Expand All @@ -74,46 +71,6 @@ public function agent()
return $this->belongsTo(Agent::class);
}

/**
* Returns the 'lat' and 'lng' attributes as the computed 'location' attribute,
* as a standard Google Maps style Point array with 'lat' and 'lng' attributes.
*
* Used by the Filament Google Maps package.
*
* Requires the 'location' attribute be included in this model's $fillable array.
*
* @return array
*/

public function getLocationAttribute(): array
{
return [
"lat" => (float)$this->lat,
"lng" => (float)$this->lng,
];
}

/**
* Takes a Google style Point array of 'lat' and 'lng' values and assigns them to the
* 'lat' and 'lng' attributes on this model.
*
* Used by the Filament Google Maps package.
*
* Requires the 'location' attribute be included in this model's $fillable array.
*
* @param ?array $location
* @return void
*/
public function setLocationAttribute(?array $location): void
{
if (is_array($location))
{
$this->attributes['lat'] = $location['lat'];
$this->attributes['lng'] = $location['lng'];
unset($this->attributes['location']);
}
}

/**
* Get the lat and lng attribute/field names used on this table
*
Expand Down
29 changes: 29 additions & 0 deletions database/migrations/2024_03_05_173029_add_address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('billboards', function (Blueprint $table) {
//
$table->string('address')->nullable()->default('')->after('location');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('billboards', function (Blueprint $table) {
//
});
}
};
9 changes: 9 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use App\Http\Controllers\AgentController;
use App\Http\Controllers\AgentDistrictController;
use App\Http\Controllers\BillboardController;
use App\Http\Controllers\OneTimePasswordController;
use App\Http\Controllers\DeviceController;
Expand Down Expand Up @@ -46,6 +47,14 @@ function () {
}
);

Route::controller(AgentDistrictController::class)
->middleware('auth:sanctum')
->group(
function () {
Route::get('/agent/districts', 'agentDistricts');
}
);

Route::controller(OneTimePasswordController::class)->group(
function () {
Route::post('/otp', 'sendOtp');
Expand Down

0 comments on commit 27ed023

Please sign in to comment.