Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix local folder not deleted after project item added #2932

Merged
merged 1 commit into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions frontend/src/js/previous-queries/list/Folders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import {
import AddFolderModal from "./AddFolderModal";
import DeleteFolderModal from "./DeleteFolderModal";
import Folder from "./Folder";
import { addFolder, useUpdateFormConfig, useUpdateQuery } from "./actions";
import {
addFolder,
removeFolder,
useUpdateFormConfig,
useUpdateQuery,
} from "./actions";
import { useFolders } from "./selector";

const DROP_TYPES = [
Expand Down Expand Up @@ -120,6 +125,9 @@ interface Props {

const Folders: FC<Props> = ({ className }) => {
const folders = useFolders();
const localFolders = useSelector<StateT, string[]>(
(state) => state.previousQueries.localFolders,
);
const folderFilter = useSelector<StateT, string[]>(
(state) => state.previousQueriesFolderFilter.folders,
);
Expand Down Expand Up @@ -151,7 +159,7 @@ const Folders: FC<Props> = ({ className }) => {

const { updateQuery } = useUpdateQuery();
const { updateFormConfig } = useUpdateFormConfig();
const onDropIntoFolder = (
const onDropIntoFolder = async (
item: DragItemQuery | DragItemFormConfig,
folder: string,
) => {
Expand All @@ -160,18 +168,23 @@ const Folders: FC<Props> = ({ className }) => {
}

if (item.type === DNDType.FORM_CONFIG) {
updateFormConfig(
await updateFormConfig(
item.id,
{ tags: [...item.tags, folder] },
t("formConfig.retagError"),
);
} else {
updateQuery(
await updateQuery(
item.id,
{ tags: [...item.tags, folder] },
t("previousQuery.retagError"),
);
}

// Delete from the temporary "localFolders", because now it's a "real" folder
if (localFolders.includes(folder)) {
dispatch(removeFolder({ folderName: folder }));
}
};

const [folderToDelete, setFolderToDelete] = useState<string | null>(null);
Expand Down
30 changes: 19 additions & 11 deletions frontend/src/js/previous-queries/list/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,24 @@ const deleteFormConfig = (
};
};

const removeLocalFolder = (
state: PreviousQueriesStateT,
{ folderName }: { folderName: string },
) => {
const { localFolders } = state;
const idx = localFolders.findIndex((f) => f === folderName);

return idx === -1
? state
: {
...state,
localFolders: [
...localFolders.slice(0, idx),
...localFolders.slice(idx + 1),
],
};
};

const previousQueriesReducer = (
state: PreviousQueriesStateT = initialState,
action: Action,
Expand Down Expand Up @@ -201,17 +219,7 @@ const previousQueriesReducer = (
localFolders: [...state.localFolders, action.payload.folderName],
};
case getType(removeFolder):
const idx = state.localFolders.indexOf(action.payload.folderName);

if (idx === -1) return state;

return {
...state,
localFolders: [
...state.localFolders.slice(0, idx),
...state.localFolders.slice(idx + 1),
],
};
return removeLocalFolder(state, action.payload);
default:
return state;
}
Expand Down