This commit is contained in:
63
frontend/src/stores/taskStore.js
Normal file
63
frontend/src/stores/taskStore.js
Normal file
@@ -0,0 +1,63 @@
|
||||
// src/stores/taskStore.js
|
||||
import { atom } from 'nanostores';
|
||||
import { api } from '../services/apiClient';
|
||||
import { showToast } from './toastStore';
|
||||
|
||||
export const $tasks = atom([]);
|
||||
export const $tasksLoading = atom(false);
|
||||
|
||||
const STATUS_LABELS = {
|
||||
aktivan: 'Aktivan',
|
||||
servis: 'U tijeku',
|
||||
neaktivan: 'Otkazano',
|
||||
};
|
||||
|
||||
export function getStatusLabel(status) {
|
||||
return STATUS_LABELS[status] ?? status;
|
||||
}
|
||||
|
||||
export function isTaskActive(task) {
|
||||
const status = String(task?.status || '').toLowerCase();
|
||||
return status === 'aktivan' || status === 'servis';
|
||||
}
|
||||
|
||||
export async function fetchTasks() {
|
||||
$tasksLoading.set(true);
|
||||
try {
|
||||
const data = await api.get('tasks/tasks/');
|
||||
$tasks.set(Array.isArray(data) ? data : (data?.results ?? []));
|
||||
} catch (error) {
|
||||
if (error?.status !== 401) {
|
||||
showToast('Greška pri dohvatu zadataka.', 'error');
|
||||
}
|
||||
} finally {
|
||||
$tasksLoading.set(false);
|
||||
}
|
||||
}
|
||||
|
||||
export async function createTask(data) {
|
||||
const response = await api.post('tasks/tasks/', data);
|
||||
$tasks.set([response, ...$tasks.get()]);
|
||||
showToast('Zadatak je uspješno kreiran.', 'success');
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function updateTaskStatus(taskId, status) {
|
||||
const response = await api.patch(`tasks/tasks/${taskId}/`, { status });
|
||||
$tasks.set($tasks.get().map((t) => (String(t.id) === String(taskId) ? response : t)));
|
||||
showToast(`Status zadatka promijenjen u: ${getStatusLabel(status)}`, 'success');
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function updateTask(taskId, changes) {
|
||||
const response = await api.patch(`tasks/tasks/${taskId}/`, changes);
|
||||
$tasks.set($tasks.get().map((t) => (String(t.id) === String(taskId) ? response : t)));
|
||||
showToast('Task je uspješno ažuriran.', 'success');
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function deleteTask(taskId) {
|
||||
await api.delete(`tasks/tasks/${taskId}/`);
|
||||
$tasks.set($tasks.get().filter((t) => String(t.id) !== String(taskId)));
|
||||
showToast('Zadatak je deaktiviran.', 'success');
|
||||
}
|
||||
Reference in New Issue
Block a user