Skip to content

Commit

Permalink
Merge pull request #19 from catenax-ng/bugfix/bugfix_47
Browse files Browse the repository at this point in the history
Bugfix/bugfix 47
  • Loading branch information
carslen authored Sep 27, 2023
2 parents de6f257 + c4530a4 commit 861f313
Show file tree
Hide file tree
Showing 8 changed files with 433 additions and 2,096 deletions.
14 changes: 7 additions & 7 deletions DEPENDENCIES_FRONTEND
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ npm/npmjs/-/fs-monkey/1.0.4, Unlicense AND (ISC AND MIT), approved, #2964
npm/npmjs/-/fs.realpath/1.0.0, ISC, approved, clearlydefined
npm/npmjs/-/fsevents/2.3.2, MIT, approved, #2967
npm/npmjs/-/function-bind/1.1.1, MIT, approved, clearlydefined
npm/npmjs/-/function.prototype.name/1.1.5, MIT, approved, clearlydefined
npm/npmjs/-/function.prototype.name/1.1.5, MIT, approved, #10255
npm/npmjs/-/functional-red-black-tree/1.0.1, MIT, approved, clearlydefined
npm/npmjs/-/functions-have-names/1.2.3, MIT, approved, clearlydefined
npm/npmjs/-/gensync/1.0.0-beta.2, MIT, approved, clearlydefined
Expand Down Expand Up @@ -896,8 +896,8 @@ npm/npmjs/-/react-native/0.72.3, MIT, approved, clearlydefined
npm/npmjs/-/react-popper/2.3.0, MIT, approved, clearlydefined
npm/npmjs/-/react-refresh/0.11.0, MIT, approved, clearlydefined
npm/npmjs/-/react-refresh/0.4.3, MIT, approved, clearlydefined
npm/npmjs/-/react-router-dom/6.14.1, MIT, approved, #9432
npm/npmjs/-/react-router/6.14.1, MIT, approved, clearlydefined
npm/npmjs/-/react-router-dom/6.15.0, MIT, restricted, clearlydefined
npm/npmjs/-/react-router/6.15.0, MIT, approved, clearlydefined
npm/npmjs/-/react-scripts/5.0.1, MIT, approved, clearlydefined
npm/npmjs/-/react-shallow-renderer/16.15.0, MIT, approved, clearlydefined
npm/npmjs/-/react-transition-group/4.4.5, BSD-3-Clause, approved, CQ22955
Expand Down Expand Up @@ -949,7 +949,7 @@ npm/npmjs/-/rimraf/3.0.2, ISC, approved, clearlydefined
npm/npmjs/-/rollup-plugin-terser/7.0.2, MIT, approved, clearlydefined
npm/npmjs/-/rollup/2.79.1, MIT, approved, clearlydefined
npm/npmjs/-/run-parallel/1.2.0, MIT, approved, clearlydefined
npm/npmjs/-/safe-array-concat/1.0.0, MIT, approved, clearlydefined
npm/npmjs/-/safe-array-concat/1.0.0, MIT, approved, #10335
npm/npmjs/-/safe-buffer/5.1.2, MIT, approved, clearlydefined
npm/npmjs/-/safe-buffer/5.2.1, MIT, approved, clearlydefined
npm/npmjs/-/safe-regex-test/1.0.0, MIT, approved, clearlydefined
Expand Down Expand Up @@ -1025,7 +1025,7 @@ npm/npmjs/-/string-natural-compare/3.0.1, MIT, approved, clearlydefined
npm/npmjs/-/string-width/3.1.0, MIT, approved, clearlydefined
npm/npmjs/-/string-width/4.2.3, MIT, approved, clearlydefined
npm/npmjs/-/string.prototype.matchall/4.0.8, MIT, approved, #4571
npm/npmjs/-/string.prototype.trim/1.2.7, MIT, approved, clearlydefined
npm/npmjs/-/string.prototype.trim/1.2.7, MIT, approved, #10361
npm/npmjs/-/string.prototype.trimend/1.0.6, MIT, approved, #4564
npm/npmjs/-/string.prototype.trimstart/1.0.6, MIT, approved, #4647
npm/npmjs/-/string_decoder/1.1.1, MIT, approved, clearlydefined
Expand All @@ -1052,7 +1052,7 @@ npm/npmjs/-/supports-color/8.1.1, MIT, approved, clearlydefined
npm/npmjs/-/supports-hyperlinks/2.3.0, MIT, approved, clearlydefined
npm/npmjs/-/supports-preserve-symlinks-flag/1.0.0, MIT, approved, clearlydefined
npm/npmjs/-/svg-parser/2.0.4, MIT, approved, clearlydefined
npm/npmjs/-/svgo/1.3.2, MIT, approved, clearlydefined
npm/npmjs/-/svgo/1.3.2, MIT AND Apache-2.0, approved, #10499
npm/npmjs/-/svgo/2.8.0, MIT AND (0BSD AND BSD-2-Clause AND MIT) AND Apache-2.0, approved, #2989
npm/npmjs/-/symbol-tree/3.2.4, MIT, approved, clearlydefined
npm/npmjs/-/table/6.8.1, BSD-3-Clause, approved, #4596
Expand Down Expand Up @@ -1446,7 +1446,7 @@ npm/npmjs/@react-native/gradle-plugin/0.72.11, MIT, approved, clearlydefined
npm/npmjs/@react-native/js-polyfills/0.72.1, MIT, approved, clearlydefined
npm/npmjs/@react-native/normalize-colors/0.72.0, MIT, approved, clearlydefined
npm/npmjs/@react-native/virtualized-lists/0.72.6, MIT, approved, #9854
npm/npmjs/@remix-run/router/1.7.1, MIT, approved, clearlydefined
npm/npmjs/@remix-run/router/1.8.0, MIT, approved, clearlydefined
npm/npmjs/@restart/hooks/0.4.9, MIT, approved, #7049
npm/npmjs/@restart/ui/1.6.6, MIT, approved, clearlydefined
npm/npmjs/@rollup/plugin-babel/5.3.1, MIT, approved, clearlydefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,9 @@

