Skip to content

Commit

Permalink
Fix topic being removed from folder on edit (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecalcc authored Jan 29, 2024
1 parent 2f33140 commit 4c3e78e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion assets/search/components/TopicEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ interface IProps {
filterGroups: {[key: string]: IFilterGroup};
availableFields: Array<string>;

saveFolder: (folder: any, data: any, global?: boolean) => void;
saveFolder: (folder: any, data: any, global?: boolean) => Promise<ITopicFolder>;
fetchNavigations(): Promise<void>;
closeEditor(): void;
saveTopic(isExisting: boolean, topic: ITopic): Promise<ITopic>;
Expand Down
19 changes: 7 additions & 12 deletions assets/search/components/TopicForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ interface IProps {

changeNotificationType(notificationType: ITopicNotificationScheduleType): void;
openEditTopicNotificationsModal(): void;
saveFolder: (folder: any, data: any, global?: boolean) => void;
saveFolder: (folder: any, data: any, global?: boolean) => Promise<ITopicFolder>;
}

const TopicForm: React.FC<IProps> = ({
Expand Down Expand Up @@ -89,13 +89,6 @@ const TopicForm: React.FC<IProps> = ({
const topicSubscriptionType = getSubscriptionNotificationType(topic, user._id);
const [newFolder, setNewFolder] = useState<Partial<ITopicFolder> | null>();

useEffect(() => {
const newlyCreatedFolder = folders.find((x) => x.name === newFolder?.name) as ITopicFolder;

setNewFolder(null);
onFolderChange(newlyCreatedFolder);
}, [folders]);

return (
<form onSubmit={save}>
<div className="nh-flex-container list-item__preview-form pt-0">
Expand Down Expand Up @@ -184,10 +177,12 @@ const TopicForm: React.FC<IProps> = ({
className="simple-card__group position-relative"
>
<TopicFolderEditor
onSave={((name) => {
saveFolder(newFolder, {name}, topic.is_global);
setNewFolder({name});
})}
onSave={((name) =>
saveFolder(newFolder, {name}, topic.is_global).then((result) => {
onFolderChange(result);
setNewFolder(null);
})
)}
folder={newFolder}
onCancel={() => setNewFolder(null)}
/>
Expand Down
23 changes: 16 additions & 7 deletions assets/user-profile/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,20 +258,19 @@ export function saveFolder(folder: ITopicFolder, data: {name: string}, global?:
const state = getState();
const url = getFoldersUrl(state.company, state.user?._id, global, folder._id);

const getFolder = (allFolders: IFoldersUnified, id: string) =>
[...allFolders.userFolders, ...allFolders.companyFolders].find(({_id}) => _id === id);

if (folder._etag) {
const updates = {...data};

return server.patch(url, updates, folder._etag)
.then(() => {
dispatch(fetchFolders());
});
.then((result) => dispatch(fetchFolders()).then((allFolders: IFoldersUnified) => getFolder(allFolders, result._id)));
} else {
const payload = {...data, section: state.selectedMenu === 'events' ? 'agenda' : 'wire'};

return server.post(url, payload)
.then(() => {
dispatch(fetchFolders());
});
.then((result) => dispatch(fetchFolders()).then((allFolders: IFoldersUnified) => getFolder(allFolders, result._id)));
}
};
}
Expand All @@ -298,7 +297,12 @@ export const RECIEVE_FOLDERS = 'RECIEVE_FOLDERS';
* @param {bool} global - fetch company or user folders
* @param {bool} skipDispatch - if true it won't replace folders in store
*/
export function fetchFolders() {
type IFoldersUnified = {
companyFolders: Array<ITopicFolder>;
userFolders: Array<ITopicFolder>;
};

export function fetchFolders(): (dispatch: any, getState: any) => Promise<IFoldersUnified> {
return (dispatch: any, getState: any) => {
const state = getState();
const companyTopicsUrl = getFoldersUrl(state.company, state.user?._id, true);
Expand All @@ -315,6 +319,11 @@ export function fetchFolders() {
userFolders: userFolders,
},
});

return {
companyFolders: companyFolders as Array<ITopicFolder>,
userFolders: userFolders as Array<ITopicFolder>,
};
}).catch((error) => {
console.error(error);
return Promise.reject();
Expand Down

0 comments on commit 4c3e78e

Please sign in to comment.