Skip to content

Commit

Permalink
refactor: removed old updateWidget function
Browse files Browse the repository at this point in the history
  • Loading branch information
fgarrec0397 committed Jan 3, 2023
1 parent bc09ea4 commit 2154ac0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ const styles: EditWidgetModalStyles = {

const EditWidgetModal: FC<EditWidgetModalProps> = ({ widget, iconWidth = 18 }) => {
const [displayNameValue, setDisplayNameValue] = useState("");
const { displayWidgetName, getWidgetDictionaryFromWidget, updateWidgetV2 } = useWidgets();
const { displayWidgetName, getWidgetDictionaryFromWidget, updateWidget } = useWidgets();

const onDisplayNameInputChange = ({ target }: ChangeEvent<HTMLInputElement>) => {
setDisplayNameValue(target.value);
};

const onSave = () => {
updateWidgetV2(widget.id, {
updateWidget(widget.id, {
...getWidgetDictionaryFromWidget(widget.id),
displayName: displayNameValue,
});
Expand Down
11 changes: 1 addition & 10 deletions app/src/App/Widgets/_actions/_data/hooks/useWidgetDispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
overrideWidgetDictionary,
removeBatchWidgetDictionary,
removeWidgetDictionary,
updateWidgetDictionary,
updateWidgetDictionaryV2,
} from "../state/widgetsInfoDictionaryReducer";

Expand All @@ -30,20 +29,13 @@ export default () => {
[dispatch]
);

const dispatchUpdateDictionaryV2 = useCallback(
const dispatchUpdateDictionary = useCallback(
(widgetId: string, value: Omit<WidgetObjectInfo, "id">) => {
return dispatch(updateWidgetDictionaryV2({ widgetId, value }));
},
[dispatch]
);

const dispatchUpdateDictionary = useCallback(
(widgetObjectInfo: WidgetObjectInfo) => {
return dispatch(updateWidgetDictionary(widgetObjectInfo));
},
[dispatch]
);

const dispatchRemoveWidgetDictionary = useCallback(
(widgetId: string | undefined) => {
if (widgetId) {
Expand Down Expand Up @@ -78,7 +70,6 @@ export default () => {
dispatchAddDictionary,
dispatchAddBatchDictionary,
dispatchUpdateDictionary,
dispatchUpdateDictionaryV2,
dispatchRemoveWidgetDictionary,
dispatchRemoveBatchWidgetDictionary,
dispatchOverrideWidgetDictionary,
Expand Down
22 changes: 2 additions & 20 deletions app/src/App/Widgets/_actions/_data/hooks/useWidgetsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
WidgetObjectInfo,
WidgetObjectInfoDictionary,
WidgetObjectsDictionaryItem,
WidgetOptionsValues,
WidgetProperties,
} from "../../widgetsTypes";
import useWidgetDispatch from "./useWidgetDispatch";
Expand All @@ -21,7 +20,6 @@ export default () => {
dispatchAddDictionary,
dispatchAddBatchDictionary,
dispatchUpdateDictionary,
dispatchUpdateDictionaryV2,
dispatchSetCurrentWidgetProperties,
dispatchRemoveWidgetDictionary,
dispatchOverrideWidgetDictionary,
Expand Down Expand Up @@ -53,24 +51,9 @@ export default () => {
[dispatchAddBatchDictionary, setWidgets]
);

const updateV2 = useCallback(
(widgetId: string, value: Omit<WidgetObjectInfo, "id">) => {
dispatchUpdateDictionaryV2(widgetId, value);
},
[dispatchUpdateDictionaryV2]
);

const update = useCallback(
(
widget: WidgetDictionaryItem,
widgetProperties?: WidgetProperties,
widgetOptions?: WidgetOptionsValues
) => {
dispatchUpdateDictionary({
id: widget.id,
options: widgetOptions,
properties: widgetProperties,
});
(widgetId: string, value: Omit<WidgetObjectInfo, "id">) => {
dispatchUpdateDictionary(widgetId, value);
},
[dispatchUpdateDictionary]
);
Expand Down Expand Up @@ -137,7 +120,6 @@ export default () => {
return {
add,
addBatch,
updateV2,
update,
select,
widgets,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,18 @@ export const widgetsSlice = createSlice({
) => {
const { widgetId, value } = action.payload;

state.widgetsObjectInfoDictionary[widgetId] = {
...state.widgetsObjectInfoDictionary[widgetId],
...value,
};
},
updateWidgetDictionary: (
state: WidgetObjectInfoDictionaryState,
action: PayloadAction<WidgetObjectInfo>
) => {
const { id, properties, options } = action.payload;
if (value.displayName) {
state.widgetsObjectInfoDictionary[widgetId].displayName = value.displayName;
}

if (properties) {
state.widgetsObjectInfoDictionary[id].properties = properties;
if (value.properties) {
state.widgetsObjectInfoDictionary[widgetId].properties = value.properties;
}

if (options) {
state.widgetsObjectInfoDictionary[id].options = {
...state.widgetsObjectInfoDictionary[id].options,
...options,
if (value.options) {
state.widgetsObjectInfoDictionary[widgetId].options = {
...state.widgetsObjectInfoDictionary[widgetId].options,
...value.options,
};
}
},
Expand Down Expand Up @@ -93,7 +86,6 @@ export const widgetsSlice = createSlice({
export const {
addWidgetDictionary,
addBatchWidgetDictionary,
updateWidgetDictionary,
updateWidgetDictionaryV2,
removeWidgetDictionary,
removeBatchWidgetDictionary,
Expand Down
57 changes: 10 additions & 47 deletions app/src/App/Widgets/_actions/hooks/useWidgets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from "../utilities/buildWidgetObjectInfoDictionary";
import widgetsConstants, { WidgetType } from "../widgetsConstants";
import {
SerializedWidgetObjectDictionaryItem,
WidgetDictionary,
WidgetDictionaryItem,
WidgetObjectInfo,
Expand All @@ -35,7 +34,6 @@ export default () => {
widgetsObjectInfoDictionary,
selectedWidgets,
removeSelection,
updateV2,
update,
remove,
updateCurrentProperties,
Expand Down Expand Up @@ -104,26 +102,11 @@ export default () => {
[getWidgetById]
);

const updateWidgetV2 = useCallback(
(widgetId: string, value: Omit<WidgetObjectInfo, "id">) => {
updateV2(widgetId, value);
},
[updateV2]
);

const updateWidget = useCallback(
(
widget: WidgetDictionaryItem,
widgetProperties?: WidgetProperties,
updateOnlyProperties?: boolean
) => {
if (updateOnlyProperties && widgetProperties) {
updateCurrentProperties(widgetProperties);
} else {
update(widget, widgetProperties);
}
(widgetId: string, value: Omit<WidgetObjectInfo, "id">) => {
update(widgetId, value);
},
[update, updateCurrentProperties]
[update]
);

const updateDisplayedProperties = useCallback(
Expand All @@ -133,31 +116,13 @@ export default () => {
[updateCurrentProperties]
);

const updateCurrentWidget = useCallback(
(widgetProperties: WidgetProperties, updateOnlyProperties?: boolean) => {
const currentWidget = selectedWidgets[0];
updateWidget(currentWidget, widgetProperties, updateOnlyProperties);
},
[selectedWidgets, updateWidget]
);

const updateWidgetOptions = useCallback(
(
widget: WidgetDictionaryItem | SerializedWidgetObjectDictionaryItem,
widgetOptions: WidgetOptionsValues
) => {
update(widget as WidgetObjectsDictionaryItem, undefined, widgetOptions);
},
[update]
);

const updateCurrentWidgetOptions = useCallback(
(widgetOptions: WidgetOptionsValues) => {
const currentWidget = selectedWidgets[0];

updateWidgetOptions(currentWidget, widgetOptions);
update(currentWidget.id, { options: widgetOptions });
},
[selectedWidgets, updateWidgetOptions]
[selectedWidgets, update]
);

const updateCurrentWidgetWithMesh = useCallback(
Expand All @@ -169,11 +134,11 @@ export default () => {
if (updateOnlyProperties) {
updateCurrentProperties(widgetProperties);
} else {
updateWidgetV2(currentWidget.id, { properties: widgetProperties });
updateWidget(currentWidget.id, { properties: widgetProperties });
}
}
},
[selectedWidgets, updateCurrentProperties, updateWidgetV2]
[selectedWidgets, updateCurrentProperties, updateWidget]
);

const addWidget = useCallback(
Expand Down Expand Up @@ -232,10 +197,11 @@ export default () => {
select(widgetsToSelect);

if (properties) {
updateCurrentWidget(properties, true);
updateCurrentProperties(properties);
updateWidget(widgetsToSelect[0].id, { properties });
}
},
[select, widgetsObjectInfoDictionary, updateCurrentWidget]
[widgetsObjectInfoDictionary, select, updateCurrentProperties, updateWidget]
);

const selectWidgetFromMeshArr = useCallback(
Expand Down Expand Up @@ -325,10 +291,7 @@ export default () => {
selectWidget,
selectWidgetFromMeshArr,
updateWidget,
updateWidgetV2,
updateWidgetOptions,
updateCurrentWidgetOptions,
updateCurrentWidget,
updateCurrentWidgetWithMesh,
updateDisplayedProperties,
copyWidget,
Expand Down

0 comments on commit 2154ac0

Please sign in to comment.