Skip to content

Commit

Permalink
Merge pull request #180 from creative-commoners/pulls/4/fix-icon-no-l…
Browse files Browse the repository at this point in the history
…inktype-2

FIX Defaul link type title and icon for disabled link types
  • Loading branch information
GuySartorelli authored Jan 23, 2024
2 parents 4bc4883 + 15f02d3 commit fce5499
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion client/src/components/LinkPicker/LinkPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ const LinkPicker = ({ types, onModalSuccess, onModalClosed, canCreate }) => {
const shouldOpenModal = typeKey !== '';
const className = classnames('link-picker', 'form-control');
const typeArray = Object.values(types);
const allowedTypes = typeArray.filter(type => type.allowed);
const message = i18n._t('LinkField.CANNOT_CREATE_LINK', 'Cannot create link');

if (!canCreate || typeArray.length === 0) {
if (!canCreate || allowedTypes.length === 0) {
return (
<div className={className}>
<div className="link-picker__cannot-create">
Expand Down
4 changes: 3 additions & 1 deletion client/src/components/LinkPicker/LinkPickerMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ const LinkPickerMenu = ({ types, onSelect }) => {
{i18n._t('LinkField.ADD_LINK', 'Add Link')}
</DropdownToggle>
<DropdownMenu>
{types.map(({key, title, icon}) =>
{types.map(({key, title, icon, allowed}) => {
return allowed &&
<DropdownItem key={key} onClick={() => onSelect(key)}>
<span className={`link-picker__menu-icon ${icon}`}></span>
{title}
</DropdownItem>
}
)}
</DropdownMenu>
</Dropdown>
Expand Down
10 changes: 9 additions & 1 deletion client/src/components/LinkPicker/tests/LinkPicker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import LinkPicker from '../LinkPicker';

function makeProps(obj = {}) {
return {
types: { phone: { key: 'phone', title: 'Phone', icon: 'font-icon-phone' } },
types: { phone: { key: 'phone', title: 'Phone', icon: 'font-icon-phone', allowed: true } },
canCreate: true,
onModalSuccess: () => {},
onModalClosed: () => {},
Expand Down Expand Up @@ -38,6 +38,14 @@ test('LinkPickerMenu render() should display cannot create message if types is e
expect(container.querySelectorAll('.link-picker__cannot-create')).toHaveLength(1);
});

test('LinkPickerMenu render() should display cannot create message if type is not allowed', () => {
const { container } = render(<LinkFieldContext.Provider value={{ loading: false }}>
<LinkPicker {...makeProps({ types: { phone: { key: 'phone', title: 'Phone', icon: 'font-icon-phone', allowed: false } } })} />
</LinkFieldContext.Provider>);
expect(container.querySelectorAll('.link-picker__menu-toggle')).toHaveLength(0);
expect(container.querySelectorAll('.link-picker__cannot-create')).toHaveLength(1);
});

test('LinkPickerMenu render() should display link type icon if can create', () => {
const { container } = render(<LinkFieldContext.Provider value={{ loading: false }}>
<LinkPicker {...makeProps({ canCreate: true })} />
Expand Down
8 changes: 4 additions & 4 deletions src/Form/Traits/AllowedLinkClassesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@ public function getTypesProps(): string
{
$typesList = [];
$typeDefinitions = $this->genarateAllowedTypes();
foreach ($typeDefinitions as $key => $class) {
$allTypes = LinkTypeService::create()->generateAllLinkTypes();
foreach ($allTypes as $key => $class) {
$type = Injector::inst()->get($class);
if (!$type->canCreate()) {
continue;
}
$allowed = array_key_exists($key, $typeDefinitions) && $type->canCreate();
$typesList[$key] = [
'key' => $key,
'title' => $type->getMenuTitle(),
'handlerName' => $type->LinkTypeHandlerName(),
'priority' => $class::config()->get('menu_priority'),
'icon' => $class::config()->get('icon'),
'allowed' => $allowed,
];
}
uasort($typesList, function ($a, $b) {
Expand Down
26 changes: 23 additions & 3 deletions tests/php/Traits/AllowedLinkClassesTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ public function sortedTypesDataProvider() : array
'expected' => [
'sitetree',
'testphone',
'file',
'external',
'email',
'phone',
],
'reorder' => true,
],
Expand Down Expand Up @@ -185,10 +188,14 @@ public function testGetTypesPropsCanCreate(): void
$json = json_decode($linkField->getTypesProps(), true);
$this->assertTrue(array_key_exists('sitetree', $json));
$this->assertTrue(array_key_exists('testphone', $json));
$this->assertTrue($json['sitetree']['allowed']);
$this->assertTrue($json['testphone']['allowed']);
TestPhoneLink::$fail = 'can-create';
$json = json_decode($linkField->getTypesProps(), true);
$this->assertTrue(array_key_exists('sitetree', $json));
$this->assertFalse(array_key_exists('testphone', $json));
$this->assertTrue(array_key_exists('testphone', $json));
$this->assertTrue($json['sitetree']['allowed']);
$this->assertFalse($json['testphone']['allowed']);
}

public function typePropsDataProvider() : array
Expand All @@ -200,41 +207,47 @@ public function typePropsDataProvider() : array
'title' => 'Page on this site',
'priority' => 0,
'icon' => 'font-icon-page',
'allowed' => true,
],
'EmailLink props' => [
'class' => EmailLink::class,
'key' => 'email',
'title' => 'Link to email address',
'priority' => 30,
'icon' => 'font-icon-p-mail',
'allowed' => false,
],
'ExternalLink props' => [
'class' => ExternalLink::class,
'key' => 'external',
'title' => 'Link to external URL',
'priority' => 20,
'icon' => 'font-icon-external-link',
'allowed' => false,
],
'FileLink props' => [
'class' => FileLink::class,
'key' => 'file',
'title' => 'Link to a file',
'priority' => 10,
'icon' => 'font-icon-image',
'allowed' => true,
],
'PhoneLink props' => [
'class' => PhoneLink::class,
'key' => 'phone',
'title' => 'Phone number',
'priority' => 40,
'icon' => 'font-icon-mobile',
'allowed' => true,
],
'TestPhoneLink props' => [
'class' => TestPhoneLink::class,
'key' => 'testphone',
'title' => 'Test Phone Link',
'priority' => 100,
'icon' => 'font-icon-link',
'allowed' => false,
],
];
}
Expand All @@ -247,14 +260,21 @@ public function testGetTypesProps(
string $key,
string $title,
int $priority,
string $icon
string $icon,
bool $allowed
): void {
$linkField = LinkField::create('LinkField');
$linkField->setAllowedTypes([$class]);
if ($allowed) {
$linkField->setAllowedTypes([$class]);
} else {
$diff = array_diff($this->link_types, [$class]);
$linkField->setAllowedTypes($diff);
}
$json = json_decode($linkField->getTypesProps(), true);
$this->assertEquals($key, $json[$key]['key']);
$this->assertEquals($title, $json[$key]['title']);
$this->assertEquals($priority, $json[$key]['priority']);
$this->assertEquals($icon, $json[$key]['icon']);
$this->assertEquals($allowed, $json[$key]['allowed']);
}
}

0 comments on commit fce5499

Please sign in to comment.