Skip to content

Commit

Permalink
Merge branch 'product-version' into consumo-com-produto
Browse files Browse the repository at this point in the history
  • Loading branch information
jfbaraky authored Jun 9, 2020
2 parents 574fa75 + a9ea9e3 commit 3bea90f
Show file tree
Hide file tree
Showing 37 changed files with 878 additions and 190 deletions.
19 changes: 18 additions & 1 deletion admin/src/components/consumptionFamilySearch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState } from 'react';
import { Alert, Button, Descriptions, Form, Input, Typography } from 'antd';
import { useSelector, useDispatch } from 'react-redux';
import { Flex } from '../flex';
import { FamilyActions, FamilyWrapper } from './styles';
import { FamilyActions, FamilyWrapper, InfoContainer } from './styles';
import { AppState } from '../../redux/rootReducer';
import { requestGetFamily } from '../../redux/families/actions';
import { Family } from '../../interfaces/family';
Expand All @@ -27,6 +27,9 @@ export const ConsumptionFamilySearch: React.FC<ComponentProps> = (props) => {
const [birthday, setBirthday] = useState('');
// Redux state
const familyLoading = useSelector<AppState, boolean>((state) => state.familiesReducer.familyLoading);
const familyError = useSelector<AppState, (Error & { status?: number }) | undefined>(
(state) => state.familiesReducer.familyError
);
const family = useSelector<AppState, Family | null | undefined>((state) => state.familiesReducer.familyItem);

// .env
Expand Down Expand Up @@ -67,6 +70,20 @@ export const ConsumptionFamilySearch: React.FC<ComponentProps> = (props) => {
}}
/>
</Form.Item>

{familyError && !familyLoading && (
<FamilyWrapper>
<Alert
type={familyError.status === 404 ? 'info' : 'error'}
message={
<InfoContainer>
<Typography.Text>{familyError.message}</Typography.Text>
</InfoContainer>
}
/>
</FamilyWrapper>
)}

