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

Add Image validator helper func #471

Closed
wants to merge 6 commits into from
Closed
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
37 changes: 37 additions & 0 deletions src/custom/Helpers/UrlValidator/image-url-validation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
type DispatchImageFunction = (url: string) => void;
export const validateImageUrl = async (
url: string,
dispatchImage: DispatchImageFunction
): Promise<boolean> => {
// Basic URL verification
if (!isUrlValid(url)) {
return false;
}

try {
const img = new Image();
img.src = url;

await new Promise<void>((resolve, reject) => {
img.onload = () => resolve();
img.onerror = () => reject();
});

// Dispatch action after image is successfully loaded
dispatchImage(url);

return true; // Image loaded successfully
} catch (error) {
return false; // Image failed to load
}
};

// Function to verify if URL is valid
const isUrlValid = (url: string): boolean => {
try {
new URL(url);
return true;
} catch (error) {
return false;
}
};
3 changes: 3 additions & 0 deletions src/custom/Helpers/UrlValidator/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { validateImageUrl } from './image-url-validation';

export { validateImageUrl };
6 changes: 6 additions & 0 deletions src/custom/Helpers/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,9 @@ To use these helper components in your project, follow these steps:
</button>
);
```

3. **ImageUrlValidator**: A custom React hook for validating image URLs.

- **File**: `ImageUrlValidator`
- **Usage**: Provides the `validateImageUrl` hook, which allows you to validate image URLs.
- **Returns**: An object containing the validation state and a function for updating the validation state.
2 changes: 2 additions & 0 deletions src/custom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { FlipCard } from './FlipCard';
import { useWindowDimensions } from './Helpers/Dimension';
import { useNotificationHandler } from './Helpers/Notification';
import { validateImageUrl } from './Helpers/UrlValidator';
import PopperListener, { IPopperListener } from './PopperListener';
import ResponsiveDataTable from './ResponsiveDataTable';
import SearchBar, { SearchBarProps } from './SearchBar';
Expand All @@ -36,6 +37,7 @@ export {
UniversalFilter,
useNotificationHandler,
useWindowDimensions,
validateImageUrl,
withErrorBoundary,
withSuppressedErrorBoundary
};
Expand Down
Loading