|
1 | 1 | import assert from "assert"; |
2 | 2 | import { createSlice, createSelector, PayloadAction } from "@reduxjs/toolkit"; |
3 | | -import { TRemoteUserView } from "src/api/services"; |
| 3 | +import { |
| 4 | + TRemoteUserView, |
| 5 | + TRemoteCustomData, |
| 6 | + TRemoteCustomMeta, |
| 7 | +} from "src/api/services"; |
4 | 8 | import { logout } from "src/api/services/user/userEndpoints"; |
5 | 9 | import { RootState } from "src/api/store/store"; |
6 | 10 | import { |
@@ -481,6 +485,56 @@ const viewsSlice = createSlice({ |
481 | 485 | "slices::views::includeTagInViewWidget: widget tags have been updated" |
482 | 486 | ); |
483 | 487 | }, |
| 488 | + updateWidgetCustomDataMeta( |
| 489 | + draft, |
| 490 | + action: PayloadAction<{ |
| 491 | + widgetHash: string; |
| 492 | + custom_data?: TRemoteCustomData | undefined; |
| 493 | + custom_meta?: TRemoteCustomMeta | undefined; |
| 494 | + }> |
| 495 | + ) { |
| 496 | + /* eslint-disable @typescript-eslint/naming-convention */ |
| 497 | + const { widgetHash, custom_data, custom_meta } = action.payload; |
| 498 | + /* eslint-enable @typescript-eslint/naming-convention */ |
| 499 | + const selectedView = getSelectedViewRefFromDraft(draft); |
| 500 | + if (selectedView === undefined) { |
| 501 | + Logger.error( |
| 502 | + "slices::views::updateWidgetCustomDataMeta: selectedView is undefined, should never happen" |
| 503 | + ); |
| 504 | + return; |
| 505 | + } |
| 506 | + const widget = selectedView.data.widgets.find( |
| 507 | + (w) => w.hash === widgetHash |
| 508 | + ); |
| 509 | + if (widget === undefined) { |
| 510 | + Logger.error( |
| 511 | + "slices::views::updateWidgetCustomDataMeta: could not find widget. Should never happen" |
| 512 | + ); |
| 513 | + return; |
| 514 | + } |
| 515 | + if (custom_data !== undefined) { |
| 516 | + widget.widget.custom_data = custom_data; |
| 517 | + } |
| 518 | + if (custom_meta !== undefined) { |
| 519 | + widget.widget.custom_meta = custom_meta; |
| 520 | + } |
| 521 | + selectedView.lastModified = new Date().toISOString(); |
| 522 | + if ( |
| 523 | + draft.subscribedViewsCache !== undefined && |
| 524 | + draft.subscribedViewsCache[selectedView.data.id] !== undefined |
| 525 | + ) { |
| 526 | + draft.subscribedViewsCache[selectedView.data.id].lastModified = |
| 527 | + selectedView.lastModified; |
| 528 | + } else if (!selectedView.isReadOnly) { |
| 529 | + Logger.warn( |
| 530 | + "slices::views::updateWidgetCustomDataMeta: could not find subscribed view in cache" |
| 531 | + ); |
| 532 | + } |
| 533 | + Logger.debug( |
| 534 | + "slices::views::updateWidgetCustomDataMeta: widget custom_data/custom_meta updated", |
| 535 | + { widgetHash } |
| 536 | + ); |
| 537 | + }, |
484 | 538 | removeTagFromAllWidgets( |
485 | 539 | draft, |
486 | 540 | action: PayloadAction<{ tagId: number }> |
@@ -703,6 +757,7 @@ export const { |
703 | 757 | removeTagFromViewWidget, |
704 | 758 | removeTagFromAllWidgets, |
705 | 759 | includeTagInViewWidget, |
| 760 | + updateWidgetCustomDataMeta, |
706 | 761 | addWidgetsToView, |
707 | 762 | removeWidgetFromView, |
708 | 763 | updateSubscribedViewsCache, |
|
0 commit comments