Skip to content

Commit d5610ad

Browse files
committed
Fixed bug with alphabetical order not working for bookmarks. Minor changes related to bookmarks form
1 parent ec5f50a commit d5610ad

File tree

12 files changed

+88
-16
lines changed

12 files changed

+88
-16
lines changed

client/src/components/Bookmarks/Bookmarks.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export const Bookmarks = (props: Props): JSX.Element => {
6969
}, [isAuthenticated]);
7070

7171
useEffect(() => {
72-
if (categoryInEdit) {
72+
if (categoryInEdit && !modalIsOpen) {
7373
setTableContentType(ContentType.bookmark);
7474
setShowTable(true);
7575
}

client/src/components/Bookmarks/Form/BookmarksForm.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ export const BookmarksForm = ({
137137
}
138138

139139
modalHandler();
140+
}
140141

141-
setFormData(newBookmarkTemplate);
142+
setFormData(newBookmarkTemplate);
142143

143-
setCustomIcon(null);
144-
}
144+
setCustomIcon(null);
145145
};
146146

147147
return (

client/src/components/Bookmarks/Form/CategoryForm.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ export const CategoryForm = ({
6060
addCategory(formData);
6161
} else {
6262
updateCategory(category.id, formData);
63+
modalHandler();
6364
}
6465

6566
setFormData(newCategoryTemplate);
66-
modalHandler();
6767
};
6868

6969
return (

client/src/components/Settings/UISettings/UISettings.tsx

+15-8
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ import { InputGroup, Button, SettingsHeadline } from '../../UI';
1616
import { otherSettingsTemplate, inputHandler } from '../../../utility';
1717

1818
export const UISettings = (): JSX.Element => {
19-
const { loading, config } = useSelector((state: State) => state.config);
19+
const {
20+
config: { loading, config },
21+
bookmarks: { categories },
22+
} = useSelector((state: State) => state);
2023

2124
const dispatch = useDispatch();
22-
const { updateConfig, sortApps, sortCategories } = bindActionCreators(
23-
actionCreators,
24-
dispatch
25-
);
25+
const { updateConfig, sortApps, sortCategories, sortBookmarks } =
26+
bindActionCreators(actionCreators, dispatch);
2627

2728
// Initial state
2829
const [formData, setFormData] = useState<OtherSettingsForm>(
@@ -46,9 +47,15 @@ export const UISettings = (): JSX.Element => {
4647
// Update local page title
4748
document.title = formData.customTitle;
4849

49-
// Sort apps and categories with new settings
50-
sortApps();
51-
sortCategories();
50+
// Sort entities with new settings
51+
if (formData.useOrdering !== config.useOrdering) {
52+
sortApps();
53+
sortCategories();
54+
55+
for (let { id } of categories) {
56+
sortBookmarks(id);
57+
}
58+
}
5259
};
5360

5461
// Input handler

client/src/interfaces/Bookmark.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ export interface NewBookmark {
88
isPublic: boolean;
99
}
1010

11-
export interface Bookmark extends Model, NewBookmark {}
11+
export interface Bookmark extends Model, NewBookmark {
12+
orderId: number;
13+
}

client/src/store/action-creators/bookmark.ts

+22
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
ReorderCategoriesAction,
2424
SetEditBookmarkAction,
2525
SetEditCategoryAction,
26+
SortBookmarksAction,
2627
SortCategoriesAction,
2728
UpdateBookmarkAction,
2829
UpdateCategoryAction,
@@ -100,6 +101,8 @@ export const addBookmark =
100101
type: ActionType.addBookmark,
101102
payload: res.data.data,
102103
});
104+
105+
dispatch<any>(sortBookmarks(res.data.data.categoryId));
103106
} catch (err) {
104107
console.log(err);
105108
}
@@ -271,6 +274,8 @@ export const updateBookmark =
271274
payload: res.data.data,
272275
});
273276
}
277+
278+
dispatch<any>(sortBookmarks(res.data.data.categoryId));
274279
} catch (err) {
275280
console.log(err);
276281
}
@@ -377,3 +382,20 @@ export const reorderBookmarks =
377382
console.log(err);
378383
}
379384
};
385+
386+
export const sortBookmarks =
387+
(categoryId: number) => async (dispatch: Dispatch<SortBookmarksAction>) => {
388+
try {
389+
const res = await axios.get<ApiResponse<Config>>('/api/config');
390+
391+
dispatch({
392+
type: ActionType.sortBookmarks,
393+
payload: {
394+
orderType: res.data.data.useOrdering,
395+
categoryId,
396+
},
397+
});
398+
} catch (err) {
399+
console.log(err);
400+
}
401+
};

