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

(feat) O3-4066 : exclude stock issue from stock operation selector #233

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions src/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export const configSchema = {
_description: 'UUID for the stock item category',
_default: '6d24eb6e-b42f-4706-ab2d-ae4472161f6a',
},
excludeStockOperationTypes: {
_type: Type.Array,
_default: ['66666666-6666-6666-6666-666666666666'],
_description: 'Exclude stock operation types from the stock operation types selector',
},
logo: {
src: {
_type: Type.String,
Expand Down Expand Up @@ -67,4 +72,5 @@ export type ConfigObject = {
alt: string;
name: string;
};
excludeStockOperationTypes: Array<string>;
};
28 changes: 20 additions & 8 deletions src/stock-lookups/stock-lookups.resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { Patient } from '../core/api/types/identity/Patient';
import { useMemo } from 'react';
import { uniqBy } from 'lodash-es';
import { UserRoleScope } from '../core/api/types/identity/UserRoleScope';
import { StockOperationTypeResponse } from '../types';
import useSWRImmutable from 'swr/immutable';

export type PatientFilterCriteria = ResourceFilterCriteria;

Expand Down Expand Up @@ -130,19 +132,29 @@ export function useRoles(filter: ResourceFilterCriteria) {
};
}

// getStockOperationTypes
/**
* Custom hook to fetch stock operation types.
*
* @returns {Object} An object containing:
* - stockOperationTypes: Array of stock operation types
* - isLoading: Boolean indicating if the data is being fetched
* - error: Any error that occurred during fetching
*/
export function useStockOperationTypes() {
const apiUrl = `${restBaseUrl}/stockmanagement/stockoperationtype?v=default`;
const { data, isLoading, error } = useSWR<
{
data: PageableResult<StockOperationType>;
},
Error
>(apiUrl, openmrsFetch);

const { data, error, isLoading, isValidating } = useSWRImmutable<StockOperationTypeResponse, Error>(
apiUrl,
openmrsFetch,
);

const stockOperationTypes = data?.data?.results ?? [];

return {
types: data?.data || <PageableResult<StockOperationType>>{},
stockOperationTypes,
isLoading,
error,
isValidating,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ const AddStockOperation: React.FC<AddStockOperationProps> = (props) => {
const { isLoading, error, result } = useInitializeStockOperations(props);
const [operationLinks, setOperationLinks] = useState<StockOperationLinkDTO[]>();
const [manageStockItems, setManageStockItems] = useState(props?.isEditing);
const { types } = useStockOperationTypes();
const { stockOperationTypes } = useStockOperationTypes();
const [requisition, setRequisition] = useState(props?.model?.uuid);
const [manageSubmitOrComplete, setManageSubmitOrComplete] = useState(props?.isEditing);

const [requiresDispatchAcknowledgement, setRequiresDispatchAcknowledgement] = useState(false);

const currentStockOperationType = types?.results?.find((p) => p.operationType === props.model?.operationType);
const currentStockOperationType = stockOperationTypes?.find((p) => p.operationType === props.model?.operationType);

useEffect(() => {
if (
Expand Down Expand Up @@ -300,7 +300,7 @@ const AddStockOperation: React.FC<AddStockOperationProps> = (props) => {
{!item?.childVoided && (
<span className={styles.relatedLink}>
<StockOperationRelatedLink
operationTypes={types.results}
operationTypes={stockOperationTypes}
operationUuid={item?.childUuid}
operationNumber={item?.childOperationNumber}
/>
Expand All @@ -324,7 +324,7 @@ const AddStockOperation: React.FC<AddStockOperationProps> = (props) => {
{!item?.parentVoided && (
<span className={styles.relatedLink}>
<StockOperationRelatedLink
operationTypes={types.results}
operationTypes={stockOperationTypes}
operationUuid={item?.parentUuid}
operationNumber={item?.parentOperationNumber}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface StockOperationReferenceProps {

const StockOperationReference = (props: StockOperationReferenceProps) => {
const { t } = useTranslation();
const { types } = useStockOperationTypes();
const { stockOperationTypes } = useStockOperationTypes();

let model: StockOperationDTO;

Expand All @@ -21,12 +21,12 @@ const StockOperationReference = (props: StockOperationReferenceProps) => {
});

const handleEdit = () => {
const operation = types.results?.find((op) => op?.uuid === model?.operationTypeUuid);
const operation = stockOperationTypes?.find((op) => op?.uuid === model?.operationTypeUuid);

if (!operation) {
return;
}
launchAddOrEditDialog(t, model, true, operation, types?.results, false);
launchAddOrEditDialog(t, model, true, operation, stockOperationTypes, false);
};
return (
<a onClick={handleEdit} style={{ cursor: 'pointer' }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useStockOperationTypes } from '../../stock-lookups/stock-lookups.resour

const StockOperationOperationsFilter: React.FC = () => {
// get stock sources
const { types, isLoading, error } = useStockOperationTypes();
const { stockOperationTypes, isLoading, error } = useStockOperationTypes();
if (isLoading || error) {
return <DropdownSkeleton />;
}
Expand All @@ -14,7 +14,7 @@ const StockOperationOperationsFilter: React.FC = () => {
<div className={styles.filterContainer}>
<Dropdown
id="stockOperationOperationsFiter"
items={types.results}
items={stockOperationTypes}
itemToString={(item) => (item ? item.name : 'Not Set')}
type="inline"
size="sm"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,42 @@ import { ButtonSkeleton, OverflowMenu, OverflowMenuItem } from '@carbon/react';
import { OverflowMenuVertical } from '@carbon/react/icons';
import { useStockOperationTypes, useUserRoles } from '../../stock-lookups/stock-lookups.resource';
import { StockOperationType } from '../../core/api/types/stockOperation/StockOperationType';
import { useConfig } from '@openmrs/esm-framework';
import { ConfigObject } from '../../config-schema';

interface StockOperationTypesSelectorProps {
onOperationTypeSelected?: (operation: StockOperationType) => void;
onOperationLoaded?: (operation: StockOperationType[]) => void;
onOperationTypeSelected?: (selectedOperation: StockOperationType) => void;
onOperationsLoaded?: (availableOperations: StockOperationType[]) => void;
}

const StockOperationTypesSelector: React.FC<StockOperationTypesSelectorProps> = ({
onOperationTypeSelected,
onOperationLoaded,
onOperationsLoaded,
}) => {
const {
types: { results: createOperationTypes },
isLoading,
error,
} = useStockOperationTypes();
const { stockOperationTypes, isLoading, error } = useStockOperationTypes();
const { userRoles } = useUserRoles();
const { excludeStockOperationTypes } = useConfig<ConfigObject>();

const filterOperationTypes = useMemo(() => {
const applicablePrivilegeScopes = userRoles?.operationTypes?.map((p) => p.operationTypeUuid) || [];
const uniqueApplicablePrivilegeScopes = [...new Set(applicablePrivilegeScopes)];
const availableOperationTypes = useMemo(() => {
if (!stockOperationTypes || !userRoles?.operationTypes) return [];

return createOperationTypes?.filter((p) => uniqueApplicablePrivilegeScopes.includes(p.uuid)) || [];
}, [createOperationTypes, userRoles]);
const userOperationTypeUuids = new Set(userRoles.operationTypes.map((role) => role.operationTypeUuid));

return stockOperationTypes.filter(
(operationType) =>
userOperationTypeUuids.has(operationType.uuid) && !excludeStockOperationTypes.includes(operationType.uuid),
);
}, [stockOperationTypes, userRoles, excludeStockOperationTypes]);

useEffect(() => {
onOperationLoaded?.(filterOperationTypes);
}, [filterOperationTypes, onOperationLoaded]);
onOperationsLoaded?.(availableOperationTypes);
}, [availableOperationTypes, onOperationsLoaded]);

if (isLoading || error) return <ButtonSkeleton />;

return filterOperationTypes && filterOperationTypes.length ? (
if (!availableOperationTypes.length) return null;

return (
<OverflowMenu
renderIcon={() => (
<>
Expand All @@ -42,30 +47,30 @@ const StockOperationTypesSelector: React.FC<StockOperationTypesSelectorProps> =
</>
)}
menuOffset={{ right: '-100px' }}
style={{
backgroundColor: '#007d79',
backgroundImage: 'none',
color: '#fff',
minHeight: '1rem',
padding: '.95rem !important',
width: '8rem',
marginRight: '0.5rem',
whiteSpace: 'nowrap',
}}
style={menuStyle}
>
{filterOperationTypes
{availableOperationTypes
.sort((a, b) => a.name.localeCompare(b.name))
.map((operation) => (
<OverflowMenuItem
key={operation.uuid}
itemText={operation.name}
onClick={() => {
onOperationTypeSelected?.(operation);
}}
onClick={() => onOperationTypeSelected?.(operation)}
/>
))}
</OverflowMenu>
) : null;
);
};

const menuStyle = {
backgroundColor: '#007d79',
backgroundImage: 'none',
color: '#fff',
minHeight: '1rem',
padding: '.95rem !important',
width: '8rem',
marginRight: '0.5rem',
whiteSpace: 'nowrap',
} as const;

export default StockOperationTypesSelector;
6 changes: 3 additions & 3 deletions src/stock-operations/stock-operations-table.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ const StockOperations: React.FC<StockOperationsTableProps> = () => {
onOperationTypeSelected={(operation) => {
launchAddOrEditDialog(t, initialStockOperationValue(), false, operation, operations, false);
}}
onOperationLoaded={(ops) => {
onOperationsLoaded={(ops) => {
setOperations(ops);
}}
/>
Expand Down Expand Up @@ -302,11 +302,11 @@ const StockOperations: React.FC<StockOperationsTableProps> = () => {
</Tile>
</div>
) : null}
{filterApplied && isLoading && (
{filterApplied && isLoading ? (
<div className={styles.rowLoadingContainer}>
<InlineLoading description={t('loading', 'Loading...')} />
</div>
)}
) : null}
</TableContainer>
)}
></DataTable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ const AddStockUserRoleScope: React.FC<AddStockUserRoleScopeProps> = ({ model, ed
const loggedInUserUuid = currentUser?.user?.uuid;

// operation types
const {
types: { results: stockOperations },
isLoading,
} = useStockOperationTypes();
const { stockOperationTypes, isLoading } = useStockOperationTypes();

// get users
const { items: users, isLoading: loadingUsers } = useUsers({
Expand Down Expand Up @@ -133,7 +130,7 @@ const AddStockUserRoleScope: React.FC<AddStockUserRoleScopeProps> = ({ model, ed
];
setFormModel({ ...formModel, operationTypes: newOperationTypes });
} else {
const stockOperationType = stockOperations?.find((x) => x.uuid === event?.target?.value);
const stockOperationType = stockOperationTypes?.find((x) => x.uuid === event?.target?.value);
const operationType: UserRoleScopeOperationType = {
operationTypeName: stockOperationType?.name,
operationTypeUuid: stockOperationType?.uuid,
Expand Down Expand Up @@ -346,8 +343,8 @@ const AddStockUserRoleScope: React.FC<AddStockUserRoleScopeProps> = ({ model, ed
</section>
<section className={styles.section}>
<CheckboxGroup className={styles.checkboxGrid}>
{stockOperations?.length > 0 &&
stockOperations.map((type) => {
{stockOperationTypes?.length > 0 &&
stockOperationTypes.map((type) => {
return (
<div style={{ display: 'flex', flexDirection: 'row' }}>
<Checkbox
Expand Down
7 changes: 7 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { PageableResult } from '../core/api/types/PageableResult';
import { StockOperationType } from '../core/api/types/stockOperation/StockOperationType';

export interface DashboardConfig {
name: string;
slot: string;
title: string;
}

export type StockOperationTypeResponse = {
data: PageableResult<StockOperationType>;
};