Skip to content

Commit

Permalink
Botão de exclusão e mudança nos botões do QuickAnswers
Browse files Browse the repository at this point in the history
  • Loading branch information
rtenorioh committed Sep 1, 2022
1 parent 66d47df commit 8bbc3dd
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 39 deletions.
12 changes: 12 additions & 0 deletions backend/src/controllers/QuickAnswerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import CreateQuickAnswerService from "../services/QuickAnswerService/CreateQuick
import ShowQuickAnswerService from "../services/QuickAnswerService/ShowQuickAnswerService";
import UpdateQuickAnswerService from "../services/QuickAnswerService/UpdateQuickAnswerService";
import DeleteQuickAnswerService from "../services/QuickAnswerService/DeleteQuickAnswerService";
import DeleteAllQuickAnswerService from "../services/QuickAnswerService/DeleteAllQuickAnswerService";

import AppError from "../errors/AppError";

Expand Down Expand Up @@ -115,3 +116,14 @@ export const remove = async (

return res.status(200).json({ message: "Quick Answer deleted" });
};

export const removeAll = async (
req: Request,
res: Response
): Promise<Response> => {
const { quickAnswerId } = req.params;

await DeleteAllQuickAnswerService();

return res.status(200).json({ message: "All Quick Answer deleted" });
};
22 changes: 4 additions & 18 deletions backend/src/routes/quickAnswerRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,10 @@ const quickAnswerRoutes = express.Router();

quickAnswerRoutes.get("/quickAnswers", isAuth, QuickAnswerController.index);

quickAnswerRoutes.get(
"/quickAnswers/:quickAnswerId",
isAuth,
QuickAnswerController.show
);

quickAnswerRoutes.get("/quickAnswers/:quickAnswerId", isAuth, QuickAnswerController.show);
quickAnswerRoutes.post("/quickAnswers", isAuth, QuickAnswerController.store);

quickAnswerRoutes.put(
"/quickAnswers/:quickAnswerId",
isAuth,
QuickAnswerController.update
);

quickAnswerRoutes.delete(
"/quickAnswers/:quickAnswerId",
isAuth,
QuickAnswerController.remove
);
quickAnswerRoutes.put("/quickAnswers/:quickAnswerId", isAuth, QuickAnswerController.update);
quickAnswerRoutes.delete("/quickAnswers/:quickAnswerId", isAuth, QuickAnswerController.remove);
quickAnswerRoutes.delete("/quickAnswers", isAuth, QuickAnswerController.removeAll);

export default quickAnswerRoutes;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import QuickAnswer from "../../models/QuickAnswer";
import AppError from "../../errors/AppError";

const DeleteAllQuickAnswerService = async (): Promise<void> => {
await QuickAnswer.findAll();

if (!QuickAnswer) {
throw new AppError("ERR_NO_QUICK_ANSWER_FOUND", 404);
}

await QuickAnswer.destroy({
where: { }
});
};

export default DeleteAllQuickAnswerService;
83 changes: 62 additions & 21 deletions frontend/src/pages/QuickAnswers/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState, useEffect, useReducer } from "react";
import openSocket from "../../services/socket-io";
import { useHistory } from "react-router-dom";