{family && props.askForBirthday && (
<Form.Item
label="Aniversário do responsável"
Expand Down
6 changes: 6 additions & 0 deletions admin/src/components/consumptionFamilySearch/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,9 @@ export const FamilyActions = styled.div`
}
}
`;

export const InfoContainer = styled.div`
display: flex;
margin: ${(props) => props.theme.spacing.sm} 0;
justify-content: center;
`;
19 changes: 12 additions & 7 deletions admin/src/components/familySearch/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from 'react';
import { Form, Input, Typography, Card, Descriptions, Row, Col } from 'antd';
import { Form, Input, Typography, Descriptions, Row, Col, Alert } from 'antd';
import { useSelector, useDispatch } from 'react-redux';
import { FamilyWrapper, InfoContainer } from './styles';
import { AppState } from '../../redux/rootReducer';
Expand All @@ -25,7 +25,9 @@ export const FamilySearch: React.FC<ComponentProps> = () => {
const [nis, setNis] = useState('');
// Redux state
const familyLoading = useSelector<AppState, boolean>((state) => state.familiesReducer.familyLoading);
const familyError = useSelector<AppState, Error | undefined>((state) => state.familiesReducer.familyError);
const familyError = useSelector<AppState, (Error & { status?: number }) | undefined>(
(state) => state.familiesReducer.familyError
);
const family = useSelector<AppState, Family | null | undefined>((state) => state.familiesReducer.familyItem);

// .env
Expand Down Expand Up @@ -54,11 +56,14 @@ export const FamilySearch: React.FC<ComponentProps> = () => {

{familyError && !familyLoading && (
<FamilyWrapper>
<Card>
<InfoContainer>
<Text>Não encontramos nenhuma família utilizando esse NIS.</Text>
</InfoContainer>
</Card>
<Alert
type={familyError.status === 404 ? 'info' : 'error'}
message={
<InfoContainer>
<Text>{familyError.message}</Text>
</InfoContainer>
}
/>
</FamilyWrapper>
)}

Expand Down
2 changes: 1 addition & 1 deletion admin/src/components/familySearch/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const PriceStyle = {

export const InfoContainer = styled.div`
display: flex;
margin-top: ${(props) => props.theme.spacing.sm};
margin: ${(props) => props.theme.spacing.sm} 0;
justify-content: center;
`;

Expand Down
36 changes: 36 additions & 0 deletions admin/src/components/numberPicker/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { PlusOutlined, MinusOutlined } from '@ant-design/icons';
import React, { useCallback } from 'react';
import { ActionWrapper, ActionButton } from './styles';

export type NumberPickerProps = {
value: number;
onChange?: (value: number) => void;
};

/**
* Resource selector component
* @param props component props
*/
export const NumberPicker: React.FC<NumberPickerProps> = ({ value, onChange }) => {
// Whenever a + or - button is clicked, send the updated value to the onChange event
const handleChangeAmount = useCallback(
(value: number) => {
if (onChange) {
onChange(value);
}
},
[onChange]
);

return (
<ActionWrapper>
<ActionButton disabled={value === 0} onClick={() => handleChangeAmount(value - 1)}>
<MinusOutlined />
</ActionButton>
{value}
<ActionButton onClick={() => handleChangeAmount(value + 1)}>
<PlusOutlined />
</ActionButton>
</ActionWrapper>
);
};
20 changes: 20 additions & 0 deletions admin/src/components/numberPicker/styles.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import styled from 'styled-components';
import { Button } from 'antd';

export const ActionWrapper = styled.div`
display: flex;
justify-content: space-between;
align-items: center;
`;

export const ActionButton = styled(Button)`
display: inline-block;
border-radius: 50%;
&& {
width: ${(props) => props.theme.spacing.md};
min-width: ${(props) => props.theme.spacing.md};
height: ${(props) => props.theme.spacing.md};
}
padding: 4px;
font-size: 12px;
`;
84 changes: 84 additions & 0 deletions admin/src/components/resourceSelector/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { Table } from 'antd';
import React, { useCallback, useMemo } from 'react';
import { NumberPicker } from '../numberPicker';

type IdType<IdName extends string> = {
[I in IdName]: string | number;
};

export type ResourceSelectorDataSourceItem<IdName extends string> = {
name: string;
amount: number;
} & IdType<IdName>;

export type ResourceSelectorValueItem<IdName extends string> = {
amount: number;
} & IdType<IdName>;

export type ResourceSelectorProps<IdName extends string> = {
idName: IdName;
title?: string;
datasource: ResourceSelectorDataSourceItem<IdName>[];
value: ResourceSelectorValueItem<IdName>[];
onChange?: (value: ResourceSelectorValueItem<IdName>[]) => void;
pagination?: boolean;
};

/**
* Resource selector component
* @param props component props
*/
export function ResourceSelector<IdName extends string = 'id'>({
idName,
title,
datasource,
value,
onChange,
pagination
}: ResourceSelectorProps<IdName>) {
// Reset the state every time the datasource changes
const resourceList = useMemo(
() =>
datasource.map((item) => ({
[idName]: item[idName],
name: item.name,
amount: value.find((resource) => resource[idName] === item[idName])?.amount || 0
})),
[datasource, value, idName]
);

// Whenever a + or - button is clicked, send the updated list of resources to the onChange event
const handleChangeAmount = useCallback(
(id: string | number, amount: number) => {
if (onChange) {
onChange(
resourceList
.map((resource) =>
resource[idName] === id
? { [idName]: id, amount }
: { [idName]: resource[idName], amount: resource.amount }
)
.filter((resource) => resource.amount > 0) as ResourceSelectorValueItem<IdName>[]
);
}
},
[resourceList, onChange, idName]
);

return (
<Table
dataSource={resourceList}
showHeader={!!title}
pagination={pagination ? { pageSize: 5, showLessItems: true } : false}
rowKey="id"
>
<Table.Column title={title} dataIndex="name" width="65%" ellipsis={true} />
<Table.Column
width="35%"
render={(item: ResourceSelectorValueItem<IdName>) => {
return <NumberPicker value={item.amount} onChange={(value) => handleChangeAmount(item[idName], value)} />;
}}
/>
</Table>
);
}
32 changes: 16 additions & 16 deletions admin/src/components/sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,36 @@ const routes: RouteItem[] = [
icon: () => <BookOutlined />,
name: 'Validar Produtos'
},
// {
// path: '/relatorios',
// icon: () => <BarChartOutlined />,
// name: 'Relatórios'
// },
{
path: '/beneficios',
path: '/consumo',
icon: () => <CarryOutOutlined />,
name: 'Beneficios'
name: 'Informar consumo'
},
{
path: '/familias',
icon: () => <IdcardOutlined />,
name: 'Famílias'
},
{
path: '/beneficios',
icon: () => <CarryOutOutlined />,
name: 'Beneficios'
},
{
path: '/usuarios',
icon: () => <UserOutlined />,
name: 'Usuários'
},
{
path: '/consumo',
icon: () => <CarryOutOutlined />,
name: 'Informar consumo'
},
path: '/instituicoes',
icon: () => <BankOutlined />,
name: 'Instituições'
}
// {
// path: '/relatorios',
// icon: () => <BarChartOutlined />,
// name: 'Relatórios'
// },
// {
// path: '/lojas',
// icon: () => <ShopOutlined />,
Expand All @@ -76,11 +81,6 @@ const routes: RouteItem[] = [
// icon: () => <SolutionOutlined />,
// name: 'Estabelecimentos'
// },
{
path: '/instituicoes',
icon: () => <BankOutlined />,
name: 'Instituições'
}
];

const privateRoutes: RouteItem[] = [
Expand Down
5 changes: 5 additions & 0 deletions admin/src/interfaces/benefit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ export interface Benefit {
createdAt?: number | Date | null;
updatedAt?: number | Date | null;
deletedAt?: number | Date | null;
benefitProduct?: {
id: number | string;
productsId: number | string;
amount: number;
}[];
}
Loading

0 comments on commit 3bea90f

Please sign in to comment.