Skip to content

Commit

Permalink
show RSVPs and RSVP button on events
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronpk committed Nov 15, 2019
1 parent 85b09bb commit 2338a54
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 10 deletions.
12 changes: 12 additions & 0 deletions app/Event.php
Expand Up @@ -17,6 +17,18 @@ public function responses() {
return $this->hasMany('\App\Response');
}

public function rsvp_for_user(User $user) {
return $this->responses()->where([
'type' => 'rsvp',
'rsvp_user_id' => $user->id
])->first();
}

public function rsvp_string_for_user(User $user) {
$rsvp = $this->rsvp_for_user($user);
return $rsvp ? $rsvp->rsvp : null;
}

public function tags() {
return $this->belongsToMany('\App\Tag');
}
Expand Down
39 changes: 39 additions & 0 deletions app/Http/Controllers/EventResponseController.php
@@ -0,0 +1,39 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use App\Event, App\Response, App\EventRevision, App\Tag;
use Illuminate\Support\Str;
use Auth;


class EventResponseController extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

public function save_rsvp(Event $event) {

# Load existing RSVP for this user if there is one already
$rsvp = $event->rsvp_for_user(Auth::user());

if(!$rsvp) {
$rsvp = new Response();
$rsvp->event_id = $event->id;
$rsvp->type = 'rsvp';
$rsvp->rsvp_user_id = Auth::user()->id;
$rsvp->created_by = Auth::user()->id;
}

$rsvp->rsvp = request('rsvp') ? 'yes' : 'no';
$rsvp->save();

return response()->json([
'redirect' => $event->permalink()
]);
}

}
17 changes: 17 additions & 0 deletions app/Response.php
Expand Up @@ -16,4 +16,21 @@ public function photos() {

return json_decode($this->photos, true);
}

public function author() {
if($this->rsvp_user_id) {
$user = User::where('id', $this->rsvp_user_id)->first();
return [
'name' => $user->name,
'photo' => $user->photo,
'url' => $user->url,
];
} else {
return [
'name' => $this->author_name,
'photo' => $this->author_photo,
'url' => $this->author_url,
];
}
}
}
32 changes: 32 additions & 0 deletions database/migrations/2019_11_15_184749_rsvp_user_link.php
@@ -0,0 +1,32 @@
<?php

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

class RsvpUserLink extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('responses', function (Blueprint $table) {
$table->bigInteger('rsvp_user_id')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('responses', function (Blueprint $table) {
$table->dropColumn('rsvp_user_id');
});
}
}
34 changes: 34 additions & 0 deletions database/migrations/2019_11_15_190126_user_profiles.php
@@ -0,0 +1,34 @@
<?php

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

class UserProfiles extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('name')->nullable();
$table->string('photo')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('name');
$table->dropColumn('photo');
});
}
}
11 changes: 11 additions & 0 deletions public/assets/script.js
Expand Up @@ -21,5 +21,16 @@ $(function(){
});
});

$("#rsvp-button").click(function(evt){
evt.preventDefault();
$.post($(this).data('action'), {
_token: $("input[name=_token]").val(),
rsvp: $(this).hasClass('is-primary') ? 0 : 1
}, function(response){
window.location = response.redirect;
});
});


});

4 changes: 2 additions & 2 deletions resources/views/edit-event.blade.php
Expand Up @@ -115,8 +115,8 @@

<div class="field">
<label class="label">Tags</label>
<input class="input" type="text" name="tags" value="{{ $event->tags_string() }}">
<div class="help">space separated</div>
<input class="input" type="text" name="tags" value="{{ $event->tags_string() }}" autocomplete="off">
<div class="help">space separated, lowercase</div>
</div>

<button class="button is-primary" type="submit">Submit</button>
Expand Down
29 changes: 21 additions & 8 deletions resources/views/event.blade.php
Expand Up @@ -74,25 +74,36 @@
{!! $event->html() !!}
</div>

<div class="segment tags are-medium">
<div class="segment tags are-medium" id="tags">
@foreach($event->tags as $tag)
<a href="{{ $tag->url() }}" class="tag is-rounded">#{{ $tag->tag }}</a>
@endforeach
</div>

@if($event->has_rsvps())
<div class="responses rsvps">
<h2>RSVPs</h2>
@if($event->has_rsvps() || Auth::user())
<div class="responses rsvps" id="rsvps">
<h2 class="subtitle">RSVPs</h2>

@if(Auth::user())
<button id="rsvp-button" class="button {{ $event->rsvp_string_for_user(Auth::user()) == 'yes' ? 'is-primary' : '' }}" data-action="{{ route('event-rsvp', $event->id) }}">I'm Going!</button>
<br><br>
@endif

<ul>
@foreach($event->rsvps as $rsvp)
<li>{{ $rsvp->id }}</li>
@if($rsvp->rsvp == 'yes')
<li class="avatar">
<img src="{{ $rsvp->author()['photo'] }}" width="48">
<a href="{{ $rsvp->author()['url'] }}">{{ $rsvp->author()['name'] ?? p3k\url\display_url($rsvp->author()['url']) }}</a>
</li>
@endif
@endforeach
</ul>
</div>
@endif

@if($event->has_photos())
<div class="responses photos">
<div class="responses photos" id="photos">
<h2>Photos</h2>
<ul>
@foreach($event->photos as $photo)
Expand All @@ -103,7 +114,7 @@
@endif

@if($event->has_posts())
<div class="responses posts">
<div class="responses posts" id="posts">
<h2>Blog Posts</h2>
<ul>
@foreach($event->posts as $post)
Expand All @@ -114,7 +125,7 @@
@endif

@if($event->has_comments())
<div class="responses comments">
<div class="responses comments" id="comments">
<h2>Comments</h2>
<ul>
@foreach($event->comments as $comment)
Expand All @@ -124,6 +135,8 @@
</div>
@endif

{{ csrf_field() }}

</article>

</section>
Expand Down
3 changes: 3 additions & 0 deletions routes/web.php
Expand Up @@ -24,11 +24,14 @@
Route::middleware('auth')->group(function(){

Route::get('/new', 'EventController@new_event')->name('new-event');

Route::post('/create', 'EventController@create_event')->name('create-event');
Route::get('/event/{event}', 'EventController@edit_event')->name('edit-event');
Route::post('/event/{event}/save', 'EventController@save_event')->name('save-event');
Route::get('/event/{event}/history', 'EventController@event_history')->name('event-history');
Route::get('/event/{event}/clone', 'EventController@clone_event')->name('clone-event');
Route::post('/event/{event}/delete', 'EventController@delete_event')->name('delete-event');

Route::post('/event/{event}/rsvp', 'EventResponseController@save_rsvp')->name('event-rsvp');

});

0 comments on commit 2338a54

Please sign in to comment.