diff --git a/client/src/components/Home/Home.tsx b/client/src/components/Home/Home.tsx index 495e408..5f8473c 100644 --- a/client/src/components/Home/Home.tsx +++ b/client/src/components/Home/Home.tsx @@ -152,7 +152,7 @@ const Home = (props: ComponentProps): JSX.Element => { category.isPinned && category.apps.length > 0) + ? appCategories.filter((category: Category) => category.isPinned && category.apps?.length > 0) : searchInCategories(localSearch, appCategories) } apps={ @@ -181,7 +181,7 @@ const Home = (props: ComponentProps): JSX.Element => { category.isPinned && category.bookmarks.length > 0) + ? bookmarkCategories.filter((category: Category) => category.isPinned && category.bookmarks?.length > 0) : searchInCategories(localSearch, bookmarkCategories) } bookmarks={ diff --git a/client/src/store/actions/app.ts b/client/src/store/actions/app.ts index 91fa3b8..58d0f0e 100644 --- a/client/src/store/actions/app.ts +++ b/client/src/store/actions/app.ts @@ -49,13 +49,11 @@ export const getAppCategories = () => async (dispatch: Dispatch) => { }); try { - const res = await axios.get>("/api/categories"); + const res = await axios.get>("/api/categories/apps"); dispatch>({ type: ActionTypes.getAppCategoriesSuccess, - payload: res.data.data.filter( - (category: Category) => category.type === "apps" - ), + payload: res.data.data }); } catch (err) { console.log(err); diff --git a/client/src/store/actions/bookmark.ts b/client/src/store/actions/bookmark.ts index 4115c5b..658f002 100644 --- a/client/src/store/actions/bookmark.ts +++ b/client/src/store/actions/bookmark.ts @@ -49,13 +49,11 @@ export const getBookmarkCategories = () => async (dispatch: Dispatch) => { }); try { - const res = await axios.get>("/api/categories"); + const res = await axios.get>("/api/categories/bookmarks"); dispatch>({ type: ActionTypes.getBookmarkCategoriesSuccess, - payload: res.data.data.filter( - (category: Category) => category.type === "bookmarks" - ), + payload: res.data.data }); } catch (err) { console.log(err); diff --git a/controllers/apps.js b/controllers/apps.js index 2f24c1b..86fdd09 100644 --- a/controllers/apps.js +++ b/controllers/apps.js @@ -201,7 +201,7 @@ async function retrieveDockerApps(apps, orderType, unpinStoppedApps) { order: [[orderType, 'ASC']] }); - containers = containers.filter(e => Object.keys(e.Labels).length !== 0); + containers = containers.filter((e) => Object.keys(e.Labels).length !== 0); const dockerApps = []; for (const container of containers) { const labels = container.Labels; @@ -270,7 +270,15 @@ async function retrieveKubernetesApps(apps, orderType, unpinStoppedApps) { order: [[orderType, 'ASC']] }); - ingresses = ingresses.filter(e => Object.keys(e.metadata.annotations).length !== 0); + + const categories = await Category.findAll({ + where: { + type: 'apps' + }, + order: [[orderType, 'ASC']] + }); + + ingresses = ingresses.filter((e) => Object.keys(e.metadata.annotations).length !== 0); const kubernetesApps = []; for (const ingress of ingresses) { const annotations = ingress.metadata.annotations; @@ -286,7 +294,7 @@ async function retrieveKubernetesApps(apps, orderType, unpinStoppedApps) { const icons = annotations['flame.pawelmalak/icon'] ? annotations['flame.pawelmalak/icon'].split(';') : [];; for (let i = 0; i < names.length; i++) { - const category = categoriesLabels[i] ? categories.find(category => category.name.toUpperCase() === categoriesLabels[i].toUpperCase()) : dockerDefaultCategory; + const category = categoriesLabels[i] ? categories.find(category => category.name.toUpperCase() === categoriesLabels[i].toUpperCase()) : kubernetesDefaultCategory; kubernetesApps.push({ name: names[i] || names[0], diff --git a/controllers/category.js b/controllers/category.js index fb62a2d..93ff909 100644 --- a/controllers/category.js +++ b/controllers/category.js @@ -21,6 +21,8 @@ exports.kubernetesDefaultCategory = { orderId: 999, }; +const defaultCategories = [exports.dockerDefaultCategory, exports.kubernetesDefaultCategory]; + // @desc Create new category // @route POST /api/categories // @access Public @@ -60,37 +62,26 @@ exports.getCategories = asyncWrapper(async (req, res, next) => { const orderType = useOrdering ? useOrdering.value : "createdAt"; let categories; + let categoryTypes = [] + if (!req.params.type || req.params.type === 'apps') { + categoryTypes.push({model: App, as: "apps"}); + } + if (!req.params.type || req.params.type === 'bookmarks') { + categoryTypes.push({model: Bookmark, as: "bookmarks"}); + } if (orderType == "name") { categories = await Category.findAll({ - include: [ - { - model: App, - as: 'apps', - }, - { - model: Bookmark, - as: 'bookmarks', - }, - ], + include: categoryTypes, order: [[Sequelize.fn('lower', Sequelize.col('Category.name')), 'ASC']], }); } else { categories = await Category.findAll({ - include: [ - { - model: App, - as: 'apps', - }, - { - model: Bookmark, - as: 'bookmarks', - }, - ], + include: categoryTypes, order: [[orderType, 'ASC']], }); - categories.push(exports.dockerDefaultCategory); } + categories.push(defaultCategories.filter((category) => categoryTypes.findIndex((includedType) => category.type === includedType.as) > -1)); res.status(200).json({ success: true, @@ -119,6 +110,8 @@ exports.getCategory = asyncWrapper(async (req, res, next) => { if (!category) { if (req.params.id === exports.dockerDefaultCategory.id) { category = exports.dockerDefaultCategory; + } else if (req.params.id === exports.kubernetesDefaultCategory.id) { + category = exports.kubernetesDefaultCategory; } else { return next( new ErrorResponse( diff --git a/routes/category.js b/routes/category.js index 64067d7..3703218 100644 --- a/routes/category.js +++ b/routes/category.js @@ -15,6 +15,10 @@ router .post(createCategory) .get(getCategories); + router + .route('/:type') + .get(getCategories); + router .route('/:id') .get(getCategory)