Skip to content

Commit

Permalink
Merge pull request #158 from OpenSynergic/fix/workflow-schedule-bugs
Browse files Browse the repository at this point in the history
Fix & Improvement: Workflow stage settings.
  • Loading branch information
rahmanramsi authored Dec 15, 2023
2 parents e6a08c1 + 7a244d6 commit d71db4e
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 28 deletions.
12 changes: 11 additions & 1 deletion app/Panel/Livewire/Submissions/Components/Files/DraftFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Panel\Livewire\Submissions\Components\Files;

use App\Constants\SubmissionFileCategory;
use App\Panel\Livewire\Workflows\Classes\StageManager;

class DraftFiles extends SubmissionFilesTable
{
Expand All @@ -18,14 +19,23 @@ public function isViewOnly(): bool
return $this->viewOnly;
}

return ! auth()->user()->can('editing', $this->submission);
return !auth()->user()->can('editing', $this->submission);
}

public function getTargetCategory(): string
{
return $this->getCategory();
}

public function getAcceptedFiles(): array
{
return StageManager::editing()
->getSetting(
'draft_allowed_file_types',
['pdf', 'doc', 'docx']
);
}

public function getSelectableCategories(): array
{
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Panel\Livewire\Submissions\Components\Files;

use App\Constants\SubmissionFileCategory;
use App\Panel\Livewire\Workflows\Classes\StageManager;

class ProductionFiles extends SubmissionFilesTable
{
Expand All @@ -18,7 +19,16 @@ public function isViewOnly(): bool
return $this->viewOnly;
}

return ! auth()->user()->can('editing', $this->submission);
return !auth()->user()->can('editing', $this->submission);
}

public function getAcceptedFiles(): array
{
return StageManager::editing()
->getSetting(
'production_allowed_file_types',
['pdf']
);
}

public function getTargetCategory(): string
Expand Down
13 changes: 13 additions & 0 deletions app/Panel/Livewire/Submissions/Components/Files/SelectFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Filament\Tables\Actions\BulkAction;
use Filament\Tables\Columns\TextColumn;
use GuzzleHttp\Psr7\MimeType;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -16,6 +17,8 @@ final class SelectFiles extends SubmissionFilesTable

public array $selectableCategories = [];

public array $allowedFileTypes = [];

public $listeners = [
'refreshList' => '$refresh',
];
Expand Down Expand Up @@ -43,6 +46,16 @@ public function tableQuery(): Builder
return $this->submission
->submissionFiles()
->whereNotIn('media_id', $selectedCategoryIDs)
->whereHas('media', function ($query) {
$query->whereIn(
'mime_type',
collect($this->allowedFileTypes)
->map(function ($type) {
return MimeType::fromExtension($type);
})
->toArray()
);
})
->whereIn('category', $this->selectableCategories)
->getQuery();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function uploadAction()
'submission' => $this->submission,
'targetCategory' => $this->getTargetCategory(),
'selectableCategories' => $this->getSelectableCategories(),
'allowedFileTypes' => $this->getAcceptedFiles(),
'lazy' => true,
]
),
Expand Down
23 changes: 13 additions & 10 deletions app/Panel/Livewire/Workflows/Components/StageSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Livewire\Component;

