Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Brunobento1990 committed Oct 4, 2024
1 parent acda97c commit 714c2a6
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 4,155 deletions.
4 changes: 2 additions & 2 deletions src/@open-adm/components/form/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Card, CardHeader, Box, Button } from "@mui/material";
import { Card, CardHeader, Box, Button, CircularProgress } from "@mui/material";
import { ReactNode } from "react";
import { useRouter } from "next/router";

Expand Down Expand Up @@ -43,7 +43,7 @@ export function Form(props: propsForm) {
variant="contained"
onClick={props.submit}
>
{props.titleButton ?? 'Salvar'}
{props.loading ? <>Aguarde ... <CircularProgress size={20} /></> : props.titleButton ?? 'Salvar'}
</Button>
}
</Box>
Expand Down
37 changes: 37 additions & 0 deletions src/@open-adm/components/input/input-date.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import CustomTextField from "src/@core/components/mui/text-field";

interface InputCustomProps {
fullWidth?: boolean;
label: string;
name: string;
id: string;
value: any;
onChange?: (id: string, newValue?: any) => void;
onBlur?: React.FocusEventHandler<HTMLInputElement | HTMLTextAreaElement>;
helperText?: any;
error?: boolean;
required?: boolean;
}

export function InputDate(props: InputCustomProps) {

return (
<CustomTextField
fullWidth={props.fullWidth}
label={props.label}
name={props.name}
id={props.id}
value={props.value}
onBlur={props.onBlur}
onChange={(e) => {
if (props.onChange) {
props.onChange(props.id, e.target.value)
}
}}
helperText={props.helperText}
error={props.error}
required={props.required}
type="date"
/>
);
}
2 changes: 1 addition & 1 deletion src/@open-adm/hooks/use-new-api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function useNewApi(props: propsUseApi) {
if (!props.notHandleError) {
handleError(error);
}
throw error;
return undefined;
}
}

Expand Down
84 changes: 50 additions & 34 deletions src/@open-adm/pages/estoque/relatorio-estoque/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Grid } from "@mui/material";
import { useFormik } from "formik";
import CustomTextField from "src/@core/components/mui/text-field";
import { useState } from "react";
import { YupAdapter } from "src/@open-adm/adapters/yup-adapter";
import { Form } from "src/@open-adm/components/form";
import { useApi } from "src/@open-adm/hooks/use-api";
import { InputDate } from "src/@open-adm/components/input/input-date";
import { useNewApi } from "src/@open-adm/hooks/use-new-api";
import { generatePdfFromBase64 } from "src/@open-adm/utils/download-pdf";

const defaultValues = {
Expand All @@ -12,28 +13,36 @@ const defaultValues = {
}

export function RelatorioEstoque() {
const { post } = useApi();
const [loading, setLoading] = useState(false);
const api = useNewApi({
method: 'POST',
url: 'movimentacao-de-produto/relatorio',
notAlert: true
});
const formik = useFormik({
initialValues: defaultValues,
validationSchema: new YupAdapter().string('dataInicial').string('dataFinal').build(),
onSubmit: (values) => submit(values),
onSubmit: submit,
});

async function submit(values: any) {
try {
const response = await post('movimentacao-de-produto/relatorio', values, 'Download efetuado com sucesso!');
if (response?.pdf) {
const pdf = await generatePdfFromBase64(response.pdf);
const link = document.createElement('a');
link.href = pdf;
link.download = `${formik.values.dataInicial}-${formik.values.dataFinal}.pdf`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
} catch (error) {

async function submit() {
setLoading(true);
const response = await api.fecth<any>({
body: {
...formik.values
},
message: 'Download efetuado com sucesso!'
});
if (response?.pdf) {
const pdf = await generatePdfFromBase64(response.pdf);
const link = document.createElement('a');
link.href = pdf;
link.download = `${formik.values.dataInicial}-${formik.values.dataFinal}.pdf`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
setLoading(false);
}

return (
Expand All @@ -42,36 +51,43 @@ export function RelatorioEstoque() {
submit={formik.submitForm}
title="Relatório de estoque"
titleButton="Download"
loading={loading}
>
<Grid container spacing={5}>
<Grid item xs={12} sm={4}>
<CustomTextField
<InputDate
fullWidth
label='Data inicial'
name='dataInicial'
id='dataInicial'
required
id="dataInicial"
label="Data inicial"
name="dataInicial"
value={formik.values.dataInicial}
onBlur={formik.handleBlur}
onChange={formik.handleChange}
required
helperText={formik.touched.dataInicial && formik.errors.dataInicial}
error={!!(formik.touched.dataInicial && formik.errors.dataInicial)}
type="date"
onChange={(_, value) => {
formik.setValues({
...formik.values,
dataInicial: value
})
}}
/>
</Grid>
<Grid item xs={12} sm={4}>
<CustomTextField
<InputDate
fullWidth
label='Data final'
name='dataFinal'
id='dataFinal'
required
id="dataFinal"
label="Data final"
name="dataFinal"
value={formik.values.dataFinal}
onBlur={formik.handleBlur}
onChange={formik.handleChange}
required
helperText={formik.touched.dataFinal && formik.errors.dataFinal}
error={!!(formik.touched.dataFinal && formik.errors.dataFinal)}
type="date"
onChange={(_, value) => {
formik.setValues({
...formik.values,
dataFinal: value
})
}}
/>
</Grid>
</Grid>
Expand Down
10 changes: 10 additions & 0 deletions src/@open-adm/utils/convert-date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function convertDate(date?: string): string | undefined {
if (!date) {
return undefined;
}
const newDateSplit = date.split("-");
if (newDateSplit.length !== 3) {
return date;
}
return `${newDateSplit[0]}-${newDateSplit[2]}-${newDateSplit[1]}`;
}
10 changes: 5 additions & 5 deletions src/navigation/vertical/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ const navigation = (): VerticalNavItemsType => {
path: '/estoque/movimentacao-produto',
icon: 'arcticons:stockswidget'
},
{
title: 'Posição estoque',
path: '/estoque/posicao-estoque',
icon: 'icon-park-outline:database-position'
},
{
title: 'Relatórios',
icon: "mdi:report-finance",
Expand All @@ -64,11 +69,6 @@ const navigation = (): VerticalNavItemsType => {
},
]
},
{
title: 'Posição estoque',
path: '/estoque/posicao-estoque',
icon: 'icon-park-outline:database-position'
}
]
},
{
Expand Down
Loading

0 comments on commit 714c2a6

Please sign in to comment.