Skip to content

Commit 13a6488

Browse files
committed
like vue component and few tests
1 parent 74def64 commit 13a6488

File tree

9 files changed

+68
-35
lines changed

9 files changed

+68
-35
lines changed

app/Helpers/HasLikes.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: bobbyborisov
5-
* Date: 12/3/17
6-
* Time: 1:51 PM
7-
*/
82

93
namespace App\Helpers;
104

@@ -27,7 +21,7 @@ public function likedBy(User $user)
2721

2822
public function dislikedBy(User $user)
2923
{
30-
$this->likes()->where('user_id', $user->id)->get()->first()->delete();
24+
optional($this->likes()->where('user_id', $user->id)->get()->first())->delete();
3125
}
3226

3327
public function likes()
@@ -37,6 +31,7 @@ public function likes()
3731

3832
public function isLikedBy(User $user)
3933
{
34+
if ($user == null) return false;
4035
return $this->likes()->where('user_id', $user->id)->exists();
4136
}
4237

resources/assets/js/app.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ require('./bootstrap');
1616
$('select.selectize').selectize({ maxItems: 3 });
1717
$('textarea.wysiwyg').markdown({ iconlibrary: 'fa' });
1818

19-
// const app = new Vue({
20-
// el: '#app'
21-
// });
19+
Vue.component('like', require('./components/Like.vue'));
20+
21+
const app = new Vue({
22+
el: '#app'
23+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<template>
2+
<div>
3+
<span style="margin-right:3px">{{count}}</span><i class="fa" :class="classes" @click="toggle"></i>
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default {
9+
data(){
10+
return {
11+
count:0,
12+
isLiked: false
13+
}
14+
},
15+
computed:{
16+
classes(){
17+
return this.isLiked ? 'fa-thumbs-down text-danger' : 'fa-thumbs-up text-success';
18+
}
19+
},
20+
methods:{
21+
toggle(){
22+
return this.isLiked ? this.dislike() : this.like();
23+
},
24+
like(){
25+
//axios call
26+
console.log('like');
27+
this.count++;
28+
this.isLiked = true;
29+
},
30+
dislike(){
31+
//axios call
32+
console.log('dislike');
33+
this.count--;
34+
this.isLiked = false;
35+
}
36+
}
37+
}
38+
</script>

resources/views/forum/threads/show.blade.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,11 @@
7171
<a class="btn btn-danger btn-xs" href="#" data-toggle="modal" data-target="#deleteReply{{ $reply->id() }}">Delete</a>
7272
</div>
7373
@endcan
74+
@auth
7475
<div class="thread-info-likes">
75-
@if(!$reply->isLikedBy(auth()->user()))
76-
{{ Form::open(['route' => ['replies.like', $reply], 'method' => 'PUT']) }}
77-
<span>{{$reply->likes_count}}</span>
78-
{{ Form::submit('like', ['class' => 'btn btn-xs btn-success']) }}
79-
{{ Form::close() }}
80-
@else
81-
{{ Form::open(['route' => ['replies.dislike', $reply], 'method' => 'DELETE']) }}
82-
<span>{{$reply->likes_count}}</span>
83-
{{ Form::submit('dislike', ['class' => 'btn btn-xs btn-danger']) }}
84-
{{ Form::close() }}
85-
@endif
76+
<like></like>
8677
</div>
78+
@endauth
8779
</div>
8880

8981
<div class="panel-body forum-content">

resources/views/layouts/default.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@extends('layouts.base')
22

33
@section('body')
4-
<div class="container">
4+
<div id="app" class="container">
55
@include('layouts._alerts')
66

77
@yield('content')

tests/Components/Jobs/DeleteThreadTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ class DeleteThreadTest extends TestCase
1616
public function we_can_delete_a_thread_and_its_replies()
1717
{
1818
$thread = factory(Thread::class)->create();
19-
factory(Reply::class)->create(['replyable_id' => $thread->id()]);
19+
$reply = factory(Reply::class)->create(['replyable_id' => $thread->id()]);
2020

2121
$this->dispatch(new DeleteThread($thread));
2222

2323
$this->assertDatabaseMissing('threads', ['id' => $thread->id()]);
2424
$this->assertDatabaseMissing('replies', ['replyable_id' => $thread->id()]);
25+
$this->assertDatabaseMissing('likes', ['liked_type' => get_class($reply), 'liked_id' => $reply->id()]);
2526
}
2627
}

tests/Components/Jobs/DislikeReplyTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,20 @@ public function we_can_dislike_a_reply()
2525

2626
$this->assertFalse($reply->fresh()->isLikedBy($user));
2727
}
28+
29+
/** @test */
30+
public function we_cannot_dislike_a_reply()
31+
{
32+
$user = factory(User::class)->create();
33+
$reply = factory(Reply::class)->create();
34+
35+
$reply->likedBy($user);
36+
$this->assertTrue($reply->fresh()->isLikedBy($user));
37+
38+
$this->dispatch(new DislikeReply($reply, $user));
39+
40+
$this->dispatch(new DislikeReply($reply, $user));
41+
42+
//$this->assertFalse($reply->fresh()->isLikedBy($user));
43+
}
2844
}

tests/Components/Jobs/LikeReplyTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function we_cannot_like_a_reply_twice()
3535
$this->assertTrue($reply->fresh()->isLikedBy($user));
3636

3737
$this->expectException(CannotLikeReplyTwice::class);
38+
3839
$this->dispatch(new LikeReply($reply, $user));
3940
}
4041
}

tests/Feature/ReplyTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,4 @@ public function users_cannot_mark_a_reply_as_the_solution_of_the_thread_if_they_
7878
$this->put('/forum/the-first-thread/mark-solution/'.$reply->id())
7979
->assertForbidden();
8080
}
81-
82-
/** @test */
83-
public function user_can_like_a_reply()
84-
{
85-
$user = factory(User::class)->create();
86-
87-
$thread = factory(Thread::class)->create(['author_id' => $user->id(), 'slug' => 'the-first-thread']);
88-
$reply = factory(Reply::class)->create(['replyable_id' => $thread->id()]);
89-
90-
$this->put('/forum/the-first-thread/'.$reply->id().'/like');
91-
//->assertForbidden();
92-
}
9381
}

0 commit comments

Comments
 (0)