Displaying tag file count on edit page.

This commit is contained in:
Roland 2023-04-09 15:08:22 -06:00
parent 977f85aebc
commit 888bd2910d
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
2 changed files with 42 additions and 29 deletions

View file

@ -27,6 +27,7 @@ import {Segment} from "./components/Theme";
import _ from "lodash";
import {HexColorPicker} from "react-colorful";
import {useRecurringTimeout} from "./hooks/customHooks";
import {Media} from "./contexts/contexts";
export const TagsContext = React.createContext({
NameToTagLabel: null,
@ -128,31 +129,34 @@ export const useTagsInterval = () => {
return tagsValue
}
function EditTagLabel({tag, onDelete, onEdit}) {
function EditTagRow({tag, onDelete, onEdit}) {
const {NameToTagLabel} = React.useContext(TagsContext);
const [confirmDeleteOpen, setConfirmDeleteOpen] = useState(false);
const {name, color, id} = tag;
const {name, color, id, count} = tag;
const deleteConfirm = <>
<SButton color='red' onClick={() => setConfirmDeleteOpen(true)} icon='trash'/>
<Confirm
id={`confirm${name}`}
open={confirmDeleteOpen}
content={`Are you sure you want to delete: ${name}?`}
confirmButton='Delete'
onCancel={() => setConfirmDeleteOpen(false)}
onConfirm={async () => onDelete(id, name)}
/>
</>;
const editButton = <SButton primary onClick={() => onEdit(name, color, id)} icon='edit'/>;
const nameLabel = <LabelGroup tag>
<NameToTagLabel name={name}/>
</LabelGroup>;
return <TableRow>
<TableCell>
<SButton color='red' onClick={() => setConfirmDeleteOpen(true)} icon='trash'/>
<Confirm
id={`confirm${name}`}
open={confirmDeleteOpen}
content={`Are you sure you want to delete: ${name}?`}
confirmButton='Delete'
onCancel={() => setConfirmDeleteOpen(false)}
onConfirm={async () => onDelete(id, name)}
/>
</TableCell>
<TableCell>
<SButton primary onClick={() => onEdit(name, color, id)} icon='edit'/>
</TableCell>
<TableCell>
<LabelGroup tag>
<NameToTagLabel name={name}/>
</LabelGroup>
</TableCell>
<TableCell>{deleteConfirm}</TableCell>
<TableCell>{editButton}</TableCell>
<TableCell>{nameLabel}</TableCell>
<Media greaterThanOrEqual='tablet'>
<TableCell><Label>{count}</Label></TableCell>
</Media>
</TableRow>
}
@ -197,8 +201,8 @@ function EditTagsModal() {
let content = <Dimmer><Loader inline active/></Dimmer>;
if (tags && tags.length) {
content = tags.map(i => <EditTagLabel key={i['name']} tag={i} onDelete={localDeleteTag}
onEdit={localEditTag}/>);
content = tags.map(i => <EditTagRow key={i['name']} tag={i} onDelete={localDeleteTag}
onEdit={localEditTag}/>);
}
return <>
@ -242,6 +246,9 @@ function EditTagsModal() {
<TableHeaderCell width={2}>Delete</TableHeaderCell>
<TableHeaderCell width={2}>Edit</TableHeaderCell>
<TableHeaderCell width={8}>Tag</TableHeaderCell>
<Media greaterThanOrEqual='tablet'>
<TableHeaderCell>Count</TableHeaderCell>
</Media>
</TableRow>
</TableHeader>
<TableBody>

View file

@ -5,9 +5,8 @@ from sqlalchemy import Column, Integer, String, ForeignKey, BigInteger
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import relationship, Session
from wrolpi.common import ModelHelper, Base, logger, ConfigFile, get_media_directory, background_task, run_after, \
limit_concurrent
from wrolpi.db import optional_session
from wrolpi.common import ModelHelper, Base, logger, ConfigFile, get_media_directory, background_task, run_after
from wrolpi.db import optional_session, get_db_curs
from wrolpi.errors import UnknownTag, UsedTag, InvalidTag
from wrolpi.vars import PYTEST
@ -174,9 +173,16 @@ def schedule_save(session: Session = None):
background_task(_())
@optional_session
def get_tags(session: Session) -> List[Tag]:
tags = list(session.query(Tag).order_by(Tag.name))
def get_tags() -> List[dict]:
with get_db_curs() as curs:
curs.execute('''
SELECT t.name, t.color, COUNT(tf.tag_id)
FROM tag t
LEFT JOIN tag_file tf on t.id = tf.tag_id
GROUP BY t.name, t.color
ORDER BY t.name
''')
tags = list(map(dict, curs.fetchall()))
return tags