Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
ezralazuardy committed Apr 29, 2024
1 parent 9cfb370 commit 744b8b2
Show file tree
Hide file tree
Showing 26 changed files with 850 additions and 289 deletions.
4 changes: 2 additions & 2 deletions app/Enums/DeviceBrand.php → app/Enums/Device/Brand.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace App\Enums;
namespace App\Enums\Device;

enum DeviceBrand: string
enum Brand: string
{
case CISCO = 'cisco';
case MIKROTIK = 'mikrotik';
Expand Down
4 changes: 2 additions & 2 deletions app/Enums/DeviceStatus.php → app/Enums/Device/Status.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace App\Enums;
namespace App\Enums\Device;

enum DeviceStatus: string
enum Status: string
{
case RUNNING = 'running';
case DOWN = 'down';
Expand Down
4 changes: 2 additions & 2 deletions app/Enums/DeviceType.php → app/Enums/Device/Type.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace App\Enums;
namespace App\Enums\Device;

enum DeviceType: string
enum Type: string
{
case ROUTER = 'router';
case ACCESS_POINT = 'access_point';
Expand Down
64 changes: 64 additions & 0 deletions app/Enums/User/Role.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace App\Enums\User;

enum Role: string
{
case SUPER_ADMINISTRATOR = 'super_administrator';
case ADMINISTRATOR = 'administrator';
case TECHNICIAN = 'technician';

public static function toArray(): array
{
return [
self::SUPER_ADMINISTRATOR,
self::ADMINISTRATOR,
self::TECHNICIAN,
];
}

public static function labels(): array
{
return [
self::SUPER_ADMINISTRATOR->label(),
self::ADMINISTRATOR->label(),
self::TECHNICIAN->label(),
];
}

public static function values(): array
{
return [
self::SUPER_ADMINISTRATOR->value,
self::ADMINISTRATOR->value,
self::TECHNICIAN->value,
];
}

public static function labelAndValues(): array
{
return [
['label' => self::SUPER_ADMINISTRATOR->label(), 'value' => self::SUPER_ADMINISTRATOR->value],
['label' => self::ADMINISTRATOR->label(), 'value' => self::ADMINISTRATOR->value],
['label' => self::TECHNICIAN->label(), 'value' => self::TECHNICIAN->value],
];
}

public function label(): string
{
return match ($this) {
self::SUPER_ADMINISTRATOR => 'Super Administrator',
self::ADMINISTRATOR => 'Administrator',
self::TECHNICIAN => 'Technician',
};
}

public function description(): string
{
return match ($this) {
self::SUPER_ADMINISTRATOR => 'Super Administrator has all permissions.',
self::ADMINISTRATOR => 'Administrator has all permissions except for managing roles and permissions.',
self::TECHNICIAN => 'Technician has limited permissions.',
};
}
}
77 changes: 77 additions & 0 deletions app/Exports/UserExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace App\Exports;

use App\Livewire\Device\Index;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class UserExport implements FromCollection, ShouldAutoSize, WithColumnFormatting, WithHeadings, WithMapping, WithStyles
{
use Exportable;

private Collection $users;

public function __construct(Collection $users)
{
$this->users = $users;
}

public function collection(): Collection
{
return $this->users;
}

public function properties(): array
{
return [
'creator' => config('app.name'),
'lastModifiedBy' => config('app.name'),
'title' => 'User Export',
'description' => 'Exported user data from '.config('app.name'),
'category' => 'Users',
'subject' => 'Users',
'keywords' => 'users,export,spreadsheet',
];
}

public function styles($sheet): array
{
return [
1 => ['font' => ['bold' => true], 'alignment' => ['horizontal' => 'center']],
];
}

public function columnFormats(): array
{
return [
'A' => NumberFormat::FORMAT_TEXT,
'B' => NumberFormat::FORMAT_TEXT,
'C' => NumberFormat::FORMAT_TEXT,
'D' => NumberFormat::FORMAT_DATE_DDMMYYYY,
];
}

public function headings(): array
{
return Index::getTableColumns();
}

public function map($user): array
{
return [
$user->name,
$user->email,
$user->role->name,
Date::dateTimeToExcel($user->createdAt),
];
}
}
16 changes: 8 additions & 8 deletions app/Livewire/Detector/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace App\Livewire\Detector;

use App\Enums\DeviceBrand;
use App\Enums\DeviceStatus;
use App\Enums\DeviceType;
use App\Enums\Device\Brand;
use App\Enums\Device\Status;
use App\Enums\Device\Type;
use App\Exports\DetectorExport;
use App\Interfaces\TableComponent;
use App\Models\Detector;
Expand All @@ -22,9 +22,9 @@ class Index extends Component implements TableComponent
use HasFilterFromEnum;

public array $filterEnums = [
'brands' => DeviceBrand::class,
'types' => DeviceType::class,
'statuses' => DeviceStatus::class,
'brands' => Brand::class,
'types' => Type::class,
'statuses' => Status::class,
];

public array $filters;
Expand All @@ -48,7 +48,7 @@ public function getTableData(): array
return [
'title' => 'Detector',
'detailRoute' => 'detectors.detail',
'storeRoute' => 'detectors.store1',
'storeRoute' => 'detectors.store',
'updateRoute' => 'detectors.update',
'actionable' => true,
'deleteable' => true,
Expand Down Expand Up @@ -116,7 +116,7 @@ public function export(DetectorRepository $detectorRepository, string $fileName)
public function delete(DetectorRepository $detectorRepository, string $detector)
{
$detectorRepository->deleteDetector($detector);
$this->detectors = $detectorRepository->getDetectors();
$this->detectors = $this->filter($detectorRepository);
}

public function mount(DetectorRepository $detectorRepository)
Expand Down
14 changes: 7 additions & 7 deletions app/Livewire/Device/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace App\Livewire\Device;

use App\Enums\DeviceBrand;
use App\Enums\DeviceStatus;
use App\Enums\DeviceType;
use App\Enums\Device\Brand;
use App\Enums\Device\Status;
use App\Enums\Device\Type;
use App\Exports\DeviceExport;
use App\Interfaces\TableComponent;
use App\Repositories\DeviceRepository;
Expand All @@ -21,9 +21,9 @@ class Index extends Component implements TableComponent
use HasFilterFromEnum;

public array $filterEnums = [
'brands' => DeviceBrand::class,
'types' => DeviceType::class,
'statuses' => DeviceStatus::class,
'brands' => Brand::class,
'types' => Type::class,
'statuses' => Status::class,
];

public array $filters;
Expand Down Expand Up @@ -115,7 +115,7 @@ public function export(DeviceRepository $deviceRepository, string $fileName): Re
public function delete(DeviceRepository $deviceRepository, string $device)
{
$deviceRepository->deleteDevice($device);
$this->devices = $deviceRepository->getDevices();
$this->devices = $this->filter($deviceRepository);
}

public function mount(DeviceRepository $deviceRepository)
Expand Down
12 changes: 6 additions & 6 deletions app/Livewire/Device/Store2.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace App\Livewire\Device;

use App\Enums\DeviceBrand;
use App\Enums\DeviceType;
use App\Enums\Device\Brand;
use App\Enums\Device\Type;
use App\Repositories\DeviceRepository;
use App\Traits\HasSessionError;
use Livewire\Component;
Expand Down Expand Up @@ -34,8 +34,8 @@ public function store(DeviceRepository $deviceRepository)
{
$this->validate([
'name' => ['required', 'string', 'max:255'],
'brand' => 'required|string|in:'.implode(',', DeviceBrand::labels()),
'type' => 'required|string|in:'.implode(',', DeviceType::labels()),
'brand' => 'required|string|in:'.implode(',', Brand::labels()),
'type' => 'required|string|in:'.implode(',', Type::labels()),
'ipAddress' => 'required|string|ip',
'port' => 'required|integer|min:1|max:65535',
'username' => 'required|string|max:255',
Expand Down Expand Up @@ -63,9 +63,9 @@ public function store(DeviceRepository $deviceRepository)

public function mount()
{
$this->brands = DeviceBrand::labelAndValues();
$this->brands = Brand::labelAndValues();
$this->brand = $this->brands[0]['label'];
$this->types = DeviceType::labelAndValues();
$this->types = Type::labelAndValues();
$this->type = $this->types[0]['label'];
}

Expand Down
Loading

0 comments on commit 744b8b2

Please sign in to comment.