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

Edit Page Enhancements #1319

Merged
merged 6 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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 app/components/ui/NewsArticles/NewsArticles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const NewsArticles = () => {

useEffect(() => {
const fetchBreakingNewsArticles = async () => {
const response = await dataService.get("/api/news_articles");
const response = await dataService.get("/api/news_articles?active=true");
const { news_articles }: { news_articles: NewsArticle[] } = response;
setBreakingNewsArticles(news_articles);
};
Expand Down
30 changes: 30 additions & 0 deletions app/pages/EditBreakingNewsPage.scss
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
}
}

.breaking-news-form-buttons {
display: flex;
justify-content: center;
margin: 10px 0px;
gap: 1em;
}

.breaking-news-footer {
display: flex;
justify-content: center;
Expand All @@ -47,3 +54,26 @@
.labelSubtext {
color: $color-grey5;
}

.breaking-news-status-badge {
min-width: 2em;
padding: 0.5em;
border-radius: 5%;
display: inline-block;
font-weight: bold;
}

.breaking-news-scheduled-status {
color: white;
background-color: $color-status-amber;
GeorgeCloud marked this conversation as resolved.
Show resolved Hide resolved
}

.breaking-news-active-status {
color: white;
background-color: $color-status-green;
}

.breaking-news-expired-status {
color: white;
background-color: $color-status-red;
}
105 changes: 73 additions & 32 deletions app/pages/EditBreakingNewsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,25 @@
>([]);
useEffect(() => {
dataService.get("/api/news_articles").then(({ news_articles }) => {
setBreakingNewsArticles(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
news_articles.map((article: NewsArticle) => ({
...article,
effective_date: article.effective_date
? formatDate(article.effective_date)
: "",
expiration_date: article.expiration_date
? formatDate(article.expiration_date)
: "",
}))
);
const sortedArticles = news_articles.map((article: NewsArticle) => ({
...article,
effective_date: article.effective_date
? formatDate(article.effective_date)
: "",
expiration_date: article.expiration_date
? formatDate(article.expiration_date)
: "",
}));

sortedArticles.sort((a, b) => {
const statusOrder = ["Scheduled", "Active", "Expired"];
const statusA = articleStatus(a).props.children;

Check failure on line 26 in app/pages/EditBreakingNewsPage.tsx

View workflow job for this annotation

GitHub Actions / lint

Unsafe argument of type `any` assigned to a parameter of type `NewsArticle`
Copy link
Member

Choose a reason for hiding this comment

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

Does this actually work? Even if it does, I don't think we should be trying to access the props on a nested React component like this. Instead, I'd factor this out into two separate pieces: 1) a pure function that just returns a status string, and 2) a React component that renders the badge icon.

const statusB = articleStatus(b).props.children;

Check failure on line 27 in app/pages/EditBreakingNewsPage.tsx

View workflow job for this annotation

GitHub Actions / lint

Unsafe argument of type `any` assigned to a parameter of type `NewsArticle`

return statusOrder.indexOf(statusA) - statusOrder.indexOf(statusB);

Check failure on line 29 in app/pages/EditBreakingNewsPage.tsx

View workflow job for this annotation

GitHub Actions / lint

Unsafe argument of type `any` assigned to a parameter of type `string`

Check failure on line 29 in app/pages/EditBreakingNewsPage.tsx

View workflow job for this annotation

GitHub Actions / lint

Unsafe argument of type `any` assigned to a parameter of type `string`
});

setBreakingNewsArticles(sortedArticles);

Check failure on line 32 in app/pages/EditBreakingNewsPage.tsx

View workflow job for this annotation

GitHub Actions / lint

Unsafe argument of type `any` assigned to a parameter of type `SetStateAction<NewsArticle[]>`
});
}, []);

Expand All @@ -46,29 +53,60 @@
]);
};

