Skip to content

Commit

Permalink
automatically create new category when category in integrations is no…
Browse files Browse the repository at this point in the history
…t found (pawelmalak#11)

automatically create new category when category in docker or kubernetes labels is not found
  • Loading branch information
fdarveau committed Oct 11, 2021
1 parent 4280511 commit 349d63c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 31 deletions.
25 changes: 6 additions & 19 deletions client/src/components/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,13 @@ const Home = (props: ComponentProps): JSX.Element => {
// Local search query
const [localSearch, setLocalSearch] = useState<null | string>(null);

// Load app categories
useEffect(() => {
if (appCategories.length === 0) {
getAppCategories();
}
}, [getAppCategories]);

// Load apps
useEffect(() => {
if (apps.length === 0) {
getApps();
}
}, [getApps]);

// Load bookmark categories
useEffect(() => {
if (bookmarkCategories.length === 0) {
getBookmarkCategories();
}
}, [getBookmarkCategories]);

// Load bookmarks
useEffect(() => {
if (bookmarks.length === 0) {
Expand Down Expand Up @@ -120,10 +106,11 @@ const Home = (props: ComponentProps): JSX.Element => {
return [category];
};

const categoryContainsItems = (category: Category, allItems: App[] | Bookmark[]): boolean => {
if (category.apps?.length > 0) return true;
const categoryContainsPinnedItems = (category: Category, allItems: App[] | Bookmark[]): boolean => {
if (category.apps?.filter((app: App) => app.isPinned).length > 0) return true;
if (category.bookmarks?.filter((bookmark: Bookmark) => bookmark.isPinned).length > 0) return true;
if (category.id < 0) { // Is a default category
return allItems.findIndex((item: App | Bookmark) => item.categoryId === category.id) >= 0;
return allItems.findIndex((item: App | Bookmark) => item.categoryId === category.id && item.isPinned) >= 0;
}
return false;
};
Expand Down Expand Up @@ -160,7 +147,7 @@ const Home = (props: ComponentProps): JSX.Element => {
<AppGrid
categories={
!localSearch
? appCategories.filter((category: Category) => category.isPinned && categoryContainsItems(category, apps))
? appCategories.filter((category: Category) => category.isPinned && categoryContainsPinnedItems(category, apps))
: searchInCategories(localSearch, appCategories)
}
apps={
Expand Down Expand Up @@ -189,7 +176,7 @@ const Home = (props: ComponentProps): JSX.Element => {
<BookmarkGrid
categories={
!localSearch
? bookmarkCategories.filter((category: Category) => category.isPinned && categoryContainsItems(category, bookmarks))
? bookmarkCategories.filter((category: Category) => category.isPinned && categoryContainsPinnedItems(category, bookmarks))
: searchInCategories(localSearch, bookmarkCategories)
}
bookmarks={
Expand Down
18 changes: 12 additions & 6 deletions client/src/store/actions/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export const getApps = () => async (dispatch: Dispatch) => {
type: ActionTypes.getAppsSuccess,
payload: res.data.data,
});

await getCategories(dispatch);
} catch (err) {
console.log(err);
}
Expand All @@ -49,17 +51,21 @@ export const getAppCategories = () => async (dispatch: Dispatch) => {
});

try {
const res = await axios.get<ApiResponse<Category[]>>("/api/categories/apps");

dispatch<GetAppCategoriesAction<Category[]>>({
type: ActionTypes.getAppCategoriesSuccess,
payload: res.data.data
});
await getCategories(dispatch);
} catch (err) {
console.log(err);
}
};

async function getCategories(dispatch: Dispatch) {
const res = await axios.get<ApiResponse<Category[]>>("/api/categories/apps");

dispatch<GetAppCategoriesAction<Category[]>>({
type: ActionTypes.getAppCategoriesSuccess,
payload: res.data.data
});
}

/**
* ADD CATEGORY
*/
Expand Down
19 changes: 13 additions & 6 deletions client/src/store/actions/bookmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export const getBookmarks = () => async (dispatch: Dispatch) => {
type: ActionTypes.getBookmarksSuccess,
payload: res.data.data,
});

await getCategories(dispatch);
} catch (err) {
console.log(err);
}
Expand All @@ -49,17 +51,21 @@ export const getBookmarkCategories = () => async (dispatch: Dispatch) => {
});

try {
const res = await axios.get<ApiResponse<Category[]>>("/api/categories/bookmarks");

dispatch<GetBookmarkCategoriesAction<Category[]>>({
type: ActionTypes.getBookmarkCategoriesSuccess,
payload: res.data.data
});
await getCategories(dispatch);
} catch (err) {
console.log(err);
}
};

async function getCategories(dispatch: Dispatch) {
const res = await axios.get<ApiResponse<Category[]>>("/api/categories/bookmarks");

dispatch<GetBookmarkCategoriesAction<Category[]>>({
type: ActionTypes.getBookmarkCategoriesSuccess,
payload: res.data.data
});
}

/**
* ADD CATEGORY
*/
Expand Down Expand Up @@ -473,3 +479,4 @@ export const reorderBookmarkCategories =
console.log(err);
}
};

21 changes: 21 additions & 0 deletions controllers/apps.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ async function retrieveDockerApps(apps, orderType, unpinStoppedApps) {

for (let i = 0; i < names.length; i++) {
const category = categoriesLabels[i] ? categories.find(category => category.name.toUpperCase() === categoriesLabels[i].toUpperCase()) : dockerDefaultCategory;
if (!category) {
category = await createNewCategory(categoriesLabels[i]);
if (category) {
categories.push(category);
}
}

dockerApps.push({
name: names[i] || names[0],
Expand Down Expand Up @@ -250,6 +256,15 @@ async function retrieveDockerApps(apps, orderType, unpinStoppedApps) {
return apps;
}

async function createNewCategory(newCategoryName) {
return await Category.create({
name: newCategoryName,
type: 'apps',
isPinned: true,
orderId: Number.MAX_SAFE_INTEGER //New category will always be last and can then be re-ordered manually by user
});
}

async function retrieveKubernetesApps(apps, orderType, unpinStoppedApps) {
let ingresses = null;

Expand Down Expand Up @@ -295,6 +310,12 @@ async function retrieveKubernetesApps(apps, orderType, unpinStoppedApps) {

for (let i = 0; i < names.length; i++) {
const category = categoriesLabels[i] ? categories.find(category => category.name.toUpperCase() === categoriesLabels[i].toUpperCase()) : kubernetesDefaultCategory;
if (!category) {
category = await createNewCategory(categoriesLabels[i]);
if (category) {
categories.push(category);
}
}

kubernetesApps.push({
name: names[i] || names[0],
Expand Down

0 comments on commit 349d63c

Please sign in to comment.