Skip to content

Commit

Permalink
feat: dashboard feed (monicahq/chandler#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Sep 1, 2022
1 parent e9ef0ba commit 5a5c572
Show file tree
Hide file tree
Showing 24 changed files with 452 additions and 28 deletions.
4 changes: 4 additions & 0 deletions app/Helpers/UserHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public static function getInformationAboutContact(User $user, Vault $vault): ?ar
'id' => $contact->id,
'name' => $contact->name,
'avatar' => $contact->avatar,
'url' => route('contact.show', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
]),
];
}
}
2 changes: 2 additions & 0 deletions app/Models/ContactFeedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class ContactFeedItem extends Model
/**
* Possible actions.
*/
public const ACTION_CONTACT_CREATED = 'contact_created';

public const ACTION_CONTACT_INFORMATION_UPDATED = 'contact_information_updated';

public const ACTION_JOB_INFORMATION_UPDATED = 'job_information_updated';
Expand Down
11 changes: 11 additions & 0 deletions app/Models/Label.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;

class Label extends Model
{
Expand Down Expand Up @@ -44,4 +45,14 @@ public function contacts(): BelongsToMany
{
return $this->belongsToMany(Contact::class);
}

/**
* Get the label's feed item.
*
* @return MorphOne
*/
public function feedItem(): MorphOne
{
return $this->morphOne(ContactFeedItem::class, 'feedable');
}
}
11 changes: 11 additions & 0 deletions domains/Contact/ManageContact/Services/CreateContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Interfaces\ServiceInterface;
use App\Models\Contact;
use App\Models\ContactFeedItem;
use App\Models\Gender;
use App\Models\Pronoun;
use App\Models\Template;
Expand Down Expand Up @@ -64,6 +65,7 @@ public function execute(array $data): Contact
$this->validate();
$this->createContact();
$this->updateLastEditedDate();
$this->createFeedItem();

return $this->contact;
}
Expand Down Expand Up @@ -117,4 +119,13 @@ private function updateLastEditedDate(): void
$this->contact->last_updated_at = Carbon::now();
$this->contact->save();
}