const onSave = (articleId: string) => {
const article = breakingNewsArticles.find(
({ id }: any) => id === articleId
const articleStatus = (article: NewsArticle): JSX.Element | null => {
Copy link
Member

Choose a reason for hiding this comment

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

Related to my other comment, to turn this into a true React component, change the argument from just a plain article to an an object with an article property. Also, the convention for React components is to capitalize the first letter of the name, since React components were originally written only as classes, before they added function-based components.

Suggested change
const articleStatus = (article: NewsArticle): JSX.Element | null => {
const ArticleStatus = ({ article}: { article: NewsArticle}) => {

(The return type can usually be omitted from React components as well)

const today = new Date();
const isEffective =
!article.effective_date || new Date(article.effective_date) <= today;
const isNotExpired =
!article.expiration_date || new Date(article.expiration_date) > today;

const getStatusClassName = () => {
if (isEffective && isNotExpired) {
return "breaking-news-active-status";
} else if (!isEffective && isNotExpired) {

Check failure on line 66 in app/pages/EditBreakingNewsPage.tsx

View workflow job for this annotation

GitHub Actions / lint

Unnecessary 'else' after 'return'
return "breaking-news-scheduled-status";
} else {
return "breaking-news-expired-status";
}
};

return (
<div className={`breaking-news-status-badge ${getStatusClassName()}`}>
{isEffective && isNotExpired

Check failure on line 75 in app/pages/EditBreakingNewsPage.tsx

View workflow job for this annotation

GitHub Actions / lint

Do not nest ternary expressions
? "Active"
: isNotExpired
? "Scheduled"
: "Expired"}
</div>
);
};

const onSave = (index: number) => {
const articleFromState = breakingNewsArticles[index];
const articleId = articleFromState.id;
const route = `/api/news_articles/${articleId ?? ""}`;

if (articleId) {
dataService.put(route, article);
dataService.put(route, articleFromState);
alert(`Article "${articleFromState.headline}" has been updated!`);

Check warning on line 91 in app/pages/EditBreakingNewsPage.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected alert
} else {
dataService.post(route, article);
dataService.post(route, articleFromState);
alert(`Article "${articleFromState.headline}" has been created!`);

Check warning on line 94 in app/pages/EditBreakingNewsPage.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected alert
}
};

const onDelete = (articleId: string, index: number) => {
const updatedBreakingNewsArticles = [...breakingNewsArticles];
updatedBreakingNewsArticles.splice(index, 1);
if (window.confirm("Are you sure you want to delete this article?")) {

Check warning on line 99 in app/pages/EditBreakingNewsPage.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected confirm
const updatedBreakingNewsArticles = [...breakingNewsArticles];
updatedBreakingNewsArticles.splice(index, 1);

if (articleId) {
dataService.APIDelete(`/api/news_articles/${articleId}`);
// Todo: Add a catch here; if this fails, front-end state will be out of sync with the actual data
}
if (articleId) {
dataService.APIDelete(`/api/news_articles/${articleId}`);
// Todo: Add a catch here; if this fails, front-end state will be out of sync with the actual data
}

setBreakingNewsArticles(updatedBreakingNewsArticles);
setBreakingNewsArticles(updatedBreakingNewsArticles);
}
};

const onFieldChange = (articleId: string, { target }: any) => {
Expand All @@ -92,15 +130,8 @@
<form className="form" key={article.id}>
<div className="form-header">
<h2>{`Breaking News Article #${index + 1}`}</h2>
<button type="button" onClick={() => onSave(article.id)}>
<RiSave3Line />
Save
</button>
<button type="button" onClick={() => onDelete(article.id, index)}>
<RiDeleteBin5Line />
Delete
</button>
</div>
<div>{articleStatus(article)}</div>
<label>
Headline
<input
Expand Down Expand Up @@ -165,6 +196,16 @@
onChange={(e) => onFieldChange(article.id, e)}
/>
</label>
<div className="breaking-news-form-buttons">
<button type="button" onClick={() => onSave(index)}>
<RiSave3Line />
Save
</button>
<button type="button" onClick={() => onDelete(article.id, index)}>
<RiDeleteBin5Line />
Delete
</button>
</div>
</form>
);

Expand Down
Loading