refactor: get new possible states from backend instead of computing them on frontend

This commit is contained in:
2025-11-02 17:30:53 +01:00
parent 323ea3902b
commit a3b21d23e9
5 changed files with 40 additions and 41 deletions

View File

@@ -31,6 +31,26 @@ class InternshipStatusController extends Controller
return response()->json($internship_statuses);
}
public function get_next_states(int $id) {
$user = auth()->user();
$internship = Internship::find($id);
if(!$internship) {
return response()->json([
'message' => 'No such internship exists.'
], 400);
}
if ($user->role !== 'ADMIN' && $internship->user_id !== $user->id && $user->id !== $internship->contact) {
abort(403, 'Unauthorized');
}
$currentStatus = $this->currentInternshipStatus($internship);
$nextPossibleStatuses = $this->possibleNewStatuses($currentStatus->status, $user->role);
return response()->json($nextPossibleStatuses);
}
/**
* Display a listing of the resource.
*/

View File

@@ -34,6 +34,7 @@ Route::prefix('/internships')->group(function () {
Route::get("/", [InternshipController::class, 'get'])->name("api.internships.get");
Route::put("/status", [InternshipStatusController::class, 'update'])->name("api.internships.status.update");
Route::get("/statuses", [InternshipStatusController::class, 'get'])->name("api.internships.get");
Route::get("/next-statuses", [InternshipStatusController::class, 'get_next_states'])->name("api.internships.status.next.get");
Route::post("/basic", [InternshipController::class, 'update_basic'])->name("api.internships.update.basic");
});

View File

@@ -1,6 +1,6 @@
<script setup lang="ts">
import type { Internship } from '~/types/internships';
import { InternshipStatus, possibleNextStates, prettyInternshipStatus, type NewInternshipStatusData } from '~/types/internship_status';
import { InternshipStatus, prettyInternshipStatus, type NewInternshipStatusData } from '~/types/internship_status';
import type { User } from '~/types/user';
import { FetchError } from 'ofetch';
@@ -14,22 +14,24 @@ const user = useSanctumUser<User>();
const rules = {
required: (v: any) => (!!v && String(v).trim().length > 0) || 'Povinné pole',
};
const possible_states = computed(() => possibleNextStates(props.internship.status.status, user.value!.role).map((state) => ({
title: prettyInternshipStatus(state),
value: state
})));
const isValid = ref(false);
const new_state = ref(null as InternshipStatus | null);
const note = ref("");
const loading = ref(false);
const error = ref(null as null | string);
const save_error = ref(null as null | string);
const client = useSanctumClient();
const { data, refresh } = await useSanctumFetch<any>(`/api/internships/${props.internship.id}/next-statuses`, undefined, {
transform: (statuses: InternshipStatus[]) => statuses.map((state) => ({
title: prettyInternshipStatus(state),
value: state
}))
});
async function submit() {
error.value = null;
save_error.value = null;
loading.value = true;
const new_status: NewInternshipStatusData = {
@@ -45,10 +47,11 @@ async function submit() {
new_state.value = null;
note.value = "";
refresh();
emit('successfulSubmit');
} catch (e) {
if (e instanceof FetchError) {
error.value = e.response?._data.message;
save_error.value = e.response?._data.message;
}
} finally {
loading.value = false;
@@ -59,11 +62,15 @@ async function submit() {
<template>
<div>
<!-- Chybová hláška -->
<v-alert v-if="error !== null" density="compact" :text="error" title="Chyba" type="error" id="login-error-alert"
class="mx-auto alert"></v-alert>
<v-alert v-if="save_error !== null" density="compact" :text="`Nepodarilo uložiť: ${save_error}`" title="Chyba"
type="error" class="mx-auto alert"></v-alert>
<!-- Chybová hláška -->
<v-alert v-if="save_error !== null" density="compact" :text="`Nepodarilo sa načítať stavy: ${save_error}`"
title="Chyba" type="error" class="mx-auto alert"></v-alert>
<v-form v-model="isValid" @submit.prevent="submit" :disabled="loading">
<v-select v-model="new_state" label="Stav" :items="possible_states" item-value="value"></v-select>
<v-select v-model="new_state" label="Stav" :items="data" item-value="value"></v-select>
<v-text-field v-model="note" :rules="[rules.required]" label="Poznámka"></v-text-field>
<v-btn type="submit" color="success" size="large" block :disabled="!isValid">

View File

@@ -87,7 +87,7 @@ const { data, error, refresh } = await useSanctumFetch<Internship>(`/api/interns
<br />
<h4>Zmena stavu</h4>
<InternshipStatusEditor :key="`e-${refreshKey}`" :internship="data!"
<InternshipStatusEditor :internship="data!"
@successful-submit="() => { refresh(); refreshKey++; }" />
</div>

View File

@@ -38,33 +38,4 @@ export function prettyInternshipStatus(status: InternshipStatus) {
default:
throw new Error("Unknown status");
}
}
export function possibleNextStates(status: InternshipStatus, user_role: Role) {
switch (status) {
case InternshipStatus.SUBMITTED:
if (user_role === Role.EMPLOYER) {
return [];
}
return [InternshipStatus.CONFIRMED, InternshipStatus.DENIED];
case InternshipStatus.CONFIRMED:
if (user_role === Role.EMPLOYER) {
return [InternshipStatus.DENIED];
}
return [InternshipStatus.SUBMITTED, InternshipStatus.DENIED, InternshipStatus.DEFENDED, InternshipStatus.NOT_DEFENDED];
case InternshipStatus.DENIED:
if (user_role === Role.EMPLOYER) {
return [InternshipStatus.CONFIRMED];
}
return [InternshipStatus.SUBMITTED, InternshipStatus.CONFIRMED];
case InternshipStatus.DEFENDED:
return [];
case InternshipStatus.NOT_DEFENDED:
return [];
default:
throw new Error("Unknown status");
}
}