Skip to content

Commit

Permalink
start adding validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
wbglaeser committed Jun 10, 2024
1 parent 88acd96 commit 5d16fd0
Show file tree
Hide file tree
Showing 9 changed files with 19,339 additions and 10,720 deletions.
29,956 changes: 19,240 additions & 10,716 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"homepage": "https://citizen-knowledge-graph.github.io/foerderfunke-react-app",
"dependencies": {
"@foerderfunke/matching-engine": "^0.4.5",
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.5",
"@mui/icons-material": "^5.15.19",
Expand Down
24 changes: 24 additions & 0 deletions public/assets/data/requirement-profiles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"datafields": "https://raw.githubusercontent.com/Citizen-Knowledge-Graph/requirement-profiles/main/datafields.ttl",
"queries": [
{
"fileUrl": "https://raw.githubusercontent.com/Citizen-Knowledge-Graph/requirement-profiles/main/shacl/dev-kindergeld.ttl",
"rpUri": "https://foerderfunke.org/default#kindergeld",
"title": "Kindergeld",
"description": "Sie haben Anspruch auf Kindergeld."
},
{
"fileUrl": "https://raw.githubusercontent.com/Citizen-Knowledge-Graph/requirement-profiles/main/shacl/dev-eauto.ttl",
"rpUri": "https://foerderfunke.org/default#eauto",
"title": "Unterstützung für den Kauf eines Elektroautos",
"description": "Sie haben Anspruch auf eine Förderung für ein neue E-Auto."
},
{
"fileUrl": "https://raw.githubusercontent.com/Citizen-Knowledge-Graph/requirement-profiles/main/shacl/kinderzuschlag.ttl",
"rpUri": "https://foerderfunke.org/default#kinderzuschlag",
"title": "Kinderzuschlag",
"description": "Sie haben Anspruch auf den Kinderzuschlag."
}
]
}

2 changes: 2 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import OnboardingUsername from "./screens/OnboardingUsername";
import ProfileSectionScreen from "./screens/profile-section/ProfileSectionScreen";
import EligibilityOverviewScreen from "./screens/eligibilty-overview/EligibilityOverviewScreen";
import AppStartup from "./AppStartup";
import AppValidation from "./AppValidation";

const theme = createTheme({
typography: {
Expand All @@ -23,6 +24,7 @@ const App = () => {
<ThemeProvider theme={theme}>
<ViewportUpdater/>
<AppStartup/>
<AppValidation/>
<Router basename={process.env.PUBLIC_URL}>
<div>
<Routes>
Expand Down
23 changes: 23 additions & 0 deletions src/AppValidation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useEffect } from 'react';
import { useUserStore } from "./storage/zustand";
import { runValidation } from "./services/validationService";

const AppValidation = () => {
const activeUserId = useUserStore((state) => state.activeUserId);


// initialise session store
useEffect(() => {
const validate = async () => {
if (activeUserId) {
await runValidation(activeUserId);
}
};

validate();
}, [activeUserId]);

return null;
};

export default AppValidation;
4 changes: 2 additions & 2 deletions src/screens/profile-section/hooks/useAddProfileField.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {useUserStore} from '../../../storage/zustand';

function useAddProfileField(value, datafield, entityData) {
return useCallback(() => {
const userId = useUserStore.getState().userId;
const activeUserId = useUserStore.getState().activeUserId;
return new Promise((resolve, reject) => {
try {
const _entityData = {
Expand All @@ -17,7 +17,7 @@ function useAddProfileField(value, datafield, entityData) {
type: entityData.parentData.type,
datafield: entityData.parentData.datafield,
};
UserModel.setField(userId, value, _entityData, _parentData);
UserModel.setField(activeUserId, value, _entityData, _parentData);
resolve();
} catch (error) {
reject(error);
Expand Down
4 changes: 4 additions & 0 deletions src/services/githubService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const fetchTurtleResource = async (url) => {
const response = await fetch(url);
return response.text();
}
41 changes: 41 additions & 0 deletions src/services/validationService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {UserModel} from "../models/UserModel";
import {
//validateAll,
validateUserProfile,
} from '@foerderfunke/matching-engine';
import {
convertUserProfileToTurtle,
} from '@foerderfunke/matching-engine/src/utils';
import readJson from "../utilities/readJson";
import {fetchTurtleResource} from "./githubService";

export const runValidation = async (activeUser) => {

// Get the active user profile
const userProfile = UserModel.retrieveUserData(activeUser);
const userProfileString = await convertUserProfileToTurtle(userProfile);

// load validation config
const validationConfig = await readJson('assets/data/requirement-profiles.json');

// validate user profile against datafields
const datafieldsString = await fetchTurtleResource(validationConfig['datafields']);
if (!(await validateUserProfile(userProfileString, datafieldsString))) {
console.error('Invalid user profile');
}

// collect requirement profiles
//let requirementProfiles = {};
console.log('Fetching requirement profiles:', validationConfig['queries'][0]['fileUrl'])
const fileUrl = await fetchTurtleResource(validationConfig['queries'][0]['fileUrl']);
console.log(fileUrl);

// for (const requirementProfile in validationConfig['queries']) {
// const fileUrl = requirementProfile['fileUrl'];
// console.log('Fetching requirement profile:', fileUrl);
// //requirementProfiles[rpUri] = await fetchTurtleResource(fileUrl);
// }
// console.log('Running validations for:', Object.keys(requirementProfiles));

return null;
}
4 changes: 2 additions & 2 deletions src/storage/zustand.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { create } from 'zustand';

export const useUserStore = create((set) => ({
userId: 'kinderzuschlag-user-profile',
activeUserId: 'kinderzuschlag-user-profile',
updateUserId: (newUserId) => {
console.log('STATE UPDATE: We are switching user');
set((state) => ({ userId: newUserId }));
set((state) => ({ activeUserId: newUserId }));
},
}));

Expand Down

0 comments on commit 5d16fd0

Please sign in to comment.