From cf3e6c484b7eee66b450dc22a3e1df04209e3df5 Mon Sep 17 00:00:00 2001 From: Alex Santos Date: Wed, 15 Jun 2022 09:46:27 -0300 Subject: [PATCH] consumption API --- .../administrativeTables/FaqTable.tsx | 21 +++--- src/contexts/AuthContext.tsx | 2 +- src/pages/administrative/faq/index.tsx | 70 +++++++++++++++++-- .../administrative/notification/index.tsx | 2 +- src/services/auth.ts | 2 +- src/services/ssrApi.ts | 2 +- 6 files changed, 80 insertions(+), 19 deletions(-) diff --git a/src/components/administrativeTables/FaqTable.tsx b/src/components/administrativeTables/FaqTable.tsx index 7877191..62e5955 100644 --- a/src/components/administrativeTables/FaqTable.tsx +++ b/src/components/administrativeTables/FaqTable.tsx @@ -19,7 +19,9 @@ import Toolbar from '@mui/material/Toolbar'; import Tooltip from '@mui/material/Tooltip'; import Typography from '@mui/material/Typography'; import { visuallyHidden } from '@mui/utils'; +import { GetServerSideProps } from 'next'; import React, { useState } from 'react'; +import getAPIClient from '../../services/ssrApi'; import { ClientTableView, StyledStatus } from './ClientsTableView'; @@ -171,7 +173,7 @@ function EnhancedTableHead(props: EnhancedTableProps) { ); } -export default function FaqTable() { +export default function FaqTable({questionData}: any) { const [order, setOrder] = useState('asc'); const [orderBy, setOrderBy] = useState('status'); const [selected, setSelected] = useState([]); @@ -179,6 +181,8 @@ export default function FaqTable() { const [dense, setDense] = useState(false); const [rowsPerPage, setRowsPerPage] = useState(5); + console.table(questionData) + console.table(rows) const handleRequestSort = ( event: React.MouseEvent, property: keyof Data, @@ -190,7 +194,7 @@ export default function FaqTable() { const handleSelectAllClick = (event: React.ChangeEvent) => { if (event.target.checked) { - const newSelecteds = rows.map((n) => n.question); + const newSelecteds = questionData.map((n) => n.questionData); setSelected(newSelecteds); return; } @@ -247,23 +251,23 @@ export default function FaqTable() { orderBy={orderBy} onSelectAllClick={handleSelectAllClick} onRequestSort={handleRequestSort} - rowCount={rows.length} + rowCount={questionData.length} /> - {stableSort(rows, getComparator(order, orderBy)) + {stableSort(questionData, getComparator(order, orderBy)) .slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage) .map((row, index) => { - const isItemSelected = isSelected(row.question); + const isItemSelected = isSelected(row.id); const labelId = `enhanced-table-checkbox-${index}`; return ( handleClick(event, row.question)} + onClick={(event) => handleClick(event, row.id)} role="checkbox" aria-checked={isItemSelected} tabIndex={-1} - key={row.question} + key={row.id} selected={isItemSelected} > @@ -284,7 +288,7 @@ export default function FaqTable() { {row.question} {row.answer} - {row.status} + {row.deleted_at? 'ativo' : 'inativo'} ); })} @@ -313,3 +317,4 @@ export default function FaqTable() { ); } + diff --git a/src/contexts/AuthContext.tsx b/src/contexts/AuthContext.tsx index 00093ae..b0ae9d6 100644 --- a/src/contexts/AuthContext.tsx +++ b/src/contexts/AuthContext.tsx @@ -4,7 +4,7 @@ import Router from 'next/router' import { setCookie } from "nookies"; import { signInRequest } from "../services/auth"; -import api from "../services/api"; +import { api } from "../services/api"; type UserType = { name: string; diff --git a/src/pages/administrative/faq/index.tsx b/src/pages/administrative/faq/index.tsx index 91ae987..1dc6001 100644 --- a/src/pages/administrative/faq/index.tsx +++ b/src/pages/administrative/faq/index.tsx @@ -9,6 +9,8 @@ import Image from 'next/image' import Link from 'next/link' import { useRouter } from 'next/router' import React, { useEffect, useState } from 'react' +import { api } from '../../../services/api'; + import FaqTable from '../../../components/administrativeTables/FaqTable'; import BasicButton from '../../../components/buttons/basicButton/BasicButton'; @@ -17,6 +19,9 @@ import FaqButton2 from '../../../components/buttons/faqButton/FaqButton2'; import Header from '../../../components/header/Header' import PageTitle from '../../../components/pageTitle/PageTitle' import { FaqView } from '../../../styles/layouts/commonQuestions/FaqView' +import getAPIClient from '../../../services/ssrApi'; +import { GetServerSideProps } from 'next'; +import { parseCookies } from 'nookies'; const style = { position: 'absolute' as const, @@ -31,7 +36,27 @@ const style = { p: 4, }; -export default function Sidebar() { +type FaqInterface = { + question: string; + answer: string; + +} + + +export default function Sidebar({faqData}) { + async function handleRegisterNewFaq({question, answer}: FaqInterface) { + await api.post('/faq', { + "question": question, + "answer": answer, + + }).then(res => console.log(res.data)) + } + + const [faq, setFaq] = useState({ + question: '', + answer: '', + }) + const [open, setOpen] = React.useState(false); const handleOpen = () => setOpen(true); @@ -61,21 +86,52 @@ export default function Sidebar() { Adicionar/Editar Pergunta
-

- + + setFaq({...faq, question:value.target.value})} sx={{width:710, ml:8}} variant="outlined" />

+ setFaq({...faq, answer:value.target.value})} sx={{width:710, ml:8}} variant="outlined" /> +

- + handleRegisterNewFaq(faq)} + /> - + ) } + +export const getServerSideProps: GetServerSideProps = async (ctx) => { + const apiClient = getAPIClient(ctx) + const { ['@smartAuth-token']: token } = parseCookies(ctx) + console.log('teste') + let faqData = []; + + +await apiClient.get('/faq').then(res => { + faqData = res.data +}).catch(res => { + console.log(res) +}) + console.table(faqData); + + if (!token) { + return { + redirect: { + destination: '/', + permanent: false + } + } + } + + return { + props: { + faqData + } + } +} diff --git a/src/pages/administrative/notification/index.tsx b/src/pages/administrative/notification/index.tsx index c608fe7..eb68a3e 100644 --- a/src/pages/administrative/notification/index.tsx +++ b/src/pages/administrative/notification/index.tsx @@ -21,7 +21,7 @@ import FaqButton1 from '../../../components/buttons/faqButton/FaqButton1'; import FaqButton2 from '../../../components/buttons/faqButton/FaqButton2'; import Header from '../../../components/header/Header' import PageTitle from '../../../components/pageTitle/PageTitle' -import api from '../../../services/api'; +import { api } from '../../../services/api'; import { FaqView } from '../../../styles/layouts/commonQuestions/FaqView' import { NotificationView } from './notificationView' diff --git a/src/services/auth.ts b/src/services/auth.ts index df78f49..3a36874 100644 --- a/src/services/auth.ts +++ b/src/services/auth.ts @@ -1,4 +1,4 @@ -import api from "./api"; +import { api } from "./api"; export const TOKEN_KEY = "@smartAuth-token"; diff --git a/src/services/ssrApi.ts b/src/services/ssrApi.ts index 4bad590..8a23c97 100644 --- a/src/services/ssrApi.ts +++ b/src/services/ssrApi.ts @@ -10,7 +10,7 @@ export default function getAPIClient(ctx?: Pick | { req: express.Request; } | null | undefined) { - const { '@smartAuth-token': token } = parseCookies() + const { '@smartAuth-token': token } = parseCookies(ctx) const api = axios.create({ baseURL: "https://smart-energia-api.herokuapp.com/api",