Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 4x 7x 7x 7x 7x 7x 7x 6x 6x 5x 5x 4x 4x 8x | import React from 'react';
import { ComboBox, InlineNotification, SelectSkeleton } from '@carbon/react';
import { Controller, useFormContext } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { useConfig } from '@openmrs/esm-framework';
import { type ConfigObject } from '../../../config-schema';
import { type Concept } from '../../../core/api/types/concept/Concept';
import { useConcept } from '../../../stock-lookups/stock-lookups.resource';
import { OperationType } from '../../../core/api/types/stockOperation/StockOperationType';
type StockOperationReasonSelectorProps = {
stockOperationType: string;
};
const StockOperationReasonSelector: React.FC<StockOperationReasonSelectorProps> = ({ stockOperationType }) => {
const { stockAdjustmentReasonUUID, stockTakeReasonUUID } = useConfig<ConfigObject>();
const operationReason =
stockOperationType === OperationType.STOCK_TAKE_OPERATION_TYPE ? stockTakeReasonUUID : stockAdjustmentReasonUUID;
const form = useFormContext<{ reasonUuid: string }>();
const {
isLoading,
error,
items: { answers: reasons },
} = useConcept(operationReason);
const { t } = useTranslation();
if (isLoading) return <SelectSkeleton role="progressbar" />;
if (error)
return (
<InlineNotification
lowContrast
kind="error"
title={t('reasonError', 'Error loading reasons concepts')}
subtitle={error?.message}
/>
);
return (
<Controller
control={form.control}
name={'reasonUuid'}
render={({ field, fieldState: { error } }) => (
<ComboBox
readOnly={field.disabled}
titleText={t('reason', 'Reason')}
placeholder={t('chooseAReason', 'Choose a reason')}
name={'reasonUuid'}
id={'reasonUuid'}
size="lg"
items={reasons}
initialSelectedItem={reasons?.find((p) => p.uuid === field.value)}
selectedItem={reasons.find((p) => p.uuid === field.value)}
itemToString={(item?: Concept) => (item && item?.display ? `${item?.display}` : '')}
onChange={(data: { selectedItem?: Concept }) => {
field.onChange(data?.selectedItem?.uuid);
}}
ref={field.ref}
invalid={!!error?.message}
invalidText={error?.message}
/>
)}
/>
);
};
export default StockOperationReasonSelector;
|