| [a932c9e] | 1 | import React, {useEffect, useState} from 'react';
|
|---|
| 2 | import {
|
|---|
| 3 | Dialog, DialogTitle, DialogContent, DialogActions, Button,
|
|---|
| 4 | Typography, Box, Grid, Chip, CircularProgress, IconButton,
|
|---|
| 5 | Table, TableBody, TableCell, TableContainer, TableRow, Paper
|
|---|
| 6 | } from '@mui/material';
|
|---|
| 7 | import CloseIcon from '@mui/icons-material/Close';
|
|---|
| 8 | import {onGetComponentDetails} from '../pages/+Layout.telefunc';
|
|---|
| 9 |
|
|---|
| 10 | export default function ComponentDetailsDialog({open, component, onClose}: any) {
|
|---|
| 11 | const [fullData, setFullData] = useState<any>(null);
|
|---|
| 12 | const [loading, setLoading] = useState(false);
|
|---|
| 13 |
|
|---|
| 14 | useEffect(() => {
|
|---|
| 15 | if (open && component) {
|
|---|
| 16 | setLoading(true);
|
|---|
| 17 | onGetComponentDetails({componentId: component.id})
|
|---|
| 18 | .then(data => setFullData(data))
|
|---|
| 19 | .catch(console.error)
|
|---|
| 20 | .finally(() => setLoading(false));
|
|---|
| 21 | } else {
|
|---|
| 22 | setFullData(null);
|
|---|
| 23 | }
|
|---|
| 24 | }, [open, component]);
|
|---|
| 25 |
|
|---|
| 26 | if (!open || !component) return null;
|
|---|
| 27 |
|
|---|
| 28 | const displayData = fullData || component;
|
|---|
| 29 | const specs = fullData?.details || {};
|
|---|
| 30 |
|
|---|
| 31 | const formatMoney = (amount: number) => `$${Number(amount).toFixed(2)}`;
|
|---|
| 32 |
|
|---|
| 33 | const renderValue = (key: string, val: any) => {
|
|---|
| 34 | if (Array.isArray(val)) {
|
|---|
| 35 | if (val.length > 0 && typeof val[0] === 'object') {
|
|---|
| 36 | return val.map((v: any) => v.formFactor || v.socket || JSON.stringify(v)).join(', ');
|
|---|
| 37 | }
|
|---|
| 38 | return val.join(', ');
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | const strVal = String(val);
|
|---|
| 42 | const lowerKey = key.toLowerCase();
|
|---|
| 43 |
|
|---|
| 44 | // Dodava merni edinici vo zavisnost koja komponenta e
|
|---|
| 45 | if (lowerKey.includes('capacity') || lowerKey.includes('vram') || lowerKey === 'memory') {
|
|---|
| 46 | return `${strVal} GB`;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | if (lowerKey.includes('tdp') || lowerKey.includes('wattage')) {
|
|---|
| 50 | return `${strVal} W`;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | if (lowerKey.includes('length') || lowerKey.includes('height') || lowerKey.includes('width')) {
|
|---|
| 54 | return `${strVal} mm`;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | if (lowerKey.includes('clock')) {
|
|---|
| 58 | return `${strVal} GHz`;
|
|---|
| 59 | }
|
|---|
| 60 | if (lowerKey.includes('speed')) {
|
|---|
| 61 | return `${strVal} MHz`;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | return strVal;
|
|---|
| 65 | };
|
|---|
| 66 |
|
|---|
| 67 | const formatKey = (key: string) => {
|
|---|
| 68 | return key
|
|---|
| 69 | .replace(/([A-Z])/g, ' $1')
|
|---|
| 70 | .replace(/_/g, ' ')
|
|---|
| 71 | .replace(/\b\w/g, c => c.toUpperCase());
|
|---|
| 72 | };
|
|---|
| 73 |
|
|---|
| 74 | return (
|
|---|
| 75 | <Dialog
|
|---|
| 76 | open={open}
|
|---|
| 77 | onClose={onClose}
|
|---|
| 78 | maxWidth="lg"
|
|---|
| 79 | // fullWidth
|
|---|
| 80 | sx={{zIndex: 1400}}
|
|---|
| 81 | >
|
|---|
| 82 | <DialogTitle sx={{
|
|---|
| 83 | display: 'flex',
|
|---|
| 84 | justifyContent: 'space-between',
|
|---|
| 85 | alignItems: 'center',
|
|---|
| 86 | bgcolor: '#333',
|
|---|
| 87 | color: 'white'
|
|---|
| 88 | }}>
|
|---|
| 89 | <Box>
|
|---|
| 90 | <Typography variant="caption" sx={{textTransform: 'uppercase', opacity: 0.7}}>
|
|---|
| 91 | {displayData.brand}
|
|---|
| 92 | </Typography>
|
|---|
| 93 | <Typography variant="h6" fontWeight="bold">
|
|---|
| 94 | {displayData.name}
|
|---|
| 95 | </Typography>
|
|---|
| 96 | </Box>
|
|---|
| 97 | <IconButton onClick={onClose} sx={{color: 'white'}}>
|
|---|
| 98 | <CloseIcon/>
|
|---|
| 99 | </IconButton>
|
|---|
| 100 | </DialogTitle>
|
|---|
| 101 |
|
|---|
| 102 | <DialogContent dividers>
|
|---|
| 103 | {loading ? (
|
|---|
| 104 | <Box sx={{display: 'flex', justifyContent: 'center', p: 5}}>
|
|---|
| 105 | <CircularProgress/>
|
|---|
| 106 | </Box>
|
|---|
| 107 | ) : (
|
|---|
| 108 | <Grid container spacing={4}>
|
|---|
| 109 | <Grid item xs={12} md={4} sx={{display: 'flex', flexDirection: 'column', alignItems: 'center'}}>
|
|---|
| 110 | <Box
|
|---|
| 111 | component="img"
|
|---|
| 112 | src={`https://placehold.co/400x400?text=${encodeURIComponent(displayData.name)}`}
|
|---|
| 113 | alt={displayData.name}
|
|---|
| 114 | sx={{
|
|---|
| 115 | width: '100%',
|
|---|
| 116 | maxHeight: 300,
|
|---|
| 117 | objectFit: 'contain',
|
|---|
| 118 | mb: 2,
|
|---|
| 119 | border: '1px solid #eee',
|
|---|
| 120 | borderRadius: 2,
|
|---|
| 121 | p: 2
|
|---|
| 122 | }}
|
|---|
| 123 | />
|
|---|
| 124 | <Chip
|
|---|
| 125 | label={displayData.type?.toUpperCase()}
|
|---|
| 126 | color="primary"
|
|---|
| 127 | sx={{fontWeight: 'bold'}}
|
|---|
| 128 | />
|
|---|
| 129 | </Grid>
|
|---|
| 130 |
|
|---|
| 131 | <Grid item xs={12} md={8}>
|
|---|
| 132 | <Typography variant="subtitle1" fontWeight="bold" gutterBottom
|
|---|
| 133 | sx={{borderBottom: '2px solid #ff8201', display: 'inline-block', mb: 0.5}}>
|
|---|
| 134 | Technical Specifications
|
|---|
| 135 | </Typography>
|
|---|
| 136 |
|
|---|
| 137 | <TableContainer component={Paper} variant="outlined">
|
|---|
| 138 | <Table size="small">
|
|---|
| 139 | <TableBody>
|
|---|
| 140 | <TableRow>
|
|---|
| 141 | <TableCell component="th" scope="row"
|
|---|
| 142 | sx={{fontWeight: 'bold', width: '30%', bgcolor: '#1e1e1e'}}>
|
|---|
| 143 | Brand
|
|---|
| 144 | </TableCell>
|
|---|
| 145 | <TableCell>{displayData.brand}</TableCell>
|
|---|
| 146 | </TableRow>
|
|---|
| 147 |
|
|---|
| 148 | {Object.entries(specs).map(([key, val]) => {
|
|---|
| 149 | if (key === 'componentId' || key === 'id') return null;
|
|---|
| 150 |
|
|---|
| 151 | return (
|
|---|
| 152 | <TableRow key={key}>
|
|---|
| 153 | <TableCell component="th" scope="row" sx={{
|
|---|
| 154 | fontWeight: 'bold',
|
|---|
| 155 | width: '30%',
|
|---|
| 156 | bgcolor: '#1e1e1e'
|
|---|
| 157 | }}>
|
|---|
| 158 | {formatKey(key)}
|
|---|
| 159 | </TableCell>
|
|---|
| 160 | <TableCell>{renderValue(key, val)}</TableCell>
|
|---|
| 161 | </TableRow>
|
|---|
| 162 | );
|
|---|
| 163 | })}
|
|---|
| 164 | </TableBody>
|
|---|
| 165 | </Table>
|
|---|
| 166 | </TableContainer>
|
|---|
| 167 | <Box sx={{display: 'flex', justifyContent: 'space-between', alignItems: 'center', mt: 0.5}}>
|
|---|
| 168 | <Typography variant="h4" color="primary.main" fontWeight="bold">
|
|---|
| 169 | {formatMoney(displayData.price)}
|
|---|
| 170 | </Typography>
|
|---|
| 171 | </Box>
|
|---|
| 172 | </Grid>
|
|---|
| 173 | </Grid>
|
|---|
| 174 | )}
|
|---|
| 175 | </DialogContent>
|
|---|
| 176 |
|
|---|
| 177 | <DialogActions sx={{p: 2}}>
|
|---|
| 178 | <Button onClick={onClose} variant="contained"
|
|---|
| 179 | sx={{
|
|---|
| 180 | backgroundColor: '#ff8201',
|
|---|
| 181 | color: 'white',
|
|---|
| 182 | borderColor: '#ff8201',
|
|---|
| 183 | onHover: {backgroundColor: '#ba5d02', borderColor: '#ba5d02'}
|
|---|
| 184 | }}
|
|---|
| 185 | >Close</Button>
|
|---|
| 186 | </DialogActions>
|
|---|
| 187 | </Dialog>
|
|---|
| 188 | );
|
|---|
| 189 | }
|
|---|