Skip to content

Commit 8d503ad

Browse files
committed
simplify user creation
1 parent a4ebe5f commit 8d503ad

File tree

7 files changed

+71
-89
lines changed

7 files changed

+71
-89
lines changed

src/ui/screens/info-pages/components/UserItem.js

Lines changed: 27 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import React, { useState } from "react";
2-
import { Typography, Button } from "@mui/material";
1+
import React from "react";
2+
import { Typography } from "@mui/material";
33
import { VBox, HBox } from "@/ui/shared-components/LayoutBoxes";
44
import theme from "@/theme";
5-
import MoreVertIcon from "@mui/icons-material/MoreVert";
65
import RegularButton from "@/ui/shared-components/RegularButton";
76
import { IconButton } from "@mui/material";
7+
import DeleteIcon from '@mui/icons-material/Delete';
8+
import FileDownloadIcon from '@mui/icons-material/FileDownload';
89

910

1011
const UserItem = ({ t, user, continueWithExisting, deleteExistingProfile, exportProfile }) => {
11-
const [isVisible, setIsVisible] = useState(false);
12-
1312
return (
1413
<HBox sx={{
1514
maxWidth: '800px',
@@ -23,13 +22,7 @@ const UserItem = ({ t, user, continueWithExisting, deleteExistingProfile, export
2322
<Typography variant="h6">{user.id}</Typography>
2423
<Typography variant="body2">{user.type}</Typography>
2524
</VBox>
26-
<HBox alignItems={'center'} justifyContent={'flex-end'}>
27-
<RegularButton
28-
variant={'yellowContained'}
29-
onClick={() => continueWithExisting(user.id)}
30-
text={'app.welcomeBack.continueBtn'}
31-
link={'/onboarding-choice'}
32-
/>
25+
<HBox alignItems={'center'} justifyContent={'flex-end'} sx={{ flexWrap : 'wrap' }}>
3326
<IconButton
3427
aria-label="more options"
3528
sx={{
@@ -40,57 +33,32 @@ const UserItem = ({ t, user, continueWithExisting, deleteExistingProfile, export
4033
color: 'black.main',
4134
},
4235
}}
43-
onClick={() => setIsVisible(!isVisible)}
36+
onClick={() => exportProfile(user.id)}
4437
>
45-
<MoreVertIcon />
38+
<FileDownloadIcon sx={{ fontSize: '20px'}} />
4639
</IconButton>
40+
<IconButton
41+
aria-label="more options"
42+
sx={{
43+
padding: '8px',
44+
color: 'blue.main',
45+
'&:hover': {
46+
backgroundColor: 'transparent',
47+
color: 'black.main',
48+
},
49+
}}
50+
onClick={() => deleteExistingProfile(user.id)}
51+
>
52+
<DeleteIcon sx={{ fontSize: '20px'}} />
53+
</IconButton>
54+
<RegularButton
55+
variant={'yellowContained'}
56+
onClick={() => continueWithExisting(user.id)}
57+
text={'app.welcomeBack.continueBtn'}
58+
link={'/onboarding-choice'}
59+
/>
4760
</HBox>
4861
</HBox>
49-
{
50-
isVisible && (
51-
<HBox alignItems={'center'} justifyContent={'flex-end'}>
52-
<Button
53-
variant="text"
54-
onClick={() => exportProfile(user.id)}
55-
sx={{
56-
color: 'blue.main',
57-
padding: '0',
58-
'&:hover': {
59-
backgroundColor: 'transparent',
60-
color: 'black.main',
61-
},
62-
'&:focus': {
63-
backgroundColor: 'transparent',
64-
color: 'black.main',
65-
}
66-
}}
67-
>
68-
<Typography variant="body2" sx={{ color: 'inherit' }}>
69-
{t("app.welcomeBack.exportBtn")}
70-
</Typography>
71-
</Button>
72-
<Button
73-
variant="text"
74-
onClick={() => deleteExistingProfile(user.id)}
75-
sx={{
76-
padding: '0',
77-
color: 'blue.main',
78-
'&:hover': {
79-
backgroundColor: 'transparent',
80-
color: 'black.main',
81-
},
82-
'&:focus': {
83-
backgroundColor: 'transparent',
84-
color: 'black.main',
85-
}
86-
}}>
87-
<Typography variant="body2" sx={{ color: 'inherit' }}>
88-
{t("app.welcomeBack.deleteBtn")}
89-
</Typography>
90-
</Button>
91-
</HBox>
92-
)
93-
}
9462
</VBox>
9563
</HBox>
9664
);
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1+
import { useEffect, useState } from "react";
12
import userManager from "@/core/managers/userManager";
23

3-
export const useFetchUserList = () => {
4-
const userIds = userManager.retrieveUserIds();
5-
const userList = userIds.map((userId) => {
6-
const userData = userManager.retrieveUserData(userId);
7-
return {
8-
id: userData['@id'],
9-
type: userData['@type'],
10-
};
11-
});
4+
export const useFetchUserList = (userDeleted) => {
5+
const [userList, setUserList] = useState([]);
6+
7+
useEffect (() => {
8+
const userIds = userManager.retrieveUserIds();
9+
const newUserList = userIds.map((userId) => {
10+
const userData = userManager.retrieveUserData(userId);
11+
return {
12+
id: userData['@id'],
13+
type: userData['@type'],
14+
};
15+
});
16+
setUserList(newUserList)
17+
}, [userDeleted]);
18+
1219
return userList;
1320
}
1421
export default useFetchUserList;

src/ui/screens/info-pages/returning-user/InfoScreenReturningUserContainer.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import React from "react";
1+
import React, { useState } from "react";
22
import useTranslation from "@/ui/language/useTranslation";
33
import dayjs from "dayjs";
44
import { convertUserProfileToTurtle } from "@foerderfunke/matching-engine/src/profile-conversion";
5-
import {
6-
questionsStackStore,
7-
useSelectedTopicsStore,
8-
useValidationReportStore,
9-
} from "@/ui/storage/zustand";
105
import { useUserStore } from "@/ui/storage/zustand";
116
import InfoScreenReturningUser from "./InfoScreenReturningUser";
127
import useJointValidationStatus from "@/ui/shared-hooks/useJointValidationStatus";
@@ -16,10 +11,11 @@ import useFetchUserList from "../hooks/useFetchUserList";
1611

1712
const InfoScreenReturningUserContainer = () => {
1813
const { t } = useTranslation();
14+
const [userDeleted, setUserDeleted] = useState(false);
1915
const isDesktop = useStore((state) => state.isDesktop);
2016
const { isLoadingJointStatus } = useJointValidationStatus();
2117
const updateUserId = useUserStore((state) => state.updateUserId);
22-
const userList = useFetchUserList();
18+
const userList = useFetchUserList(userDeleted);
2319

2420
const exportProfile = async (userId) => {
2521
const userProfile = userManager.retrieveUserData(userId);
@@ -40,17 +36,15 @@ const InfoScreenReturningUserContainer = () => {
4036

4137
const deleteExistingProfile = (userId) => {
4238
userManager.deleteUser(userId);
43-
useValidationReportStore.getState().clear();
44-
useSelectedTopicsStore.getState().clear();
45-
questionsStackStore.getState().resetQuestionsStack();
39+
setUserDeleted(!userDeleted);
4640
};
4741

4842
return (
4943
<InfoScreenReturningUser
44+
t={t}
5045
isLoading={isLoadingJointStatus}
5146
isDesktop={isDesktop}
5247
userList={userList}
53-
t={t}
5448
exportProfile={exportProfile}
5549
continueWithExisting={continueWithExisting}
5650
deleteExistingProfile={deleteExistingProfile}

src/ui/screens/onboarding-pages/user/OnboardingUserContainer.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useState } from 'react';
2+
import { useNavigate } from "react-router-dom";
23
import { Typography, TextField } from "@mui/material";
34
import Layout from "@/ui/shared-components/Layout";
45
import AppScreenWrapper from "@/ui/shared-components/AppScreenWrapper";
@@ -10,6 +11,19 @@ const OnboardingUser = () => {
1011
const [userId, setUserId] = useState('');
1112
const userType = 'Citizen'; // This is hardcoded for now, but it should be dynamic based on the user type
1213
const { initialiseNewUser, error } = useInitialiseNewUser();
14+
const navigate = useNavigate();
15+
16+
const handleCreateUser = () => {
17+
const { success, error } = initialiseNewUser(userId, userType);
18+
console.log('User creation result:', success, error);
19+
20+
if (success) {
21+
navigate("/onboarding-choice");
22+
console.log('User created successfully');
23+
} else {
24+
console.error('Failed to create user:', error.message);
25+
}
26+
};
1327

1428
return (
1529
<Layout isApp={true} logo={false}>
@@ -43,17 +57,16 @@ const OnboardingUser = () => {
4357
value={userId}
4458
onChange={(e) => setUserId(e.target.value)}
4559
/>
46-
{error ? (
60+
{error?.message ? (
4761
<Typography variant="body1" color="error">
48-
{error}
62+
{error.message}
4963
</Typography>
5064
) : null}
5165
</VBox>
5266
<RegularButton
5367
variant={'blueContained'}
5468
text={'app.welcomeBack.createNewBtn'}
55-
onClick={() => initialiseNewUser(userId, userType)}
56-
link={'/onboarding-choice'}
69+
onClick={handleCreateUser}
5770
/>
5871
</VBox>
5972
</AppScreenWrapper>

src/ui/shared-components/RegularButton.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ const RegularButton = ({
107107
<Button
108108
variant="contained"
109109
sx={{...buttonStyles[variant] || buttonStyles.default}}
110-
component={Link}
110+
component={link ? Link : 'button'}
111111
to={link}
112112
onClick={onClick}
113113
>
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { useState, useCallback } from "react";
1+
import { useCallback } from "react";
22
import userManager from "@/core/managers/userManager";
33
import { useUserStore } from "@/ui/storage/zustand";
44

55
const useInitialiseNewUser = () => {
6-
const [error, setError] = useState(null);
76
const updateUserId = useUserStore((state) => state.updateUserId);
87

98
const initialiseNewUser = useCallback((userId, userType) => {
@@ -13,13 +12,14 @@ const useInitialiseNewUser = () => {
1312
try {
1413
userManager.initialiseNewUser(userIdWithNameSpace, userTypeWithNameSpace);
1514
updateUserId(userIdWithNameSpace);
15+
return { success: true, error: null };
1616
} catch (err) {
1717
console.error(`Error initializing new user: ${err.message}`);
18-
setError(err);
18+
return { success: false, error: err };
1919
}
2020
}, [updateUserId]);
2121

22-
return { initialiseNewUser, error };
22+
return { initialiseNewUser };
2323
};
2424

2525
export default useInitialiseNewUser;

src/ui/shared-hooks/useJointValidationStatus.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useState, useEffect } from "react";
22
import { useQuestionsUpdate, useValidationUpdate } from "@/ui/storage/updates";
33

44
const useJointValidationStatus = () => {
5-
const [isLoadingJointStatus, setIsLoadingJointStatus] = useState(false);
5+
const [isLoadingJointStatus, setIsLoadingJointStatus] = useState(true);
66
const validationIsLoading = useValidationUpdate((s) => s.validationIsLoading);
77
const questionsAreLoading = useQuestionsUpdate((s) => s.questionsAreLoading);
88

0 commit comments

Comments
 (0)