import {
Button,
Expand All @@ -13,20 +14,27 @@ import {
TableRow,
InputAdornment,
TextField,
Tooltip
} from "@material-ui/core";
import { Edit, DeleteOutline } from "@material-ui/icons";
import SearchIcon from "@material-ui/icons/Search";
import {
AddCircleOutline,
DeleteForever,
DeleteOutline,
Edit,
Search
} from "@material-ui/icons";

import api from "../../services/api";
import { i18n } from "../../translate/i18n";

import MainContainer from "../../components/MainContainer";
import MainHeader from "../../components/MainHeader";
import MainHeaderButtonsWrapper from "../../components/MainHeaderButtonsWrapper";
import Title from "../../components/Title";

import api from "../../services/api";
import { i18n } from "../../translate/i18n";
import TableRowSkeleton from "../../components/TableRowSkeleton";
import QuickAnswersModal from "../../components/QuickAnswersModal";
import ConfirmationModal from "../../components/ConfirmationModal";

import { toast } from "react-toastify";
import toastError from "../../errors/toastError";

Expand Down Expand Up @@ -85,6 +93,7 @@ const useStyles = makeStyles((theme) => ({

const QuickAnswers = () => {
const classes = useStyles();
const history = useHistory();

const [loading, setLoading] = useState(false);
const [pageNumber, setPageNumber] = useState(1);
Expand All @@ -93,6 +102,7 @@ const QuickAnswers = () => {
const [selectedQuickAnswers, setSelectedQuickAnswers] = useState(null);
const [quickAnswersModalOpen, setQuickAnswersModalOpen] = useState(false);
const [deletingQuickAnswers, setDeletingQuickAnswers] = useState(null);
const [deletingAllQuickAnswers, setDeletingAllQuickAnswers] = useState(null);
const [confirmModalOpen, setConfirmModalOpen] = useState(false);
const [hasMore, setHasMore] = useState(false);

Expand Down Expand Up @@ -173,6 +183,19 @@ const QuickAnswers = () => {
setPageNumber(1);
};

const handleDeleteAllQuickAnswers = async () => {
try {
await api.delete("/quickAnswers");
toast.success(i18n.t("quickAnswers.toasts.deletedAll"));
history.go(0);
} catch (err) {
toastError(err);
}
setDeletingAllQuickAnswers(null);
setSearchParam("");
setPageNumber();
};

const loadMore = () => {
setPageNumber((prevState) => prevState + 1);
};
Expand All @@ -189,16 +212,20 @@ const QuickAnswers = () => {
<MainContainer>
<ConfirmationModal
title={
deletingQuickAnswers &&
`${i18n.t("quickAnswers.confirmationModal.deleteTitle")} ${
deletingQuickAnswers.shortcut
}?`
deletingQuickAnswers ? `${i18n.t("quickAnswers.confirmationModal.deleteTitle")} ${deletingQuickAnswers.shortcut}?`
: `${i18n.t("quickAnswers.confirmationModal.deleteAllTitle")}`
}
open={confirmModalOpen}
onClose={setConfirmModalOpen}
onConfirm={() => handleDeleteQuickAnswers(deletingQuickAnswers.id)}
onConfirm={() =>
deletingQuickAnswers ? handleDeleteQuickAnswers(deletingQuickAnswers.id)
: handleDeleteAllQuickAnswers(deletingAllQuickAnswers)
}
>
{i18n.t("quickAnswers.confirmationModal.deleteMessage")}
{
deletingQuickAnswers ? `${i18n.t("quickAnswers.confirmationModal.deleteMessage")}`
: `${i18n.t("quickAnswers.confirmationModal.deleteAllMessage")}`
}
</ConfirmationModal>
<QuickAnswersModal
open={quickAnswersModalOpen}
Expand All @@ -207,7 +234,7 @@ const QuickAnswers = () => {
quickAnswerId={selectedQuickAnswers && selectedQuickAnswers.id}
></QuickAnswersModal>
<MainHeader>
<Title>{i18n.t("quickAnswers.title")}</Title>
<Title>{i18n.t("quickAnswers.title")} ({quickAnswers.length})</Title>
<MainHeaderButtonsWrapper>
<TextField
placeholder={i18n.t("quickAnswers.searchPlaceholder")}
Expand All @@ -217,18 +244,32 @@ const QuickAnswers = () => {
InputProps={{
startAdornment: (
<InputAdornment position="start">
<SearchIcon color="secondary" />
<Search color="secondary" />
</InputAdornment>
),
}}
/>
<Button
variant="contained"
color="primary"
onClick={handleOpenQuickAnswersModal}
>
{i18n.t("quickAnswers.buttons.add")}
</Button>
<Tooltip title={i18n.t("quickAnswers.buttons.add")}>
<Button
variant="contained"
color="primary"
onClick={handleOpenQuickAnswersModal}
>
<AddCircleOutline />
</Button>
</Tooltip>
<Tooltip title={i18n.t("quickAnswers.buttons.deleteAll")}>
<Button
variant="contained"
color="primary"
onClick={(e) => {
setConfirmModalOpen(true);
setDeletingAllQuickAnswers(quickAnswers);
}}
>
<DeleteForever />
</Button>
</Tooltip>
</MainHeaderButtonsWrapper>
</MainHeader>
<Paper
Expand Down Expand Up @@ -281,7 +322,7 @@ const QuickAnswers = () => {
</TableBody>
</Table>
</Paper>
</MainContainer>
</MainContainer >
);
};

Expand Down
4 changes: 4 additions & 0 deletions frontend/src/translate/languages/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,18 @@ const messages = {
},
buttons: {
add: "Add Quick Reply",
deleteAll: "Delete All Quick Replies",
},
toasts: {
deleted: "Quick Reply deleted successfully.",
deletedAll: "All Quick Replies deleted.",
},
searchPlaceholder: "Search...",
confirmationModal: {
deleteTitle: "Are you sure you want to delete this Quick Reply: ",
deleteAllTitle: "Are you sure you want to delete all Quick Replies?",
deleteMessage: "This action cannot be reversed.",
deleteAllMessage: "This action cannot be reversed.",
},
},
users: {
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/translate/languages/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,18 @@ const messages = {
},
buttons: {
add: "Agregar respuesta rápida",
deletedAll: "Eliminar todas las respuestas rápidas",
},
toasts: {
deleted: "Respuesta rápida eliminada con éxito.",
deletedAll: "Todas las respuestas rápidas eliminadas.",
},
searchPlaceholder: "Buscar...",
confirmationModal: {
deleteTitle: "¿Está seguro de que desea eliminar esta respuesta rápida: ",
deletedAll: "¿Está seguro de que desea eliminar todas las respuestas rápidas?",
deleteMessage: "Esta acción no se puede revertir.",
deleteAllMessage: "Esta acción no se puede revertir.",
},
},
users: {
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/translate/languages/pt.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,18 @@ const messages = {
},
buttons: {
add: "Adicionar Resposta Rápida",
deleteAll: "Excluir Todas Respostas Rápidas",
},
toasts: {
deleted: "Resposta Rápida excluída com sucesso.",
deletedAll: "Todas as Respostas Rápidas excluídas.",
},
searchPlaceholder: "Pesquisar...",
confirmationModal: {
deleteTitle: "Você tem certeza que quer excluir esta Resposta Rápida: ",
deleteAllTitle: "Você tem certeza que quer excluir todas Respostas Rápidas?",
deleteMessage: "Esta ação não pode ser revertida.",
deleteAllMessage: "Esta ação não pode ser revertida.",
},
},
users: {
Expand Down

0 comments on commit 8bbc3dd

Please sign in to comment.