Skip to content

Commit

Permalink
fix: standardize toaster messages (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
calisio authored Jan 14, 2025
1 parent 76c3e73 commit 9b12a2f
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 126 deletions.
2 changes: 1 addition & 1 deletion frontend/src/Components/FavoriteCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const FavoriteCard: React.FC<FavoriteCardProps> = ({
: `libraries/${favorite.content_id}/favorite`;
const response = await API.put(endpoint, {});
if (response.success) {
toaster(`removed from favorites`, ToastState.success);
toaster(`Removed from favorites`, ToastState.success);
await mutate();
} else {
toaster('Failed to unfavorite', ToastState.error);
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/Components/FeatureLevelCheckboxes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ export default function FeatureLevelCheckboxes({
{}
);
if (!resp.success) {
toaster(resp.message ?? 'error toggling feature', ToastState.error);
toaster('Error toggling feature', ToastState.error);
return;
}
setCheckboxStates((prevStates) =>
prevStates.map((state) =>
state.kind === feature ? { ...state, checked } : state
)
);
toaster(resp.message, ToastState.success);
toaster('Feature toggled successfully', ToastState.success);
const user = await fetchUser();
setUser(user);
navigate('/authcallback');
Expand Down
59 changes: 28 additions & 31 deletions frontend/src/Components/LibraryCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,40 @@ export default function LibraryCard({
}) {
const { toaster } = useToast();
const [visible, setVisible] = useState<boolean>(library.visibility_status);
const [favorite, setFavorite] = useState<boolean>(library.is_favorited);
const navigate = useNavigate();
const route = useLocation();

function changeVisibility(visibilityStatus: boolean) {
if (visibilityStatus == !visible) {
setVisible(visibilityStatus);
void handleToggleVisibility();
}
}

const handleToggleVisibility = async () => {
if (!mutate) return;
const resp = await API.put<null, object>(
`libraries/${library.id}/toggle`,
{}
);
if (resp.success) {
mutate();
}
toaster(
resp.message,
resp.success ? ToastState.success : ToastState.error
);
};

async function toggleLibraryFavorite(e: MouseEvent) {
async function handleToggleAction(
action: 'favorite' | 'toggle',
e?: MouseEvent
) {
if (!mutate) return;
e.stopPropagation();
if (e) e.stopPropagation();
const actionString =
action == 'favorite'
? favorite
? role == UserRole.Student
? 'unfavorited'
: 'unfeatured'
: role == UserRole.Student
? 'favorited'
: 'featured'
: visible
? 'is now hidden'
: 'is now visible';
const resp = await API.put<null, object>(
`libraries/${library.id}/favorite`,
`libraries/${library.id}/${action}`,
{}
);
if (resp.success) {
mutate();
toaster(`Library ${actionString}`, ToastState.success);
if (action == 'favorite') setFavorite(!favorite);
else setVisible(!visible);
} else {
toaster(`Library {${actionString}}`, ToastState.error);
}
toaster(
resp.message,
resp.success ? ToastState.success : ToastState.error
);
}

return (
Expand All @@ -80,7 +75,7 @@ export default function LibraryCard({

<div
className="absolute right-2 top-2 z-100"
onClick={(e) => void toggleLibraryFavorite(e)}
onClick={(e) => void handleToggleAction('favorite', e)}
>
{!route.pathname.includes('knowledge-insights') && (
<ULIComponent
Expand Down Expand Up @@ -112,7 +107,9 @@ export default function LibraryCard({
{AdminRoles.includes(role) && (
<VisibleHiddenToggle
visible={visible}
changeVisibility={changeVisibility}
changeVisibility={() =>
void handleToggleAction('toggle')
}
/>
)}
</div>
Expand Down
27 changes: 17 additions & 10 deletions frontend/src/Components/VideoCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,33 @@ export default function VideoCard({
handleRetryVideo?: (video: Video) => Promise<void>;
}) {
const [visible, setVisible] = useState<boolean>(video.visibility_status);
const [favorite, setFavorite] = useState<boolean>(video.is_favorited);
const navigate = useNavigate();
const { toaster } = useToast();

function changeVisibility(visibilityStatus: boolean) {
if (visibilityStatus == !visible) {
setVisible(visibilityStatus);
void handleToggleAction('visibility');
}
}

const handleToggleAction = async (action: 'favorite' | 'visibility') => {
const response = await API.put<null, object>(
`videos/${video.id}/${action}`,
{}
);
const actionString =
action == 'favorite'
? favorite
? 'unfavorited'
: 'favorited'
: visible
? 'is now hidden'
: 'is now visible';
if (response.success) {
if (toaster) toaster(response.message, ToastState.success);
toaster(`Video ${actionString}`, ToastState.success);
await mutate();
if (action == 'favorite') setFavorite(!favorite);
else setVisible(!visible);
} else {
if (toaster) toaster(response.message, ToastState.error);
toaster(`Video ${actionString}`, ToastState.error);
}
};

const toMinutes = (duration: number): string => {
return `${Math.round(duration / 60)} min`;
};
Expand Down Expand Up @@ -109,7 +114,9 @@ export default function VideoCard({
(videoIsAvailable(video) ? (
<VisibleHiddenToggle
visible={visible}
changeVisibility={changeVisibility}
changeVisibility={() =>
void handleToggleAction('visibility')
}
/>
) : handleRetryVideo ? (
<div>
Expand Down
58 changes: 25 additions & 33 deletions frontend/src/Components/cards/HelpfulLinkCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,35 @@ export default function HelpfulLinkCard({
role: UserRole;
}) {
const [visible, setVisible] = useState<boolean>(link.visibility_status);
const [favorite, setFavorite] = useState<boolean>(link.is_favorited);
const { toaster } = useToast();

function changeVisibility(visibilityStatus: boolean) {
if (visibilityStatus == !visible) {
setVisible(visibilityStatus);
void handleToggleVisibility();
}
}

const handleToggleVisibility = async () => {
const handleToggleAction = async (
action: 'favorite' | 'toggle',
e?: React.MouseEvent
) => {
if (e) e.stopPropagation();
const response = await API.put<null, object>(
`helpful-links/toggle/${link.id}`,
`helpful-links/${action}/${link.id}`,
{}
);
toaster(
response.message,
response.success ? ToastState.success : ToastState.error
);
if (mutate) void mutate();
};

async function toggleLinkFavorite(e: React.MouseEvent) {
if (!mutate) {
toaster('Error updating favorite status', ToastState.error);
return;
const actionString =
action == 'favorite'
? favorite
? 'unfavorited'
: 'favorited'
: visible
? 'is now hidden'
: 'is now visible';
if (response.success) {
toaster(`Helpful link ${actionString}`, ToastState.success);
if (mutate) mutate();
if (action == 'favorite') setFavorite(!favorite);
else setVisible(!visible);
} else {
toaster(`Helpful link ${actionString}`, ToastState.error);
}
e.stopPropagation();
const resp = await API.put<null, object>(
`helpful-links/favorite/${link.id}`,
{}
);
toaster(
resp.message,
resp.success ? ToastState.success : ToastState.error
);
mutate();
}
};

async function handleHelpfulLinkClick(id: number): Promise<void> {
const resp = (await API.put<{ url: string }, object>(
Expand Down Expand Up @@ -110,7 +102,7 @@ export default function HelpfulLinkCard({
iconClassName={`absolute right-1 w-6 h-6 cursor-pointer ${link.is_favorited ? 'text-primary-yellow' : ''}`}
icon={link.is_favorited ? StarIcon : StarIconOutline}
onClick={(e) => {
if (e) void toggleLinkFavorite(e);
if (e) void handleToggleAction('favorite', e);
}}
/>
)}
Expand All @@ -125,7 +117,7 @@ export default function HelpfulLinkCard({
{AdminRoles.includes(role) && (
<VisibleHiddenToggle
visible={visible}
changeVisibility={changeVisibility}
changeVisibility={() => void handleToggleAction('toggle')}
/>
)}
</div>
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/Components/forms/AddLinkForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export default function AddLinkForm({
const onSubmit: SubmitHandler<Inputs> = async (data) => {
const response = await API.put(`helpful-links`, data);
if (response.success) {
toaster(response.message, ToastState.success);
toaster('Helpful link added successfully', ToastState.success);
onSuccess(data.title, data.url);
} else {
toaster(response.message || 'An error occurred', ToastState.error);
toaster('Error adding helpful link', ToastState.error);
}
reset();
};
Expand Down
7 changes: 2 additions & 5 deletions frontend/src/Components/forms/EditLinkForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,10 @@ export default function EditLinkForm({
const onSubmit: SubmitHandler<Inputs> = async (data) => {
const response = await API.patch(`helpful-links/${link.id}/edit`, data);
if (response.success) {
toaster(response.message, ToastState.success);
toaster('Updated helpful link successfully', ToastState.success);
onSuccess();
} else {
toaster(
response.message || 'An error occurred during update',
ToastState.error
);
toaster('Error updating helpful link', ToastState.error);
}
return;
};
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/Pages/AdminManagement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default function AdminManagement() {
: ToastState.error;
const message = response.success
? 'Administrator deleted successfully'
: response.message;
: 'Failed to delete administrator';
deleteUserModal.current?.close();
toaster(message, toastType);
resetModal();
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/Pages/FacilityManagement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ export default function FacilityManagement() {
.then((response) => {
if (response.success) {
toaster(
'Facility successfully deleted.',
'Facility successfully deleted',
ToastState.success
);
revalidate();
} else {
toaster('Error deleting Facility.', ToastState.success);
toaster('Error deleting facility', ToastState.success);
}
})
.catch(() => {
Expand Down
7 changes: 3 additions & 4 deletions frontend/src/Pages/HelpfulLinksManagement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ export default function HelpfulLinksManagement() {
const response = await API.delete(`helpful-links/${id}`);
if (response.success) {
updateLinks();
toaster('Helpful link deleted successfully', ToastState.success);
} else {
toaster('Error deleting helpful link', ToastState.error);
}
toaster(
response.message,
response.success ? ToastState.success : ToastState.error
);
deleteLinkModal.current?.close();
}

Expand Down
Loading

0 comments on commit 9b12a2f

Please sign in to comment.