add text area validation
This commit is contained in:
@@ -83,6 +83,15 @@ export const loginFormSchema = z
|
|||||||
.trim(),
|
.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 localDevelopmentUrl = process.env.DEV_URL ? process.env.DEV_URL : 'http://localhost';
|
||||||
|
|
||||||
export const API_BASE_URL = process.env.NODE_ENV === 'development'
|
export const API_BASE_URL = process.env.NODE_ENV === 'development'
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
'use server'
|
'use server'
|
||||||
|
|
||||||
import { getSessionData } from '@/app/actions/session';
|
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() {
|
export async function getViolationSearchStatus() {
|
||||||
const token = await getSessionData('token');
|
const token = await getSessionData('token');
|
||||||
@@ -151,15 +151,6 @@ export async function fetchViolationStats() {
|
|||||||
export async function getFileViolations(fileId: string, page: number, status?: string) {
|
export async function getFileViolations(fileId: string, page: number, status?: string) {
|
||||||
const token = await getSessionData('token');
|
const token = await getSessionData('token');
|
||||||
|
|
||||||
console.log({
|
|
||||||
file_id: fileId,
|
|
||||||
page: page,
|
|
||||||
size: 5,
|
|
||||||
sort_direction: "desc",
|
|
||||||
token: token,
|
|
||||||
status: status
|
|
||||||
})
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
|
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@@ -183,7 +174,6 @@ export async function getFileViolations(fileId: string, page: number, status?: s
|
|||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
let parsed = await response.json();
|
let parsed = await response.json();
|
||||||
console.log(parsed);
|
|
||||||
|
|
||||||
if (parsed.message_body) {
|
if (parsed.message_body) {
|
||||||
return parsed.message_body;
|
return parsed.message_body;
|
||||||
@@ -276,7 +266,7 @@ interface complainBody {
|
|||||||
previousText: string,
|
previousText: string,
|
||||||
violationID: string | null,
|
violationID: string | null,
|
||||||
success: boolean,
|
success: boolean,
|
||||||
errorMessage?: string | null
|
errorMessage?: Record<string, string> | null
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createComplaint(
|
export async function createComplaint(
|
||||||
@@ -285,7 +275,32 @@ export async function createComplaint(
|
|||||||
): Promise<complainBody> {
|
): Promise<complainBody> {
|
||||||
const email = await getSessionData('email');
|
const email = await getSessionData('email');
|
||||||
const violationId = formData.get('violationId') as string || '';
|
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 {
|
try {
|
||||||
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
|
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
|
||||||
@@ -296,7 +311,7 @@ export async function createComplaint(
|
|||||||
message_body: {
|
message_body: {
|
||||||
action: 'create',
|
action: 'create',
|
||||||
violation_id: violationId,
|
violation_id: violationId,
|
||||||
text: text,
|
text: textArea,
|
||||||
email: email
|
email: email
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
@@ -312,7 +327,7 @@ export async function createComplaint(
|
|||||||
if (parsed?.message_code === 0) {
|
if (parsed?.message_code === 0) {
|
||||||
return {
|
return {
|
||||||
violationID: violationId,
|
violationID: violationId,
|
||||||
previousText: text,
|
previousText: textArea,
|
||||||
success: true
|
success: true
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
@@ -321,24 +336,29 @@ export async function createComplaint(
|
|||||||
} else {
|
} else {
|
||||||
throw (`${response.status}`);
|
throw (`${response.status}`);
|
||||||
}
|
}
|
||||||
} catch (error: unknown) {
|
} catch (error) {
|
||||||
|
const errors: Record<string, string> = {}
|
||||||
|
|
||||||
if (error && typeof error === 'object' && 'message_code' in error) {
|
if (error && typeof error === 'object' && 'message_code' in error) {
|
||||||
const err = error as { message_code: number };
|
const err = error as { message_code: number };
|
||||||
if (err.message_code === 2) {
|
if (err.message_code === 2) {
|
||||||
|
errors['server'] = 'the-complaint-has-not-been-registered'
|
||||||
|
|
||||||
return {
|
return {
|
||||||
violationID: violationId,
|
violationID: violationId,
|
||||||
previousText: text,
|
previousText: textArea,
|
||||||
success: false,
|
success: false,
|
||||||
errorMessage: 'the-complaint-has-not-been-registered'
|
errorMessage: errors
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
errors['server'] = 'error-save';
|
||||||
|
|
||||||
return {
|
return {
|
||||||
violationID: violationId,
|
violationID: violationId,
|
||||||
previousText: text,
|
previousText: textArea,
|
||||||
success: false,
|
success: false,
|
||||||
errorMessage: 'error-save'
|
errorMessage: errors
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,8 +57,10 @@ export default function CaseComplaint({ selectedViolation }: { selectedViolation
|
|||||||
|
|
||||||
setShowCreateCase(false);
|
setShowCreateCase(false);
|
||||||
} else if (state && !state.success) {
|
} else if (state && !state.success) {
|
||||||
if (state.errorMessage) {
|
console.log('state.errorMessage');
|
||||||
toast.warning(t(state.errorMessage));
|
console.log(state.errorMessage);
|
||||||
|
if (state.errorMessage?.server) {
|
||||||
|
toast.warning(t(state.errorMessage.server));
|
||||||
} else {
|
} else {
|
||||||
toast.warning(t('the-complaint-has-not-been-registered'));
|
toast.warning(t('the-complaint-has-not-been-registered'));
|
||||||
}
|
}
|
||||||
@@ -98,13 +100,35 @@ export default function CaseComplaint({ selectedViolation }: { selectedViolation
|
|||||||
className="note-textarea"
|
className="note-textarea"
|
||||||
placeholder={`${t('text-of-the-complaint')}...`}>
|
placeholder={`${t('text-of-the-complaint')}...`}>
|
||||||
</textarea>
|
</textarea>
|
||||||
<button
|
<div
|
||||||
type="submit"
|
className="flex gap-3"
|
||||||
className="btn-small btn-primary-small"
|
|
||||||
disabled={isPending}
|
|
||||||
>
|
>
|
||||||
{t('submit-violation')}
|
<button
|
||||||
</button>
|
type="submit"
|
||||||
|
className="btn-small btn-primary-small"
|
||||||
|
disabled={isPending}
|
||||||
|
>
|
||||||
|
{t('submit-violation')}
|
||||||
|
</button>
|
||||||
|
|
||||||
|
{state?.errorMessage?.textArea && (
|
||||||
|
<p
|
||||||
|
className="text-sm text-red-500"
|
||||||
|
>
|
||||||
|
|
||||||
|
{
|
||||||
|
state?.errorMessage?.textArea.split('&').map((e, index) => {
|
||||||
|
return (
|
||||||
|
<span key={index}>
|
||||||
|
{t(e)}
|
||||||
|
<br />
|
||||||
|
</span>
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -387,7 +387,8 @@
|
|||||||
"mark-all-as-read": "Mark all as read",
|
"mark-all-as-read": "Mark all as read",
|
||||||
"all-notifications": "All notifications",
|
"all-notifications": "All notifications",
|
||||||
"deselect": "Deselect",
|
"deselect": "Deselect",
|
||||||
"select-all": "Select all"
|
"select-all": "Select all",
|
||||||
|
"text-area-error-fill-complaint-text": "Fill out the complaint text"
|
||||||
},
|
},
|
||||||
"Login-register-form": {
|
"Login-register-form": {
|
||||||
"and": "and",
|
"and": "and",
|
||||||
|
|||||||
@@ -387,7 +387,8 @@
|
|||||||
"mark-all-as-read": "Отметить все как прочитанное",
|
"mark-all-as-read": "Отметить все как прочитанное",
|
||||||
"all-notifications": "Все уведомления",
|
"all-notifications": "Все уведомления",
|
||||||
"deselect": "Снять выделение",
|
"deselect": "Снять выделение",
|
||||||
"select-all": "Выбрать все"
|
"select-all": "Выбрать все",
|
||||||
|
"text-area-error-fill-complaint-text": "Заполните текст жалобы"
|
||||||
},
|
},
|
||||||
"Login-register-form": {
|
"Login-register-form": {
|
||||||
"and": "и",
|
"and": "и",
|
||||||
|
|||||||
Reference in New Issue
Block a user