Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ShaungBhone/fb80b7913472930168de5698ee21b130 to your computer and use it in GitHub Desktop.
Save ShaungBhone/fb80b7913472930168de5698ee21b130 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Livewire\Job;
use App\Enums\Currency;
use App\Enums\EmploymentType;
use App\Enums\Experience;
use App\Enums\TeamRange;
use App\Traits\EnumOptions;
use Filament\Forms;
use Illuminate\Http\Response;
use Illuminate\View\View;
use Livewire\Component;
class CreateJob extends Component implements Forms\Contracts\HasForms
{
use Forms\Concerns\InteractsWithForms;
use EnumOptions;
public ?array $data = [];
public function mount(): void
{
$this->form->fill();
}
protected function getFormSchema(): array
{
return [
Forms\Components\Card::make()->schema([
Forms\Components\Wizard::make([
Forms\Components\Wizard\Step::make('Details')
->schema([
Forms\Components\Select::make('company_id')
->label('Company Name')
->options(\App\Models\Company::all()->pluck('name', 'id'))
->searchable()
->reactive()
->afterStateUpdated(fn (callable $set) => $set('location_id', null))
->placeholder('What is the name of your company?')
->required(),
Forms\Components\Select::make('category_id')
->label('Job Type')
->searchable()
->options(\App\Models\Job\Category::all()->pluck('name', 'id'))
->placeholder('Web Design, Marketing...')
->required(),
Forms\Components\Select::make('location_id')
->label('Location')
->options(function (callable $get) {
$company = \App\Models\Company::find($get('company_id'));
if (! $company) {
return \App\Models\Location::all()->pluck('name', 'id');
}
return $company->locations->pluck('name', 'id');
})
->string()
->searchable()
->required(),
Forms\Components\TextInput::make('email')
->label('Company Email')
->email()
->required(),
Forms\Components\TextInput::make('title')
->label('Job Title')
->placeholder('E.g. Software Engineer, Backend Engineer')
->minLength(2)
->maxLength(100)
->string()
->required(),
Forms\Components\Select::make('experience')
->options($this->getEnumOptions(Experience::class))
->label('Work Experience')
->required(),
Forms\Components\Select::make('type')
->options($this->getEnumOptions(EmploymentType::class))
->label('Employment')
->required(),
Forms\Components\Select::make('company_size')
->options($this->getEnumOptions(TeamRange::class))
->label('Team Size')
->required(),
])
->columns(2),
Forms\Components\Wizard\Step::make('Salary')->schema([
Forms\Components\Fieldset::make('Salary Range')
->schema([
Forms\Components\TextInput::make('min_salary')
->label('Min')
->placeholder('E.g. 60,000, 60K, 15/hr')
->string()
->required(),
Forms\Components\TextInput::make('max_salary')
->label('Max')
->placeholder('E.g. 80,000, 90K, 30/hr')
->string()
->required(),
Forms\Components\Select::make('currency')
->label('Currency')
->options($this->getEnumOptions(Currency::class))
->searchable()
->required(),
])
->columns(3),
]),
Forms\Components\Wizard\Step::make('Description')->schema([
Forms\Components\Select::make('technology_id')
->label('Technologies')
->multiple()
->relationship('technologies', 'prefix')
->required()
->searchable(),
Forms\Components\RichEditor::make('description')
->disableToolbarButtons([
'attachFiles',
'codeBlock',
])->required(),
Forms\Components\RichEditor::make('job_requirement')
->disableToolbarButtons([
'attachFiles',
'codeBlock',
])->required(),
Forms\Components\RichEditor::make('benefit')
->disableToolbarButtons([
'attachFiles',
'codeBlock',
])->required(),
Forms\Components\TextInput::make('application_url')
->label('Add External Link')
->helperText('Add an optional external link where applicants can submit their application.')
->placeholder('www.yourcompany.com/careers')
->url(),
]),
Forms\Components\Wizard\Step::make('Preview')
->schema([
Forms\Components\Placeholder::make('application_url')
->content(fn ($get) => $get('application_url')),
]),
])->submitAction(view('components.filament.button')),
]),
];
}
public function create()
{
abort_if(auth()->guest(), Response::HTTP_FORBIDDEN);
$record = \App\Models\Job\Post::create($this->form->getState());
$this->form->model($record)->saveRelationships();
$this->reset();
return redirect()->route('dashboard');
}
protected function getFormModel(): string
{
return \App\Models\Job\Post::class;
}
protected function getFormStatePath(): string
{
return 'data';
}
public function render(): View
{
return view('livewire.job.create-job');
}
}
it('can validate input', function () {
livewire(CreateJob::class)
->fillForm([
'title' => null,
'description' => null
])
->call('create')
->assertHasFormErrors([
'title' => 'required',
'description' => 'required'
]);
})->skip('TO DO');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment