Skip to content

Commit

Permalink
push kinderzuschlag profile to session storage
Browse files Browse the repository at this point in the history
  • Loading branch information
wbglaeser committed Jun 6, 2024
1 parent 5c342c5 commit 5862cdb
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Onboarding from './screens/Onboarding';
import OnboardingWelcome from "./screens/OnboardingWelcome";
import OnboardingSectionsScreen from "./screens/onboarding-sections/OnboardingSectionsScreen";
import OnboardingUsername from "./screens/OnboardingUsername";
import AppStartup from "./AppStartup";

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

const AppStartup = () => {
//const userId = useUserStore((state) => state.userId);
const [sessionStoreInitialised, setSessionStoreInitialised] = useState(false);

// initialise session store
useEffect(() => {
const initialiseSessionStore = async () => {
if (!sessionStoreInitialised) {
await loadUserData();
console.log('Session store initialised');
setSessionStoreInitialised(true);
}
};

initialiseSessionStore();
}, [sessionStoreInitialised]);

return null;
};

export default AppStartup;
31 changes: 15 additions & 16 deletions src/models/UserModel.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { storage } from '../storage/mmkv';

export class UserModel {
static initialiseNewUser(userId) {
// check if the user already exists
if (storage.getString(userId)) {
if (sessionStorage.getItem(userId)) {
throw new Error('User already exists');
}

// create a new user
let userString = `{"@id":"${userId}","@type":"ff:Citizen"}`;
storage.set(userId, userString);
sessionStorage.setItem(userId, userString);

// add the user to the list of user ids
const userIds = JSON.parse(storage.getString('userIds') || '[]');
const userIds = JSON.parse(sessionStorage.getItem('userIds') || '[]');
if (!userIds.includes(userId)) {
userIds.push(userId);
storage.set('userIds', JSON.stringify(userIds));
sessionStorage.setItem('userIds', JSON.stringify(userIds));
}
}

Expand All @@ -39,7 +37,7 @@ export class UserModel {

if (!updated) {
throw new Error(
`Could not set datafiled ${entityData.datafield} in user profile`
`Could not set datafield ${entityData.datafield} in user profile`
);
}

Expand All @@ -56,30 +54,31 @@ export class UserModel {
return retrieveField(userProfile, datafield, entityData);
}

// store user data to mmkv
static storeUserData(userId, userData) {
storage.set(userId, JSON.stringify(userData));
const userIds = JSON.parse(storage.getString('userIds') || '[]');
// store user data to session storage
static storeUserData(userData) {
const userId = userData['@id'];
sessionStorage.setItem(userId, JSON.stringify(userData));
const userIds = JSON.parse(sessionStorage.getItem('userIds') || '[]');
if (!userIds.includes(userId)) {
userIds.push(userId);
storage.set('userIds', JSON.stringify(userIds));
sessionStorage.setItem('userIds', JSON.stringify(userIds));
}
}

// retrieve the user data from mmkv
// retrieve the user data from session storage
static retrieveUserData(entityId) {
let userString = storage.getString(entityId);
let userString = sessionStorage.getItem(entityId);
if (!userString) {
userString = `{"@id":"${entityId}", "@type":"ff:Citizen"}`;
storage.set(entityId, userString);
sessionStorage.setItem(entityId, userString);
}

return JSON.parse(userString);
}

// return all user ids
static retrieveAllUserIds() {
return JSON.parse(storage.getString('userIds') || '[]');
return JSON.parse(sessionStorage.getItem('userIds') || '[]');
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/services/userService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import readJson from '../utilities/readJson';
import {UserModel} from "../models/UserModel";

export const loadUserData = async () => {
// initialise kinderzuschlag profile
const profilePath = 'kinderzuschlag-user-profile';
const userProfilePath = `assets/data/user-profile-examples/${profilePath}.json`;

// fetch the JSON data from the specified path
const userProfileData = await readJson(userProfilePath);

// Store user data in session storage
UserModel.storeUserData(userProfileData);
};



// Assuming UserStore's storeUserData function
export class UserStore {
static storeUserData(userId, userData) {
// Assuming this logic now stores data in session storage
sessionStorage.setItem(userId, JSON.stringify(userData));
}
}
9 changes: 9 additions & 0 deletions src/utilities/readJson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const readJson = async (filePath) => {
const response = await fetch(filePath);
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
};

export default readJson;

0 comments on commit 5862cdb

Please sign in to comment.