diff --git a/src/app/actions/definitions.ts b/src/app/actions/definitions.ts index 37c0062..77924dd 100644 --- a/src/app/actions/definitions.ts +++ b/src/app/actions/definitions.ts @@ -83,6 +83,15 @@ export const loginFormSchema = z .trim(), }) + +export const createComplaintSchema = z + .object({ + textArea: z + .string() + .min(6, { error: 'text-area-error-fill-complaint-text' }) + .trim(), + }) + export const localDevelopmentUrl = process.env.DEV_URL ? process.env.DEV_URL : 'http://localhost'; export const API_BASE_URL = process.env.NODE_ENV === 'development' diff --git a/src/app/actions/violationActions.ts b/src/app/actions/violationActions.ts index 04f0d54..f32d7d8 100644 --- a/src/app/actions/violationActions.ts +++ b/src/app/actions/violationActions.ts @@ -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 | null } export async function createComplaint( @@ -285,7 +275,32 @@ export async function createComplaint( ): Promise { 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 = {} + 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 = {} + 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 } } } diff --git a/src/app/ui/file-page/violation-table/case-complaint.tsx b/src/app/ui/file-page/violation-table/case-complaint.tsx index 6a96356..53590e6 100644 --- a/src/app/ui/file-page/violation-table/case-complaint.tsx +++ b/src/app/ui/file-page/violation-table/case-complaint.tsx @@ -57,8 +57,10 @@ export default function CaseComplaint({ selectedViolation }: { selectedViolation setShowCreateCase(false); } else if (state && !state.success) { - if (state.errorMessage) { - toast.warning(t(state.errorMessage)); + console.log('state.errorMessage'); + console.log(state.errorMessage); + if (state.errorMessage?.server) { + toast.warning(t(state.errorMessage.server)); } else { toast.warning(t('the-complaint-has-not-been-registered')); } @@ -98,13 +100,35 @@ export default function CaseComplaint({ selectedViolation }: { selectedViolation className="note-textarea" placeholder={`${t('text-of-the-complaint')}...`}> - + + + {state?.errorMessage?.textArea && ( +

+ + { + state?.errorMessage?.textArea.split('&').map((e, index) => { + return ( + + {t(e)} +
+
+ ) + }) + } +

+ )} + diff --git a/src/i18n/messages/en.json b/src/i18n/messages/en.json index 3fe6ccc..b4a546f 100644 --- a/src/i18n/messages/en.json +++ b/src/i18n/messages/en.json @@ -387,7 +387,8 @@ "mark-all-as-read": "Mark all as read", "all-notifications": "All notifications", "deselect": "Deselect", - "select-all": "Select all" + "select-all": "Select all", + "text-area-error-fill-complaint-text": "Fill out the complaint text" }, "Login-register-form": { "and": "and", diff --git a/src/i18n/messages/ru.json b/src/i18n/messages/ru.json index f5a6f12..fad10fe 100644 --- a/src/i18n/messages/ru.json +++ b/src/i18n/messages/ru.json @@ -387,7 +387,8 @@ "mark-all-as-read": "Отметить все как прочитанное", "all-notifications": "Все уведомления", "deselect": "Снять выделение", - "select-all": "Выбрать все" + "select-all": "Выбрать все", + "text-area-error-fill-complaint-text": "Заполните текст жалобы" }, "Login-register-form": { "and": "и",