Skip to content

Commit

Permalink
feat: introduce sortByCollator (monicahq/chandler#534)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored May 27, 2023
1 parent 8e39de4 commit 039f478
Show file tree
Hide file tree
Showing 44 changed files with 487 additions and 296 deletions.
5 changes: 3 additions & 2 deletions app/Actions/Jetstream/UserProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Domains\Vault\ManageVault\Web\ViewHelpers\VaultIndexViewHelper;
use Illuminate\Http\Request;
use LaravelWebauthn\Models\WebauthnKey;

class UserProfile
{
Expand All @@ -22,7 +23,7 @@ public function __invoke(Request $request, array $data): array
]);

$webauthnKeys = $request->user()->webauthnKeys
->map(fn ($key) => [
->map(fn (WebauthnKey $key) => [
'id' => $key->id,
'name' => $key->name,
'type' => $key->type,
Expand All @@ -34,7 +35,7 @@ public function __invoke(Request $request, array $data): array
$data['userTokens'] = $request->user()->userTokens()->get();
$data['webauthnKeys'] = $webauthnKeys;

$data['locales'] = collect(config('lang-detector.languages'))->map(fn ($locale) => [
$data['locales'] = collect(config('lang-detector.languages'))->map(fn (string $locale) => [
'id' => $locale,
'name' => __('auth.lang', [], $locale),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private function getPrincipalUri(): string
public function getChildren(): array
{
return collect($this->carddavBackend->getAddressBooksForUser($this->getPrincipalUri()))
->map(fn ($addressBookInfo) => new AddressBook($this->carddavBackend, $addressBookInfo))
->map(fn (array $addressBookInfo) => new AddressBook($this->carddavBackend, $addressBookInfo))
->toArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public function getObjects(?string $collectionId): Collection
}

return $vaults->get()
->map(fn ($vault) => $vault->contacts()
->map(fn (Vault $vault) => $vault->contacts()
->active()
->get()
)
Expand All @@ -272,7 +272,7 @@ public function getDeletedObjects(?string $collectionId): Collection
}

return $vaults->get()
->map(fn ($vault) => $vault->contacts()
->map(fn (Vault $vault) => $vault->contacts()
->onlyTrashed()
->get()
)
Expand Down Expand Up @@ -302,7 +302,7 @@ public function getCards($addressbookId): array
$contacts = $this->getObjects($addressbookId);

return $contacts
->map(fn ($contact) => $this->prepareCard($contact))
->map(fn (Contact $contact) => $this->prepareCard($contact))
->toArray();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ContactController extends Controller
{
public function index(Request $request, Vault $vault)
{
$contacts = Contact::where('vault_id', $vault->id)
$contacts = $vault->contacts()
->where('listed', true);

switch (Auth::user()->contact_sort_order) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace App\Domains\Contact\ManageContact\Web\ViewHelpers;

use App\Models\Gender;
use App\Models\Pronoun;
use App\Models\Template;
use App\Models\Vault;

class ContactCreateViewHelper
Expand All @@ -10,35 +13,34 @@ public static function data(Vault $vault): array
{
$account = $vault->account;

$genders = $account->genders()->orderBy('name', 'asc')->get();
$genderCollection = $genders->map(function ($gender) {
return [
$genders = $account->genders()
->get()
->sortByCollator('name')
->map(fn (Gender $gender) => [
'id' => $gender->id,
'name' => $gender->name,
];
});
]);

$pronouns = $account->pronouns()->orderBy('name', 'asc')->get();
$pronounCollection = $pronouns->map(function ($pronoun) {
return [
$pronouns = $account->pronouns()
->get()
->sortByCollator('name')
->map(fn (Pronoun $pronoun) => [
'id' => $pronoun->id,
'name' => $pronoun->name,
];
});
]);

$templates = $account->templates;
$templateCollection = $templates->map(function ($template) use ($vault) {
return [
$templates = $account->templates
->sortByCollator('name')
->map(fn (Template $template) => [
'id' => $template->id,
'name' => $template->name,
'selected' => $template->id === $vault->default_template_id,
];
});
]);

return [
'genders' => $genderCollection,
'pronouns' => $pronounCollection,
'templates' => $templateCollection,
'genders' => $genders,
'pronouns' => $pronouns,
'templates' => $templates,
'url' => [
'store' => route('contact.store', [
'vault' => $vault->id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public static function data($contacts, Vault $vault, int $labelId = null, User $
}

$labelsCollection = $vault->labels()
->orderBy('name', 'asc')
->withCount('contacts')
->get()
->sortByCollator('name')
->filter(fn (Label $label): bool => $label->contacts_count > 0)
->map(fn (Label $label) => [
'id' => $label->id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public static function data(Contact $contact, User $user): array
{
$vaultsCollection = $user->vaults()
->withCount('contacts')
->orderBy('name', 'asc')
->get()
->sortByCollator('name')
->filter(fn (Vault $vault) => $vault->id !== $contact->vault_id)
->map(fn (Vault $vault) => [
'id' => $vault->id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ModuleFeedViewHelper
{
public static function data($items, User $user, Vault $vault): array
{
$itemsCollection = $items->map(fn ($item) => [
$itemsCollection = $items->map(fn (ContactFeedItem $item) => [
'id' => $item->id,
'action' => $item->action,
'author' => self::getAuthor($item, $vault),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Contact;
use App\Models\ContactInformation;
use App\Models\ContactInformationType;
use App\Models\User;

class ModuleContactInformationViewHelper
Expand All @@ -12,21 +13,20 @@ public static function data(Contact $contact, User $user): array
{
$infos = $contact->contactInformations()
->with('contactInformationType')
->get();

$infosCollection = $infos->map(fn ($info) => self::dto($contact, $info));
->get()
->map(fn (ContactInformation $info) => self::dto($contact, $info));

$infoTypesCollection = $user->account
$infoTypes = $user->account
->contactInformationTypes()
->get()
->map(fn ($contactInformationType) => [
->map(fn (ContactInformationType $contactInformationType) => [
'id' => $contactInformationType->id,
'name' => $contactInformationType->name,
]);

return [
'contact_information' => $infosCollection,
'contact_information_types' => $infoTypesCollection,
'contact_information' => $infos,
'contact_information_types' => $infoTypes,
'url' => [
'store' => route('contact.contact_information.store', [
'vault' => $contact->vault_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class GroupIndexViewHelper
public static function data(Vault $vault): Collection
{
return $vault->groups()->with('contacts')
->orderBy('name')
->get()
->sortByCollator('name')
->map(function (Group $group) {
$contactsCollection = $group->contacts()
->get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Domains\Contact\ManageGroups\Web\ViewHelpers;

use App\Models\Contact;
use App\Models\Group;
use Illuminate\Support\Collection;

class GroupsViewHelper
Expand All @@ -14,10 +15,11 @@ class GroupsViewHelper
*/
public static function summary(Contact $contact): Collection
{
$groupsInContact = $contact->groups()->with('contacts')->orderBy('name')->get();

return $groupsInContact->map(function ($group) {
return [
return $contact->groups()
->with('contacts')
->get()
->sortByCollator('name')
->map(fn (Group $group) => [
'id' => $group->id,
'name' => $group->name,
'url' => [
Expand All @@ -26,7 +28,6 @@ public static function summary(Contact $contact): Collection
'group' => $group->id,
]),
],
];
});
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@ class ModuleGroupsViewHelper
*/
public static function data(Contact $contact): array
{
$groupsInVault = $contact->vault->groups()->with('contacts')->orderBy('name')->get();
$groupsInContact = $contact->groups()->with('contacts')->orderBy('name')->get();
$groupsInVault = $contact->vault->groups()
->with('contacts')
->get()
->sortByCollator('name');
$groupsInContact = $contact->groups()
->with('contacts')
->get()
->sortByCollator('name');

$availableGroups = $groupsInVault->diff($groupsInContact);

$availableGroupsCollection = $availableGroups->map(function ($group) use ($contact) {
return self::dto($contact, $group);
});
$availableGroupsCollection = $availableGroups->map(fn (Group $group) => self::dto($contact, $group));
$availableGroupsCollection->prepend([
'id' => 0,
'name' => trans('+ Create a group'),
'selected' => false,
]);

$groupsInContactCollection = $groupsInContact->map(function ($group) use ($contact) {
return self::dto($contact, $group);
});
$groupsInContactCollection = $groupsInContact->map(fn (Group $group) => self::dto($contact, $group));

$groupTypes = $contact->vault->account->groupTypes()->orderBy('position')->get();
$groupTypesCollection = $groupTypes->map(function ($groupType) {
return self::dtoGroupType($groupType);
});
$groupTypesCollection = $groupTypes->map(fn (GroupType $groupType) => self::dtoGroupType($groupType));

return [
'groups' => $groupsInContactCollection,
Expand All @@ -55,8 +55,8 @@ public static function data(Contact $contact): array
public static function dto(Contact $contact, Group $group, bool $taken = false): array
{
$contacts = $group->contacts()
->orderBy('first_name')
->get()
->sortByCollator('first_name')
->map(function ($contact) {
return [
'id' => $contact->id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ public static function data(Contact $contact): array

public static function list(Vault $vault, Contact $contact): Collection
{
$collection = $vault->companies()->orderBy('name', 'asc')
->get()->map(function ($company) use ($contact) {
return self::dto($company, $contact);
});

return $collection;
return $vault->companies()
->get()
->sortByCollator('name')
->map(fn (Company $company) => self::dto($company, $contact));
}

public static function dto(Company $company, Contact $contact): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public static function dtoLoan(Loan $loan, Contact $contact, User $user): array
$loaners = $loan->loaners->unique('id');
$loanees = $loan->loanees->unique('id');

$loanersCollection = $loaners->map(fn ($loaner) => ContactCardHelper::data($loaner));
$loaneesCollection = $loanees->map(fn ($loanee) => ContactCardHelper::data($loanee));
$loanersCollection = $loaners->map(fn (Contact $loaner) => ContactCardHelper::data($loaner));
$loaneesCollection = $loanees->map(fn (Contact $loanee) => ContactCardHelper::data($loanee));

$currency = optional($loan->currency)->code;

Expand Down
Loading

0 comments on commit 039f478

Please sign in to comment.