-
Notifications
You must be signed in to change notification settings - Fork 1
User stuff #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
User stuff #20
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8ff6a1c
Test lien backend/frontend user
Actarus35 15c73c7
User Connection work
Actarus35 63f1485
Correction pull request
Actarus35 028a25e
Merge branch 'main' into UserStuff
floflo0 f92f1a4
Fix user is loading
floflo0 ef78047
Fix Loading never end for User connection
Actarus35 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { createContext } from 'react'; | ||
| import { UserRole } from '../models/UserRole'; | ||
| import { User } from '../models/User'; | ||
|
|
||
| export interface UserContextType { | ||
| user: User | null; | ||
| createUser: (role: UserRole) => Promise<void>; | ||
| } | ||
|
|
||
| export const UserContext = createContext<UserContextType | undefined>(undefined); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import React, { useState, useEffect, ReactNode } from 'react'; | ||
| import { getUserInfo, createUser as apiCreateUser } from '../services/userServices.tsx'; | ||
| import { UserRole } from '../models/UserRole.ts'; | ||
| import { User } from '../models/User.ts'; | ||
| import { UserContext } from './UserContext.tsx'; | ||
|
|
||
| interface UserProviderProps { | ||
| children: ReactNode; | ||
| } | ||
|
|
||
| export const UserProvider: React.FC<UserProviderProps> = ({ children }) => { | ||
| const [user, setUser] = useState<User | null | undefined>(undefined); | ||
|
|
||
| useEffect(() => { | ||
| const loadUserInfo = async () => { | ||
| try { | ||
| const userInfo = await getUserInfo(); | ||
| setUser(userInfo); | ||
| } catch { | ||
| setUser(null); | ||
| } | ||
| }; | ||
| loadUserInfo(); | ||
| }, []); | ||
|
|
||
| const createUser = async (role: UserRole) => { | ||
| const newUser = await apiCreateUser(role); | ||
| setUser(newUser); | ||
| }; | ||
|
|
||
| if (user === undefined) { | ||
| return <div>Loading...</div>; | ||
| } | ||
|
|
||
| return ( | ||
| <UserContext.Provider value={{ user, createUser }}> | ||
| {children} | ||
| </UserContext.Provider> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { useContext } from 'react'; | ||
| import { UserContext } from './UserContext.tsx'; | ||
|
|
||
| export const useUser = () => { | ||
| const context = useContext(UserContext); | ||
| if (!context) { | ||
| throw new Error('useUser must be used within a UserProvider'); | ||
| } | ||
| return context; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { UserRole } from './UserRole.ts'; | ||
|
|
||
| export interface User { | ||
| homeAssistantUserId: string; | ||
| userName: string; | ||
| userDisplayName: string; | ||
| role: UserRole; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export enum UserRole { | ||
| HELPED = 0, | ||
| HELPER = 1 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,6 @@ | |
| align-items: center; | ||
| flex-grow: 1; | ||
| gap: 10px; | ||
|
|
||
| } | ||
|
|
||
| .Link { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| .CreateUser { | ||
| display: flex; | ||
| padding: 24%; | ||
| flex-direction: column; | ||
| align-items: center; | ||
| box-sizing: border-box; | ||
| justify-content: center; | ||
| position: relative; | ||
| background: linear-gradient(135deg, rgb(33, 150, 243), rgb(156, 39, 176)); | ||
| } | ||
|
|
||
| .title { | ||
| font-size: 3rem; | ||
| text-align: center; | ||
| } | ||
|
|
||
| .questionnaire { | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: 10px; | ||
| align-items: center; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { useState } from 'react'; | ||
| import { useNavigate } from 'react-router-dom'; | ||
| import { useUser } from '../context/useUser'; | ||
| import { UserRole } from '../models/UserRole'; | ||
| import './CreateUser.css'; | ||
|
|
||
| const CreateUser = () => { | ||
| const { createUser } = useUser(); | ||
| const [role] = useState(UserRole.HELPED); | ||
floflo0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const navigate = useNavigate(); | ||
|
|
||
| const handleSubmit = async (event: React.FormEvent) => { | ||
| event.preventDefault(); | ||
| await createUser(role); | ||
| navigate('/dashboard'); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="CreateUser"> | ||
| <div className="title">Bienvenue sur PillMate</div> | ||
| <form className="questionnaire" onSubmit={handleSubmit}> | ||
| <h2>Quel est votre rôle ?</h2> | ||
| <select name="role"> | ||
| <option value={UserRole.HELPED}>Personne aidée</option> | ||
| <option value={UserRole.HELPER}>Personne aidante</option> | ||
| </select> | ||
| <button type="submit">Valider</button> | ||
| </form> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default CreateUser; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { UserRole } from '../models/UserRole'; | ||
| import { User } from '../models/User'; | ||
|
|
||
| const API_URL = 'api'; | ||
|
|
||
| export const getUserInfo = async (): Promise<User | null> => { | ||
| const response = await fetch(`${API_URL}/user/me`); | ||
| if (response.ok){ | ||
| return response.json(); | ||
| } | ||
|
|
||
| if (response.status === 404) { | ||
| return null; | ||
| } | ||
| throw new Error('Erreur lors de la récupération des informations de l\'utilisateur'); | ||
| }; | ||
|
|
||
| export const createUser = async (role: UserRole): Promise<User> => { | ||
| const response = await fetch(`${API_URL}/user`, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify({ role }), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error('Erreur lors de la création de l\'utilisateur'); | ||
| } | ||
|
|
||
| return response.json(); | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.