-
-
Notifications
You must be signed in to change notification settings - Fork 37
feat(frontend): add product catalog import and invoice autofill #157
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
Open
Atharva0506
wants to merge
8
commits into
StabilityNexus:main
Choose a base branch
from
Atharva0506:feature/product-catalog-import
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
66e5b4d
feat(frontend): add product catalog import and invoice autofill
Atharva0506 443ada9
fix(catalog): address CodeRabbit codebase review feedback
Atharva0506 b5d3377
fix(catalog): address remaining CodeRabbit feedback
Atharva0506 f26a7ef
Update frontend/src/hooks/useProductCatalog.js
Atharva0506 4967852
fix(catalog): add focus management to batch invoice UI
Atharva0506 b7f28e8
Merge branch 'feature/product-catalog-import' of https://github.com/A…
Atharva0506 0db2b42
fix(useProductCatalog): remove duplicate code and fix syntax error in…
Atharva0506 222a912
fix(catalog): address PapaParse error handling and hook dependencies
Atharva0506 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| import React, { useState, useEffect, useRef, useCallback } from 'react'; | ||
| import { Input } from '@/components/ui/input'; | ||
|
|
||
| const PAGE_SIZE = 12; | ||
| const DEBOUNCE_MS = 250; | ||
| const normalizeSearchText = (text) => String(text || '').trim().toLocaleLowerCase(); | ||
|
|
||
| export default function ProductAutocompleteInput({ | ||
| value, | ||
| onChange, | ||
| onSelectProduct, | ||
| catalogMetadata, | ||
| placeholder, | ||
| className, | ||
| name, | ||
| inputRef, | ||
| }) { | ||
| const [showSuggestions, setShowSuggestions] = useState(false); | ||
| const [suggestions, setSuggestions] = useState([]); | ||
| const [activeIndex, setActiveIndex] = useState(-1); | ||
| const [visibleCount, setVisibleCount] = useState(PAGE_SIZE); | ||
| const [totalMatches, setTotalMatches] = useState(0); | ||
| const wrapperRef = useRef(null); | ||
| const internalInputRef = useRef(null); | ||
| const listRef = useRef(null); | ||
| const listId = useRef(`autocomplete-list-${Math.random().toString(36).slice(2, 9)}`); | ||
|
|
||
| const setRefs = useCallback( | ||
| (el) => { | ||
| internalInputRef.current = el; | ||
| if (typeof inputRef === 'function') { | ||
| inputRef(el); | ||
| } else if (inputRef) { | ||
| inputRef.current = el; | ||
| } | ||
| }, | ||
| [inputRef], | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| const handleClickOutside = (event) => { | ||
| if (wrapperRef.current && !wrapperRef.current.contains(event.target)) { | ||
| setShowSuggestions(false); | ||
| } | ||
| }; | ||
| document.addEventListener('mousedown', handleClickOutside); | ||
| return () => document.removeEventListener('mousedown', handleClickOutside); | ||
| }, []); | ||
|
|
||
| const catalogData = catalogMetadata?.data; | ||
| const hasCatalog = Array.isArray(catalogData) && catalogData.length > 0; | ||
|
|
||
| useEffect(() => { | ||
| if (!hasCatalog) { | ||
| setSuggestions([]); | ||
| setActiveIndex(-1); | ||
| setTotalMatches(0); | ||
| return; | ||
| } | ||
|
|
||
| const timeoutId = setTimeout(() => { | ||
| const searchTerm = normalizeSearchText(value); | ||
| const baseList = searchTerm | ||
| ? catalogData.filter((product) => { | ||
| const productName = normalizeSearchText(product.name || product.description); | ||
| return productName.includes(searchTerm); | ||
| }) | ||
| : catalogData; | ||
|
|
||
| setTotalMatches(baseList.length); | ||
| setSuggestions(baseList.slice(0, visibleCount)); | ||
| setActiveIndex(-1); | ||
| }, DEBOUNCE_MS); | ||
|
|
||
| return () => clearTimeout(timeoutId); | ||
| }, [value, visibleCount, catalogData, hasCatalog]); | ||
|
|
||
| useEffect(() => { | ||
| setVisibleCount(PAGE_SIZE); | ||
| }, [value]); | ||
|
|
||
| const scrollActiveIntoView = useCallback((index) => { | ||
| if (!listRef.current) return; | ||
| const items = listRef.current.querySelectorAll('[role="option"]'); | ||
| if (items[index]) { | ||
| items[index].scrollIntoView({ block: 'nearest' }); | ||
| } | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| scrollActiveIntoView(activeIndex); | ||
| }, [activeIndex, scrollActiveIntoView]); | ||
|
|
||
| const handleInputChange = useCallback( | ||
| (e) => { | ||
| onChange(e); | ||
| setShowSuggestions(true); | ||
| }, | ||
| [onChange], | ||
| ); | ||
|
|
||
| const handleSelect = useCallback( | ||
| (product) => { | ||
| setShowSuggestions(false); | ||
| setActiveIndex(-1); | ||
| onSelectProduct(product); | ||
| }, | ||
| [onSelectProduct], | ||
| ); | ||
|
|
||
| const handleKeyDown = useCallback( | ||
| (e) => { | ||
| if (e.key === 'Escape') { | ||
| setShowSuggestions(false); | ||
| setActiveIndex(-1); | ||
| return; | ||
| } | ||
|
|
||
| if (e.key === 'Enter' && showSuggestions) { | ||
| e.preventDefault(); | ||
| if (activeIndex >= 0 && activeIndex < suggestions.length) { | ||
| handleSelect(suggestions[activeIndex]); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (!showSuggestions || suggestions.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| if (e.key === 'ArrowDown') { | ||
| e.preventDefault(); | ||
| setActiveIndex((prev) => { | ||
| const next = prev < suggestions.length - 1 ? prev + 1 : prev; | ||
| return next; | ||
| }); | ||
| } else if (e.key === 'ArrowUp') { | ||
| e.preventDefault(); | ||
| setActiveIndex((prev) => { | ||
| const next = prev > 0 ? prev - 1 : 0; | ||
| return next; | ||
| }); | ||
| } | ||
| }, | ||
| [showSuggestions, suggestions, activeIndex, handleSelect, scrollActiveIntoView], | ||
| ); | ||
|
|
||
| const productKey = useCallback( | ||
| (product, idx) => `${normalizeSearchText(product.name || product.description)}-${product.price}-${idx}`, | ||
| [], | ||
| ); | ||
|
|
||
| const showNoResults = showSuggestions && suggestions.length === 0 && hasCatalog && value?.trim(); | ||
|
|
||
| return ( | ||
| <div className="relative w-full" ref={wrapperRef}> | ||
| <Input | ||
| type="text" | ||
| placeholder={placeholder} | ||
| className={className} | ||
| name={name} | ||
| value={value} | ||
| onChange={handleInputChange} | ||
| onFocus={() => setShowSuggestions(true)} | ||
| onKeyDown={handleKeyDown} | ||
| ref={setRefs} | ||
| autoComplete="off" | ||
| role="combobox" | ||
| aria-expanded={showSuggestions && suggestions.length > 0} | ||
| aria-controls={listId.current} | ||
| aria-activedescendant={activeIndex >= 0 ? `${listId.current}-option-${activeIndex}` : undefined} | ||
| /> | ||
| {showSuggestions && suggestions.length > 0 && ( | ||
| <ul | ||
| id={listId.current} | ||
| ref={listRef} | ||
| role="listbox" | ||
| className="absolute z-50 w-full mt-1 bg-white border border-gray-200 rounded-md shadow-lg max-h-60 overflow-auto text-sm" | ||
| > | ||
| {suggestions.map((product, idx) => ( | ||
| <li | ||
| key={productKey(product, idx)} | ||
| id={`${listId.current}-option-${idx}`} | ||
| role="option" | ||
| aria-selected={activeIndex === idx} | ||
| className={`px-4 py-2 cursor-pointer border-b border-gray-100 last:border-0 flex justify-between items-center ${activeIndex === idx ? 'bg-green-50 font-medium' : 'hover:bg-gray-50'}`} | ||
| onClick={() => handleSelect(product)} | ||
| > | ||
| <div className="font-medium text-gray-800 truncate pr-2" title={product.name || product.description}> | ||
| {product.name || product.description} | ||
| </div> | ||
| <div className="text-gray-500 font-mono shrink-0">{product.price}</div> | ||
| </li> | ||
| ))} | ||
| {totalMatches > suggestions.length && ( | ||
| <li className="px-2 py-2 bg-gray-50 border-t border-gray-100" role="presentation"> | ||
| <button | ||
| type="button" | ||
| className="w-full text-xs font-medium text-gray-700 hover:text-gray-900 py-1" | ||
| onClick={() => setVisibleCount((prev) => prev + PAGE_SIZE)} | ||
| > | ||
| Load more products ({totalMatches - suggestions.length} remaining) | ||
| </button> | ||
| </li> | ||
| )} | ||
| </ul> | ||
| )} | ||
| {showNoResults && ( | ||
| <div | ||
| role="status" | ||
| className="absolute z-50 w-full mt-1 bg-white border border-gray-200 rounded-md shadow-lg text-sm px-3 py-2 text-gray-500" | ||
| > | ||
| No matching products found | ||
| </div> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.