Skip to content

Commit

Permalink
allow adding multiple ids to detail and use in editor (#1880)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ptrhnk authored Nov 2, 2023
1 parent ac10181 commit b22476f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
31 changes: 31 additions & 0 deletions packages/client/src/hooks/useParamsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const INITIAL_CONTEXT = {
selectedDetailId: "",
setSelectedDetailId: UNINITIALISED,
appendDetailId: UNINITIALISED,
appendMultipleDetailIds: UNINITIALISED,
removeDetailId: UNINITIALISED,
clearAllDetailIds: UNINITIALISED,
cleanAllParams: UNINITIALISED,
Expand All @@ -36,6 +37,7 @@ interface SearchParamsContext {
selectedDetailId: string;
setSelectedDetailId: (id: string) => void;
appendDetailId: (id: string) => void;
appendMultipleDetailIds: (ids: string[]) => void;
removeDetailId: (id: string) => void;
clearAllDetailIds: () => void;
cleanAllParams: () => void;
Expand Down Expand Up @@ -114,6 +116,34 @@ export const SearchParamsProvider = ({
setTimeout(() => setSelectedDetailId(id), 100);
};

const appendMultipleDetailIds = (ids: string[]) => {
const detailIdArray = getDetailIdArray();
let newDetailIdArray: string[] = [];

if (ids.length === 10) {
// use as it is
newDetailIdArray = ids;
} else if (ids.length > 10) {
// cut and use the first 10
newDetailIdArray = ids.slice(0, maxTabCount);
} else {
// merge with existing
// remove already added ids and add them at the end
const filteredArray: string[] = detailIdArray.filter(
(id) => !ids.includes(id)
);
newDetailIdArray = filteredArray.concat(ids);
if (newDetailIdArray.length > maxTabCount) {
newDetailIdArray = newDetailIdArray.slice(
newDetailIdArray.length - maxTabCount
);
}
}

setDetailId(newDetailIdArray.join(arrJoinChar));
setTimeout(() => setSelectedDetailId(ids[0]), 100);
};

const removeDetailId = (id: string) => {
const detailIdArray = getDetailIdArray();
const index = detailIdArray.indexOf(id);
Expand Down Expand Up @@ -228,6 +258,7 @@ export const SearchParamsProvider = ({
selectedDetailId,
setSelectedDetailId,
appendDetailId,
appendMultipleDetailIds,
removeDetailId,
clearAllDetailIds,
cleanAllParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,13 @@ export const StatementEditor: React.FC<StatementEditor> = ({
updateStatementDataMutation,
moveStatementMutation,
}) => {
const { statementId, territoryId, setTerritoryId, appendDetailId } =
useSearchParams();
const {
statementId,
territoryId,
setTerritoryId,
appendDetailId,
appendMultipleDetailIds,
} = useSearchParams();

const queryClient = useQueryClient();

Expand Down Expand Up @@ -550,6 +555,14 @@ export const StatementEditor: React.FC<StatementEditor> = ({
);
const dispatch = useAppDispatch();

useEffect(() => {
if (showWarnings && statement.data.actions.length > 0) {
appendMultipleDetailIds(
Array.from(new Set(statement.data.actions.map((a) => a.actionId)))
);
}
}, [showWarnings]);

return (
<>
<div style={{ marginBottom: "4rem" }} key={statement.id}>
Expand Down

0 comments on commit b22476f

Please sign in to comment.