Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Use getStatusFlags() instead of hardcoded statuses #351

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/dist/styles/bundle.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion client/src/components/LinkField/LinkField.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ const LinkField = ({
title={linkData.title}
description={linkData.description}
versionState={linkData.versionState}
statusFlags={linkData.statusFlags}
typeTitle={type.title || ''}
typeIcon={type.icon}
onDelete={handleDelete}
Expand Down Expand Up @@ -511,7 +512,7 @@ const LinkField = ({
.then(async () => {
onChange(newLinkIDs);
actions.toasts.success(i18n._t('LinkField.SORT_SUCCESS', 'Updated link sort order'));
// Force a rerender so that links are retched so that versionState badges are up to date
// Force a rerender so that links are retched so that status flag badges are up to date
setForceFetch(forceFetch + 1);
})
.catch(() => {
Expand Down
6 changes: 4 additions & 2 deletions client/src/components/LinkPicker/LinkPicker.scss
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,11 @@
width: 100%;

.badge {
color: #cf3f00;
background-color: #fff2ea;
Comment on lines -201 to -202
Copy link
Member Author

@GuySartorelli GuySartorelli Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rely on styling in admin for colours

padding: 2px 3px 2px 4px;

&:not(:last-child) {
margin-right: 5px;
}
Comment on lines +203 to +205
Copy link
Member Author

@GuySartorelli GuySartorelli Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a gap between badges - this value matches the right margin on the title text

}
}

Expand Down
30 changes: 17 additions & 13 deletions client/src/components/LinkPicker/LinkPickerTitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,30 @@ const stopPropagation = (fn) => (e) => {
fn && fn();
}

const getVersionedBadge = (versionState) => {
let title = '';
let label = ''
if (versionState === versionStates.draft) {
title = i18n._t('LinkField.LINK_DRAFT_TITLE', 'Link has draft changes');
label = i18n._t('LinkField.LINK_DRAFT_LABEL', 'Draft');
} else if (versionState === versionStates.modified) {
title = i18n._t('LinkField.LINK_MODIFIED_TITLE', 'Link has unpublished changes');
label = i18n._t('LinkField.LINK_MODIFIED_LABEL', 'Modified');
} else {
const renderStatusFlagBadges = (statusFlags) => {
if (!statusFlags) {
return null;
}
const className = classnames('badge', `status-${versionState}`);
return <span className={className} title={title}>{label}</span>;
const badges = [];
for (let [cssClasses, data] of Object.entries(statusFlags)) {
cssClasses = `badge status-${cssClasses}`;
if (typeof data === 'string') {
data = {text: data};
}
if (!data.title) {
data.title = '';
}
badges.push(<span key={cssClasses} className={cssClasses} title={data.title}>{data.text}</span>);
}
return badges;
};

const LinkPickerTitle = ({
id,
title,
description,
versionState,
statusFlags,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this prop being passed in? There's no corresponding PHP change in this PR?

Copy link
Member Author

@GuySartorelli GuySartorelli Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typeTitle,
typeIcon,
onDelete,
Expand Down Expand Up @@ -132,7 +135,7 @@ const LinkPickerTitle = ({
<div className="link-picker__link-detail">
<div className="link-picker__title">
<span className="link-picker__title-text">{title}</span>
{getVersionedBadge(versionState)}
{renderStatusFlagBadges(statusFlags)}
</div>
{typeTitle && (
<small className="link-picker__type">
Expand Down Expand Up @@ -175,6 +178,7 @@ LinkPickerTitle.propTypes = {
title: PropTypes.string,
description: PropTypes.string,
versionState: PropTypes.oneOf(Object.values(versionStates)),
statusFlags: PropTypes.object,
typeTitle: PropTypes.string.isRequired,
typeIcon: PropTypes.string.isRequired,
onDelete: PropTypes.func.isRequired,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function makeProps(obj = {}) {
title: 'My title',
description: 'My description',
versionState: 'draft',
statusFlags: { draft: 'Draft' },
typeTitle: 'Phone',
typeIcon: 'font-icon-phone',
canDelete: true,
Expand Down
3 changes: 2 additions & 1 deletion src/Models/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ public function getData(): array
'description' => $this->getDescription(),
'canDelete' => $this->canDelete(),
'versionState' => $this->getVersionedState(),
'statusFlags' => $this->getStatusFlags(),
'typeKey' => $typeKey,
'sort' => $this->Sort,
];
Expand All @@ -253,7 +254,7 @@ public function forTemplate(): string
{
// First look for a subclass of the email template e.g. EmailLink.ss which may be defined
// in a project. Fallback to using the generic Link.ss template which this module provides
return $this->renderWith([static::class, Link::class]);
return (string) $this->renderWith([static::class, Link::class]);
Comment on lines -256 to +257
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated change - my IDE was upset that the return type was string but this returns DBHtmlText - probably just because we're using strict mode here when we don't use it anywhere else.

}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/php/Controllers/LinkFieldControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,12 @@ public static function provideLinkData(): array
'versionState' => 'draft',
'typeKey' => 'testphone',
'sort' => 1,
'statusFlags' => [
'addedtodraft' => [
'text' => 'Draft',
'title' => 'Item has not been published yet',
],
],
],
],
'Reject invalid ID' => [
Expand Down
Loading