Skip to content

Commit

Permalink
🚸 improve events form in backend (#2304)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu authored Jan 29, 2024
1 parent c96e52c commit b081f0e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 107 deletions.
29 changes: 16 additions & 13 deletions app/Http/Controllers/Frontend/Admin/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function renderSuggestionCreation(int $id): View {
}

public function renderEdit(int $id): View {
return view('admin.events.edit', ['event' => Event::findOrFail($id)]);
return view('admin.events.form', ['event' => Event::findOrFail($id)]);
}

public function denySuggestion(Request $request): RedirectResponse {
Expand Down Expand Up @@ -117,16 +117,16 @@ public function acceptSuggestion(Request $request): RedirectResponse {
]);

$eventSuggestion = EventSuggestion::find($validated['suggestionId']);
$trainStation = null;
$station = null;

if ($eventSuggestion->user_id === auth()->user()->id && !auth()->user()?->hasRole('admin')) {
return back()->with('alert-danger', 'You can\'t accept your own suggestion.');
}

if (isset($validated['nearest_station_name'])) {
$trainStation = HafasController::getStations($validated['nearest_station_name'], 1)->first();
$station = HafasController::getStations($validated['nearest_station_name'], 1)->first();

if ($trainStation === null) {
if ($station === null) {
return back()->with('alert-danger', 'Die Station konnte nicht gefunden werden.');
}
}
Expand All @@ -136,7 +136,7 @@ public function acceptSuggestion(Request $request): RedirectResponse {
'slug' => AdminEventBackend::createSlugFromName($validated['name']),
'hashtag' => $validated['hashtag'],
'host' => $validated['host'],
'station_id' => $trainStation?->id,
'station_id' => $station?->id,
'begin' => Carbon::parse($validated['begin'])->toIso8601String(),
'end' => Carbon::parse($validated['end'])->toIso8601String(),
'event_start' => Carbon::parse($validated['event_start'] ?? $validated['begin'])->toIso8601String(),
Expand Down Expand Up @@ -170,11 +170,11 @@ public function acceptSuggestion(Request $request): RedirectResponse {
public function create(Request $request): RedirectResponse {
$validated = $request->validate(self::VALIDATOR_RULES);

$trainStation = null;
$station = null;
if (isset($validated['nearest_station_name'])) {
$trainStation = HafasController::getStations($validated['nearest_station_name'], 1)->first();
$station = HafasController::getStations($validated['nearest_station_name'], 1)->first();

if ($trainStation === null) {
if ($station === null) {
return back()->with('alert-danger', 'Die Station konnte nicht gefunden werden.');
}
}
Expand All @@ -184,7 +184,7 @@ public function create(Request $request): RedirectResponse {
'slug' => AdminEventBackend::createSlugFromName($validated['name']),
'hashtag' => $validated['hashtag'],
'host' => $validated['host'],
'station_id' => $trainStation?->id,
'station_id' => $station?->id,
'begin' => Carbon::parse($validated['begin'])->toIso8601String(),
'end' => Carbon::parse($validated['end'])->toIso8601String(),
'event_start' => Carbon::parse($validated['event_start'] ?? $validated['begin'])->toIso8601String(),
Expand All @@ -201,12 +201,15 @@ public function edit(int $id, Request $request): RedirectResponse {

$event = Event::findOrFail($id);

$trainStation = HafasController::getStations($validated['nearest_station_name'], 1)->first();
$validated['station_id'] = null;
if ($validated['nearest_station_name']) {
$station = HafasController::getStations($validated['nearest_station_name'], 1)->first();

if ($trainStation === null) {
return back()->with('alert-danger', 'Die Station konnte nicht gefunden werden.');
if ($station === null) {
return back()->with('alert-danger', 'Die Station konnte nicht gefunden werden.');
}
$validated['station_id'] = $station->id;
}
$validated['station_id'] = $trainStation->id;

$event->update($validated);

Expand Down
85 changes: 0 additions & 85 deletions resources/views/admin/events/edit.blade.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@extends('admin.layout')

@section('title', 'Create event')
@section('title', isset($event) ? 'Edit Event' : 'Create Event')
@php($event ??= null)

@section('content')
<form method="POST">
Expand All @@ -15,30 +16,33 @@
{{ __('events.name') }}<span class="text-danger">*</span>:
</label>
<div class="col-md-8 text-center">
<input id="name" type="text" class="form-control" name="name" required/>
<input id="name" type="text" class="form-control" name="name" required
value="{{$event?->name}}"/>
</div>
</div>
<div class="form-group row">
<label for="hashtag" class="col-md-4 col-form-label text-md-right">
{{ __('events.hashtag') }}:
</label>
<div class="col-md-8 text-center">
<input id="hashtag" type="text" class="form-control" name="hashtag"/>
<input id="hashtag" type="text" class="form-control" name="hashtag"
value="{{$event?->hashtag}}"/>
</div>
</div>
<div class="form-group row">
<label for="host" class="col-md-4 col-form-label text-md-right">
{{ __('events.host') }}:
</label>
<div class="col-md-8 text-center">
<input id="host" type="text" class="form-control" name="host"/>
<input id="host" type="text" class="form-control" name="host"
value="{{$event?->host}}"/>
</div>
</div>
<div class="form-group row">
<label for="url" class="col-md-4 col-form-label text-md-right">{{ __('events.url') }}
:</label>
<div class="col-md-8 text-center">
<input id="url" type="url" class="form-control" name="url"/>
<input id="url" type="url" class="form-control" name="url" value="{{$event?->url}}"/>
</div>
</div>
<div class="form-group row">
Expand All @@ -48,6 +52,7 @@
<div class="col-md-8 text-left" id="station-autocomplete-container">
<input type="text" id="station-autocomplete" name="nearest_station_name"
class="form-control" placeholder="{{ __('stationboard.station-placeholder') }}"
value="{{$event?->nearest_station_name}}"
/>
</div>
</div>
Expand All @@ -64,15 +69,15 @@ class="form-control" placeholder="{{ __('stationboard.station-placeholder') }}"
<div class="col-6">
<div class="form-floating">
<input id="begin" type="datetime-local" class="form-control" name="begin"
required
required value="{{$event?->begin}}"
/>
<label for="begin">Checkin {{ __('events.begin') }}:</label>
</div>
</div>
<div class="col-6">
<div class="form-floating">
<input id="end" type="datetime-local" class="form-control" name="end"
required
required value="{{$event?->end}}"
/>
<label for="end">Checkin {{ __('events.end') }}:</label>
</div>
Expand All @@ -92,14 +97,15 @@ class="form-control" placeholder="{{ __('stationboard.station-placeholder') }}"
<div class="col-6">
<div class="form-floating">
<input id="event-start" type="datetime-local" class="form-control"
name="event_start"
name="event_start" value="{{$event?->event_start}}"
/>
<label for="event-begin">{{ __('events.begin') }}</label>
</div>
</div>
<div class="col-6">
<div class="form-floating">
<input id="event-end" type="datetime-local" class="form-control" name="event_end"
value="{{$event?->event_end}}"
/>
<label for="event-end">{{ __('events.end') }}</label>
</div>
Expand Down
2 changes: 1 addition & 1 deletion routes/web/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
//->middleware(['can:accept-events']) - TODO: working in the browser, but not in the tests
->name('admin.events.suggestions.accept.do');

Route::view('/create', 'admin.events.create')
Route::view('/create', 'admin.events.form')
->middleware('permission:create-events')
->name('admin.events.create');
Route::post('/create', [AdminEventController::class, 'create'])
Expand Down

0 comments on commit b081f0e

Please sign in to comment.