Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update modal delete project #59

Merged
merged 2 commits into from
Nov 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/main/java/br/ufrn/dct/apf/controller/ProjectController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import static java.util.Objects.isNull;

@Controller
public class ProjectController extends AbstractController {
Expand Down Expand Up @@ -65,10 +66,28 @@ public ModelAndView edit(@PathVariable("id") Long id) {
}

@GetMapping("/project/delete/{id}")
public ModelAndView delete(@PathVariable("id") Long id) {
projectService.delete(id);
public ModelAndView deleteView(@PathVariable("id") Long id) {
ModelAndView mv = new ModelAndView("project/list");

return findAll();
User current = getCurrentUser();
List<Project> projects = projectService.findByUserId(current.getId());
Project project = projectService.findOne(id);

mv.addObject("projects", projects);
mv.addObject("project", project);
mv.addObject("showModalDelete", true);

return mv;
}

@PostMapping("/project/delete/{id}")
public String delete(@PathVariable("id") Long id) {
// @TODO validar permissões
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mais uma validação para ser feita nos projetos #23, melhor alterar o título para revisar o delete, update e view

if (!isNull(projectService.findOne(id))) {
projectService.delete(id);
}

return "redirect:/project";
}

@PostMapping("/project/save")
Expand Down Expand Up @@ -123,7 +142,7 @@ public ProjectSuggestionWrapper autocompleteSuggestions(@RequestParam("searchstr
}

// truncate the list to the first n, max 20 elements
int n = suggestions.size() > 20 ? 20 : suggestions.size();
int n = Math.min(suggestions.size(), 20);
List<ProjectSuggestion> sulb = new ArrayList<>(suggestions.subList(0, n));

ProjectSuggestionWrapper sw = new ProjectSuggestionWrapper();
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/project/add.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
</div>
</div>
</div>
<div class="form-group row">
<div class="form-group row" th:if="${project.id}">
<div class="col-md-1">
<input type="text" class="form-control input-sm" id="id" th:field="*{id}" readOnly="readonly"/>
</div>
Expand Down
74 changes: 26 additions & 48 deletions src/main/resources/templates/project/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,34 @@
<body>
<div th:replace="fragments/header :: header"></div>

<div class="modal modal-delete" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Deletar Registro</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<p id="texto-modal"></p>
</div>
<div class="modal-footer">
<a id="link-delete">
<button type="button" class="btn btn-danger">Sim, quero deletar</button>
</a>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>
<!-- DIALOG DELETE PROJECT-->
<form method="POST" th:action="@{/project/delete/{id}(id=${project.id})}" th:if="${project != null}">
<div id="modal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<span class="modal-title">Excluir projeto</span>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<span th:text="${'Tem certeza que deseja excluir o projeto ' + project.id + ' - ' + project.name+ '?'}"></span>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-danger">Sim, quero deletar</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>
</div>
</div>
</div>

<script type="text/javascript">
$(document).ready(() => {
$('#modal').modal('show');
})
</script>
</div>
</div>
</form>

<div id="wrapper" class="wrapper.toggled toggled" style="margin-top: 50px">
<!-- Sidebar -->
Expand Down Expand Up @@ -80,6 +87,7 @@ <h5 class="modal-title">Deletar Registro</h5>
class="btn btn-sm btn-primary"
th:href="@{/project/edit/{id}(id=${project.id})}">Editar</a>
<a th:id="${project.id}"
th:href="@{/project/delete/{id}(id=${project.id})}"
class="delete btn btn-sm btn-danger confirModal">Excluir</a>
</div>
</td>
Expand All @@ -99,35 +107,5 @@ <h5 class="modal-title">Deletar Registro</h5>
<div th:include="fragments/footer :: footer-authenticated" align="center"></div>
</div>
</div>

<script type="text/javascript">
/* Iniciando documento jQuery */
$(document).ready(
function () {

/* Adicionando ouvindo no clique do botão delete */
$(".delete").click(
function () {

/* Guardando o ID do registro (foi colocado como um atributo n o botão) */

const id = $(this).attr("id");

/* Adicionando o texto ao modal */
$("#texto-modal").text("Tem certeza que deseja deletar o registro #" + id + "?");

/* Adicionando a URL para delete */
$("#link-delete")
.attr(
"href",
"/apf/project/delete/"
+ id);

/* Abrindo o Modal */
$('.modal-delete').modal()
})
})
</script>

</body>
</html>