Skip to content

Commit

Permalink
update CommonHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
RoieNa committed Dec 26, 2023
1 parent 684f96b commit a978e50
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 17 deletions.
8 changes: 2 additions & 6 deletions src/pages/App/handlers/CommonHandler/CommonHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,8 @@ const CommonHandler: FC = () => {
);

dispatch(commonLayoutActions.deleteCommon({ commonId }));

/**
* TODO: need to update current actions or to add a delete action in
* commonLayoutActions, multipleSpacesLayoutActions and projectsActions
* in order to update the UI.
*/
dispatch(multipleSpacesLayoutActions.deleteCommon({ commonId }));
dispatch(projectsActions.deleteCommon({ commonId }));
};

CommonEventEmitter.on(CommonEvent.CommonDeleted, handler);
Expand Down
1 change: 0 additions & 1 deletion src/store/states/commonLayout/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ export const reducer = createReducer<CommonLayoutState, Action>(initialState)
)
.handleAction(actions.deleteCommon, (state, { payload }) =>
produce(state, (nextState) => {
console.log("deleteCommon action reducer");
updateCommonOrProject(nextState, payload, true);
}),
);
4 changes: 4 additions & 0 deletions src/store/states/multipleSpacesLayout/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ export const setBackUrl = createStandardAction(
export const setMainWidth = createStandardAction(
MultipleSpacesLayoutActionType.SET_MIN_WIDTH,
)<number>();

export const deleteCommon = createStandardAction(
MultipleSpacesLayoutActionType.DELETE_COMMON,
)<{ commonId: string }>();
2 changes: 2 additions & 0 deletions src/store/states/multipleSpacesLayout/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ export enum MultipleSpacesLayoutActionType {
SET_BACK_URL = "@MULTIPLE_SPACES_LAYOUT/SET_BACK_URL",

SET_MIN_WIDTH = "@MULTIPLE_SPACES_LAYOUT/SET_MIN_WIDTH",

DELETE_COMMON = "@MULTIPLE_SPACES_LAYOUT/DELETE_COMMON",
}
22 changes: 16 additions & 6 deletions src/store/states/multipleSpacesLayout/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const initialState: MultipleSpacesLayoutState = {
const updateProjectInBreadcrumbs = (
state: WritableDraft<MultipleSpacesLayoutState>,
payload: { commonId: string } & Partial<Omit<ProjectsStateItem, "commonId">>,
removeIfExists = false,
): void => {
if (state.breadcrumbs?.type !== InboxItemType.FeedItemFollow) {
return;
Expand All @@ -27,13 +28,17 @@ const updateProjectInBreadcrumbs = (
);

if (itemIndex > -1) {
const item = state.breadcrumbs.items[itemIndex];
if (removeIfExists) {
state.breadcrumbs.items.splice(itemIndex, 1);
} else {
const item = state.breadcrumbs.items[itemIndex];

state.breadcrumbs.items[itemIndex] = {
...item,
name: payload.name ?? item.name,
image: payload.image ?? item.image,
};
state.breadcrumbs.items[itemIndex] = {
...item,
name: payload.name ?? item.name,
image: payload.image ?? item.image,
};
}
}
};

Expand Down Expand Up @@ -73,4 +78,9 @@ export const reducer = createReducer<MultipleSpacesLayoutState, Action>(
produce(state, (nextState) => {
nextState.mainWidth = payload;
}),
)
.handleAction(actions.deleteCommon, (state, { payload }) =>
produce(state, (nextState) => {
updateProjectInBreadcrumbs(nextState, payload, true);
}),
);
4 changes: 4 additions & 0 deletions src/store/states/projects/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ export const removeMembershipFromProjectAndChildren = createStandardAction(
export const setIsCommonCreationDisabled = createStandardAction(
ProjectsActionType.SET_IS_COMMON_CREATION_DISABLED,
)<boolean>();

export const deleteCommon = createStandardAction(
ProjectsActionType.DELETE_COMMON,
)<{ commonId: string }>();
2 changes: 2 additions & 0 deletions src/store/states/projects/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ export enum ProjectsActionType {
REMOVE_MEMBERSHIP_FROM_PROJECT_AND_CHILDREN = "@PROJECTS/REMOVE_MEMBERSHIP_FROM_PROJECT_AND_CHILDREN",

SET_IS_COMMON_CREATION_DISABLED = "@PROJECTS/SET_IS_COMMON_CREATION_DISABLED",

DELETE_COMMON = "@PROJECTS/DELETE_COMMON",
}
18 changes: 14 additions & 4 deletions src/store/states/projects/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@ const clearProjects = (state: WritableDraft<ProjectsState>): void => {
const updateProject = (
state: WritableDraft<ProjectsState>,
payload: { commonId: string } & Partial<Omit<ProjectsStateItem, "commonId">>,
removeIfExists = false,
): boolean => {
const itemIndex = state.data.findIndex(
(item) => item.commonId === payload.commonId,
);

if (itemIndex > -1) {
state.data[itemIndex] = {
...state.data[itemIndex],
...payload,
};
if (removeIfExists) {
state.data.splice(itemIndex, 1);
} else {
state.data[itemIndex] = {
...state.data[itemIndex],
...payload,
};
}
return true;
}

Expand Down Expand Up @@ -129,4 +134,9 @@ export const reducer = createReducer<ProjectsState, Action>(initialState)
produce(state, (nextState) => {
nextState.isCommonCreationDisabled = payload;
}),
)
.handleAction(actions.deleteCommon, (state, { payload }) =>
produce(state, (nextState) => {
updateProject(nextState, payload, true);
}),
);

0 comments on commit a978e50

Please sign in to comment.