Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Ellison committed Nov 5, 2023
1 parent d1b9a19 commit 2c89cf4
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 68 deletions.
30 changes: 17 additions & 13 deletions components/resourcing/DemandTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ function Resources({ role, resources }) {
useEffect(() => {
const fetchData = async () => {
// console.log('Resource:useEffect')
// console.log('Resource:role.code: ', role.Code)
console.log('Resource:role: ', role)
try {
const response = await fetch('/api/resourcing/placeholder?code=' + role.Code);
const response = await fetch('/api/resourcing/placeholder?code=' + role.Code + '&role_id=' + String(role.role_id));
if (!response.ok) throw new Error('Network response was not ok');
const fetchedData = await response.json();
// console.log('Resource:response: ', response)
Expand All @@ -67,7 +67,7 @@ function Resources({ role, resources }) {
// console.log('Resource:jsonParsedData: ', jsonParsedData)

setResource(jsonParsedData); // Adjust according to actual API response
console.log('Resource:jsonParsedData: ', jsonParsedData)
// console.log('Resource:jsonParsedData: ', jsonParsedData)


} catch (err) {
Expand All @@ -76,12 +76,12 @@ function Resources({ role, resources }) {
};

fetchData();
}, [ role, showPopup]);
}, [role, showPopup]);

const handleDelete = async (event) => {
console.debug('Resource:handleDelete: ', event)
try {
const response = await fetch(`/api/resourcing/placeholder?code=${event.Code}`, {
const response = await fetch(`/api/resourcing/placeholder?code=${event.Code}&id=${event.role_id}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
Expand All @@ -101,7 +101,8 @@ function Resources({ role, resources }) {
}

return (
<><Stack direction="row" spacing={1} alignItems="center">
<><Stack direction="row" spacing={1} justifyContent="flex-end"
alignItems="center">

{resource && <Chip variant="outlined" label={resource.displayName} icon={<PersonIcon />} onDelete={() => handleDelete(role)} />}
<IconButton
Expand Down Expand Up @@ -201,6 +202,8 @@ export function DemandTable({ users }) {
Code: item.Code,
Description: item.Description,
role: roleDetail.role,
role_id: roleDetail.role_id,
grade: roleDetail.grade,
monthlyDetails: { [month]: roleDetail }
});
}
Expand Down Expand Up @@ -252,7 +255,7 @@ export function DemandTable({ users }) {
<TableRow>
<TableCell>Customer
<Select
value='{}'
value=''
onChange={(e) => setCustomerFilter(e.target.value)}
displayEmpty
size="small"
Expand All @@ -274,7 +277,6 @@ export function DemandTable({ users }) {
}}
>
<MenuItem value="">
<em>None</em>
</MenuItem>
{Array.from(new Set(data?.map(item => item.Customer) || []))
.sort() // This will sort the array alphabetically
Expand All @@ -287,10 +289,10 @@ export function DemandTable({ users }) {
</TableCell>
<TableCell>Description</TableCell>
<TableCell>Role</TableCell>
<TableCell>Proposed Resource</TableCell>
<TableCell>Resource</TableCell>

{displayedMonths.map(month => (
<TableCell key={month}>{new Date(month).toLocaleString('default', { month: 'long' })}</TableCell>
<TableCell align="center" key={month}>{new Date(month).toLocaleString('default', { month: 'long' })}</TableCell>
))}
</TableRow>
</TableHead>
Expand All @@ -299,19 +301,21 @@ export function DemandTable({ users }) {
<TableRow key={index}>
<TableCell>{item.Customer}</TableCell>
<TableCell>{item.Description}</TableCell>
<TableCell>{item.role}</TableCell>
<TableCell>{item.role} {item.grade !== 'N/A' ? `[${item.grade}]` : ''}</TableCell>
<TableCell>
<Resources role={item} resources={resources} />
</TableCell>
{displayedMonths.map(month => (
<TableCell key={month} style={{ backgroundColor: (item.monthlyDetails && item.monthlyDetails[month] && item.monthlyDetails[month].days_allocated) ? 'blue' : 'transparent' }} >
<TableCell key={month}
// style={{ backgroundColor: (item.monthlyDetails && item.monthlyDetails[month] && item.monthlyDetails[month].days_allocated) ? 'blue' : 'transparent' }}
>
<div style={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
height: '100%'
}}>
{item.monthlyDetails && item.monthlyDetails[month] && <Chip color="primary" label={item.monthlyDetails[month].days_allocated} />}
{item.monthlyDetails && item.monthlyDetails[month] && <Chip sx={{ minWidth: '100px' }} color="primary" label={item.monthlyDetails[month].days_allocated} />}
</div>
</TableCell>
))}
Expand Down
167 changes: 141 additions & 26 deletions components/resourcing/ResourceTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,41 @@ import {
DialogContent,
DialogTitle,
Button,
Skeleton
Skeleton,
Stack
} from '@mui/material';

import ChevronLeftIcon from '@mui/icons-material/ChevronLeft';
import ChevronRightIcon from '@mui/icons-material/ChevronRight';
import WorkOutlineIcon from '@mui/icons-material/WorkOutline';

function determineColor(daysAllocated, daysHypo) {
const hypo = parseInt(daysHypo, 10);
if (daysAllocated > hypo + 3) return 'red';
if (daysAllocated < hypo - 1) return 'blue';
return 'green';
if (daysAllocated > hypo + 3) return 'error';
if (daysAllocated < hypo - 1) return 'primary';
return 'success';
}

function Row({ data, displayedMonths, setPopupContent, setShowPopup }) {
console.debug('Row: ', data)
function Row({ data, displayedMonths, setPopupContent, setShowPopup, placeholder }) {
// console.debug('Row: ', data)
if (placeholder) {
console.debug('Placeholder: ', placeholder)
}
// {
// "name": "PRISACARIU, Vlad",
// "resource_bu": "arwuk_cons",
// "nobody": "N",
// "jobs": [],
// "displayName": "Vlad Prisacariu",
// "givenName": "Vlad",
// "surname": "Prisacariu",
// "jobTitle": "Graduate Engineer",
// "mail": "[email protected]",
// "officeLocation": "Sheffield",
// "department": "Engineering",
// "managerEmail": "[email protected]"
// }

return (
<TableRow>
<TableCell style={{ whiteSpace: 'nowrap', width: 'max-content' }}>
Expand All @@ -43,22 +63,50 @@ function Row({ data, displayedMonths, setPopupContent, setShowPopup }) {

{displayedMonths.map(month => (
<TableCell
align="center"
size="small"
sx={{ width: '100px' }}
key={month}
style={{
backgroundColor: data.jobs.some(item => item.month === month) ?
determineColor(data.jobs.find(item => item.month === month).days_allocated,
data.jobs.find(item => item.month === month).days_hypo)
: 'transparent'
}}
// style={{
// backgroundColor: data.jobs.some(item => item.month === month) ?
// determineColor(data.jobs.find(item => item.month === month).days_allocated,
// data.jobs.find(item => item.month === month).days_hypo)
// : 'transparent'
// }}
onClick={() => {
const monthData = data.jobs.find(item => item.month === month);
setPopupContent(monthData ? monthData.jobs : null);
setShowPopup(true);
}}
>
{data.jobs.some(item => item.month === month) &&
<Chip sx={{ color: 'white' }} variant="outlined" color="info" label={data.jobs.find(item => item.month === month).jobs.reduce((sum, job) => sum + parseInt(job.days, 10), 0)} />
}
><Stack direction="row" spacing={1} justifyContent="center"
alignItems="center">
{data.jobs.some(item => item.month === month) &&

<Chip
sx={{
color: 'white',
minWidth: (placeholder?.monthlyDetails?.[month]?.days_allocated > 0) ? '50px' : '100px',
}}
color={
data.jobs.some(item => item.month === month) ?
determineColor(data.jobs.find(item => item.month === month).days_allocated,
data.jobs.find(item => item.month === month).days_hypo)
: 'transparent'
} label={data.jobs.find(item => item.month === month).jobs.reduce((sum, job) => sum + parseInt(job.days, 10), 0)} />
}
{
placeholder &&
placeholder.monthlyDetails &&
placeholder.monthlyDetails[month] && (
// <WorkOutlineIcon />
<Chip
icon={<WorkOutlineIcon />}
sx={{ color: 'primary' }}
label={placeholder.monthlyDetails[month].days_allocated}
/>
)
}
</Stack>
</TableCell>
))}
</TableRow>
Expand Down Expand Up @@ -90,7 +138,7 @@ const ResourceTableSkeleton = () => {
);
};

export function ResourceTable({bench = false}) {
export function ResourceTable({ bench = false }) {
const [data, setData] = useState(null);
const [filteredData, setFilteredData] = useState([]);
const [disciplineFilter, setDisciplineFilter] = useState('');
Expand All @@ -99,6 +147,7 @@ export function ResourceTable({bench = false}) {
const [popupContent, setPopupContent] = useState(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState(null);
const [placeholder, setPlaceholder] = useState(null);
const [months, setMonths] = useState([]);
// const [months, setMonths] = useState([]);

Expand All @@ -120,26 +169,84 @@ export function ResourceTable({bench = false}) {
setIsLoading(false);
}
};
const fetchPlaceholderData = async () => {

try {
const response = await fetch('/api/resourcing/placeholder');
if (!response.ok) throw new Error('Network response was not ok');
const fetchedData = await response.json();
console.debug('Resource:placeholder: ', fetchedData.content)

// const jsonParsedData = JSON.parse(fetchedData.content.length > 0 ? fetchedData.content : null)
setPlaceholder(groupByMailAndSumDays(fetchedData.content)); // Adjust according to actual API response
// console.debug('Resource:placeholder: ', jsonParsedData)

} catch (err) {
console.error('Resource:ERROR: ', err)
}
};

fetchData();
fetchPlaceholderData();
}, []);

function groupByMailAndSumDays(data) {
const groupedData = {};

data.forEach(item => {
if (!groupedData[item.resource]) {
groupedData[item.resource] = { displayName: item.displayName, monthlyDetails: {} };
Object.keys(item.monthlyDetails).forEach(month => {
groupedData[item.resource].monthlyDetails[month] = { days_allocated: item.monthlyDetails[month].days_allocated };
})
} else {
Object.keys(item.monthlyDetails).forEach(month => {
if (!groupedData[item.resource].monthlyDetails[month]) {
groupedData[item.resource].monthlyDetails[month] = { days_allocated: item.monthlyDetails[month].days_allocated };
} else {
groupedData[item.resource].monthlyDetails[month].days_allocated += item.monthlyDetails[month].days_allocated;
}
});
}
});

return groupedData;
}


useEffect(() => {
const sortUsersByName = (users) => {
return users
.filter(user => user.displayName != null)
.sort((a, b) => a.displayName.localeCompare(b.displayName));
};
console.debug(data)

const filterRowsWithNoJobsInDisplayedMonths = (users) => {
return users.filter(user =>
displayedMonths.some(month =>
!user.jobs.some(job => job.month === month)
)
);
};

// console.debug(data)
if (data && !isLoading) {
let usersToDisplay = sortUsersByName(data);

if (disciplineFilter) {
setFilteredData(sortUsersByName(data.filter(item => item.department === disciplineFilter)));
} else {
setFilteredData(sortUsersByName(data));
usersToDisplay = usersToDisplay.filter(item => item.department === disciplineFilter);
}

if (bench) { // Only apply this filter if bench prop is true
usersToDisplay = filterRowsWithNoJobsInDisplayedMonths(usersToDisplay);
}

setFilteredData(usersToDisplay);
}
}, [disciplineFilter, data]);
}, [disciplineFilter, data, bench, displayedMonths]);


// console.log('placeholder: ', placeholder)
// Ensure we only proceed if data is an array
// const isDataArray = Array.isArray(data);
// const months = isDataArray ? Array.from(new Set(data.flatMap(item => item.booked.map(b => b.month)))).sort() : [];
Expand Down Expand Up @@ -185,7 +292,7 @@ export function ResourceTable({bench = false}) {
<TableCell>Name</TableCell>
<TableCell>Discipline
<Select
value='{}'
value=''
onChange={(e) => setDisciplineFilter(e.target.value)}
displayEmpty
size="small"
Expand All @@ -207,7 +314,7 @@ export function ResourceTable({bench = false}) {
}}
>
<MenuItem value="">
<em>None</em>

</MenuItem>
{Array.from(new Set(data?.map(item => item.department) || []))
.sort() // This will sort the array alphabetically
Expand All @@ -219,7 +326,7 @@ export function ResourceTable({bench = false}) {

</Select>
</TableCell>
{displayedMonths.map((month, index) => (<TableCell key={month} sx={{ pl: index === 1 ? '0px' : '8px' }}>
{displayedMonths.map((month, index) => (<TableCell align='center' key={month} sx={{ mx: '5px', width: '120px', pl: index === 0 ? '0px' : '5px', pr: index === 2 ? '0px' : '5px' }}>
{index === 0 && (
<IconButton
// sx={{ position: 'absolute', right: 8, top: 8 }}
Expand Down Expand Up @@ -247,7 +354,15 @@ export function ResourceTable({bench = false}) {
</TableHead>
<TableBody>
{filteredData.map(item => (
<Row key={item.name} data={item} displayedMonths={displayedMonths} setPopupContent={setPopupContent} setShowPopup={setShowPopup} />
<Row
key={item.name}
data={item}
displayedMonths={displayedMonths}
setPopupContent={setPopupContent}
setShowPopup={setShowPopup}
placeholder={placeholder && placeholder[item.mail] ? placeholder[item.mail] : null}
/>

))}
</TableBody>
</Table>
Expand Down
Loading

0 comments on commit 2c89cf4

Please sign in to comment.