add text area validation
This commit is contained in:
@@ -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'
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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')}...`}>
|
||||
</textarea>
|
||||
<button
|
||||
type="submit"
|
||||
className="btn-small btn-primary-small"
|
||||
disabled={isPending}
|
||||
<div
|
||||
className="flex gap-3"
|
||||
>
|
||||
{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>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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": "и",
|
||||
|
||||
Reference in New Issue
Block a user