package org.eclipse.tractusx.demandcapacitymgmt.demandcapacitymgmtbackend.entities;

import java.time.LocalDateTime;
import java.time.LocalDate;
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -54,7 +47,7 @@ public class DemandSeriesValues {
private DemandSeries demandSeries;

@Column(name = "calendar_week", nullable = false)
private LocalDateTime calendarWeek;
private LocalDate calendarWeek;

@Column(name = "demand", nullable = false)
private Double demand;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import eclipse.tractusx.demand_capacity_mgmt_specification.model.*;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -303,7 +304,7 @@ private List<DemandSeriesValues> enrichDemandSeriesValues(List<MaterialDemandSer
DemandSeriesValues
.builder()
.demand(materialDemandSeriesValue.getDemand().doubleValue())
.calendarWeek(DataConverterUtil.convertFromString(materialDemandSeriesValue.getCalendarWeek()))
.calendarWeek(LocalDate.parse(materialDemandSeriesValue.getCalendarWeek()))
.build()
)
.toList();
Expand Down
2 changes: 2 additions & 0 deletions demand-capacity-mgmt-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"bootstrap": "^5.3.0",
"date-fns": "^2.30.0",
"http-proxy-middleware": "^2.0.6",
"moment": "^2.29.4",
"moment-weekday-calc": "^1.1.4",
"prop-types": "^15.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
153 changes: 68 additions & 85 deletions demand-capacity-mgmt-frontend/src/components/WeeklyView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,73 +26,95 @@ import { DemandCategoryContext } from '../contexts/DemandCategoryProvider';
import { Demand, DemandCategory, DemandProp, DemandSeriesValue, MaterialDemandSery } from '../interfaces/demand_interfaces';
import { Button, ButtonGroup, ToggleButton, OverlayTrigger, Tooltip } from 'react-bootstrap';
import { DemandContext } from '../contexts/DemandContextProvider';
import moment from 'moment';
import 'moment-weekday-calc';

import {getISOWeek, startOfMonth, addDays, format, addWeeks, addMonths} from 'date-fns';

import {getISOWeek, format,} from 'date-fns';

interface WeeklyViewProps {
demandData: DemandProp;
}

function getISOWeekMonday(year: number, isoWeek: number): Date {
const january4 = new Date(year, 0, 4);
const diff = (isoWeek - 1) * 7 + (1 - january4.getDay());
return addDays(january4, diff);
function getISOWeekMonday(year: number, isoWeek: number): moment.Moment {
return moment().year(year).isoWeek(isoWeek).startOf('isoWeek');
}

function getWeeksInMonth(year: number, monthIndex: number): number[] {
const firstDayOfMonth = startOfMonth(new Date(year, monthIndex));
const nextMonth = startOfMonth(addMonths(firstDayOfMonth, 1));
function getYearOfWeek(date: moment.Moment): number {
return date.add(3, 'days').year();
}

let weeks = [];
let currentDay = firstDayOfMonth;

while (currentDay < nextMonth) {
weeks.push(getISOWeek(currentDay));
currentDay = addWeeks(currentDay, 1);
function getWeeksInMonth(year: number, monthIndex: number, knownNextMonthWeeks?: Set<number>): number[] {
const weeks: Set<number> = new Set();

const firstDayOfMonth = moment().year(year).month(monthIndex).startOf('month');
const lastDayOfMonth = moment().year(year).month(monthIndex).endOf('month');
// Fetch weeks of the next month if not provided.
if (!knownNextMonthWeeks && monthIndex < 11) {
knownNextMonthWeeks = new Set(getWeeksInMonth(year, monthIndex + 1));
}

return weeks;
let currentDay = firstDayOfMonth;
while (currentDay <= lastDayOfMonth) {
const weekNum = currentDay.week();
const isoWeekYear = getYearOfWeek(currentDay);

// If the month is January and the week year is the previous year, skip it
if (monthIndex === 0 && isoWeekYear < year) {
currentDay = currentDay.add(1, 'days');
continue;
}

// If it's the last week of the month and it's also in the next month, skip it.
if (currentDay.isAfter(moment(new Date(year, monthIndex, 24))) && knownNextMonthWeeks?.has(weekNum)) {
currentDay = currentDay.add(1, 'days');
continue;
}

weeks.add(weekNum);
currentDay = currentDay.add(1, 'days');
}
return Array.from(weeks).sort((a, b) => a - b);
}



const WeeklyView: React.FC<WeeklyViewProps> = ({ demandData }) => {
const { updateDemand } = useContext(DemandContext)!;
const { demandcategories } = useContext(DemandCategoryContext) || {};
const { demandcategories } = useContext(DemandCategoryContext) ?? {};
const currentYear = new Date().getFullYear();

const [editMode, setEditMode] = useState(false);

const monthsCurrentYear = Array.from({ length: 12 }, (_, monthIndex) => {
const monthStart = new Date(currentYear, monthIndex, 1);
const monthName = format(monthStart, 'MMM');
const weeks = getWeeksInMonth(currentYear, monthIndex);

const monthsPreviousYear = Array.from({ length: 1 }, (_, monthIndex) => {
const monthStart = new Date(currentYear - 1, monthIndex + 11, 1);
const monthName = format(monthStart, 'MMM'); // <-- This line
let weeks = getWeeksInMonth(currentYear - 1, monthIndex + 11);
return {
name: monthName,
year: currentYear,
year: currentYear - 1,
weeks: weeks,
monthIndex: monthIndex,
monthIndex: monthIndex + 11,
};
});

const monthsPreviousYear = Array.from({ length: 1 }, (_, monthIndex) => {
const monthStart = new Date(currentYear - 1, monthIndex + 11, 1);
const monthsCurrentYear = Array.from({ length: 12 }, (_, monthIndex) => {
const monthStart = new Date(currentYear, monthIndex, 1);
const monthName = format(monthStart, 'MMM');
const weeks = getWeeksInMonth(currentYear - 1, monthIndex + 11);

let weeks = getWeeksInMonth(currentYear, monthIndex);
return {
name: monthName,
year: currentYear - 1,
year: currentYear,
weeks: weeks,
monthIndex: monthIndex + 11,
monthIndex: monthIndex,
};
});

const monthsNextYear = Array.from({ length: 1 }, (_, monthIndex) => {
const monthStart = new Date(currentYear + 1, monthIndex, 1);
const monthName = format(monthStart, 'MMM');
const weeks = getWeeksInMonth(currentYear + 1, monthIndex);

return {
name: monthName,
year: currentYear + 1,
Expand All @@ -118,30 +140,33 @@ const WeeklyView: React.FC<WeeklyViewProps> = ({ demandData }) => {
});
}

console.log(demandValuesMap);
// Function to get the beginning and end dates of the week
const getWeekDates = (year: number, month: string,week: number) => {
const getWeekDates = (year: number, month: string, week: number) => {
const startDate = getISOWeekMonday(year, week);

const endDate = new Date(startDate);
endDate.setDate(endDate.getDate() + 6); // Assuming weeks end on Saturdays
const endDate = startDate.clone().add(6, 'days'); // Instead of using native Date() methods

return {
startDate: startDate.toDateString(),
endDate: endDate.toDateString(),
startDate: startDate.format('YYYY-MM-DD'),
endDate: endDate.format('YYYY-MM-DD'),
};
};




useEffect(() => {
console.log("aqui")
const newDemandValuesMap: DemandValuesMap = {};

demandData.demandSeries?.forEach((series) => {
const categoryId = series.demandCategory.id;

series.demandSeriesValues.forEach((value) => {
const date = new Date(value.calendarWeek);
const year = date.getFullYear();
const week = getISOWeek(date).toString();
const date = moment(value.calendarWeek);
const year = moment().year();
const week = date.week();
console.log(week);

if (!newDemandValuesMap[categoryId]) {
newDemandValuesMap[categoryId] = {};
Expand All @@ -153,7 +178,6 @@ const WeeklyView: React.FC<WeeklyViewProps> = ({ demandData }) => {
});
});

console.log('New Demand Values Map:', newDemandValuesMap);
setDemandValuesMap(newDemandValuesMap);
}, [demandData]);

Expand All @@ -173,11 +197,12 @@ const WeeklyView: React.FC<WeeklyViewProps> = ({ demandData }) => {
monthsCurrentYear.forEach((month) => {
month.weeks.forEach((week) => {
const isoWeekMonday = getISOWeekMonday(month.year, week);
isoWeekMonday.format('YYYY-MM-dd')
// Get the Monday of the ISO week
const demand = demandValuesMap[categoryId]?.[month.year]?.[week];
if (demand !== undefined) {
demandSeriesValues.push({
calendarWeek: format(isoWeekMonday, 'yyyy-MM-dd'),
calendarWeek: isoWeekMonday.format('YYYY-MM-DD'),
demand: demand,
});
}
Expand Down Expand Up @@ -215,7 +240,7 @@ const WeeklyView: React.FC<WeeklyViewProps> = ({ demandData }) => {
};

// Perform save operation with updatedDemandData
console.log(updatedDemand);

if (filteredUpdatedDemandSeries.length > 0) {
try {
await updateDemand(updatedDemand);
Expand Down Expand Up @@ -323,47 +348,7 @@ const WeeklyView: React.FC<WeeklyViewProps> = ({ demandData }) => {
</tr>
<tr>
<th className="empty-header-cell"></th>
{monthsPreviousYear.map((month) =>
month.weeks.map((week) => (
<th key={month.name + week} className="header-cell week-header-cell">
<OverlayTrigger
placement="top"
overlay={
<Tooltip id={`week-tooltip-${month.year}-${week}`}>
{`Week ${week} - ${getWeekDates(month.year, month.name, week).startDate} to ${getWeekDates(
month.year,
month.name,
week
).endDate}`}
</Tooltip>
}
>
<span>{week}</span>
</OverlayTrigger>
</th>
))
)}
{monthsCurrentYear.map((month) =>
month.weeks.map((week) => (
<th key={month.name + week} className="header-cell week-header-cell">
<OverlayTrigger
placement="top"
overlay={
<Tooltip id={`week-tooltip-${month.year}-${week}`}>
{`Week ${week} - ${getWeekDates(month.year, month.name, week).startDate} to ${getWeekDates(
month.year,
month.name,
week
).endDate}`}
</Tooltip>
}
>
<span>{week}</span>
</OverlayTrigger>
</th>
))
)}
{monthsNextYear.map((month) =>
{[monthsPreviousYear, monthsCurrentYear, monthsNextYear].reduce((acc, curr) => acc.concat(curr), []).map((month) =>
month.weeks.map((week) => (
<th key={month.name + week} className="header-cell week-header-cell">
<OverlayTrigger
Expand All @@ -384,9 +369,7 @@ const WeeklyView: React.FC<WeeklyViewProps> = ({ demandData }) => {
))
)}
</tr>
{demandcategories &&
demandcategories
.sort((a, b) => a.id.localeCompare(b.id))
{demandcategories?.sort((a, b) => a.id.localeCompare(b.id))
.map((category: DemandCategory) => (
<tr key={category.id}>
<th className="sticky-header-cell">
Expand Down
Loading

0 comments on commit 861f313

Please sign in to comment.