Skip to content

Commit

Permalink
feat: manage date format in user preferences (monicahq/chandler#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Feb 15, 2022
1 parent 7981beb commit b7b349c
Show file tree
Hide file tree
Showing 24 changed files with 513 additions and 45 deletions.
14 changes: 14 additions & 0 deletions app/Helpers/DateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@
namespace App\Helpers;

use Carbon\Carbon;
use App\Models\User;

class DateHelper
{
/**
* Return a date according to the timezone of the user, and the format
* stored in the preferences for this user.
*
* @param Carbon $date
* @param User $user
* @return string
*/
public static function format(Carbon $date, User $user): string
{
return $date->isoFormat($user->date_format);
}

/**
* Return a date according to the timezone of the user, in a
* short format like "Oct 29, 1981".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
namespace App\Http\Controllers\Settings\Preferences;

use Inertia\Inertia;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Services\User\StoreNameOrderPreference;
use App\Http\Controllers\Vault\ViewHelpers\VaultIndexViewHelper;
use App\Http\Controllers\Settings\Preferences\ViewHelpers\PreferencesIndexViewHelper;

Expand All @@ -19,19 +17,4 @@ public function index()
'data' => PreferencesIndexViewHelper::data(Auth::user()),
]);
}

public function store(Request $request)
{
$data = [
'account_id' => Auth::user()->account_id,
'author_id' => Auth::user()->id,
'name_order' => $request->input('nameOrder'),
];

$user = (new StoreNameOrderPreference)->execute($data);

return response()->json([
'data' => PreferencesIndexViewHelper::dtoNameOrder($user),
], 200);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Http\Controllers\Settings\Preferences;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Services\User\StoreDateFormatPreference;
use App\Http\Controllers\Settings\Preferences\ViewHelpers\PreferencesIndexViewHelper;

class PreferencesDateFormatController extends Controller
{
public function store(Request $request)
{
$data = [
'account_id' => Auth::user()->account_id,
'author_id' => Auth::user()->id,
'date_format' => $request->input('dateFormat'),
];

$user = (new StoreDateFormatPreference)->execute($data);

return response()->json([
'data' => PreferencesIndexViewHelper::dtoDateFormat($user),
], 200);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Http\Controllers\Settings\Preferences;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Services\User\StoreNameOrderPreference;
use App\Http\Controllers\Settings\Preferences\ViewHelpers\PreferencesIndexViewHelper;

class PreferencesNameOrderController extends Controller
{
public function store(Request $request)
{
$data = [
'account_id' => Auth::user()->account_id,
'author_id' => Auth::user()->id,
'name_order' => $request->input('nameOrder'),
];

$user = (new StoreNameOrderPreference)->execute($data);

return response()->json([
'data' => PreferencesIndexViewHelper::dtoNameOrder($user),
], 200);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Settings\Preferences\ViewHelpers;

use Carbon\Carbon;
use App\Models\User;
use App\Models\Contact;
use App\Helpers\NameHelper;
Expand All @@ -12,6 +13,7 @@ public static function data(User $user): array
{
return [
'name_order' => self::dtoNameOrder($user),
'date_format' => self::dtoDateFormat($user),
'url' => [
'settings' => route('settings.index'),
'back' => route('settings.index'),
Expand All @@ -35,7 +37,43 @@ public static function dtoNameOrder(User $user): array
'name_example' => $nameExample,
'name_order' => $user->name_order,
'url' => [
'store' => route('settings.preferences.store'),
'store' => route('settings.preferences.name.store'),
],
];
}

public static function dtoDateFormat(User $user): array
{
$date = Carbon::now();
$collection = collect();

$collection->push([
'id' => 1,
'format' => 'MMM DD, YYYY',
'value' => $date->isoFormat('MMM DD, YYYY'),
]);
$collection->push([
'id' => 2,
'format' => 'DD MMM YYYY',
'value' => $date->isoFormat('DD MMM YYYY'),
]);
$collection->push([
'id' => 3,
'format' => 'YYYY/MM/DD',
'value' => $date->isoFormat('YYYY/MM/DD'),
]);
$collection->push([
'id' => 4,
'format' => 'DD/MM/YYYY',
'value' => $date->isoFormat('DD/MM/YYYY'),
]);

return [
'dates' => $collection,
'date_format' => $user->date_format,
'human_date_format' => Carbon::now()->isoFormat($user->date_format),
'url' => [
'store' => route('settings.preferences.date.store'),
],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
namespace App\Http\Controllers\Vault\Contact\Modules\Note\ViewHelpers;

use App\Models\Note;
use App\Models\User;
use App\Models\Contact;
use App\Helpers\DateHelper;
use Illuminate\Support\Str;

class ModuleNotesViewHelper
{
public static function data(Contact $contact): array
public static function data(Contact $contact, User $user): array
{
$notes = $contact->notes()->orderBy('created_at', 'desc')->take(3)->get();
$notesCollection = $notes->map(function ($note) use ($contact) {
return self::dto($contact, $note);
$notesCollection = $notes->map(function ($note) use ($contact, $user) {
return self::dto($contact, $note, $user);
});
$emotions = $contact->vault->account->emotions()->get();
$emotionsCollection = $emotions->map(function ($emotion) {
Expand All @@ -40,7 +41,7 @@ public static function data(Contact $contact): array
];
}

public static function dto(Contact $contact, Note $note): array
public static function dto(Contact $contact, Note $note, User $user): array
{
return [
'id' => $note->id,
Expand All @@ -53,7 +54,7 @@ public static function dto(Contact $contact, Note $note): array
'name' => $note->emotion->name,
] : null,
'author' => $note->author ? $note->author->name : $note->author_name,
'written_at' => DateHelper::formatDate($note->created_at),
'written_at' => DateHelper::format($note->created_at, $user),
'url' => [
'update' => route('contact.note.update', [
'vault' => $contact->vault_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class NotesIndexViewHelper
{
public static function data(Contact $contact, $notes, User $user): array
{
$notesCollection = $notes->map(function ($note) use ($contact) {
return self::dto($contact, $note);
$notesCollection = $notes->map(function ($note) use ($contact, $user) {
return self::dto($contact, $note, $user);
});
$emotions = $contact->vault->account->emotions()->get();
$emotionsCollection = $emotions->map(function ($emotion) {
Expand Down Expand Up @@ -43,7 +43,7 @@ public static function data(Contact $contact, $notes, User $user): array
];
}

public static function dto(Contact $contact, Note $note): array
public static function dto(Contact $contact, Note $note, User $user): array
{
return [
'id' => $note->id,
Expand All @@ -56,7 +56,7 @@ public static function dto(Contact $contact, Note $note): array
'id' => $note->emotion->id,
'name' => $note->emotion->name,
] : null,
'written_at' => DateHelper::formatDate($note->created_at),
'written_at' => DateHelper::format($note->created_at, $user),
'url' => [
'update' => route('contact.note.update', [
'vault' => $contact->vault_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function data(Contact $contact, User $user): array
'contact_name' => ModuleContactNameViewHelper::data($contact, $user),
'template_pages' => self::getTemplatePagesList($templatePages, $contact),
'contact_information' => self::getContactInformation($templatePages, $contact, $user),
'modules' => $firstPage ? self::modules($firstPage, $contact) : [],
'modules' => $firstPage ? self::modules($firstPage, $contact, $user) : [],
'url' => [
'destroy' => route('contact.destroy', [
'vault' => $contact->vault_id,
Expand Down Expand Up @@ -104,18 +104,18 @@ private static function getContactInformation(EloquentCollection $templatePages,
/**
* Get the modules list and data in the given page.
*/
public static function modules(TemplatePage $page, Contact $contact): Collection
public static function modules(TemplatePage $page, Contact $contact, User $user): Collection
{
$modules = $page->modules()->orderBy('position', 'asc')->get();

$modulesCollection = collect();
foreach ($modules as $module) {
if ($module->type == Module::TYPE_NOTES) {
$data = ModuleNotesViewHelper::data($contact);
$data = ModuleNotesViewHelper::data($contact, $user);
}

if ($module->type == Module::TYPE_FEED) {
$data = ModuleNotesViewHelper::data($contact);
$data = ModuleNotesViewHelper::data($contact, $user);
}

$modulesCollection->push([
Expand Down
1 change: 1 addition & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class User extends Authenticatable implements MustVerifyEmail
'invitation_code',
'invitation_accepted_at',
'name_order',
'date_format',
];

/**
Expand Down
60 changes: 60 additions & 0 deletions app/Services/User/StoreDateFormatPreference.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Services\User;

use App\Models\User;
use App\Services\BaseService;
use App\Interfaces\ServiceInterface;

class StoreDateFormatPreference extends BaseService implements ServiceInterface
{
private array $data;

/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules(): array
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'author_id' => 'required|integer|exists:users,id',
'date_format' => 'required|string|max:255',
];
}

/**
* Get the permissions that apply to the user calling the service.
*
* @return array
*/
public function permissions(): array
{
return [
'author_must_belong_to_account',
];
}

/**
* Store date format preferences for the given user.
*
* @param array $data
* @return User
*/
public function execute(array $data): User
{
$this->data = $data;

$this->validateRules($data);
$this->updateUser();

return $this->author;
}

private function updateUser(): void
{
$this->author->date_format = $this->data['date_format'];
$this->author->save();
}
}
1 change: 1 addition & 0 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function definition()
'remember_token' => Str::random(10),
'is_account_administrator' => false,
'name_order' => '%first_name% %last_name%',
'date_format' => 'MMM DD, YYYY',
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function up()
$table->string('invitation_code')->nullable();
$table->dateTime('invitation_accepted_at')->nullable();
$table->string('name_order')->default('%first_name% %last_name%');
$table->string('date_format')->default('MMM DD, YYYY');
$table->rememberToken();
$table->timestamps();
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
Expand Down
4 changes: 4 additions & 0 deletions resources/js/Pages/Settings/Preferences/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
<main class="relative sm:mt-20">
<div class="mx-auto max-w-3xl px-2 py-2 sm:py-6 sm:px-6 lg:px-8">
<name-order :data="data.name_order" />

<date-format :data="data.date_format" />
</div>
</main>
</layout>
Expand All @@ -55,11 +57,13 @@
<script>
import Layout from '@/Shared/Layout';
import NameOrder from '@/Pages/Settings/Preferences/Partials/NameOrder';
import DateFormat from '@/Pages/Settings/Preferences/Partials/DateFormat';
export default {
components: {
Layout,
NameOrder,
DateFormat,
},
props: {
Expand Down
Loading

0 comments on commit b7b349c

Please sign in to comment.