diff --git a/libs/ui/src/lib/historical-market-data-editor/historical-market-data-editor.component.ts b/libs/ui/src/lib/historical-market-data-editor/historical-market-data-editor.component.ts index f99c34b03..0955e0ce5 100644 --- a/libs/ui/src/lib/historical-market-data-editor/historical-market-data-editor.component.ts +++ b/libs/ui/src/lib/historical-market-data-editor/historical-market-data-editor.component.ts @@ -123,75 +123,79 @@ export class GfHistoricalMarketDataEditorComponent } public ngOnChanges() { - if (this.dateOfFirstActivity) { - let date = this.dateOfFirstActivity; - - const missingMarketData: { date: Date; marketPrice?: number }[] = []; - - if (this.historicalDataItems()?.[0]?.date) { - while ( - isBefore( - date, - parse(this.historicalDataItems()[0].date, DATE_FORMAT, new Date()) - ) - ) { - missingMarketData.push({ - date, - marketPrice: undefined - }); - - date = addDays(date, 1); - } - } + const startDate = + this.dateOfFirstActivity ?? first(this.marketData())?.date; + + if (!startDate) { + return; + } + + let date = startDate; + + const missingMarketData: { date: Date; marketPrice?: number }[] = []; - const marketDataItems = [...missingMarketData, ...this.marketData()]; + if (this.historicalDataItems()?.[0]?.date) { + while ( + isBefore( + date, + parse(this.historicalDataItems()[0].date, DATE_FORMAT, new Date()) + ) + ) { + missingMarketData.push({ + date, + marketPrice: undefined + }); - const lastDate = last(marketDataItems)?.date; - if (!lastDate || !isToday(lastDate)) { - marketDataItems.push({ date: new Date() }); + date = addDays(date, 1); } + } - this.marketDataByMonth = {}; + const marketDataItems = [...missingMarketData, ...this.marketData()]; - for (const marketDataItem of marketDataItems) { - const currentDay = parseInt(format(marketDataItem.date, 'd'), 10); - const key = format(marketDataItem.date, 'yyyy-MM'); + const lastDate = last(marketDataItems)?.date; + if (!lastDate || !isToday(lastDate)) { + marketDataItems.push({ date: new Date() }); + } - if (!this.marketDataByMonth[key]) { - this.marketDataByMonth[key] = {}; - } + this.marketDataByMonth = {}; + + for (const marketDataItem of marketDataItems) { + const currentDay = parseInt(format(marketDataItem.date, 'd'), 10); + const key = format(marketDataItem.date, 'yyyy-MM'); - this.marketDataByMonth[key][ - currentDay < 10 ? `0${currentDay}` : currentDay - ] = { - date: marketDataItem.date, - day: currentDay, - marketPrice: marketDataItem.marketPrice - }; + if (!this.marketDataByMonth[key]) { + this.marketDataByMonth[key] = {}; } - // Fill up missing months - const dates = Object.keys(this.marketDataByMonth).sort(); - const startDateString = first(dates); - const startDate = min([ - this.dateOfFirstActivity, - ...(startDateString ? [parseISO(startDateString)] : []) - ]); - const endDateString = last(dates); + this.marketDataByMonth[key][ + currentDay < 10 ? `0${currentDay}` : currentDay + ] = { + date: marketDataItem.date, + day: currentDay, + marketPrice: marketDataItem.marketPrice + }; + } - if (endDateString) { - const endDate = parseISO(endDateString); + // Fill up missing months + const dates = Object.keys(this.marketDataByMonth).sort(); + const startDateString = first(dates); + const endDateString = last(dates); - let currentDate = startDate; + if (endDateString) { + const endDate = parseISO(endDateString); - while (isBefore(currentDate, endDate)) { - const key = format(currentDate, 'yyyy-MM'); - if (!this.marketDataByMonth[key]) { - this.marketDataByMonth[key] = {}; - } + let currentDate = min([ + startDate, + ...(startDateString ? [parseISO(startDateString)] : []) + ]); - currentDate = addMonths(currentDate, 1); + while (isBefore(currentDate, endDate)) { + const key = format(currentDate, 'yyyy-MM'); + if (!this.marketDataByMonth[key]) { + this.marketDataByMonth[key] = {}; } + + currentDate = addMonths(currentDate, 1); } } }