Skip to content

Commit

Permalink
fix lint format warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
fembau committed Aug 29, 2023
1 parent 964df25 commit f393aa4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 37 deletions.
2 changes: 1 addition & 1 deletion frontend/components/software/AboutSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import AboutLanguages from './AboutLanguages'
import AboutLicense from './AboutLicense'
import AboutSourceCode from './AboutSourceCode'
import SoftwareLogo from './SoftwareLogo'
import { SoftwareCategoriesWithHeadline } from './SoftwareCategories'
import {SoftwareCategoriesWithHeadline} from './SoftwareCategories'

type AboutSectionType = {
brand_name: string
Expand Down
46 changes: 22 additions & 24 deletions frontend/components/software/SoftwareCategories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,34 @@
// SPDX-License-Identifier: EUPL-1.2

import CategoryIcon from '@mui/icons-material/Category'
import { CategoriesForSoftware, CategoryEntry, CategoryID, CategoryPath } from '../../types/SoftwareTypes'
import {CategoriesForSoftware, CategoryEntry, CategoryID, CategoryPath} from '../../types/SoftwareTypes'
import TagChipFilter from '../layout/TagChipFilter'
import { ssrSoftwareUrl } from '~/utils/postgrestUrl'
import logger from '../../utils/logger'
import React from 'react'
import { Chip } from '@mui/material'
import {Chip} from '@mui/material'

const interleave = <T,>(arr: T[], createElement: (index: number) => T) => arr.reduce((result, element, index, array) => {
result.push(element);
result.push(element)
if (index < array.length - 1) {
result.push(createElement(index));
result.push(createElement(index))
}
return result;
}, [] as T[]);
return result
}, [] as T[])


type CategoryTreeLevel = {
cat: CategoryEntry
children: CategoryTreeLevel[]
}

