Compare commits
3 Commits
9957383e88
...
feature/00
| Author | SHA1 | Date | |
|---|---|---|---|
| 3f152eeccc | |||
| 050f248857 | |||
| 1ec7a72908 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,5 +1,8 @@
|
|||||||
# Backend
|
# Backend
|
||||||
backend/target/
|
backend/target/
|
||||||
|
backend/data/
|
||||||
|
backend/*.mv.db
|
||||||
|
backend/*.trace.db
|
||||||
|
|
||||||
# Frontend
|
# Frontend
|
||||||
frontend/node_modules/
|
frontend/node_modules/
|
||||||
|
|||||||
@ -6,6 +6,12 @@ innehåller två separata applikationer:
|
|||||||
- en backend byggd med Java 21, Spring Boot och Maven
|
- en backend byggd med Java 21, Spring Boot och Maven
|
||||||
- en frontend byggd med React, TypeScript, Vite och pnpm
|
- en frontend byggd med React, TypeScript, Vite och pnpm
|
||||||
|
|
||||||
|
Backend använder en lokal filbaserad H2-databas i `backend/data`. Databasschemat
|
||||||
|
hanteras med Flyway. Databasfilerna är lokala och ignoreras av Git.
|
||||||
|
|
||||||
|
API:t innehåller endpoints under `/api/users` för användare och `/api/tasks` för
|
||||||
|
att skapa och lista gemensamma hushållsuppgifter.
|
||||||
|
|
||||||
## Starta backend
|
## Starta backend
|
||||||
|
|
||||||
Backend startar på port 8080.
|
Backend startar på port 8080.
|
||||||
@ -43,4 +49,3 @@ Frontend:
|
|||||||
cd frontend
|
cd frontend
|
||||||
pnpm test
|
pnpm test
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@ -26,6 +26,19 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-flyway</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
@ -42,4 +55,3 @@
|
|||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
</project>
|
</project>
|
||||||
|
|
||||||
|
|||||||
5
backend/src/main/java/se/rubble/hemhub/api/ApiError.java
Normal file
5
backend/src/main/java/se/rubble/hemhub/api/ApiError.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package se.rubble.hemhub.api;
|
||||||
|
|
||||||
|
public record ApiError(String code, String message) {
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,37 @@
|
|||||||
|
package se.rubble.hemhub.api;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
import se.rubble.hemhub.task.InvalidTaskException;
|
||||||
|
import se.rubble.hemhub.user.InvalidUserNameException;
|
||||||
|
import se.rubble.hemhub.user.UserNameAlreadyExistsException;
|
||||||
|
|
||||||
|
@RestControllerAdvice
|
||||||
|
public class ApiExceptionHandler {
|
||||||
|
|
||||||
|
@ExceptionHandler(InvalidUserNameException.class)
|
||||||
|
public ResponseEntity<ApiError> handleInvalidUserName() {
|
||||||
|
return ResponseEntity.badRequest()
|
||||||
|
.body(new ApiError(
|
||||||
|
"INVALID_USER_NAME",
|
||||||
|
"Namnet måste innehålla mellan 1 och 50 tecken."));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(UserNameAlreadyExistsException.class)
|
||||||
|
public ResponseEntity<ApiError> handleDuplicateUserName() {
|
||||||
|
return ResponseEntity.status(HttpStatus.CONFLICT)
|
||||||
|
.body(new ApiError(
|
||||||
|
"USER_NAME_ALREADY_EXISTS",
|
||||||
|
"En användare med det namnet finns redan."));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(InvalidTaskException.class)
|
||||||
|
public ResponseEntity<ApiError> handleInvalidTask(InvalidTaskException exception) {
|
||||||
|
return ResponseEntity.badRequest()
|
||||||
|
.body(new ApiError("INVALID_TASK", exception.getMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
package se.rubble.hemhub.task;
|
||||||
|
|
||||||
|
public record CreateTaskRequest(String title, String description) {
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
package se.rubble.hemhub.task;
|
||||||
|
|
||||||
|
public class InvalidTaskException extends RuntimeException {
|
||||||
|
|
||||||
|
public InvalidTaskException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
69
backend/src/main/java/se/rubble/hemhub/task/Task.java
Normal file
69
backend/src/main/java/se/rubble/hemhub/task/Task.java
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
package se.rubble.hemhub.task;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.EnumType;
|
||||||
|
import jakarta.persistence.Enumerated;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "task")
|
||||||
|
class Task {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private UUID id;
|
||||||
|
|
||||||
|
@Column(nullable = false, length = 100)
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
@Column(length = 500)
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
@Enumerated(EnumType.STRING)
|
||||||
|
@Column(nullable = false, length = 20)
|
||||||
|
private TaskStatus status;
|
||||||
|
|
||||||
|
@Column(name = "created_at", nullable = false)
|
||||||
|
private Instant createdAt;
|
||||||
|
|
||||||
|
protected Task() {
|
||||||
|
}
|
||||||
|
|
||||||
|
Task(
|
||||||
|
UUID id,
|
||||||
|
String title,
|
||||||
|
String description,
|
||||||
|
TaskStatus status,
|
||||||
|
Instant createdAt) {
|
||||||
|
this.id = id;
|
||||||
|
this.title = title;
|
||||||
|
this.description = description;
|
||||||
|
this.status = status;
|
||||||
|
this.createdAt = createdAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
UUID getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
TaskStatus getStatus() {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
Instant getCreatedAt() {
|
||||||
|
return createdAt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,36 @@
|
|||||||
|
package se.rubble.hemhub.task;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/tasks")
|
||||||
|
public class TaskController {
|
||||||
|
|
||||||
|
private final TaskService taskService;
|
||||||
|
|
||||||
|
TaskController(TaskService taskService) {
|
||||||
|
this.taskService = taskService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<TaskResponse> findAll() {
|
||||||
|
return taskService.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
|
public TaskResponse create(@RequestBody(required = false) CreateTaskRequest request) {
|
||||||
|
return taskService.create(
|
||||||
|
request == null ? null : request.title(),
|
||||||
|
request == null ? null : request.description());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
package se.rubble.hemhub.task;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
interface TaskRepository extends JpaRepository<Task, UUID> {
|
||||||
|
|
||||||
|
List<Task> findAllByOrderByCreatedAtAscIdAsc();
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package se.rubble.hemhub.task;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public record TaskResponse(
|
||||||
|
UUID id,
|
||||||
|
String title,
|
||||||
|
String description,
|
||||||
|
TaskStatus status,
|
||||||
|
Instant createdAt) {
|
||||||
|
|
||||||
|
static TaskResponse from(Task task) {
|
||||||
|
return new TaskResponse(
|
||||||
|
task.getId(),
|
||||||
|
task.getTitle(),
|
||||||
|
task.getDescription(),
|
||||||
|
task.getStatus(),
|
||||||
|
task.getCreatedAt());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
73
backend/src/main/java/se/rubble/hemhub/task/TaskService.java
Normal file
73
backend/src/main/java/se/rubble/hemhub/task/TaskService.java
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
package se.rubble.hemhub.task;
|
||||||
|
|
||||||
|
import java.time.Clock;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class TaskService {
|
||||||
|
|
||||||
|
private final TaskRepository taskRepository;
|
||||||
|
private final Clock clock;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
TaskService(TaskRepository taskRepository) {
|
||||||
|
this(taskRepository, Clock.systemUTC());
|
||||||
|
}
|
||||||
|
|
||||||
|
TaskService(TaskRepository taskRepository, Clock clock) {
|
||||||
|
this.taskRepository = taskRepository;
|
||||||
|
this.clock = clock;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
List<TaskResponse> findAll() {
|
||||||
|
return taskRepository.findAllByOrderByCreatedAtAscIdAsc().stream()
|
||||||
|
.map(TaskResponse::from)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
TaskResponse create(String requestedTitle, String requestedDescription) {
|
||||||
|
String title = requestedTitle == null ? "" : requestedTitle.trim();
|
||||||
|
String description = normalizeDescription(requestedDescription);
|
||||||
|
|
||||||
|
if (title.isEmpty() || codePointLength(title) > 100) {
|
||||||
|
throw new InvalidTaskException(
|
||||||
|
"Titeln måste innehålla mellan 1 och 100 tecken.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (description != null && codePointLength(description) > 500) {
|
||||||
|
throw new InvalidTaskException(
|
||||||
|
"Beskrivningen får innehålla högst 500 tecken.");
|
||||||
|
}
|
||||||
|
|
||||||
|
Task task = new Task(
|
||||||
|
UUID.randomUUID(),
|
||||||
|
title,
|
||||||
|
description,
|
||||||
|
TaskStatus.WAITING,
|
||||||
|
Instant.now(clock));
|
||||||
|
|
||||||
|
return TaskResponse.from(taskRepository.save(task));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String normalizeDescription(String requestedDescription) {
|
||||||
|
if (requestedDescription == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
String description = requestedDescription.trim();
|
||||||
|
return description.isEmpty() ? null : description;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int codePointLength(String value) {
|
||||||
|
return value.codePointCount(0, value.length());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
package se.rubble.hemhub.task;
|
||||||
|
|
||||||
|
public enum TaskStatus {
|
||||||
|
WAITING,
|
||||||
|
IN_PROGRESS,
|
||||||
|
COMPLETED
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
package se.rubble.hemhub.user;
|
||||||
|
|
||||||
|
public record CreateUserRequest(String name) {
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
package se.rubble.hemhub.user;
|
||||||
|
|
||||||
|
public class InvalidUserNameException extends RuntimeException {
|
||||||
|
}
|
||||||
|
|
||||||
48
backend/src/main/java/se/rubble/hemhub/user/User.java
Normal file
48
backend/src/main/java/se/rubble/hemhub/user/User.java
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package se.rubble.hemhub.user;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "app_user")
|
||||||
|
class User {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private UUID id;
|
||||||
|
|
||||||
|
@Column(nullable = false, length = 50)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@Column(name = "normalized_name", nullable = false, length = 150, unique = true)
|
||||||
|
private String normalizedName;
|
||||||
|
|
||||||
|
@Column(name = "created_at", nullable = false)
|
||||||
|
private Instant createdAt;
|
||||||
|
|
||||||
|
protected User() {
|
||||||
|
}
|
||||||
|
|
||||||
|
User(UUID id, String name, String normalizedName, Instant createdAt) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.normalizedName = normalizedName;
|
||||||
|
this.createdAt = createdAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
UUID getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
Instant getCreatedAt() {
|
||||||
|
return createdAt;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
package se.rubble.hemhub.user;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/users")
|
||||||
|
public class UserController {
|
||||||
|
|
||||||
|
private final UserService userService;
|
||||||
|
|
||||||
|
UserController(UserService userService) {
|
||||||
|
this.userService = userService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<UserResponse> findAll() {
|
||||||
|
return userService.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
|
public UserResponse create(@RequestBody(required = false) CreateUserRequest request) {
|
||||||
|
return userService.create(request == null ? null : request.name());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
package se.rubble.hemhub.user;
|
||||||
|
|
||||||
|
public class UserNameAlreadyExistsException extends RuntimeException {
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
package se.rubble.hemhub.user;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
interface UserRepository extends JpaRepository<User, UUID> {
|
||||||
|
|
||||||
|
boolean existsByNormalizedName(String normalizedName);
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
package se.rubble.hemhub.user;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public record UserResponse(UUID id, String name, Instant createdAt) {
|
||||||
|
|
||||||
|
static UserResponse from(User user) {
|
||||||
|
return new UserResponse(user.getId(), user.getName(), user.getCreatedAt());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
66
backend/src/main/java/se/rubble/hemhub/user/UserService.java
Normal file
66
backend/src/main/java/se/rubble/hemhub/user/UserService.java
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
package se.rubble.hemhub.user;
|
||||||
|
|
||||||
|
import java.time.Clock;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class UserService {
|
||||||
|
|
||||||
|
private static final Comparator<User> BY_DISPLAY_NAME =
|
||||||
|
Comparator.comparing(User::getName, String.CASE_INSENSITIVE_ORDER)
|
||||||
|
.thenComparing(User::getName)
|
||||||
|
.thenComparing(User::getId);
|
||||||
|
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
private final Clock clock;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
UserService(UserRepository userRepository) {
|
||||||
|
this(userRepository, Clock.systemUTC());
|
||||||
|
}
|
||||||
|
|
||||||
|
UserService(UserRepository userRepository, Clock clock) {
|
||||||
|
this.userRepository = userRepository;
|
||||||
|
this.clock = clock;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
List<UserResponse> findAll() {
|
||||||
|
return userRepository.findAll().stream()
|
||||||
|
.sorted(BY_DISPLAY_NAME)
|
||||||
|
.map(UserResponse::from)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
UserResponse create(String requestedName) {
|
||||||
|
String name = requestedName == null ? "" : requestedName.trim();
|
||||||
|
|
||||||
|
if (name.isEmpty() || name.codePointCount(0, name.length()) > 50) {
|
||||||
|
throw new InvalidUserNameException();
|
||||||
|
}
|
||||||
|
|
||||||
|
String normalizedName = name.toLowerCase(Locale.ROOT);
|
||||||
|
|
||||||
|
if (userRepository.existsByNormalizedName(normalizedName)) {
|
||||||
|
throw new UserNameAlreadyExistsException();
|
||||||
|
}
|
||||||
|
|
||||||
|
User user = new User(UUID.randomUUID(), name, normalizedName, Instant.now(clock));
|
||||||
|
|
||||||
|
try {
|
||||||
|
return UserResponse.from(userRepository.saveAndFlush(user));
|
||||||
|
} catch (DataIntegrityViolationException exception) {
|
||||||
|
throw new UserNameAlreadyExistsException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
7
backend/src/main/resources/application.properties
Normal file
7
backend/src/main/resources/application.properties
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
spring.datasource.url=jdbc:h2:file:./data/hemhub;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH
|
||||||
|
spring.datasource.username=sa
|
||||||
|
spring.datasource.password=
|
||||||
|
spring.jpa.hibernate.ddl-auto=validate
|
||||||
|
spring.jpa.open-in-view=false
|
||||||
|
spring.flyway.enabled=true
|
||||||
|
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
CREATE TABLE app_user (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
name VARCHAR(50) NOT NULL,
|
||||||
|
normalized_name VARCHAR(150) NOT NULL,
|
||||||
|
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||||
|
CONSTRAINT uk_app_user_normalized_name UNIQUE (normalized_name)
|
||||||
|
);
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
CREATE TABLE task (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
title VARCHAR(100) NOT NULL,
|
||||||
|
description VARCHAR(500),
|
||||||
|
status VARCHAR(20) NOT NULL,
|
||||||
|
created_at TIMESTAMP WITH TIME ZONE NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
107
backend/src/test/java/se/rubble/hemhub/task/TaskApiTest.java
Normal file
107
backend/src/test/java/se/rubble/hemhub/task/TaskApiTest.java
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
package se.rubble.hemhub.task;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
class TaskApiTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WebApplicationContext context;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TaskRepository taskRepository;
|
||||||
|
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
taskRepository.deleteAll();
|
||||||
|
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void createsWaitingTaskWithTrimmedValues() throws Exception {
|
||||||
|
mockMvc.perform(post("/api/tasks")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content("""
|
||||||
|
{
|
||||||
|
"title": " Dammsuga ",
|
||||||
|
"description": " Bottenvåningen "
|
||||||
|
}
|
||||||
|
"""))
|
||||||
|
.andExpect(status().isCreated())
|
||||||
|
.andExpect(jsonPath("$.id").isString())
|
||||||
|
.andExpect(jsonPath("$.title").value("Dammsuga"))
|
||||||
|
.andExpect(jsonPath("$.description").value("Bottenvåningen"))
|
||||||
|
.andExpect(jsonPath("$.status").value("WAITING"))
|
||||||
|
.andExpect(jsonPath("$.createdAt").isString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void storesBlankDescriptionAsNull() throws Exception {
|
||||||
|
mockMvc.perform(post("/api/tasks")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content("""
|
||||||
|
{"title": "Dammsuga", "description": " "}
|
||||||
|
"""))
|
||||||
|
.andExpect(status().isCreated())
|
||||||
|
.andExpect(jsonPath("$.description").value((Object) null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rejectsBlankAndTooLongTitles() throws Exception {
|
||||||
|
assertInvalidTask("""
|
||||||
|
{"title": " "}
|
||||||
|
""");
|
||||||
|
assertInvalidTask("{\"title\": \"%s\"}".formatted("a".repeat(101)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rejectsTooLongDescription() throws Exception {
|
||||||
|
assertInvalidTask("""
|
||||||
|
{"title": "Dammsuga", "description": "%s"}
|
||||||
|
""".formatted("a".repeat(501)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void listsTasksOldestFirstWithIdAsTieBreaker() throws Exception {
|
||||||
|
Instant older = Instant.parse("2026-07-24T10:00:00Z");
|
||||||
|
Instant newer = Instant.parse("2026-07-24T11:00:00Z");
|
||||||
|
UUID firstId = UUID.fromString("00000000-0000-0000-0000-000000000001");
|
||||||
|
UUID secondId = UUID.fromString("00000000-0000-0000-0000-000000000002");
|
||||||
|
UUID newestId = UUID.fromString("00000000-0000-0000-0000-000000000003");
|
||||||
|
|
||||||
|
taskRepository.save(new Task(newestId, "Nyast", null, TaskStatus.WAITING, newer));
|
||||||
|
taskRepository.save(new Task(secondId, "Andra", null, TaskStatus.IN_PROGRESS, older));
|
||||||
|
taskRepository.save(new Task(firstId, "Första", null, TaskStatus.COMPLETED, older));
|
||||||
|
|
||||||
|
mockMvc.perform(get("/api/tasks"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$[0].title").value("Första"))
|
||||||
|
.andExpect(jsonPath("$[1].title").value("Andra"))
|
||||||
|
.andExpect(jsonPath("$[2].title").value("Nyast"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertInvalidTask(String body) throws Exception {
|
||||||
|
mockMvc.perform(post("/api/tasks")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content(body))
|
||||||
|
.andExpect(status().isBadRequest())
|
||||||
|
.andExpect(jsonPath("$.code").value("INVALID_TASK"));
|
||||||
|
}
|
||||||
|
}
|
||||||
108
backend/src/test/java/se/rubble/hemhub/user/UserApiTest.java
Normal file
108
backend/src/test/java/se/rubble/hemhub/user/UserApiTest.java
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
package se.rubble.hemhub.user;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
class UserApiTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WebApplicationContext context;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
userRepository.deleteAll();
|
||||||
|
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void listsNoUsersWhenDatabaseIsEmpty() throws Exception {
|
||||||
|
mockMvc.perform(get("/api/users"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$").isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void createsAndListsUserWithTrimmedName() throws Exception {
|
||||||
|
mockMvc.perform(post("/api/users")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content("""
|
||||||
|
{"name": " Urban "}
|
||||||
|
"""))
|
||||||
|
.andExpect(status().isCreated())
|
||||||
|
.andExpect(jsonPath("$.id").isString())
|
||||||
|
.andExpect(jsonPath("$.name").value("Urban"))
|
||||||
|
.andExpect(jsonPath("$.createdAt").isString())
|
||||||
|
.andExpect(jsonPath("$.normalizedName").doesNotExist());
|
||||||
|
|
||||||
|
mockMvc.perform(get("/api/users"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$[0].name").value("Urban"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rejectsInvalidNames() throws Exception {
|
||||||
|
mockMvc.perform(post("/api/users")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content("""
|
||||||
|
{"name": " "}
|
||||||
|
"""))
|
||||||
|
.andExpect(status().isBadRequest())
|
||||||
|
.andExpect(jsonPath("$.code").value("INVALID_USER_NAME"));
|
||||||
|
|
||||||
|
mockMvc.perform(post("/api/users")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content("{\"name\": \"%s\"}".formatted("a".repeat(51))))
|
||||||
|
.andExpect(status().isBadRequest())
|
||||||
|
.andExpect(jsonPath("$.code").value("INVALID_USER_NAME"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rejectsDuplicateNameIgnoringCase() throws Exception {
|
||||||
|
createUser("Urban");
|
||||||
|
|
||||||
|
mockMvc.perform(post("/api/users")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content("""
|
||||||
|
{"name": "urban"}
|
||||||
|
"""))
|
||||||
|
.andExpect(status().isConflict())
|
||||||
|
.andExpect(jsonPath("$.code").value("USER_NAME_ALREADY_EXISTS"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void listsUsersSortedByDisplayName() throws Exception {
|
||||||
|
createUser("Urban");
|
||||||
|
createUser("Anna");
|
||||||
|
createUser("Bertil");
|
||||||
|
|
||||||
|
mockMvc.perform(get("/api/users"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$[0].name").value("Anna"))
|
||||||
|
.andExpect(jsonPath("$[1].name").value("Bertil"))
|
||||||
|
.andExpect(jsonPath("$[2].name").value("Urban"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createUser(String name) throws Exception {
|
||||||
|
mockMvc.perform(post("/api/users")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content("{\"name\": \"%s\"}".formatted(name)))
|
||||||
|
.andExpect(status().isCreated());
|
||||||
|
}
|
||||||
|
}
|
||||||
7
backend/src/test/resources/application.properties
Normal file
7
backend/src/test/resources/application.properties
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
spring.datasource.url=jdbc:h2:mem:hemhub-test;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH;DB_CLOSE_DELAY=-1
|
||||||
|
spring.datasource.username=sa
|
||||||
|
spring.datasource.password=
|
||||||
|
spring.jpa.hibernate.ddl-auto=validate
|
||||||
|
spring.jpa.open-in-view=false
|
||||||
|
spring.flyway.enabled=true
|
||||||
|
|
||||||
@ -1,21 +1,314 @@
|
|||||||
import { render, screen } from '@testing-library/react'
|
import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'
|
||||||
import { afterEach, expect, test, vi } from 'vitest'
|
import { afterEach, beforeEach, expect, test, vi } from 'vitest'
|
||||||
import App from './App'
|
import App from './App'
|
||||||
|
|
||||||
|
const users = [
|
||||||
|
{
|
||||||
|
id: 'd56b54dd-31b0-4d71-8a10-82464be59a61',
|
||||||
|
name: 'Urban',
|
||||||
|
createdAt: '2026-07-24T10:15:30Z',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '2213a673-c859-462b-a031-7b68b6f01c73',
|
||||||
|
name: 'Anna',
|
||||||
|
createdAt: '2026-07-24T10:16:30Z',
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
const tasks = [
|
||||||
|
{
|
||||||
|
id: '00000000-0000-0000-0000-000000000001',
|
||||||
|
title: 'Dammsuga',
|
||||||
|
description: 'Bottenvåningen',
|
||||||
|
status: 'WAITING',
|
||||||
|
createdAt: '2026-07-24T10:00:00Z',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '00000000-0000-0000-0000-000000000002',
|
||||||
|
title: 'Diska',
|
||||||
|
description: null,
|
||||||
|
status: 'IN_PROGRESS',
|
||||||
|
createdAt: '2026-07-24T10:01:00Z',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '00000000-0000-0000-0000-000000000003',
|
||||||
|
title: 'Vattna blommor',
|
||||||
|
description: null,
|
||||||
|
status: 'COMPLETED',
|
||||||
|
createdAt: '2026-07-24T10:02:00Z',
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
window.localStorage.clear()
|
||||||
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
|
cleanup()
|
||||||
vi.restoreAllMocks()
|
vi.restoreAllMocks()
|
||||||
})
|
})
|
||||||
|
|
||||||
test('visar sidans rubrik', () => {
|
test('tom användarlista visar Skapa användare', async () => {
|
||||||
vi.spyOn(globalThis, 'fetch').mockResolvedValue(
|
mockJsonResponse([])
|
||||||
new Response(JSON.stringify({ status: 'UP' }), {
|
|
||||||
status: 200,
|
render(<App />)
|
||||||
|
|
||||||
|
expect(await screen.findByRole('heading', { name: 'Skapa användare' })).toBeInTheDocument()
|
||||||
|
})
|
||||||
|
|
||||||
|
test('befintliga användare utan lokalt val visar Vem är du', async () => {
|
||||||
|
mockJsonResponse(users)
|
||||||
|
|
||||||
|
render(<App />)
|
||||||
|
|
||||||
|
expect(await screen.findByRole('heading', { name: 'Vem är du?' })).toBeInTheDocument()
|
||||||
|
expect(screen.getByRole('button', { name: 'Urban' })).toBeInTheDocument()
|
||||||
|
expect(screen.getByRole('button', { name: 'Anna' })).toBeInTheDocument()
|
||||||
|
})
|
||||||
|
|
||||||
|
test('val av användare lagrar id och visar brädan', async () => {
|
||||||
|
mockUsersAndTasks(users, [])
|
||||||
|
render(<App />)
|
||||||
|
|
||||||
|
fireEvent.click(await screen.findByRole('button', { name: 'Urban' }))
|
||||||
|
|
||||||
|
expect(await screen.findByRole('heading', { name: 'Uppgifter' })).toBeInTheDocument()
|
||||||
|
expect(screen.getByText('Urban')).toBeInTheDocument()
|
||||||
|
expect(window.localStorage.getItem('hemhub.activeUserId')).toBe(users[0].id)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('skapad användare blir automatiskt aktiv', async () => {
|
||||||
|
const createdUser = users[0]
|
||||||
|
const fetchMock = vi.spyOn(globalThis, 'fetch')
|
||||||
|
fetchMock.mockResolvedValueOnce(jsonResponse([]))
|
||||||
|
fetchMock.mockResolvedValueOnce(jsonResponse(createdUser, 201))
|
||||||
|
fetchMock.mockResolvedValueOnce(jsonResponse([]))
|
||||||
|
|
||||||
|
render(<App />)
|
||||||
|
|
||||||
|
fireEvent.change(await screen.findByLabelText('Namn'), { target: { value: ' Urban ' } })
|
||||||
|
fireEvent.click(screen.getByRole('button', { name: 'Skapa användare' }))
|
||||||
|
|
||||||
|
expect(await screen.findByRole('heading', { name: 'Uppgifter' })).toBeInTheDocument()
|
||||||
|
expect(window.localStorage.getItem('hemhub.activeUserId')).toBe(createdUser.id)
|
||||||
|
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/users', {
|
||||||
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
}),
|
body: JSON.stringify({ name: 'Urban' }),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('skapandefel för användare visas utan att namnet försvinner', async () => {
|
||||||
|
const fetchMock = vi.spyOn(globalThis, 'fetch')
|
||||||
|
fetchMock.mockResolvedValueOnce(jsonResponse([]))
|
||||||
|
fetchMock.mockResolvedValueOnce(
|
||||||
|
jsonResponse(
|
||||||
|
{
|
||||||
|
code: 'USER_NAME_ALREADY_EXISTS',
|
||||||
|
message: 'En användare med det namnet finns redan.',
|
||||||
|
},
|
||||||
|
409,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
render(<App />)
|
render(<App />)
|
||||||
|
|
||||||
expect(screen.getByRole('heading', { name: 'HemHub' })).toBeInTheDocument()
|
const input = await screen.findByLabelText('Namn')
|
||||||
|
fireEvent.change(input, { target: { value: 'Urban' } })
|
||||||
|
fireEvent.click(screen.getByRole('button', { name: 'Skapa användare' }))
|
||||||
|
|
||||||
|
expect(await screen.findByRole('alert')).toHaveTextContent(
|
||||||
|
'En användare med det namnet finns redan.',
|
||||||
|
)
|
||||||
|
expect(input).toHaveValue('Urban')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('Logga ut rensar valt id och väljer inte användaren automatiskt', async () => {
|
||||||
|
window.localStorage.setItem('hemhub.activeUserId', users[0].id)
|
||||||
|
mockUsersAndTasks(users, [])
|
||||||
|
render(<App />)
|
||||||
|
|
||||||
|
fireEvent.click(await screen.findByRole('button', { name: 'Logga ut' }))
|
||||||
|
|
||||||
|
expect(screen.getByRole('heading', { name: 'Vem är du?' })).toBeInTheDocument()
|
||||||
|
expect(screen.getByRole('button', { name: 'Urban' })).toBeInTheDocument()
|
||||||
|
expect(screen.queryByRole('heading', { name: 'Uppgifter' })).not.toBeInTheDocument()
|
||||||
|
expect(window.localStorage.getItem('hemhub.activeUserId')).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
test('hämtningsfel för användare visas som fel och kan återförsökas', async () => {
|
||||||
|
const fetchMock = vi.spyOn(globalThis, 'fetch')
|
||||||
|
fetchMock.mockRejectedValueOnce(new Error('Nätverksfel'))
|
||||||
|
fetchMock.mockResolvedValueOnce(jsonResponse(users))
|
||||||
|
render(<App />)
|
||||||
|
|
||||||
|
expect(await screen.findByText('Kunde inte ansluta till HemHub.')).toBeInTheDocument()
|
||||||
|
expect(screen.queryByRole('heading', { name: 'Skapa användare' })).not.toBeInTheDocument()
|
||||||
|
|
||||||
|
fireEvent.click(screen.getByRole('button', { name: 'Försök igen' }))
|
||||||
|
|
||||||
|
expect(await screen.findByRole('heading', { name: 'Vem är du?' })).toBeInTheDocument()
|
||||||
|
expect(fetchMock).toHaveBeenCalledTimes(2)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('ogiltigt lagrat användar-id rensas', async () => {
|
||||||
|
window.localStorage.setItem('hemhub.activeUserId', 'finns-inte')
|
||||||
|
mockJsonResponse(users)
|
||||||
|
|
||||||
|
render(<App />)
|
||||||
|
|
||||||
|
expect(await screen.findByRole('heading', { name: 'Vem är du?' })).toBeInTheDocument()
|
||||||
|
expect(window.localStorage.getItem('hemhub.activeUserId')).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
test('brädan visar tre kolumner och grupperar hämtade uppgifter', async () => {
|
||||||
|
window.localStorage.setItem('hemhub.activeUserId', users[0].id)
|
||||||
|
mockUsersAndTasks(users, tasks)
|
||||||
|
|
||||||
|
render(<App />)
|
||||||
|
|
||||||
|
const waiting = await screen.findByRole('region', { name: 'Väntande' })
|
||||||
|
const inProgress = screen.getByRole('region', { name: 'Pågående' })
|
||||||
|
const completed = screen.getByRole('region', { name: 'Klart' })
|
||||||
|
|
||||||
|
expect(within(waiting).getByText('Dammsuga')).toBeInTheDocument()
|
||||||
|
expect(within(waiting).getByText('Bottenvåningen')).toBeInTheDocument()
|
||||||
|
expect(within(inProgress).getByText('Diska')).toBeInTheDocument()
|
||||||
|
expect(within(completed).getByText('Vattna blommor')).toBeInTheDocument()
|
||||||
|
expect(screen.queryByText(/Inga uppgifter/i)).not.toBeInTheDocument()
|
||||||
|
})
|
||||||
|
|
||||||
|
test('Ny uppgift öppnar modalen med fokus i titelfältet', async () => {
|
||||||
|
window.localStorage.setItem('hemhub.activeUserId', users[0].id)
|
||||||
|
mockUsersAndTasks(users, [])
|
||||||
|
render(<App />)
|
||||||
|
|
||||||
|
fireEvent.click(await screen.findByRole('button', { name: 'Ny uppgift' }))
|
||||||
|
|
||||||
|
expect(screen.getByRole('dialog', { name: 'Skapa ny uppgift' })).toBeInTheDocument()
|
||||||
|
expect(screen.getByLabelText('Titel')).toHaveFocus()
|
||||||
|
})
|
||||||
|
|
||||||
|
test.each([
|
||||||
|
{
|
||||||
|
sätt: 'stängningskrysset',
|
||||||
|
close: () => fireEvent.click(screen.getByRole('button', { name: 'Stäng' })),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
sätt: 'Escape',
|
||||||
|
close: () => fireEvent.keyDown(window, { key: 'Escape' }),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
sätt: 'modalens bakgrund',
|
||||||
|
close: () => {
|
||||||
|
const backdrop = screen.getByRole('dialog', { name: 'Skapa ny uppgift' }).parentElement
|
||||||
|
|
||||||
|
if (!backdrop) {
|
||||||
|
throw new Error('Modalens bakgrund saknas')
|
||||||
|
}
|
||||||
|
|
||||||
|
fireEvent.mouseDown(backdrop)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
])('modalen stängs med $sätt och rensar formuläret', async ({ close }) => {
|
||||||
|
window.localStorage.setItem('hemhub.activeUserId', users[0].id)
|
||||||
|
mockUsersAndTasks(users, [])
|
||||||
|
render(<App />)
|
||||||
|
|
||||||
|
fireEvent.click(await screen.findByRole('button', { name: 'Ny uppgift' }))
|
||||||
|
fireEvent.change(screen.getByLabelText('Titel'), { target: { value: 'Dammsuga' } })
|
||||||
|
fireEvent.change(screen.getByLabelText('Beskrivning (valfri)'), {
|
||||||
|
target: { value: 'Bottenvåningen' },
|
||||||
|
})
|
||||||
|
|
||||||
|
close()
|
||||||
|
|
||||||
|
expect(screen.queryByRole('dialog', { name: 'Skapa ny uppgift' })).not.toBeInTheDocument()
|
||||||
|
|
||||||
|
fireEvent.click(screen.getByRole('button', { name: 'Ny uppgift' }))
|
||||||
|
|
||||||
|
expect(screen.getByLabelText('Titel')).toHaveValue('')
|
||||||
|
expect(screen.getByLabelText('Beskrivning (valfri)')).toHaveValue('')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('en skapad uppgift visas längst ned i Väntande och modalen stängs', async () => {
|
||||||
|
const createdTask = {
|
||||||
|
id: '00000000-0000-0000-0000-000000000004',
|
||||||
|
title: 'Putsa fönster',
|
||||||
|
description: 'Köket',
|
||||||
|
status: 'WAITING',
|
||||||
|
createdAt: '2026-07-24T10:03:00Z',
|
||||||
|
}
|
||||||
|
window.localStorage.setItem('hemhub.activeUserId', users[0].id)
|
||||||
|
const fetchMock = vi.spyOn(globalThis, 'fetch')
|
||||||
|
fetchMock.mockResolvedValueOnce(jsonResponse(users))
|
||||||
|
fetchMock.mockResolvedValueOnce(jsonResponse([tasks[0]]))
|
||||||
|
fetchMock.mockResolvedValueOnce(jsonResponse(createdTask, 201))
|
||||||
|
|
||||||
|
render(<App />)
|
||||||
|
|
||||||
|
await waitFor(() => expect(fetchMock).toHaveBeenCalledTimes(2))
|
||||||
|
fireEvent.click(screen.getByRole('button', { name: 'Ny uppgift' }))
|
||||||
|
fireEvent.change(screen.getByLabelText('Titel'), { target: { value: ' Putsa fönster ' } })
|
||||||
|
fireEvent.change(screen.getByLabelText('Beskrivning (valfri)'), {
|
||||||
|
target: { value: ' Köket ' },
|
||||||
|
})
|
||||||
|
fireEvent.click(screen.getByRole('button', { name: 'Skapa uppgift' }))
|
||||||
|
|
||||||
|
await waitFor(() =>
|
||||||
|
expect(screen.queryByRole('dialog', { name: 'Skapa ny uppgift' })).not.toBeInTheDocument(),
|
||||||
|
)
|
||||||
|
const waiting = screen.getByRole('region', { name: 'Väntande' })
|
||||||
|
expect(within(waiting).getAllByRole('article').map((card) => card.textContent)).toEqual([
|
||||||
|
'DammsugaBottenvåningen',
|
||||||
|
'Putsa fönsterKöket',
|
||||||
|
])
|
||||||
|
expect(fetchMock).toHaveBeenLastCalledWith('/api/tasks', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ title: 'Putsa fönster', description: 'Köket' }),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('formulärdata bevaras när skapande av uppgift misslyckas', async () => {
|
||||||
|
window.localStorage.setItem('hemhub.activeUserId', users[0].id)
|
||||||
|
const fetchMock = vi.spyOn(globalThis, 'fetch')
|
||||||
|
fetchMock.mockResolvedValueOnce(jsonResponse(users))
|
||||||
|
fetchMock.mockResolvedValueOnce(jsonResponse([]))
|
||||||
|
fetchMock.mockResolvedValueOnce(
|
||||||
|
jsonResponse({ code: 'INVALID_TASK', message: 'Uppgiften är ogiltig.' }, 400),
|
||||||
|
)
|
||||||
|
|
||||||
|
render(<App />)
|
||||||
|
|
||||||
|
await waitFor(() => expect(fetchMock).toHaveBeenCalledTimes(2))
|
||||||
|
fireEvent.click(screen.getByRole('button', { name: 'Ny uppgift' }))
|
||||||
|
const title = screen.getByLabelText('Titel')
|
||||||
|
const description = screen.getByLabelText('Beskrivning (valfri)')
|
||||||
|
fireEvent.change(title, { target: { value: 'Dammsuga' } })
|
||||||
|
fireEvent.change(description, { target: { value: 'Bottenvåningen' } })
|
||||||
|
fireEvent.click(screen.getByRole('button', { name: 'Skapa uppgift' }))
|
||||||
|
|
||||||
|
expect(await screen.findByRole('alert')).toHaveTextContent('Uppgiften är ogiltig.')
|
||||||
|
expect(screen.getByRole('dialog', { name: 'Skapa ny uppgift' })).toBeInTheDocument()
|
||||||
|
expect(title).toHaveValue('Dammsuga')
|
||||||
|
expect(description).toHaveValue('Bottenvåningen')
|
||||||
|
})
|
||||||
|
|
||||||
|
function mockUsersAndTasks(userResponse: unknown, taskResponse: unknown) {
|
||||||
|
const fetchMock = vi.spyOn(globalThis, 'fetch')
|
||||||
|
fetchMock.mockResolvedValueOnce(jsonResponse(userResponse))
|
||||||
|
fetchMock.mockResolvedValueOnce(jsonResponse(taskResponse))
|
||||||
|
return fetchMock
|
||||||
|
}
|
||||||
|
|
||||||
|
function mockJsonResponse(body: unknown, status = 200) {
|
||||||
|
return vi.spyOn(globalThis, 'fetch').mockResolvedValue(jsonResponse(body, status))
|
||||||
|
}
|
||||||
|
|
||||||
|
function jsonResponse(body: unknown, status = 200) {
|
||||||
|
return new Response(JSON.stringify(body), {
|
||||||
|
status,
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@ -1,48 +1,207 @@
|
|||||||
import { useEffect, useState } from 'react'
|
import { FormEvent, useEffect, useRef, useState } from 'react'
|
||||||
|
import TaskBoard from './TaskBoard'
|
||||||
|
|
||||||
type HealthResponse = {
|
type User = {
|
||||||
status: string
|
id: string
|
||||||
|
name: string
|
||||||
|
createdAt: string
|
||||||
}
|
}
|
||||||
|
|
||||||
function App() {
|
type ApiError = {
|
||||||
const [backendStatus, setBackendStatus] = useState<string | null>(null)
|
code?: string
|
||||||
const [hasError, setHasError] = useState(false)
|
message?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const ACTIVE_USER_KEY = 'hemhub.activeUserId'
|
||||||
|
|
||||||
|
function App() {
|
||||||
|
const [users, setUsers] = useState<User[]>([])
|
||||||
|
const [activeUser, setActiveUser] = useState<User | null>(null)
|
||||||
|
const [loadState, setLoadState] = useState<'loading' | 'ready' | 'error'>('loading')
|
||||||
|
const [showCreateUser, setShowCreateUser] = useState(false)
|
||||||
|
|
||||||
|
const loadUsers = async () => {
|
||||||
|
setLoadState('loading')
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const loadHealth = async () => {
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/api/health')
|
const response = await fetch('/api/users')
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`Backend svarade med status ${response.status}`)
|
throw new Error('Kunde inte hämta användare')
|
||||||
}
|
}
|
||||||
|
|
||||||
const health = (await response.json()) as HealthResponse
|
const loadedUsers = (await response.json()) as User[]
|
||||||
setBackendStatus(health.status)
|
const storedUserId = window.localStorage.getItem(ACTIVE_USER_KEY)
|
||||||
|
const storedUser = loadedUsers.find((user) => user.id === storedUserId) ?? null
|
||||||
|
|
||||||
|
if (storedUserId && !storedUser) {
|
||||||
|
window.localStorage.removeItem(ACTIVE_USER_KEY)
|
||||||
|
}
|
||||||
|
|
||||||
|
setUsers(loadedUsers)
|
||||||
|
setActiveUser(storedUser)
|
||||||
|
setShowCreateUser(loadedUsers.length === 0)
|
||||||
|
setLoadState('ready')
|
||||||
} catch {
|
} catch {
|
||||||
setHasError(true)
|
setLoadState('error')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loadHealth()
|
useEffect(() => {
|
||||||
|
void loadUsers()
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
let statusMessage = 'Kontrollerar backend…'
|
const selectUser = (user: User) => {
|
||||||
|
window.localStorage.setItem(ACTIVE_USER_KEY, user.id)
|
||||||
|
setActiveUser(user)
|
||||||
|
}
|
||||||
|
|
||||||
if (hasError) {
|
const logOut = () => {
|
||||||
statusMessage = 'Backend kunde inte nås'
|
window.localStorage.removeItem(ACTIVE_USER_KEY)
|
||||||
} else if (backendStatus) {
|
setActiveUser(null)
|
||||||
statusMessage = `Backend: ${backendStatus}`
|
setShowCreateUser(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loadState === 'loading') {
|
||||||
|
return <main className="panel">Laddar HemHub…</main>
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loadState === 'error') {
|
||||||
|
return (
|
||||||
|
<main className="panel">
|
||||||
|
<h1>HemHub</h1>
|
||||||
|
<p>Kunde inte ansluta till HemHub.</p>
|
||||||
|
<button type="button" onClick={() => void loadUsers()}>
|
||||||
|
Försök igen
|
||||||
|
</button>
|
||||||
|
</main>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (activeUser) {
|
||||||
|
return <TaskBoard activeUserName={activeUser.name} onLogOut={logOut} />
|
||||||
|
}
|
||||||
|
|
||||||
|
if (showCreateUser) {
|
||||||
|
return (
|
||||||
|
<CreateUserForm
|
||||||
|
hasExistingUsers={users.length > 0}
|
||||||
|
onCancel={() => setShowCreateUser(false)}
|
||||||
|
onCreated={(user) => {
|
||||||
|
setUsers((currentUsers) => [...currentUsers, user])
|
||||||
|
selectUser(user)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main>
|
<main className="panel">
|
||||||
<h1>HemHub</h1>
|
<h1>HemHub</h1>
|
||||||
<p>Frontend har startat.</p>
|
<h2>Vem är du?</h2>
|
||||||
<p aria-live="polite">{statusMessage}</p>
|
<div className="user-list">
|
||||||
|
{users.map((user) => (
|
||||||
|
<button type="button" key={user.id} onClick={() => selectUser(user)}>
|
||||||
|
{user.name}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
<button type="button" className="secondary" onClick={() => setShowCreateUser(true)}>
|
||||||
|
Skapa användare
|
||||||
|
</button>
|
||||||
|
</main>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
type CreateUserFormProps = {
|
||||||
|
hasExistingUsers: boolean
|
||||||
|
onCancel: () => void
|
||||||
|
onCreated: (user: User) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
function CreateUserForm({ hasExistingUsers, onCancel, onCreated }: CreateUserFormProps) {
|
||||||
|
const [name, setName] = useState('')
|
||||||
|
const [error, setError] = useState('')
|
||||||
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||||
|
const isSubmittingRef = useRef(false)
|
||||||
|
|
||||||
|
const submit = async (event: FormEvent<HTMLFormElement>) => {
|
||||||
|
event.preventDefault()
|
||||||
|
|
||||||
|
if (isSubmittingRef.current) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const trimmedName = name.trim()
|
||||||
|
|
||||||
|
if (!trimmedName || [...trimmedName].length > 50) {
|
||||||
|
setError('Namnet måste innehålla mellan 1 och 50 tecken.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
setError('')
|
||||||
|
isSubmittingRef.current = true
|
||||||
|
setIsSubmitting(true)
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/users', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ name: trimmedName }),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const apiError = (await response.json().catch(() => ({}))) as ApiError
|
||||||
|
|
||||||
|
if (
|
||||||
|
apiError.code === 'INVALID_USER_NAME' ||
|
||||||
|
apiError.code === 'USER_NAME_ALREADY_EXISTS'
|
||||||
|
) {
|
||||||
|
setError(apiError.message ?? 'Det gick inte att skapa användaren.')
|
||||||
|
} else {
|
||||||
|
setError('Det gick inte att skapa användaren. Försök igen.')
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const createdUser = (await response.json()) as User
|
||||||
|
onCreated(createdUser)
|
||||||
|
} catch {
|
||||||
|
setError('Det gick inte att skapa användaren. Försök igen.')
|
||||||
|
} finally {
|
||||||
|
isSubmittingRef.current = false
|
||||||
|
setIsSubmitting(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main className="panel">
|
||||||
|
<h1>HemHub</h1>
|
||||||
|
<h2>Skapa användare</h2>
|
||||||
|
<form onSubmit={(event) => void submit(event)}>
|
||||||
|
<label htmlFor="user-name">Namn</label>
|
||||||
|
<input
|
||||||
|
id="user-name"
|
||||||
|
value={name}
|
||||||
|
disabled={isSubmitting}
|
||||||
|
onChange={(event) => setName(event.target.value)}
|
||||||
|
/>
|
||||||
|
{error && (
|
||||||
|
<p className="error" role="alert">
|
||||||
|
{error}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
<button type="submit" disabled={isSubmitting}>
|
||||||
|
{isSubmitting ? 'Skapar…' : 'Skapa användare'}
|
||||||
|
</button>
|
||||||
|
{hasExistingUsers && (
|
||||||
|
<button type="button" className="secondary" disabled={isSubmitting} onClick={onCancel}>
|
||||||
|
Tillbaka
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</form>
|
||||||
</main>
|
</main>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default App
|
export default App
|
||||||
|
|
||||||
|
|||||||
249
frontend/src/TaskBoard.tsx
Normal file
249
frontend/src/TaskBoard.tsx
Normal file
@ -0,0 +1,249 @@
|
|||||||
|
import { FormEvent, MouseEvent, useEffect, useRef, useState } from 'react'
|
||||||
|
|
||||||
|
type TaskStatus = 'WAITING' | 'IN_PROGRESS' | 'COMPLETED'
|
||||||
|
|
||||||
|
type Task = {
|
||||||
|
id: string
|
||||||
|
title: string
|
||||||
|
description: string | null
|
||||||
|
status: TaskStatus
|
||||||
|
createdAt: string
|
||||||
|
}
|
||||||
|
|
||||||
|
type ApiError = {
|
||||||
|
message?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
type TaskBoardProps = {
|
||||||
|
activeUserName: string
|
||||||
|
onLogOut: () => void
|
||||||
|
}
|
||||||
|
|
||||||
|
const columns: { status: TaskStatus; title: string }[] = [
|
||||||
|
{ status: 'WAITING', title: 'Väntande' },
|
||||||
|
{ status: 'IN_PROGRESS', title: 'Pågående' },
|
||||||
|
{ status: 'COMPLETED', title: 'Klart' },
|
||||||
|
]
|
||||||
|
|
||||||
|
function TaskBoard({ activeUserName, onLogOut }: TaskBoardProps) {
|
||||||
|
const [tasks, setTasks] = useState<Task[]>([])
|
||||||
|
const [loadState, setLoadState] = useState<'loading' | 'ready' | 'error'>('loading')
|
||||||
|
const [showCreateTask, setShowCreateTask] = useState(false)
|
||||||
|
|
||||||
|
const loadTasks = async () => {
|
||||||
|
setLoadState('loading')
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/tasks')
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Kunde inte hämta uppgifter')
|
||||||
|
}
|
||||||
|
|
||||||
|
setTasks((await response.json()) as Task[])
|
||||||
|
setLoadState('ready')
|
||||||
|
} catch {
|
||||||
|
setLoadState('error')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
void loadTasks()
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main className="task-app">
|
||||||
|
<header className="app-header">
|
||||||
|
<div>
|
||||||
|
<p className="eyebrow">HemHub</p>
|
||||||
|
<h1>Uppgifter</h1>
|
||||||
|
</div>
|
||||||
|
<div className="user-controls">
|
||||||
|
<span>{activeUserName}</span>
|
||||||
|
<button type="button" className="secondary compact" onClick={onLogOut}>
|
||||||
|
Logga ut
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<div className="board-toolbar">
|
||||||
|
<div aria-live="polite">
|
||||||
|
{loadState === 'loading' && 'Laddar uppgifter…'}
|
||||||
|
{loadState === 'error' && (
|
||||||
|
<>
|
||||||
|
Kunde inte hämta uppgifter.
|
||||||
|
<button type="button" className="link-button" onClick={() => void loadTasks()}>
|
||||||
|
Försök igen
|
||||||
|
</button>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<button type="button" onClick={() => setShowCreateTask(true)}>
|
||||||
|
Ny uppgift
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<section className="board" aria-label="Uppgiftsbräda">
|
||||||
|
{columns.map((column) => (
|
||||||
|
<section className="board-column" key={column.status} aria-labelledby={column.status}>
|
||||||
|
<h2 id={column.status}>{column.title}</h2>
|
||||||
|
<div className="task-list">
|
||||||
|
{tasks
|
||||||
|
.filter((task) => task.status === column.status)
|
||||||
|
.map((task) => (
|
||||||
|
<article className="task-card" key={task.id}>
|
||||||
|
<h3>{task.title}</h3>
|
||||||
|
{task.description && <p>{task.description}</p>}
|
||||||
|
</article>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
))}
|
||||||
|
</section>
|
||||||
|
|
||||||
|
{showCreateTask && (
|
||||||
|
<CreateTaskModal
|
||||||
|
onClose={() => setShowCreateTask(false)}
|
||||||
|
onCreated={(task) => {
|
||||||
|
setTasks((currentTasks) => [...currentTasks, task])
|
||||||
|
setShowCreateTask(false)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</main>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
type CreateTaskModalProps = {
|
||||||
|
onClose: () => void
|
||||||
|
onCreated: (task: Task) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
function CreateTaskModal({ onClose, onCreated }: CreateTaskModalProps) {
|
||||||
|
const [title, setTitle] = useState('')
|
||||||
|
const [description, setDescription] = useState('')
|
||||||
|
const [error, setError] = useState('')
|
||||||
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||||
|
const isSubmittingRef = useRef(false)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const closeOnEscape = (event: KeyboardEvent) => {
|
||||||
|
if (event.key === 'Escape' && !isSubmittingRef.current) {
|
||||||
|
onClose()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.addEventListener('keydown', closeOnEscape)
|
||||||
|
return () => window.removeEventListener('keydown', closeOnEscape)
|
||||||
|
}, [onClose])
|
||||||
|
|
||||||
|
const closeFromBackdrop = (event: MouseEvent<HTMLDivElement>) => {
|
||||||
|
if (event.target === event.currentTarget && !isSubmittingRef.current) {
|
||||||
|
onClose()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const submit = async (event: FormEvent<HTMLFormElement>) => {
|
||||||
|
event.preventDefault()
|
||||||
|
|
||||||
|
if (isSubmittingRef.current) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const trimmedTitle = title.trim()
|
||||||
|
const trimmedDescription = description.trim()
|
||||||
|
|
||||||
|
if (!trimmedTitle || [...trimmedTitle].length > 100) {
|
||||||
|
setError('Titeln måste innehålla mellan 1 och 100 tecken.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if ([...trimmedDescription].length > 500) {
|
||||||
|
setError('Beskrivningen får innehålla högst 500 tecken.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
setError('')
|
||||||
|
isSubmittingRef.current = true
|
||||||
|
setIsSubmitting(true)
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/tasks', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({
|
||||||
|
title: trimmedTitle,
|
||||||
|
description: trimmedDescription || null,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const apiError = (await response.json().catch(() => ({}))) as ApiError
|
||||||
|
setError(apiError.message ?? 'Det gick inte att skapa uppgiften. Försök igen.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
onCreated((await response.json()) as Task)
|
||||||
|
} catch {
|
||||||
|
setError('Det gick inte att skapa uppgiften. Försök igen.')
|
||||||
|
} finally {
|
||||||
|
isSubmittingRef.current = false
|
||||||
|
setIsSubmitting(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="modal-backdrop" onMouseDown={closeFromBackdrop}>
|
||||||
|
<section
|
||||||
|
className="modal"
|
||||||
|
role="dialog"
|
||||||
|
aria-modal="true"
|
||||||
|
aria-labelledby="create-task-title"
|
||||||
|
>
|
||||||
|
<div className="modal-header">
|
||||||
|
<h2 id="create-task-title">Skapa ny uppgift</h2>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="close-button"
|
||||||
|
aria-label="Stäng"
|
||||||
|
disabled={isSubmitting}
|
||||||
|
onClick={onClose}
|
||||||
|
>
|
||||||
|
×
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<form onSubmit={(event) => void submit(event)}>
|
||||||
|
<label htmlFor="task-title">Titel</label>
|
||||||
|
<input
|
||||||
|
id="task-title"
|
||||||
|
autoFocus
|
||||||
|
value={title}
|
||||||
|
disabled={isSubmitting}
|
||||||
|
onChange={(event) => setTitle(event.target.value)}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<label htmlFor="task-description">Beskrivning (valfri)</label>
|
||||||
|
<textarea
|
||||||
|
id="task-description"
|
||||||
|
rows={5}
|
||||||
|
value={description}
|
||||||
|
disabled={isSubmitting}
|
||||||
|
onChange={(event) => setDescription(event.target.value)}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{error && (
|
||||||
|
<p className="error" role="alert">
|
||||||
|
{error}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<button type="submit" disabled={isSubmitting}>
|
||||||
|
{isSubmitting ? 'Skapar…' : 'Skapa uppgift'}
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default TaskBoard
|
||||||
@ -8,7 +8,7 @@ body {
|
|||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
main {
|
.panel {
|
||||||
max-width: 40rem;
|
max-width: 40rem;
|
||||||
margin: 6rem auto;
|
margin: 6rem auto;
|
||||||
padding: 2rem;
|
padding: 2rem;
|
||||||
@ -21,3 +21,217 @@ h1 {
|
|||||||
margin-top: 0;
|
margin-top: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
button,
|
||||||
|
input,
|
||||||
|
textarea {
|
||||||
|
font: inherit;
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
padding: 0.65rem 1rem;
|
||||||
|
border: 0;
|
||||||
|
border-radius: 0.4rem;
|
||||||
|
color: white;
|
||||||
|
background: #2563eb;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
button:disabled,
|
||||||
|
input:disabled,
|
||||||
|
textarea:disabled {
|
||||||
|
cursor: not-allowed;
|
||||||
|
opacity: 0.65;
|
||||||
|
}
|
||||||
|
|
||||||
|
.secondary {
|
||||||
|
margin-top: 1rem;
|
||||||
|
color: #1f2937;
|
||||||
|
background: #e5e7eb;
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-list {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 0.75rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
form {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: flex-start;
|
||||||
|
gap: 0.75rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
input {
|
||||||
|
box-sizing: border-box;
|
||||||
|
width: 100%;
|
||||||
|
padding: 0.6rem;
|
||||||
|
border: 1px solid #9ca3af;
|
||||||
|
border-radius: 0.4rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
textarea {
|
||||||
|
box-sizing: border-box;
|
||||||
|
width: 100%;
|
||||||
|
padding: 0.6rem;
|
||||||
|
border: 1px solid #9ca3af;
|
||||||
|
border-radius: 0.4rem;
|
||||||
|
resize: vertical;
|
||||||
|
}
|
||||||
|
|
||||||
|
.error {
|
||||||
|
margin: 0;
|
||||||
|
color: #b91c1c;
|
||||||
|
}
|
||||||
|
|
||||||
|
.task-app {
|
||||||
|
min-height: 100vh;
|
||||||
|
padding: 2rem clamp(1rem, 4vw, 4rem);
|
||||||
|
background: #f3f4f6;
|
||||||
|
}
|
||||||
|
|
||||||
|
.app-header,
|
||||||
|
.board-toolbar,
|
||||||
|
.user-controls,
|
||||||
|
.modal-header {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.app-header {
|
||||||
|
margin: 0 auto 2rem;
|
||||||
|
max-width: 90rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.app-header h1,
|
||||||
|
.eyebrow {
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.eyebrow {
|
||||||
|
color: #64748b;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-controls {
|
||||||
|
justify-content: flex-end;
|
||||||
|
}
|
||||||
|
|
||||||
|
.compact {
|
||||||
|
margin-top: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.board-toolbar {
|
||||||
|
min-height: 2.75rem;
|
||||||
|
margin: 0 auto 1rem;
|
||||||
|
max-width: 90rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.link-button {
|
||||||
|
padding: 0.25rem 0.5rem;
|
||||||
|
color: #2563eb;
|
||||||
|
background: transparent;
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
|
||||||
|
.board {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(3, minmax(0, 1fr));
|
||||||
|
gap: 1rem;
|
||||||
|
margin: 0 auto;
|
||||||
|
max-width: 90rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.board-column {
|
||||||
|
min-height: 20rem;
|
||||||
|
padding: 1rem;
|
||||||
|
border-radius: 0.75rem;
|
||||||
|
background: #e5e7eb;
|
||||||
|
}
|
||||||
|
|
||||||
|
.board-column h2 {
|
||||||
|
margin: 0 0 1rem;
|
||||||
|
font-size: 1.1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.task-list {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.75rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.task-card {
|
||||||
|
padding: 1rem;
|
||||||
|
border-radius: 0.5rem;
|
||||||
|
background: white;
|
||||||
|
box-shadow: 0 0.125rem 0.4rem rgb(0 0 0 / 8%);
|
||||||
|
}
|
||||||
|
|
||||||
|
.task-card h3,
|
||||||
|
.task-card p {
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.task-card p {
|
||||||
|
margin-top: 0.5rem;
|
||||||
|
color: #475569;
|
||||||
|
white-space: pre-wrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal-backdrop {
|
||||||
|
position: fixed;
|
||||||
|
inset: 0;
|
||||||
|
display: grid;
|
||||||
|
place-items: center;
|
||||||
|
padding: 1rem;
|
||||||
|
background: rgb(15 23 42 / 55%);
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal {
|
||||||
|
width: min(100%, 34rem);
|
||||||
|
padding: 1.5rem;
|
||||||
|
border-radius: 0.75rem;
|
||||||
|
background: white;
|
||||||
|
box-shadow: 0 1rem 3rem rgb(0 0 0 / 25%);
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal-header {
|
||||||
|
margin-bottom: 1.25rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal-header h2 {
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.close-button {
|
||||||
|
padding: 0.2rem 0.55rem;
|
||||||
|
color: #475569;
|
||||||
|
background: transparent;
|
||||||
|
font-size: 1.75rem;
|
||||||
|
line-height: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 48rem) {
|
||||||
|
.panel {
|
||||||
|
margin: 2rem 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.app-header {
|
||||||
|
align-items: flex-start;
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-controls {
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: flex-end;
|
||||||
|
}
|
||||||
|
|
||||||
|
.board {
|
||||||
|
grid-template-columns: 1fr;
|
||||||
|
}
|
||||||
|
|
||||||
|
.board-column {
|
||||||
|
min-height: 8rem;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -1,2 +1,27 @@
|
|||||||
import '@testing-library/jest-dom/vitest'
|
import '@testing-library/jest-dom/vitest'
|
||||||
|
|
||||||
|
const storedValues = new Map<string, string>()
|
||||||
|
|
||||||
|
Object.defineProperty(window, 'localStorage', {
|
||||||
|
configurable: true,
|
||||||
|
value: {
|
||||||
|
get length() {
|
||||||
|
return storedValues.size
|
||||||
|
},
|
||||||
|
clear() {
|
||||||
|
storedValues.clear()
|
||||||
|
},
|
||||||
|
getItem(key: string) {
|
||||||
|
return storedValues.get(key) ?? null
|
||||||
|
},
|
||||||
|
key(index: number) {
|
||||||
|
return [...storedValues.keys()][index] ?? null
|
||||||
|
},
|
||||||
|
removeItem(key: string) {
|
||||||
|
storedValues.delete(key)
|
||||||
|
},
|
||||||
|
setItem(key: string, value: string) {
|
||||||
|
storedValues.set(key, String(value))
|
||||||
|
},
|
||||||
|
} satisfies Storage,
|
||||||
|
})
|
||||||
|
|||||||
@ -11,6 +11,11 @@ export default defineConfig({
|
|||||||
},
|
},
|
||||||
test: {
|
test: {
|
||||||
environment: 'jsdom',
|
environment: 'jsdom',
|
||||||
|
environmentOptions: {
|
||||||
|
jsdom: {
|
||||||
|
url: 'http://localhost',
|
||||||
|
},
|
||||||
|
},
|
||||||
setupFiles: './src/test/setup.ts',
|
setupFiles: './src/test/setup.ts',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user