Skip to content

Commit

Permalink
Fix: #320 Previous values of Data Source configuration gets erased wh…
Browse files Browse the repository at this point in the history
…en only saved with privacy Dashboard URL value
  • Loading branch information
MARZOOQUE authored and georgepadayatti committed Oct 4, 2024
1 parent 80fad49 commit 2c1770f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
40 changes: 27 additions & 13 deletions src/components/dataAgreements/DataAgreementActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,17 @@ export const DataAgreementPayload = (
},
};

// Update dataSources if AttributeType is "data_using_service" and all names are not empty
if (
createdData.AttributeType === "data_using_service" &&
createdData.dataSources
) {
const validDataSources = createdData.dataSources.filter(
(source: any) => source.name !== ""
);
if (validDataSources.length === createdData.dataSources.length) {
payload.dataAgreement.dataSources = validDataSources;
}
}
// Update dataSources to include only objects with non-empty name
if (createdData.dataSources && Array.isArray(createdData.dataSources)) {
payload.dataAgreement.dataSources = createdData.dataSources.filter(
(source) => source.name && source.name.trim() !== ""
);
}

return payload;
};

export function validateSources(data: any[]): boolean {
export function validateDataSources(data: any[]): boolean {
if (!data || data.length === 0) {
return true; // Return true for empty or undefined data
}
Expand All @@ -145,6 +139,26 @@ export function validateSources(data: any[]): boolean {
const name = item.name || "";
const location = item.location || "";
const sector = item.sector || "";
const privacyDashboardUrl = item.privacyDashboardUrl || "";

// Check if only privacyDashboardUrl is present
if (
privacyDashboardUrl.length > 0 &&
name.length === 0 &&
location.length === 0 &&
sector.length === 0
) {
return false;
}

// Validate URL if privacyDashboardUrl is not empty and a valid url
if (privacyDashboardUrl.length > 0) {
try {
new URL(privacyDashboardUrl);
} catch (error) {
return false; // Invalid URL
}
}

const allEmpty =
name.length === 0 && location.length === 0 && sector.length === 0;
Expand Down
10 changes: 5 additions & 5 deletions src/components/modals/dataAgreementModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
} from "./modalStyle";
import {
DataAgreementPayload,
validateSources,
validateDataSources,
} from "../dataAgreements/DataAgreementActions";
import { HttpService } from "../../service/HTTPService";
import { Purpose } from "../dataAgreements/Purpose";
Expand Down Expand Up @@ -393,7 +393,7 @@ export default function DataAgreementModal(props: Props) {
dataSources &&
dataSources.length > 0 &&
AttributeType === "data_using_service"
? validateSources(dataSources)
? validateDataSources(dataSources)
: true
) {
if (mode === "Create") {
Expand Down Expand Up @@ -458,7 +458,7 @@ export default function DataAgreementModal(props: Props) {
dataSources &&
dataSources.length > 0 &&
AttributeType === "data_using_service"
? validateSources(dataSources)
? validateDataSources(dataSources)
: true
) {
if (mode === "Create") {
Expand Down Expand Up @@ -518,7 +518,7 @@ export default function DataAgreementModal(props: Props) {
(dataSources &&
dataSources.length > 0 &&
AttributeType === "data_using_service"
? validateSources(dataSources)
? validateDataSources(dataSources)
: true) &&
isFormDataChanged(methods.formState)
) {
Expand All @@ -534,7 +534,7 @@ export default function DataAgreementModal(props: Props) {
(dataSources &&
dataSources.length > 0 &&
AttributeType === "data_using_service"
? validateSources(dataSources)
? validateDataSources(dataSources)
: true) &&
// if da is saved initially so during edit user can directly publish
(selectedDataAgreement && selectedDataAgreement.lifecycle === "draft"
Expand Down

0 comments on commit 2c1770f

Please sign in to comment.