From 749bebca18e074c44b9c2d0e214e6c334dced92a Mon Sep 17 00:00:00 2001 From: Alfonso Pierantonio Date: Wed, 18 Dec 2024 19:21:18 +0100 Subject: [PATCH] Profilo --- src/App.tsx | 9 -- src/api/persistance/auth.ts | 2 + src/api/persistance/users.ts | 43 +++++++ src/pages/Account.tsx | 194 +++++++++++++++++++++++++---- src/pages/Auth.tsx | 7 +- src/pages/components/Edit/Edit.tsx | 75 +++++++++-- 6 files changed, 280 insertions(+), 50 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index d39f6016e..e6deb6f95 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -141,12 +141,3 @@ export const AppConnected = connect )(App); export default AppConnected; - - -/* SPLASH SCREEN -return(
- - -
); -*/ diff --git a/src/api/persistance/auth.ts b/src/api/persistance/auth.ts index 8631d6734..823334c20 100644 --- a/src/api/persistance/auth.ts +++ b/src/api/persistance/auth.ts @@ -21,6 +21,8 @@ class AuthApi { const user = DUser.new('Offline', 'User', 'Unknown', 'Unknown', 'Unknown', false, 'Unknown', 'Unknown', `Pointer_OfflineUser`);//`Pointer${Date.now()}_OfflineUser`); Storage.write('user', user); } + + } export {AuthApi}; diff --git a/src/api/persistance/users.ts b/src/api/persistance/users.ts index 6f96930e1..0665f9f9f 100644 --- a/src/api/persistance/users.ts +++ b/src/api/persistance/users.ts @@ -9,11 +9,54 @@ class UsersApi { const rawUser = DUser.new(user.name, user.surname, user.nickname, user.affiliation, user.country, user.newsletter, user.email, '', user.id); return LUser.fromD(rawUser); } + static async getUserById(id: string): Promise { + const response = await Api.get(`${Api.persistance}/users?id=${id}`); + if(response.code !== 200) return null; + const user = U.wrapper(response.data); + const rawUser = DUser.new(user.name, user.surname, user.nickname, user.affiliation, user.country, user.newsletter, user.email, '', user.id); + return LUser.fromD(rawUser); + } static async getAllEmails(): Promise { const response = await Api.get(`${Api.persistance}/users`); if(response.code !== 200) return []; const users = U.wrapper(response.data); return users.filter(u => u.id !== DUser.current).map(u => u.email); } + + static async updateUserById( + + id: string, + name: string, + surname: string, + nickname: string, + country: string, + affiliation: string, + newsletter: boolean): Promise { + + const response = await Api.get(`${Api.persistance}/users?id=${id}`); + if(response.code !== 200) return null; + + const patch_response = await Api.patch(`${Api.persistance}/users/update?id=${id}`, {name: name, surname: surname, country: country, nickname: nickname, affiliation: affiliation, newsletter: newsletter}); + + if(patch_response.code !== 200) {return null}; + const user = U.wrapper(response.data); + + return LUser.fromD(user); + } + + static async updatePasswordById(id: string, password: string): Promise { + + const patch_response = await Api.patch(`${Api.persistance}/users/set_password?id=${id}`, {password: password}); + + if (patch_response.code === 400) { + return null; + }; + + const user = U.wrapper(patch_response.data); + + + return LUser.fromD(user); + } + } export {UsersApi}; diff --git a/src/pages/Account.tsx b/src/pages/Account.tsx index 2037fff6a..8465d68b0 100644 --- a/src/pages/Account.tsx +++ b/src/pages/Account.tsx @@ -1,93 +1,235 @@ import {DState, DUser, LUser, Try} from '../joiner'; import {Dashboard} from './components'; -import {FakeStateProps} from '../joiner/types'; +import {FakeStateProps, windoww} from '../joiner/types'; import React, {Component, Dispatch, ReactElement} from "react"; import {connect} from "react-redux"; import { Edit, EditCountry } from './components/Edit/Edit'; - - +import { UsersApi } from '../api/persistance'; +import { useStateIfMounted } from 'use-state-if-mounted'; +import { on } from 'events'; +import Api from '../data/api'; +import Storage from '../data/storage'; function AccountComponent(props: AllProps): JSX.Element { const {user} = props; + + const [name, setName] = useStateIfMounted(user.name); + const [surname, setSurname] = useStateIfMounted(user.surname); + const [nickname, setNickname] = useStateIfMounted(user.nickname); + const [country, setCountry] = useStateIfMounted(user.country); + const [affiliation, setAffiliation] = useStateIfMounted(user.affiliation); + const [newsletter, setNewsletter] = useStateIfMounted(user.newsletter); + + const [email, setEmail] = useStateIfMounted(user.email); + + const [old_password, setOldPassword] = useStateIfMounted('01234567'); + const [new_password, setNewPassword] = useStateIfMounted('12345678'); + const [check_password, setCheckPassword] = useStateIfMounted('23456789'); + + + async function update_password( + old_password: string, + new_password:string, + check_password:string) { + + const U = windoww.U; + + const response = await Api.post(`${Api.persistance}/auth/login`, {email: email, password: old_password}); + + if (response.code !== 200) { + U.alert('e', 'Your password does not match our records.'); + return; + } + if (new_password !== check_password) { + U.alert('e', 'Paswords do not match.'); + return; + } + + const response_password = await UsersApi.updatePasswordById(user.id, new_password); + + if (response_password === null) { + U.alert('e', 'Something went wrong.'); + return; + } + + + U.alert('i', 'Your password has been successfully updated!'); + + + + + setNewPassword('01234567'); + setCheckPassword('12345678'); + + + } + + function update_newsletter(check_value: boolean) { + setNewsletter(check_value); + } + + function update_profile ( + id: string, + name: string, + surname: string, + nickname: string, + country: string, + affiliation: string, + newsletter: boolean) { + + const U = windoww.U; + + const response = UsersApi.updateUserById( + user.id, + name, + surname, + nickname, + country, + affiliation, + newsletter); + + + if (response === null) { + U.alert('e', 'Something went wrong while updating your profile.'); + return; + } + + const updated_user = DUser.new(name, surname, nickname, affiliation, country, newsletter, user.email, user.token, user.id); + Storage.write('user', updated_user); + U.resetState(); + + U.alert('i', 'Your profile has been updated!'); + + } + return( <>

Profile

- setName(e.target.value)} + tooltip={'Your first name.'} /> - setSurname(e.target.value)} tooltip={'Your family name.'} /> - setNickname(e.target.value)} tooltip={'Your nickname, it will be used as a short form for addressing you.'} /> - - setAffiliation(e.target.value)} tooltip={'Your current affiliation.'} /> - setCountry(e.target.value)} tooltip={'Select your affiliation country.'} /> - update_newsletter(!newsletter)} tooltip={'Select it if you want to receive low-intensity updates from us (e.g., new releases, new learning and teaching material, and the likes).'} /> - + + +

