Skip to content

Commit

Permalink
Migrate ClientForm to composable api #248
Browse files Browse the repository at this point in the history
  • Loading branch information
robertfausk committed Dec 28, 2024
1 parent 052cb2a commit aabe410
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 295 deletions.
354 changes: 124 additions & 230 deletions web/assets/js/components/Clients/ClientForm.vue

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion web/assets/js/components/Clients/ClientList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ export default {
});
try {
const clients = await ClientApi.find(data);
console.log(clients)
const items = clients.data['hydra:member'];
const total = clients.data['hydra:totalItems'];
Expand Down
2 changes: 1 addition & 1 deletion web/assets/js/components/Walk/WalkRemoveForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<script>
'use strict';
import GlobalFormError from '../Common/GlobalFormError.vue';
import getViolationsFeedback from '../../utils/validation.js';
import { getViolationsFeedback } from '../../utils';
import { useTagStore } from '../../stores/tag';
import { useWayPointStore } from '../../stores/way-point';
import { useTeamStore } from '../../stores/team';
Expand Down
2 changes: 1 addition & 1 deletion web/assets/js/components/WalkEpilogue.vue
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ import GlobalFormError from './Common/GlobalFormError.vue';
import WayPointList from './Walk/WayPointList.vue';
import WalkRating from './Walk/WalkRating.vue';
import dayjs from 'dayjs';
import getViolationsFeedback from '../utils/validation.js';
import { getViolationsFeedback } from '../utils';
import {useAlertStore, useClientStore, useTeamStore, useWayPointStore, useWalkStore} from '../stores';
export default {
Expand Down
2 changes: 1 addition & 1 deletion web/assets/js/components/WayPoint/WayPointForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@
'use strict';
import GlobalFormError from '../Common/GlobalFormError.vue';
import ColorBadge from '../Tags/ColorBadge.vue';
import getViolationsFeedback from '../../utils/validation.js';
import { getViolationsFeedback } from '../../utils';
import axios from 'axios';
import dayjs from 'dayjs';
import {useAlertStore, useAuthStore, useTagStore, useTeamStore, useWalkStore, useWayPointStore} from '../../stores';
Expand Down
2 changes: 1 addition & 1 deletion web/assets/js/components/WayPoint/WayPointRemoveForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<script>
'use strict';
import GlobalFormError from '../Common/GlobalFormError.vue';
import getViolationsFeedback from '../../utils/validation.js';
import { getViolationsFeedback } from '../../utils';
import { useTagStore } from '../../stores/tag';
import { useTeamStore } from '../../stores/team';
import { useWayPointStore } from '../../stores/way-point';
Expand Down
60 changes: 0 additions & 60 deletions web/assets/js/utils/validation.js

This file was deleted.

98 changes: 98 additions & 0 deletions web/assets/js/utils/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
interface Violation {
propertyPath?: string;
message: string;
}

interface ErrorData {
violations?: Violation[];
data?: ErrorData;
"hydra:description"?: string;
}

interface ValidationErrors {
global: string[];
[key: string]: string[] | undefined;
}

/**
* Converts error data into a structured object containing global and field-specific errors.
*
* @param errorData - The error data object to process.
* @returns A structured object containing validation errors.
*/
function getViolationErrorsObject(errorData?: ErrorData): ValidationErrors {
const errors: ValidationErrors = {
global: [],
};

let localErrorData: ErrorData | undefined = errorData;

if (!errorData) {
return errors;
}

if (errorData.data) {
localErrorData = errorData.data;
}

if (localErrorData?.violations) {
localErrorData.violations.forEach((violation) => {
if (violation.propertyPath) {
if (!errors[violation.propertyPath]) {
errors[violation.propertyPath] = [];
}
errors[violation.propertyPath]!.push(violation.message);
} else {
errors.global.push(violation.message);
}
});

return errors;
}

if (localErrorData["hydra:description"]) {
errors.global.push(localErrorData["hydra:description"]);
}

return errors;
}

/**
* Retrieves validation feedback messages based on specified fields and error data.
*
* @param fields - The list of field names to include in the feedback.
* @param errorData - The error data to process.
* @param isResultInverted - If true, feedback will include errors not in the specified fields.
* @returns A string containing concatenated feedback messages.
*/
function getViolationsFeedback(
fields: string[],
errorData?: ErrorData,
isResultInverted: boolean = false
): string {
const validationErrors = getViolationErrorsObject(errorData);
let message = '';

if (isResultInverted) {
for (const [fieldName, errorMessageArray] of Object.entries(validationErrors)) {
if (!fields.includes(fieldName) && fieldName !== 'global') {
message += ` ${errorMessageArray!.join(' ')}`;
}
}
} else {
fields.forEach(fieldName => {
if (validationErrors[fieldName]) {
message += ` ${validationErrors[fieldName]!.join(' ')}`;
}
});
}

return message.trim();
}

export {
getViolationErrorsObject,
getViolationsFeedback
};

export default getViolationsFeedback;

0 comments on commit aabe410

Please sign in to comment.