Fixed bug with alphabetical order not working for bookmarks. Minor changes related to bookmarks form

This commit is contained in:
Paweł Malak 2021-11-26 14:04:46 +01:00
parent ec5f50aba4
commit d5610ad6be
12 changed files with 89 additions and 17 deletions

View file

@ -69,7 +69,7 @@ export const Bookmarks = (props: Props): JSX.Element => {
}, [isAuthenticated]); }, [isAuthenticated]);
useEffect(() => { useEffect(() => {
if (categoryInEdit) { if (categoryInEdit && !modalIsOpen) {
setTableContentType(ContentType.bookmark); setTableContentType(ContentType.bookmark);
setShowTable(true); setShowTable(true);
} }

View file

@ -137,11 +137,11 @@ export const BookmarksForm = ({
} }
modalHandler(); modalHandler();
setFormData(newBookmarkTemplate);
setCustomIcon(null);
} }
setFormData(newBookmarkTemplate);
setCustomIcon(null);
}; };
return ( return (

View file

@ -60,10 +60,10 @@ export const CategoryForm = ({
addCategory(formData); addCategory(formData);
} else { } else {
updateCategory(category.id, formData); updateCategory(category.id, formData);
modalHandler();
} }
setFormData(newCategoryTemplate); setFormData(newCategoryTemplate);
modalHandler();
}; };
return ( return (

View file

@ -16,13 +16,14 @@ import { InputGroup, Button, SettingsHeadline } from '../../UI';
import { otherSettingsTemplate, inputHandler } from '../../../utility'; import { otherSettingsTemplate, inputHandler } from '../../../utility';
export const UISettings = (): JSX.Element => { export const UISettings = (): JSX.Element => {
const { loading, config } = useSelector((state: State) => state.config); const {
config: { loading, config },
bookmarks: { categories },
} = useSelector((state: State) => state);
const dispatch = useDispatch(); const dispatch = useDispatch();
const { updateConfig, sortApps, sortCategories } = bindActionCreators( const { updateConfig, sortApps, sortCategories, sortBookmarks } =
actionCreators, bindActionCreators(actionCreators, dispatch);
dispatch
);
// Initial state // Initial state
const [formData, setFormData] = useState<OtherSettingsForm>( const [formData, setFormData] = useState<OtherSettingsForm>(
@ -46,9 +47,15 @@ export const UISettings = (): JSX.Element => {
// Update local page title // Update local page title
document.title = formData.customTitle; document.title = formData.customTitle;
// Sort apps and categories with new settings // Sort entities with new settings
sortApps(); if (formData.useOrdering !== config.useOrdering) {
sortCategories(); sortApps();
sortCategories();
for (let { id } of categories) {
sortBookmarks(id);
}
}
}; };
// Input handler // Input handler

View file

@ -8,4 +8,6 @@ export interface NewBookmark {
isPublic: boolean; isPublic: boolean;
} }
export interface Bookmark extends Model, NewBookmark {} export interface Bookmark extends Model, NewBookmark {
orderId: number;
}

View file

@ -23,6 +23,7 @@ import {
ReorderCategoriesAction, ReorderCategoriesAction,
SetEditBookmarkAction, SetEditBookmarkAction,
SetEditCategoryAction, SetEditCategoryAction,
SortBookmarksAction,
SortCategoriesAction, SortCategoriesAction,
UpdateBookmarkAction, UpdateBookmarkAction,
UpdateCategoryAction, UpdateCategoryAction,
@ -100,6 +101,8 @@ export const addBookmark =
type: ActionType.addBookmark, type: ActionType.addBookmark,
payload: res.data.data, payload: res.data.data,
}); });
dispatch<any>(sortBookmarks(res.data.data.categoryId));
} catch (err) { } catch (err) {
console.log(err); console.log(err);
} }
@ -271,6 +274,8 @@ export const updateBookmark =
payload: res.data.data, payload: res.data.data,
}); });
} }
dispatch<any>(sortBookmarks(res.data.data.categoryId));
} catch (err) { } catch (err) {
console.log(err); console.log(err);
} }
@ -377,3 +382,20 @@ export const reorderBookmarks =
console.log(err); console.log(err);
} }
}; };
export const sortBookmarks =
(categoryId: number) => async (dispatch: Dispatch<SortBookmarksAction>) => {
try {
const res = await axios.get<ApiResponse<Config>>('/api/config');
dispatch({
type: ActionType.sortBookmarks,
payload: {
orderType: res.data.data.useOrdering,
categoryId,
},
});
} catch (err) {
console.log(err);
}
};

View file

@ -41,6 +41,7 @@ export enum ActionType {
updateBookmark = 'UPDATE_BOOKMARK', updateBookmark = 'UPDATE_BOOKMARK',
setEditBookmark = 'SET_EDIT_BOOKMARK', setEditBookmark = 'SET_EDIT_BOOKMARK',
reorderBookmarks = 'REORDER_BOOKMARKS', reorderBookmarks = 'REORDER_BOOKMARKS',
sortBookmarks = 'SORT_BOOKMARKS',
// AUTH // AUTH
login = 'LOGIN', login = 'LOGIN',
logout = 'LOGOUT', logout = 'LOGOUT',

View file

@ -74,3 +74,11 @@ export interface ReorderBookmarksAction {
categoryId: number; categoryId: number;
}; };
} }
export interface SortBookmarksAction {
type: ActionType.sortBookmarks;
payload: {
orderType: string;
categoryId: number;
};
}

View file

@ -41,6 +41,7 @@ import {
SetEditCategoryAction, SetEditCategoryAction,
SetEditBookmarkAction, SetEditBookmarkAction,
ReorderBookmarksAction, ReorderBookmarksAction,
SortBookmarksAction,
} from './bookmark'; } from './bookmark';
import { import {
@ -87,6 +88,7 @@ export type Action =
| UpdateBookmarkAction | UpdateBookmarkAction
| SetEditBookmarkAction | SetEditBookmarkAction
| ReorderBookmarksAction | ReorderBookmarksAction
| SortBookmarksAction
// Auth // Auth
| LoginAction | LoginAction
| LogoutAction | LogoutAction

View file

@ -216,6 +216,29 @@ export const bookmarksReducer = (
}; };
} }
case ActionType.sortBookmarks: {
const categoryIdx = state.categories.findIndex(
(category) => category.id === action.payload.categoryId
);
const sortedBookmarks = sortData<Bookmark>(
state.categories[categoryIdx].bookmarks,
action.payload.orderType
);
return {
...state,
categories: [
...state.categories.slice(0, categoryIdx),
{
...state.categories[categoryIdx],
bookmarks: sortedBookmarks,
},
...state.categories.slice(categoryIdx + 1),
],
};
}
default: default:
return state; return state;
} }

View file

@ -13,4 +13,5 @@ export const bookmarkTemplate: Bookmark = {
id: -1, id: -1,
createdAt: new Date(), createdAt: new Date(),
updatedAt: new Date(), updatedAt: new Date(),
orderId: 0,
}; };

View file

@ -18,8 +18,14 @@ const getAllCategories = asyncWrapper(async (req, res, next) => {
const order = const order =
orderType == 'name' orderType == 'name'
? [[Sequelize.fn('lower', Sequelize.col('bookmarks.name')), 'ASC']] ? [
: [[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC']]; [Sequelize.fn('lower', Sequelize.col('Category.name')), 'ASC'],
[Sequelize.fn('lower', Sequelize.col('bookmarks.name')), 'ASC'],
]
: [
[orderType, 'ASC'],
[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC'],
];
categories = categories = await Category.findAll({ categories = categories = await Category.findAll({
include: [ include: [