Skip to content

Commit

Permalink
Merge pull request fwickert#39 from fwickert/38-bug-on-menu-sometimes…
Browse files Browse the repository at this point in the history
…-for-admin-all-menu-not-display-calling-user-twice-1-without-useremail

Add 404 response type and refactor async handling
  • Loading branch information
fwickert authored Nov 25, 2024
2 parents 2d43412 + a0ca899 commit 5c27a67
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public UserController(UserService userService)
//getUser
[HttpGet("{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<UserItem>> Get(string id)
{
var user = await _userService.GetUserAsync(id);
Expand Down
85 changes: 46 additions & 39 deletions virtualteachergenaidemo.client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,54 +51,61 @@ function AuthenticatedApp(props: any) {
const { setRole } = useUserRole();

useEffect(() => {
const fetchUserRole = async () => {
const fetchUserRole = () => {
if (email) {
try {
const response = await fetch(`/api/User/${email}`);
if (!response.ok) {
throw new Error('Failed to fetch user role');
}
const userData = await response.json();
setRole(userData.role);
} catch (error) {
console.error('Error fetching user role:', error);
}
fetch(`/api/User/${email}`)
.then(response => {
if (!response.ok) {
if (response.status === 404) {
return createUserIfNotExists();
} else {
console.error('Failed to fetch user role:', response.statusText);
return Promise.reject('Failed to fetch user role');
}
}
return response.json();
})
.then(userData => {
if (userData) {
setRole(userData.role);
}
})
.catch(error => {
console.error('Error fetching user role:', error);
});
}
};

fetchUserRole();
}, [email, setRole]);

useEffect(() => {
const createUserIfNotExists = async () => {
const createUserIfNotExists = () => {
if (email) {
try {
const response = await fetch('/api/User', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: email,
name: email,
role: UserRoleEnum.User,
settings: {}
})
fetch('/api/User', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: email,
name: email,
role: UserRoleEnum.User,
settings: {}
})
})
.then(response => {
if (!response.ok) {
console.error('Failed to create or get user:', response.statusText);
return Promise.reject('Failed to create or get user');
} else {
setRole(UserRoleEnum.User);
}
})
.catch(error => {
console.error('Error creating user:', error);
});
if (!response.ok) {
throw new Error('Failed to create or get user');
}
else {
setRole(UserRoleEnum.User);
}
} catch (error) {
console.error('Error creating user:', error);
}
}
};

createUserIfNotExists();
}, []);
fetchUserRole();
}, [email, setRole]);

return (
<Router>
Expand Down

0 comments on commit 5c27a67

Please sign in to comment.