Password

- setOldPassword(e.target.value)} /> - setNewPassword(e.target.value)} + className={'space-above'} /> - setCheckPassword(e.target.value)} /> - +
diff --git a/src/pages/Auth.tsx b/src/pages/Auth.tsx index d306fe1ac..c59ab5835 100644 --- a/src/pages/Auth.tsx +++ b/src/pages/Auth.tsx @@ -8,6 +8,7 @@ import logo from '../static/img/jjodel.jpg'; import {Tooltip} from '../components/forEndUser/Tooltip'; function AuthPage(): JSX.Element { + const [action, setAction] = useStateIfMounted<'login'|'register'|'retrieve-password'>('login'); const [nickname, setNickname] = useStateIfMounted(''); const [name, setName] = useStateIfMounted(''); @@ -18,6 +19,7 @@ function AuthPage(): JSX.Element { const [password, setPassword] = useStateIfMounted(''); const [passwordCheck, setPasswordCheck] = useStateIfMounted(''); const [newsletter, setNewsletter] = useStateIfMounted(false); + const navigate = useNavigate(); const onSubmit = async(e: FormEvent) => { @@ -49,11 +51,12 @@ function AuthPage(): JSX.Element { U.alert('e', 'Email or password unknown.'); return; } + const data = U.wrapper(response.data); + const user = DUser.new(data.name, data.surname, data.nickname, data.affiliation, data.country, data.newsletter || false, data.email, data.token, data.id); Storage.write('user', user); Storage.write('token', user.token); - //navigate('/dashboard'); navigate('/allProjects'); U.resetState(); } @@ -475,7 +478,7 @@ function AuthPage(): JSX.Element { - {(true || window.location.host.includes('localhost')) && + {(window.location.host.includes('localhost')) && } } diff --git a/src/pages/components/Edit/Edit.tsx b/src/pages/components/Edit/Edit.tsx index 5d3274ee9..ec132f8f2 100644 --- a/src/pages/components/Edit/Edit.tsx +++ b/src/pages/components/Edit/Edit.tsx @@ -1,8 +1,10 @@ +import { UsersApi } from '../../../api/persistance/users'; import { Tooltip } from '../../../components/forEndUser/Tooltip'; import './edit.scss'; type EditProps = { + id: string; name: string; label: string; type: 'text' | 'email' | 'password' | 'checkbox' | 'country'; @@ -11,6 +13,7 @@ type EditProps = { disabled?: boolean; tooltip?:string; className?:string; + onChange?: (e: any)=>void; // MouseEvent } const EditCountry = (props: EditProps) => { @@ -28,9 +31,9 @@ const EditCountry = (props: EditProps) => { name={props.name} style={{height: 'calc(var(--input-height)*1.3)'}} defaultValue={props.value} - onChange={e => alert('')} required={required} - disabled = {disabled}> + disabled = {disabled} + onChange={props.onChange}> @@ -294,14 +297,14 @@ const EditCheckbox = (props: EditProps) => {
@@ -322,13 +325,15 @@ const EditDefault = (props: EditProps) => { @@ -336,6 +341,50 @@ const EditDefault = (props: EditProps) => { ); } +const EditPassword = (props: EditProps) => { + + let required = props.required ? props.required : false; + let disabled = props.disabled ? props.disabled : false; + let tooltip = props.tooltip ? props.tooltip : ''; + let className = props.className ? props.className : ''; + + function focus_in(e: any) { + if (e.target.value === props.value) { + e.target.value = ''; + } + } + function focus_out(e: any) { + if (e.target.value === '') { + e.target.value = props.value; + } + } + + return (<> + +
+ + + + +
+ + ); +} + + const Edit = (props: EditProps) => { return (<> @@ -343,7 +392,7 @@ const Edit = (props: EditProps) => { {props.type === 'checkbox' && } {props.type === 'text' && } {props.type === 'email' && } - {props.type === 'password' && } + {props.type === 'password' && } ); }