/**
Expand All @@ -18,10 +19,7 @@
*/
class StageSchedule extends Component implements HasActions, HasForms
{
use CanOpenStage;
use InteractsWithActions;
use InteractsWithForms;
use InteractWithTenant;
use CanOpenStage, InteractsWithActions, InteractsWithForms, InteractWithTenant;

public string $stage;

Expand All @@ -34,7 +32,7 @@ public function closeAction()
{
return Action::make('closeAction')
->hidden(
fn (): bool => ! $this->isStageOpen()
fn (): bool => !$this->isStageOpen()
)
->modalWidth('xl')
->modalAlignment('center')
Expand Down Expand Up @@ -76,15 +74,21 @@ public function scheduleAction()
->label('Schedule')
->icon('iconpark-calendar-o')
->modalWidth('xl')
->mountUsing(function (Form $form) {
$form->fill([
'start_date' => $this->getSetting('start_date'),
'end_date' => $this->getSetting('end_date'),
]);
})
->form([
DatePicker::make("settings.{$this->stage}.start_date")
DatePicker::make("start_date")
->label('Start')
->required()
->native(false)
->displayFormat('d-F-Y')
->default(now())
->maxDate(now()->addYear()),
DatePicker::make("settings.{$this->stage}.end_date")
DatePicker::make("end_date")
->label('End')
->required()
->native(false)
Expand All @@ -94,10 +98,9 @@ public function scheduleAction()
])
->successNotificationTitle('Scheduled')
->action(function (array $data, Action $action) {
$setting = $data['settings'][$this->stage];
$this->setSchedule(
$setting['start_date'],
$setting['end_date']
$data['start_date'],
$data['end_date']
);
$action->success();
});
Expand Down
11 changes: 6 additions & 5 deletions app/Panel/Livewire/Workflows/Concerns/CanOpenStage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ trait CanOpenStage
#[On('stage-status-changed')]
public function isStageOpen(): bool
{
if (Carbon::now() >= Carbon::parse($this->getSetting('start_date')) && ! $this->getSetting('end_date')) {
$this->stageOpen = true;
}
$currentTime = Carbon::now();
$startDate = Carbon::parse($this->getSetting('start_date'));
$endDate = Carbon::parse($this->getSetting('end_date'));

if (Carbon::now() >= Carbon::parse($this->getSetting('end_date'))) {
$this->stageOpen = false;
if ($currentTime >= $startDate && $currentTime < $endDate) {
$this->stageOpen = true;
}

return $this->stageOpen;
Expand All @@ -45,5 +45,6 @@ public function setSchedule(string $start, string $end): void
{
$this->updateSetting('start_date', $start);
$this->updateSetting('end_date', $end);
$this->dispatch('stage-status-changed');
}
}
51 changes: 41 additions & 10 deletions app/Panel/Livewire/Workflows/EditingSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use App\Panel\Livewire\Workflows\Base\WorkflowStage;
use Awcodes\Shout\Components\Shout;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\TagsInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Filament\Notifications\Notification;

class EditingSetting extends WorkflowStage implements HasForms
{
Expand All @@ -17,24 +19,53 @@ class EditingSetting extends WorkflowStage implements HasForms

protected ?string $stageLabel = 'Editing';

public array $settings = [];

public function mount()
{
$this->form->fill([
'settings' => [],
'settings' => [
'production_allowed_file_types' => $this->getSetting('production_allowed_file_types', ['pdf']),
'draft_allowed_file_types' => $this->getSetting('draft_allowed_file_types', ['pdf', 'doc', 'docx'])
]
]);
}

public function form(Form $form): Form
{
return $form->schema([
Shout::make('stage-closed')
->hidden(fn (): bool => $this->isStageOpen())
->color('warning')
->content("The {$this->getStageLabel()} is not open yet, Start now or schedule opening"),
Grid::make()
->schema([])
->columns(1),
]);
return $form
->schema([
Shout::make('stage-closed')
->hidden(fn (): bool => $this->isStageOpen())
->color('warning')
->content("The {$this->getStageLabel()} is not open yet, Start now or schedule opening"),
Grid::make()
->schema([
TagsInput::make('settings.draft_allowed_file_types')
->label('Draft File Type')
->helperText('Allowed file types for draft files')
->splitKeys([',']),
TagsInput::make('settings.production_allowed_file_types')
->label('Production File Type')
->helperText('Allowed file types for production files')
->splitKeys([',']),
])
->columns(1),
]);
}

public function save()
{
$data = $this->form->getState();
foreach ($data['settings'] as $settingName => $settingValue) {
$this->updateSetting($settingName, $settingValue);
}

Notification::make('editing-saved')
->title('Saved')
->body('Settings Saved successfully')
->success()
->send();
}

public function render()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
@livewire(App\Panel\Livewire\Workflows\Components\StageSchedule::class, ['stage' => $this->getStage()])
</div>
<div>
{{ $this->form }}
<form wire:submit='save' class="space-y-4">
{{ $this->form }}
<x-filament::button type="submit" color="primary" icon='lineawesome-save-solid'>
{{ __('Save') }}
</x-filament::button>
</form>
</div>
</div>

0 comments on commit d71db4e

Please sign in to comment.