Skip to content

Commit

Permalink
adds better error handling on part searches
Browse files Browse the repository at this point in the history
  • Loading branch information
replaysMike committed Mar 13, 2024
1 parent 15df07b commit a423424
Show file tree
Hide file tree
Showing 7 changed files with 1,053 additions and 919 deletions.
3 changes: 2 additions & 1 deletion Binner/Binner.Web/ClientApp/src/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ section.formHeader {
}

.page-banner {
height: 30px;
min-height: 30px;
}

.page-notice {
Expand Down Expand Up @@ -648,6 +648,7 @@ section.formHeader {
cursor: pointer;
width: 100%;
max-height: 28px;
margin-bottom: 1px;
}

.page-error div {
Expand Down
32 changes: 15 additions & 17 deletions Binner/Binner.Web/ClientApp/src/pages/Inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export function Inventory(props) {
const [loadingPartTypes, setLoadingPartTypes] = useState(true);
const [loadingRecent, setLoadingRecent] = useState(true);
const [partMetadataIsSubscribed, setPartMetadataIsSubscribed] = useState(false);
const [partMetadataError, setPartMetadataError] = useState(null);
const [partMetadataErrors, setPartMetadataErrors] = useState([]);
const [saveMessage, setSaveMessage] = useState("");
const [bulkScanIsOpen, setBulkScanIsOpen] = useState(false);
const [partExistsInInventory, setPartExistsInInventory] = useState(false);
Expand All @@ -139,7 +139,7 @@ export function Inventory(props) {
setIsEditing(newIsEditing);
const fetchData = async () => {
setPartMetadataIsSubscribed(false);
setPartMetadataError(null);
setPartMetadataErrors([]);
await fetchPartTypes();
await fetchRecentRows();
if (partNumberStr) {
Expand Down Expand Up @@ -179,7 +179,7 @@ export function Inventory(props) {
Inventory.infoAbortController = new AbortController();
setLoadingPartMetadata(true);
setPartMetadataIsSubscribed(false);
setPartMetadataError(null);
setPartMetadataErrors([]);
try {
const includeInventorySearch = !pageHasParameters;
const { data, existsInInventory } = await doFetchPartMetadata(input, localPart, includeInventorySearch);
Expand Down Expand Up @@ -328,11 +328,7 @@ export function Inventory(props) {
if (!data) return;

if (data.errors && data.errors.length > 0) {
setPartMetadataError(`Error: [${data.apiName}] ${data.errors.join()}`);
setMetadataParts([]);
setInfoResponse({});
setLoadingPartMetadata(false);
return;
setPartMetadataErrors(data.errors);
}

let metadataParts = [];
Expand Down Expand Up @@ -489,7 +485,7 @@ export function Inventory(props) {
// barcode found
if (cleanPartNumber) {
setPartMetadataIsSubscribed(false);
setPartMetadataError(null);
setPartMetadataErrors([]);
if (!isEditing) setPartFromMetadata(metadataParts, { ...partInfo, quantity: partInfo.quantityAvailable });
if (viewPreferences.rememberLast) updateViewPreferences({ lastQuantity: partInfo.quantityAvailable });

Expand All @@ -501,7 +497,7 @@ export function Inventory(props) {
// no barcode info found, try searching the part number
if (cleanPartNumber) {
setPartMetadataIsSubscribed(false);
setPartMetadataError(null);
setPartMetadataErrors([]);
let newQuantity = parseInt(input.value?.quantity) || DefaultQuantity;
if (isNaN(newQuantity)) newQuantity = 1;
const newPart = {
Expand Down Expand Up @@ -780,7 +776,7 @@ export function Inventory(props) {
//e.preventDefault();
//e.stopPropagation();
setPartMetadataIsSubscribed(false);
setPartMetadataError(null);
setPartMetadataErrors([]);
let searchPartNumber = control.value;

if (searchPartNumber && searchPartNumber.length >= MinSearchKeywordLength) {
Expand All @@ -806,7 +802,7 @@ export function Inventory(props) {
e.preventDefault();
e.stopPropagation();
setPartMetadataIsSubscribed(false);
setPartMetadataError(null);
setPartMetadataErrors([]);
const updatedPart = { ...part };

updatedPart[control.name] = control.value;
Expand Down Expand Up @@ -985,10 +981,12 @@ export function Inventory(props) {
</div>
</div>
)}
{partMetadataError && (
<div className="page-error" onClick={() => setPartMetadataError(null)}>
<Icon name="close" /> {partMetadataError}
</div>
{partMetadataErrors?.length > 0 && (
partMetadataErrors.map((error, key) => (
<div key={key} className="page-error" onClick={() => setPartMetadataErrors(_.filter(partMetadataErrors, i => i !== error))}>
<Icon name="close" /> {error}
</div>
))
)}
</div>

Expand Down Expand Up @@ -1421,7 +1419,7 @@ export function Inventory(props) {
</Dimmer.Dimmable>

</>);
}, [inputPartNumber, part, viewPreferences.rememberLast, loadingPart, loadingPartMetadata, partMetadataError, isEditing, allPartTypes, isDirty, handlePartTypeChange]);
}, [inputPartNumber, part, viewPreferences.rememberLast, loadingPart, loadingPartMetadata, partMetadataErrors, isEditing, allPartTypes, isDirty, handlePartTypeChange]);

return (
<div className="inventory mask">
Expand Down
35 changes: 21 additions & 14 deletions Binner/Binner.Web/Controllers/PartController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,24 +450,31 @@ public async Task<IActionResult> GetLowStockAsync([FromQuery] PaginatedRequest r
[HttpGet("info")]
public async Task<IActionResult> GetPartInfoAsync([FromQuery] string partNumber, [FromQuery] string partTypeId = "", [FromQuery] string mountingTypeId = "", [FromQuery] string supplierPartNumbers = "")
{
var partType = partTypeId;
var mountingType = mountingTypeId;
if (int.TryParse(partTypeId, out var parsedPartTypeId))
{
var partTypeWithName = await _partTypeService.GetPartTypeAsync(parsedPartTypeId);
if (partTypeWithName != null) partType = partTypeWithName.Name;
}
if (int.TryParse(mountingTypeId, out var parsedMountingTypeId))
try
{
if (Enum.IsDefined(typeof(MountingType), parsedMountingTypeId))
var partType = partTypeId;
var mountingType = mountingTypeId;
if (int.TryParse(partTypeId, out var parsedPartTypeId))
{
var mountingTypeEnum = (MountingType)parsedMountingTypeId;
mountingType = mountingTypeEnum.ToString();
var partTypeWithName = await _partTypeService.GetPartTypeAsync(parsedPartTypeId);
if (partTypeWithName != null) partType = partTypeWithName.Name;
}
if (int.TryParse(mountingTypeId, out var parsedMountingTypeId))
{
if (Enum.IsDefined(typeof(MountingType), parsedMountingTypeId))
{
var mountingTypeEnum = (MountingType)parsedMountingTypeId;
mountingType = mountingTypeEnum.ToString();
}
}
}

var metadata = await _partService.GetPartInformationAsync(partNumber, partType ?? string.Empty, mountingType, supplierPartNumbers);
return Ok(metadata);
var metadata = await _partService.GetPartInformationAsync(partNumber, partType ?? string.Empty, mountingType, supplierPartNumbers);
return Ok(metadata);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, new ExceptionResponse("Failed to fetch part information! ", ex));
}
}

/// <summary>
Expand Down
Loading

0 comments on commit a423424

Please sign in to comment.