Skip to content

Commit

Permalink
fix: classification validation, classification on list, can repeat sa…
Browse files Browse the repository at this point in the history
…me publisher on title screen (#166)

* fix: classification validation, classification on list, can repeat same publisher on title screen
  • Loading branch information
danilolutz authored May 28, 2022
1 parent b0bc697 commit 84f29a7
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/app/components/Person/List/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const PersonList: React.FC = () => {
},
},
{
Header: () => null,
Header: (): void => null,
id: 'edit',
Cell: (row: Cell<Person>) => {
return (
Expand Down
16 changes: 10 additions & 6 deletions src/app/components/Person/Read/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ const ReadPerson: React.FC<{ item: Person }> = ({ item }) => {
<Card
title={item.name}
actions={
<FaPen
size={20}
title={i18n.t('title.edit')}
onClick={() => handleEditClick(item)}
/>
<>
{item.login !== 'admin' && (
<FaPen
size={20}
title={i18n.t('title.edit')}
onClick={() => handleEditClick(item)}
/>
)}
</>
}
>
{item.login} <br />
Expand Down Expand Up @@ -92,7 +96,7 @@ const ReadPerson: React.FC<{ item: Person }> = ({ item }) => {
</Card>
<Card title={i18n.t('person.addresses')}>
{item.addresses.map((address) => (
<Subpanel key={address.id} >
<Subpanel key={address.id}>
{i18n.t('country.label')}:{' '}
{i18n.t(`countries.${address.city.region.country.name}`)} <br />
{i18n.t('region.label')}: {address.city.region.name} <br />
Expand Down
1 change: 0 additions & 1 deletion src/app/components/SearchMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ const SearchMenu: React.FC<SearchMenuProps> = ({ isOpen, setOpen }) => {

const factorySearchHandler = useCallback((ret) => {
const processed = ret.map((item: AdapterBaseProps) => {
console.log(item);
return BehaviourFactory.make(item);
});
return processed;
Expand Down
28 changes: 23 additions & 5 deletions src/app/components/Title/Create/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { isValidEAN13 } from '../../../hooks/useBarcode';

import { ButtonContainer, Container, List, ListItem, Row } from './styles';
import { Title } from '../Title';
import {ActionSave} from '../../Tabs';
import { ActionSave } from '../../Tabs';
interface SelectType {
id: string;
name: string;
Expand All @@ -40,6 +40,13 @@ interface Publisher {
publishedAt: Date;
}

interface ClassificationWithTitle {
classification: string;
title: {
name: string;
};
}

const TitleCreate: React.FC<{ globalSave: ActionSave }> = ({ globalSave }) => {
const { addToast } = useToast();

Expand Down Expand Up @@ -92,13 +99,24 @@ const TitleCreate: React.FC<{ globalSave: ActionSave }> = ({ globalSave }) => {
return;
}

if (
publishers.filter((a) => a.publisher.name === publisher.name).length > 0
) {
const classificationWithTitle = window.api.sendSync(
'checkTitleClassification',
{
entity: 'TitlePublisher',
value: {
where: { classification },
},
}
) as ClassificationWithTitle;

if (classificationWithTitle) {
addToast({
title: i18n.t('notifications.warning'),
type: 'error',
description: `${publisher.name} ${i18n.t('title.hasBeenAdd')}`,
description: i18n
.t('title.classificationCheck')
.replace('#classification#', classification)
.replace('#title#', classificationWithTitle.title.name),
});
return;
}
Expand Down
68 changes: 43 additions & 25 deletions src/app/components/Title/List/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useToast } from '../../../hooks/toast';
import { AppEvent } from '../../../../common/AppEvent';
import { trigger } from '../../../util/EventHandler';
import { Actions } from '../../../../common/Actions';
import { FaPen } from 'react-icons/fa';
import { FaPen, FaTags } from 'react-icons/fa';

const TitleList: React.FC = () => {
const { addToast } = useToast();
Expand All @@ -22,25 +22,24 @@ const TitleList: React.FC = () => {

useEffect(() => {
try {
setLoading(true);
const response = window.api.sendSync('listTitle', {
entity: 'Title',
value: {
where: null,
pageStart: 0,
pageSize: rowsPerPage,
},
}) as PaginatedSearch<Title>;
setList(response.data);
setLoading(false);
} catch (err) {
console.error(err);
}

setLoading(true);
const response = window.api.sendSync('listTitle', {
entity: 'Title',
value: {
where: null,
pageStart: 0,
pageSize: rowsPerPage,
},
}) as PaginatedSearch<Title>;
setList(response.data);
setLoading(false);
} catch (err) {
console.error(err);
}
}, [rowsPerPage]);

const handleUpdate = (item: Title): void => {
trigger(AppEvent.titleTab, { action: Actions.update, value: item});
trigger(AppEvent.titleTab, { action: Actions.update, value: item });
};

const handleRowClick = (item: Title) => {
Expand All @@ -49,6 +48,18 @@ const TitleList: React.FC = () => {

const columns: Array<Column<Title>> = useMemo(
() => [
{
Header: () => null,
id: 'info',
Cell: (row: Cell<Title>) => {
const classifications = row.row.original.titlePublishers.map(
(publisher) => {
return publisher.classification;
}
);
return <FaTags title={classifications.join(', ')} size={15} />;
},
},
{
Header: i18n.t('title.label'),
accessor: 'name',
Expand All @@ -62,16 +73,23 @@ const TitleList: React.FC = () => {
id: 'edit',
Cell: (row: Cell<Title>) => {
return (
<>
<FaPen size={20} title={i18n.t('title.edit')} onClick={(event) => { event.stopPropagation(); handleUpdate(row.row.original)}} />
</>)
}
}
<>
<FaPen
size={20}
title={i18n.t('title.edit')}
onClick={(event) => {
event.stopPropagation();
handleUpdate(row.row.original);
}}
/>
</>
);
},
},
],
[],
[]
);


const handleSubmit = useCallback(
async ({ pageIndex = 0 }: Search) => {
try {
Expand Down Expand Up @@ -99,7 +117,7 @@ const TitleList: React.FC = () => {
});
}
},
[addToast, rowsPerPage],
[addToast, rowsPerPage]
);

return (
Expand Down
28 changes: 23 additions & 5 deletions src/app/components/Title/Update/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { isValidEAN13 } from '../../../hooks/useBarcode';

import { ButtonContainer, Container, List, ListItem, Row } from './styles';
import { Title } from '../Title';
import {ActionSave} from '../../Tabs';
import { ActionSave } from '../../Tabs';

interface SelectType {
id: string;
Expand All @@ -42,6 +42,13 @@ interface Publisher {
publishedAt: Date;
}

interface ClassificationWithTitle {
classification: string;
title: {
name: string;
};
}

const TitleUpdate: React.FC<{ item: Title; globalSave: ActionSave }> = ({
item,
globalSave,
Expand Down Expand Up @@ -134,13 +141,24 @@ const TitleUpdate: React.FC<{ item: Title; globalSave: ActionSave }> = ({
return;
}

if (
publishers.filter((a) => a.publisher.name === publisher.name).length > 0
) {
const classificationWithTitle = window.api.sendSync(
'checkTitleClassification',
{
entity: 'TitlePublisher',
value: {
where: { classification },
},
}
) as ClassificationWithTitle;

if (classificationWithTitle) {
addToast({
title: i18n.t('notifications.warning'),
type: 'error',
description: `${publisher.name} ${i18n.t('title.hasBeenAdd')}`,
description: i18n
.t('title.classificationCheck')
.replace('#classification#', classification)
.replace('#title#', classificationWithTitle.title.name),
});
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/electron/infra/db/repositories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './GenericUpdateRepository';
export * from './GlobalSearchRepository';
export * from './title/ListTitleRepository';
export * from './title/ReadTitleRepository';
export * from './title/CheckTitleClassificationRepository';
export * from './title/TitleSearchRepository';
export * from './person/ListPersonRepository';
export * from './person/ReadPersonRepository';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import RepositoryBase from '../../RepositoryBase';
import typeORM from 'typeorm';

interface Where {
where: unknown;
}

export class CheckTitleClassificationRepository extends RepositoryBase {
private static instance: CheckTitleClassificationRepository = null;

static repositoryName = 'CheckTitleClassification';

private constructor() {
super();
}

public static getInstance(
typeOrm: typeORM.Repository<unknown>
): CheckTitleClassificationRepository {
if (!CheckTitleClassificationRepository.instance) {
CheckTitleClassificationRepository.instance =
new CheckTitleClassificationRepository();
}

CheckTitleClassificationRepository.instance.repository = typeOrm;

return CheckTitleClassificationRepository.instance;
}

public async execute(content: Where): Promise<unknown> {
try {
const classifictaionWithTitle = await this.repository.findOne({
relations: ['title'],
where: content.where,
});

return classifictaionWithTitle;
} catch (err) {
console.log(err);
throw err;
}
}
}
4 changes: 4 additions & 0 deletions src/electron/infra/listeners/ListenersConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export default class ListenersConfigs {

{ listenerName: 'listTitle', repositoryName: 'ListTitle' },
{ listenerName: 'readTitle', repositoryName: 'ReadTitle' },
{
listenerName: 'checkTitleClassification',
repositoryName: 'CheckTitleClassification',
},

{ listenerName: 'listPerson', repositoryName: 'ListPerson' },
{ listenerName: 'readPerson', repositoryName: 'ReadPerson' },
Expand Down
4 changes: 3 additions & 1 deletion src/locales/en-US/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@
"edit": "Edit",

"invalidISBN": "Invalid ISBN",
"successSave": "Title saved successfully "
"successSave": "Title saved successfully ",

"classificationCheck": "Classification: #classification#, already exists for title: #title#"
},
"settings": {
"label": "Settings",
Expand Down
4 changes: 3 additions & 1 deletion src/locales/pt-BR/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@
"edit": "Editar",

"invalidISBN": "ISBN inválido",
"successSave": "Título salvo com sucesso"
"successSave": "Título salvo com sucesso",

"classificationCheck": "Classificação (Tombo): #classification#, já existe para o título: #title#"
},
"settings": {
"label": "Configurações",
Expand Down

0 comments on commit 84f29a7

Please sign in to comment.