|
| 1 | +import React from 'react'; |
| 2 | +import { |
| 3 | + BasicSpinner, |
| 4 | + InfoTooltipIcon, |
| 5 | + InputWithLabelRow, |
| 6 | + Typography, |
| 7 | +} from '@common/components'; |
| 8 | +import { useTranslation } from '@common/intl'; |
| 9 | +import { ArrayUtils, Box, PropertyInput } from '@openmsupply-client/common'; |
| 10 | +import { DraftAsset } from '../../types'; |
| 11 | +import { useAssets } from '../../api'; |
| 12 | + |
| 13 | +interface DetailsProps { |
| 14 | + draft?: DraftAsset; |
| 15 | + onChange: (patch: Partial<DraftAsset>) => void; |
| 16 | +} |
| 17 | + |
| 18 | +const Container = ({ children }: { children: React.ReactNode }) => ( |
| 19 | + <Box display="flex" flexDirection="column" alignContent="center" padding={4}> |
| 20 | + {children} |
| 21 | + </Box> |
| 22 | +); |
| 23 | + |
| 24 | +const Section = ({ |
| 25 | + children, |
| 26 | + heading, |
| 27 | +}: { |
| 28 | + children: React.ReactNode; |
| 29 | + heading: string; |
| 30 | +}) => ( |
| 31 | + <Box |
| 32 | + display="flex" |
| 33 | + flexDirection="column" |
| 34 | + padding={2} |
| 35 | + paddingRight={4} |
| 36 | + sx={{ maxWidth: '600px', width: '100%' }} |
| 37 | + > |
| 38 | + <Heading>{heading}</Heading> |
| 39 | + {children} |
| 40 | + </Box> |
| 41 | +); |
| 42 | + |
| 43 | +const Heading = ({ children }: { children: React.ReactNode }) => ( |
| 44 | + <Typography |
| 45 | + sx={{ |
| 46 | + marginLeft: '158px', |
| 47 | + fontSize: '20px', |
| 48 | + fontWeight: 'bold', |
| 49 | + }} |
| 50 | + > |
| 51 | + {children} |
| 52 | + </Typography> |
| 53 | +); |
| 54 | + |
| 55 | +const Row = ({ |
| 56 | + children, |
| 57 | + tooltip, |
| 58 | + label, |
| 59 | +}: { |
| 60 | + children: React.ReactNode; |
| 61 | + tooltip?: string; |
| 62 | + label: string; |
| 63 | +}) => ( |
| 64 | + <Box paddingTop={1.5}> |
| 65 | + <InputWithLabelRow |
| 66 | + labelWidth="300px" |
| 67 | + label={label} |
| 68 | + labelProps={{ |
| 69 | + sx: { |
| 70 | + fontSize: '16px', |
| 71 | + paddingRight: 2, |
| 72 | + textAlign: 'right', |
| 73 | + }, |
| 74 | + }} |
| 75 | + Input={ |
| 76 | + <> |
| 77 | + <Box sx={{}} flex={1}> |
| 78 | + {children}{' '} |
| 79 | + </Box> |
| 80 | + <Box> |
| 81 | + {tooltip && ( |
| 82 | + <InfoTooltipIcon |
| 83 | + iconSx={{ color: 'gray.main' }} |
| 84 | + title={tooltip} |
| 85 | + /> |
| 86 | + )} |
| 87 | + </Box> |
| 88 | + </> |
| 89 | + } |
| 90 | + /> |
| 91 | + </Box> |
| 92 | +); |
| 93 | + |
| 94 | +export const Details = ({ draft, onChange }: DetailsProps) => { |
| 95 | + const t = useTranslation('coldchain'); |
| 96 | + |
| 97 | + const { data: assetProperties, isLoading } = useAssets.properties.list({ |
| 98 | + assetCategoryId: { equalAnyOrNull: [draft?.assetCategory?.id ?? ''] }, |
| 99 | + assetClassId: { equalAnyOrNull: [draft?.assetClass?.id ?? ''] }, |
| 100 | + assetTypeId: { equalAnyOrNull: [draft?.assetType?.id ?? ''] }, |
| 101 | + }); |
| 102 | + |
| 103 | + if (!draft) return null; |
| 104 | + |
| 105 | + return ( |
| 106 | + <Box display="flex" flex={3} justifyContent={'center'}> |
| 107 | + <Container> |
| 108 | + {isLoading ? <BasicSpinner /> : null} |
| 109 | + <Section heading={t('label.asset-properties')}> |
| 110 | + {!draft.parsedProperties ? ( |
| 111 | + <Typography sx={{ textAlign: 'center' }}> |
| 112 | + {t('messages.no-properties')} |
| 113 | + </Typography> |
| 114 | + ) : ( |
| 115 | + <> |
| 116 | + {assetProperties && |
| 117 | + ArrayUtils.uniqBy(assetProperties, 'key').map(property => { |
| 118 | + const isCatalogue = |
| 119 | + draft.parsedCatalogProperties?.hasOwnProperty( |
| 120 | + property.key |
| 121 | + ) ?? false; |
| 122 | + const value = |
| 123 | + draft.parsedCatalogProperties?.[property.key] ?? |
| 124 | + draft.parsedProperties?.[property.key] ?? |
| 125 | + null; |
| 126 | + |
| 127 | + return ( |
| 128 | + <Row |
| 129 | + key={property.key} |
| 130 | + label={property.name} |
| 131 | + tooltip={ |
| 132 | + isCatalogue |
| 133 | + ? t('messages.catalogue-property') |
| 134 | + : undefined |
| 135 | + } |
| 136 | + > |
| 137 | + <PropertyInput |
| 138 | + valueType={property.valueType} |
| 139 | + allowedValues={property.allowedValues?.split(',')} |
| 140 | + value={value} |
| 141 | + onChange={v => |
| 142 | + onChange({ |
| 143 | + parsedProperties: { |
| 144 | + ...draft.parsedProperties, |
| 145 | + [property.key]: v ?? null, |
| 146 | + }, |
| 147 | + }) |
| 148 | + } |
| 149 | + disabled={isCatalogue} |
| 150 | + /> |
| 151 | + </Row> |
| 152 | + ); |
| 153 | + })} |
| 154 | + </> |
| 155 | + )} |
| 156 | + </Section> |
| 157 | + </Container> |
| 158 | + </Box> |
| 159 | + ); |
| 160 | +}; |
0 commit comments