private function createFeedItem(): void
{
ContactFeedItem::create([
'author_id' => $this->author->id,
'contact_id' => $this->contact->id,
'action' => ContactFeedItem::ACTION_CONTACT_CREATED,
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Contact\ManageContact\Web\Controllers;

use App\Contact\ManageContact\Web\ViewHelpers\ContactIndexViewHelper;
use App\Helpers\PaginatorHelper;
use App\Http\Controllers\Controller;
use App\Models\Label;
use App\Models\Vault;
Expand All @@ -26,6 +27,7 @@ public function index(Request $request, int $vaultId, int $labelId)
return Inertia::render('Vault/Contact/Index', [
'layoutData' => VaultIndexViewHelper::layoutData($vault),
'data' => ContactIndexViewHelper::data($contacts, $vault, $labelId),
'paginator' => PaginatorHelper::getData($contacts),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use App\Contact\ManageTasks\Web\ViewHelpers\ModuleContactTasksViewHelper;
use App\Helpers\StorageHelper;
use App\Models\Contact;
use App\Models\ContactFeedItem;
use App\Models\Module;
use App\Models\TemplatePage;
use App\Models\User;
Expand Down Expand Up @@ -225,7 +226,12 @@ public static function modules(TemplatePage $page, Contact $contact, User $user)
}

if ($module->type == Module::TYPE_FEED) {
$data = ModuleFeedViewHelper::data($contact, $user);
$items = ContactFeedItem::where('contact_id', $contact->id)
->with('author')
->with('contact')
->orderBy('created_at', 'desc')
->get();
$data = ModuleFeedViewHelper::data($items, $user);
}

if ($module->type == Module::TYPE_REMINDERS) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Contact\ManageContactFeed\Web\ViewHelpers\Actions;

use App\Models\ContactFeedItem;

class ActionFeedGenericContactInformation
{
public static function data(ContactFeedItem $item): array
{
$contact = $item->contact;

return [
'id' => $contact->id,
'name' => $contact->name,
'age' => $contact->age,
'avatar' => $contact->avatar,
'url' => route('contact.show', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
]),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Contact\ManageContactFeed\Web\ViewHelpers\Actions;

use App\Models\ContactFeedItem;

class ActionFeedLabelAssigned
{
public static function data(ContactFeedItem $item): array
{
$contact = $item->contact;
$label = $item->feedable;

return [
'label' => [
'object' => $label ? [
'id' => $label->id,
'name' => $label->name,
'bg_color' => $label->bg_color,
'text_color' => $label->text_color,
'url' => route('contact.label.index', [
'vault' => $contact->vault_id,
'label' => $label->id,
]),
] : null,
'description' => $item->description,
],
'contact' => [
'id' => $contact->id,
'name' => $contact->name,
'age' => $contact->age,
'avatar' => $contact->avatar,
'url' => route('contact.show', [
'vault' => $contact->vault_id,
'contact' => $contact->id,
]),
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,24 @@

namespace App\Contact\ManageContactFeed\Web\ViewHelpers;

use App\Contact\ManageContactFeed\Web\ViewHelpers\Actions\ActionFeedGenericContactInformation;
use App\Contact\ManageContactFeed\Web\ViewHelpers\Actions\ActionFeedLabelAssigned;
use App\Helpers\DateHelper;
use App\Helpers\UserHelper;
use App\Models\Contact;
use App\Models\ContactFeedItem;
use App\Models\User;

class ModuleFeedViewHelper
{
public static function data(Contact $contact, User $user): array
public static function data($items, User $user): array
{
$items = ContactFeedItem::where('contact_id', $contact->id)
->with('author')
->orderBy('created_at', 'desc')
->get();

$itemsCollection = $items->map(function ($item) use ($user) {
return [
'id' => $item->id,
'action' => $item->action,
'author' => self::getAuthor($item, $user),
'author' => self::getAuthor($item),
'sentence' => self::getSentence($item),
'description' => $item->description,
'data' => self::getData($item),
'created_at' => DateHelper::format($item->created_at, $user),
];
});
Expand All @@ -38,10 +34,12 @@ private static function getSentence(ContactFeedItem $item): mixed
return trans('contact.feed_item_'.$item->action);
}

private static function getAuthor(ContactFeedItem $item, User $user): ?array
private static function getAuthor(ContactFeedItem $item): ?array
{
$author = $item->author;
if (! $author) {
// the author is not existing anymore, so we have to display a random
// avatar and an unknown name
$monicaSvg = '<svg viewBox="0 0 390 353" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M198.147 353C289.425 353 390.705 294.334 377.899 181.5C365.093 68.6657 289.425 10 198.147 10C106.869 10 31.794 61.4285 12.2144 181.5C-7.36527 301.571 106.869 353 198.147 353Z" fill="#2C2B29"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M196.926 320C270.146 320 351.389 272.965 341.117 182.5C330.844 92.0352 270.146 45 196.926 45C123.705 45 63.4825 86.2328 47.7763 182.5C32.0701 278.767 123.705 320 196.926 320Z" fill="white"/>
Expand All @@ -57,9 +55,22 @@ private static function getAuthor(ContactFeedItem $item, User $user): ?array
return [
'name' => trans('contact.feed_item_author_deleted'),
'avatar' => $monicaSvg,
'url' => null,
];
}

return UserHelper::getInformationAboutContact($author, $item->contact->vault);
}

private static function getData(ContactFeedItem $item)
{
switch ($item->action) {
case 'label_assigned':
case 'label_removed':
return ActionFeedLabelAssigned::data($item);

default:
return ActionFeedGenericContactInformation::data($item);
}
}
}
3 changes: 2 additions & 1 deletion domains/Contact/ManageLabels/Services/AssignLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ public function execute(array $data): Label

private function createFeedItem(): void
{
ContactFeedItem::create([
$feedItem = ContactFeedItem::create([
'author_id' => $this->author->id,
'contact_id' => $this->contact->id,
'action' => ContactFeedItem::ACTION_LABEL_ASSIGNED,
'description' => $this->label->name,
]);
$this->label->feedItem()->save($feedItem);
}
}
3 changes: 2 additions & 1 deletion domains/Contact/ManageLabels/Services/RemoveLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ public function execute(array $data): Label

private function createFeedItem(): void
{
ContactFeedItem::create([
$feedItem = ContactFeedItem::create([
'author_id' => $this->author->id,
'contact_id' => $this->contact->id,
'action' => ContactFeedItem::ACTION_LABEL_REMOVED,
'description' => $this->label->name,
]);
$this->label->feedItem()->save($feedItem);
}
}
12 changes: 12 additions & 0 deletions domains/Vault/ManageVault/Web/Controllers/VaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace App\Vault\ManageVault\Web\Controllers;

use App\Contact\ManageContactFeed\Web\ViewHelpers\ModuleFeedViewHelper;
use App\Http\Controllers\Controller;
use App\Models\Contact;
use App\Models\ContactFeedItem;
use App\Models\Vault;
use App\Vault\ManageVault\Services\CreateVault;
use App\Vault\ManageVault\Services\DestroyVault;
Expand Down Expand Up @@ -52,11 +55,20 @@ public function show(Request $request, int $vaultId)
{
$vault = Vault::find($vaultId);

$contactIds = Contact::where('vault_id', $vaultId)->select('id')->get()->toArray();

$items = ContactFeedItem::whereIn('contact_id', $contactIds)
->with('author')
->with('contact')
->orderBy('created_at', 'desc')
->get();

return Inertia::render('Vault/Dashboard/Index', [
'layoutData' => VaultIndexViewHelper::layoutData($vault),
'lastUpdatedContacts' => VaultShowViewHelper::lastUpdatedContacts($vault),
'upcomingReminders' => VaultShowViewHelper::upcomingReminders($vault, Auth::user()),
'favorites' => VaultShowViewHelper::favorites($vault, Auth::user()),
'feed' => ModuleFeedViewHelper::data($items, Auth::user()),
'dueTasks' => VaultShowViewHelper::dueTasks($vault, Auth::user()),
]);
}
Expand Down
3 changes: 2 additions & 1 deletion lang/en/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
* MODULE: FEED
**************************************************************/

'feed_item_author_deleted' => 'Deleted user',
'feed_item_contact_created' => 'created the contact',
'feed_item_author_deleted' => 'Deleted author',
'feed_item_contact_information_updated' => 'updated the contact information',
'feed_item_important_date_created' => 'added an important date',
'feed_item_important_date_updated' => 'updated an important date',
Expand Down
8 changes: 8 additions & 0 deletions resources/js/Pages/Vault/Dashboard/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
</button>
</div>

<feed :data="feed" :contact-view-mode="false" />

<!-- feed -->
<div class="mb-10 overflow-auto">
<h3 class="mb-5 font-bold">Janvier 2010</h3>
Expand Down Expand Up @@ -119,6 +121,7 @@ import Goal from '@/Pages/Vault/Dashboard/Partials/Feed/Goal.vue';
import FeedItem from '@/Pages/Vault/Dashboard/Partials/Feed/FeedItem.vue';
import CreateLifeEvent from '@/Pages/Vault/Dashboard/Partials/Feed/CreateLifeEvent.vue';
import PrettyButton from '@/Shared/Form/PrettyButton.vue';
import Feed from '@/Shared/Modules/Feed.vue';
export default {
components: {
Expand All @@ -133,6 +136,7 @@ export default {
Goal,
FeedItem,
CreateLifeEvent,
Feed,
},
props: {
Expand All @@ -152,6 +156,10 @@ export default {
type: Object,
default: null,
},
feed: {
type: Object,
default: null,
},
dueTasks: {
type: Object,
default: null,
Expand Down
Loading

0 comments on commit 5a5c572

Please sign in to comment.