client/src/store/action-types/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export enum ActionType {
4141
updateBookmark = 'UPDATE_BOOKMARK',
4242
setEditBookmark = 'SET_EDIT_BOOKMARK',
4343
reorderBookmarks = 'REORDER_BOOKMARKS',
44+
sortBookmarks = 'SORT_BOOKMARKS',
4445
// AUTH
4546
login = 'LOGIN',
4647
logout = 'LOGOUT',

client/src/store/actions/bookmark.ts

+8
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,11 @@ export interface ReorderBookmarksAction {
7474
categoryId: number;
7575
};
7676
}
77+
78+
export interface SortBookmarksAction {
79+
type: ActionType.sortBookmarks;
80+
payload: {
81+
orderType: string;
82+
categoryId: number;
83+
};
84+
}

client/src/store/actions/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
SetEditCategoryAction,
4242
SetEditBookmarkAction,
4343
ReorderBookmarksAction,
44+
SortBookmarksAction,
4445
} from './bookmark';
4546

4647
import {
@@ -87,6 +88,7 @@ export type Action =
8788
| UpdateBookmarkAction
8889
| SetEditBookmarkAction
8990
| ReorderBookmarksAction
91+
| SortBookmarksAction
9092
// Auth
9193
| LoginAction
9294
| LogoutAction

client/src/store/reducers/bookmark.ts

+23
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,29 @@ export const bookmarksReducer = (
216216
};
217217
}
218218

219+
case ActionType.sortBookmarks: {
220+
const categoryIdx = state.categories.findIndex(
221+
(category) => category.id === action.payload.categoryId
222+
);
223+
224+
const sortedBookmarks = sortData<Bookmark>(
225+
state.categories[categoryIdx].bookmarks,
226+
action.payload.orderType
227+
);
228+
229+
return {
230+
...state,
231+
categories: [
232+
...state.categories.slice(0, categoryIdx),
233+
{
234+
...state.categories[categoryIdx],
235+
bookmarks: sortedBookmarks,
236+
},
237+
...state.categories.slice(categoryIdx + 1),
238+
],
239+
};
240+
}
241+
219242
default:
220243
return state;
221244
}

client/src/utility/templateObjects/bookmarkTemplate.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ export const bookmarkTemplate: Bookmark = {
1313
id: -1,
1414
createdAt: new Date(),
1515
updatedAt: new Date(),
16+
orderId: 0,
1617
};

controllers/categories/getAllCategories.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ const getAllCategories = asyncWrapper(async (req, res, next) => {
1818

1919
const order =
2020
orderType == 'name'
21-
? [[Sequelize.fn('lower', Sequelize.col('bookmarks.name')), 'ASC']]
22-
: [[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC']];
21+
? [
22+
[Sequelize.fn('lower', Sequelize.col('Category.name')), 'ASC'],
23+
[Sequelize.fn('lower', Sequelize.col('bookmarks.name')), 'ASC'],
24+
]
25+
: [
26+
[orderType, 'ASC'],
27+
[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC'],
28+
];
2329

2430
categories = categories = await Category.findAll({
2531
include: [

0 commit comments

Comments
 (0)