function CategoryTree({ categories }: { categories: CategoryPath[] }) {
function CategoryTree({categories}: { categories: CategoryPath[] }) {

const tree: CategoryTreeLevel[] = []
for (const path of categories) {
let cursor = tree
for (const item of path) {
const found = cursor.find(el => el.cat.id == item.id)
if (!found) {
const sub: CategoryTreeLevel = { cat: item, children: [] }
const sub: CategoryTreeLevel = {cat: item, children: []}
cursor.push(sub)
cursor = sub.children
} else {
Expand All @@ -42,8 +40,8 @@ function CategoryTree({ categories }: { categories: CategoryPath[] }) {
}
}

const TreeLevel = ({ items, indent = false }: { items: CategoryTreeLevel[], indent?: boolean }) => {
return <ul className={"list-disc list-inside -indent-4" + (indent ? ' pl-7' : ' pl-4')}>
const TreeLevel = ({items, indent = false}: { items: CategoryTreeLevel[], indent?: boolean }) => {
return <ul className={'list-disc list-inside -indent-4' + (indent ? ' pl-7' : ' pl-4')}>
{items.map((item, index) => (
<li key={index}>
{item.cat.short_name}
Expand Down Expand Up @@ -74,7 +72,7 @@ type SoftwareCategoriesProps = {
buttonTitle?: never
})

export function SoftwareCategories({ categories, buttonTitle, onClick }: SoftwareCategoriesProps) {
export function SoftwareCategories({categories, buttonTitle, onClick}: SoftwareCategoriesProps) {
if (categories.length === 0) {
return (
<i>No categories</i>
Expand All @@ -83,14 +81,14 @@ export function SoftwareCategories({ categories, buttonTitle, onClick }: Softwar

// TODO: useCallback or move out of component
const onClickHandler = (event: React.MouseEvent<HTMLElement>) => {
event.stopPropagation();
event.stopPropagation()
console.log(event)
const categoryIdx = parseInt(event.currentTarget.dataset.idx ?? '')
if (isNaN(categoryIdx)) return
const path = categories[categoryIdx]
const categoryId = path[path.length - 1].id
console.log('click', { id: categoryId, index: categoryIdx })
onClick?.({ id: categoryId, index: categoryIdx })
console.log('click', {id: categoryId, index: categoryIdx})
onClick?.({id: categoryId, index: categoryIdx})
}

return <div className="py-1">
Expand All @@ -104,18 +102,18 @@ export function SoftwareCategories({ categories, buttonTitle, onClick }: Softwar
))

return (
<div key={index} className={"clear-both mb-2" + (onClick ? " hover:bg-neutral-100" : "")}>
<span className='px-2 py-1 bg-neutral-100 inline-block'>
<div key={index} className={'clear-both mb-2' + (onClick ? ' hover:bg-neutral-100' : '')}>
<span className="px-2 py-1 bg-neutral-100 inline-block">
{interleave(chunks, (index) => <span key={index} className="px-2">::</span>)}
</span>

{onClick && <span className="ml-2 float-right" onClick={onClickHandler} data-idx={index}>{buttonTitle}</span>}
</div>
)
})}
<div className='clear-both'></div>
<div className="clear-both"></div>

<div className='mt-5 italic'>other variant using TagChipFilter:</div>
<div className="mt-5 italic">other variant using TagChipFilter:</div>
{categories.map((path, index) => {
const text = path.map((category, index) => category.short_name).join(' :: ')
return (
Expand All @@ -125,23 +123,23 @@ export function SoftwareCategories({ categories, buttonTitle, onClick }: Softwar
)
})}

<div className='mt-5 italic'>other variant using Chip:</div>
<div className="mt-5 italic">other variant using Chip:</div>
{categories.map((path, index) => {
const text = path.map((category, index) => category.short_name).join(' :: ')
return (
<div className="my-1" key={index}>
<Chip key={index} label={text} title={text} onDelete={onClick && (() => onClick({ id: path[path.length - 1].id, index }))} />
<Chip key={index} label={text} title={text} onDelete={onClick && (() => onClick({id: path[path.length - 1].id, index}))} />
</div>
)
})}
<div className='mt-5 italic'>other variant using a tree:</div>
<div className="mt-5 italic">other variant using a tree:</div>
<CategoryTree categories={categories} />
</div>
}

// FIXME: I think AboutSection should define headers instead of here
// FIXME: and a separate header component should be created and use for all blocks in AboutSection
export function SoftwareCategoriesWithHeadline({ categories }: { categories: CategoriesForSoftware }) {
export function SoftwareCategoriesWithHeadline({categories}: { categories: CategoriesForSoftware }) {
return (
<>
<div className="pt-8 pb-2">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
//
// SPDX-License-Identifier: EUPL-1.2

import { ChangeEventHandler, useEffect, useMemo, useState } from 'react'
import {ChangeEventHandler, useEffect, useMemo, useState} from 'react'

import { useSession } from '~/auth'
import { softwareInformation as config } from '../editSoftwareConfig'
import { CategoryID, CategoryPath } from '~/types/SoftwareTypes'
import {useSession} from '~/auth'
import {softwareInformation as config} from '../editSoftwareConfig'
import {CategoryID, CategoryPath} from '~/types/SoftwareTypes'
import useSnackbar from '~/components/snackbar/useSnackbar'
import EditSectionTitle from '~/components/layout/EditSectionTitle'
import { SelectedCategory, SoftwareCategories } from '../../SoftwareCategories'
import { addCategoryToSoftware, deleteCategoryToSoftware, getAvailableCategories } from '~/utils/getSoftware'
import {SelectedCategory, SoftwareCategories} from '../../SoftwareCategories'
import {addCategoryToSoftware, deleteCategoryToSoftware, getAvailableCategories} from '~/utils/getSoftware'

export type SoftwareCategoriesProps = {
softwareId: string
Expand All @@ -22,9 +22,9 @@ function leaf<T>(list: T[]) {
return list[list.length - 1]
}

export default function AutosaveSoftwareCategories({ softwareId, categories: defaultCategoryPaths }: SoftwareCategoriesProps) {
const { token } = useSession()
const { showErrorMessage, showInfoMessage } = useSnackbar()
export default function AutosaveSoftwareCategories({softwareId, categories: defaultCategoryPaths}: SoftwareCategoriesProps) {
const {token} = useSession()
const {showErrorMessage, showInfoMessage} = useSnackbar()
const [categoryPaths, setCategoryPaths] = useState(defaultCategoryPaths)
const [availableCategoryPaths, setAvailableCategoryPaths] = useState<CategoryPath[]>([])

Expand All @@ -39,7 +39,7 @@ export default function AutosaveSoftwareCategories({ softwareId, categories: def

useEffect(() => {
getAvailableCategories()
.then(setAvailableCategoryPaths);
.then(setAvailableCategoryPaths)
}, [])

const addCategory = (categoryPath: CategoryPath) => {
Expand Down
4 changes: 2 additions & 2 deletions frontend/utils/getSoftware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ function prepareQueryURL(path: string, params: Record<string, string> = {}) {

export async function getCategoriesForSoftware(software_id: string, token?: string): Promise<CategoriesForSoftware> {
try {
const url = prepareQueryURL('/rpc/category_paths_by_software_expanded', { software_id })
const url = prepareQueryURL('/rpc/category_paths_by_software_expanded', {software_id})
const resp = await fetch(url, {
method: 'GET',
headers: createJsonHeaders(token)
Expand Down Expand Up @@ -243,7 +243,7 @@ export async function getAvailableCategories(): Promise<CategoryPath[]> {
// FIXME: add return type
export async function addCategoryToSoftware(softwareId: string, categoryId: CategoryID, token: string) {
const url = '/api/v1/category_for_software'
const data = { software_id: softwareId, category_id: categoryId }
const data = {software_id: softwareId, category_id: categoryId}

const resp = await fetch(url, {
method: 'POST',
Expand Down

0 comments on commit f393aa4

Please sign in to comment.