add text area validation
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
'use server'
|
||||
|
||||
import { getSessionData } from '@/app/actions/session';
|
||||
import { API_BASE_URL } from '@/app/actions/definitions';
|
||||
import { API_BASE_URL, createComplaintSchema } from '@/app/actions/definitions';
|
||||
|
||||
export async function getViolationSearchStatus() {
|
||||
const token = await getSessionData('token');
|
||||
@@ -151,15 +151,6 @@ export async function fetchViolationStats() {
|
||||
export async function getFileViolations(fileId: string, page: number, status?: string) {
|
||||
const token = await getSessionData('token');
|
||||
|
||||
console.log({
|
||||
file_id: fileId,
|
||||
page: page,
|
||||
size: 5,
|
||||
sort_direction: "desc",
|
||||
token: token,
|
||||
status: status
|
||||
})
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
|
||||
method: 'POST',
|
||||
@@ -183,7 +174,6 @@ export async function getFileViolations(fileId: string, page: number, status?: s
|
||||
|
||||
if (response.ok) {
|
||||
let parsed = await response.json();
|
||||
console.log(parsed);
|
||||
|
||||
if (parsed.message_body) {
|
||||
return parsed.message_body;
|
||||
@@ -276,7 +266,7 @@ interface complainBody {
|
||||
previousText: string,
|
||||
violationID: string | null,
|
||||
success: boolean,
|
||||
errorMessage?: string | null
|
||||
errorMessage?: Record<string, string> | null
|
||||
}
|
||||
|
||||
export async function createComplaint(
|
||||
@@ -285,7 +275,32 @@ export async function createComplaint(
|
||||
): Promise<complainBody> {
|
||||
const email = await getSessionData('email');
|
||||
const violationId = formData.get('violationId') as string || '';
|
||||
const text = formData.get('note') as string || '';
|
||||
const textArea = formData.get('note') as string || '';
|
||||
|
||||
const validatedFields = createComplaintSchema.safeParse({
|
||||
textArea
|
||||
});
|
||||
|
||||
if (!validatedFields.success) {
|
||||
const errors: Record<string, string> = {}
|
||||
JSON.parse(validatedFields.error.message).forEach((obj: {
|
||||
message: string,
|
||||
path: string
|
||||
}) => {
|
||||
if (errors[obj.path[0]]) {
|
||||
errors[obj.path[0]] = `${errors[obj.path[0]]} ${obj.message}`;
|
||||
} else {
|
||||
errors[obj.path[0]] = obj.message;
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
violationID: violationId,
|
||||
previousText: textArea,
|
||||
success: false,
|
||||
errorMessage: errors
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
|
||||
@@ -296,7 +311,7 @@ export async function createComplaint(
|
||||
message_body: {
|
||||
action: 'create',
|
||||
violation_id: violationId,
|
||||
text: text,
|
||||
text: textArea,
|
||||
email: email
|
||||
}
|
||||
}),
|
||||
@@ -312,7 +327,7 @@ export async function createComplaint(
|
||||
if (parsed?.message_code === 0) {
|
||||
return {
|
||||
violationID: violationId,
|
||||
previousText: text,
|
||||
previousText: textArea,
|
||||
success: true
|
||||
};
|
||||
} else {
|
||||
@@ -321,24 +336,29 @@ export async function createComplaint(
|
||||
} else {
|
||||
throw (`${response.status}`);
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
} catch (error) {
|
||||
const errors: Record<string, string> = {}
|
||||
|
||||
if (error && typeof error === 'object' && 'message_code' in error) {
|
||||
const err = error as { message_code: number };
|
||||
if (err.message_code === 2) {
|
||||
errors['server'] = 'the-complaint-has-not-been-registered'
|
||||
|
||||
return {
|
||||
violationID: violationId,
|
||||
previousText: text,
|
||||
previousText: textArea,
|
||||
success: false,
|
||||
errorMessage: 'the-complaint-has-not-been-registered'
|
||||
errorMessage: errors
|
||||
}
|
||||
}
|
||||
}
|
||||
errors['server'] = 'error-save';
|
||||
|
||||
return {
|
||||
violationID: violationId,
|
||||
previousText: text,
|
||||
previousText: textArea,
|
||||
success: false,
|
||||
errorMessage: 'error-save'
|
||||
errorMessage: errors
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user