|
1 | 1 | import React, { useEffect, useState } from 'react';
|
2 |
| -import { getUserById } from '../../../APIFunctions/User'; |
| 2 | +import { getUserById, editUser } from '../../../APIFunctions/User'; |
3 | 3 | import ChangePasswordModal from './ChangePassword';
|
4 | 4 | import DeleteAccountModal from './DeleteAccountModal';
|
5 | 5 | import GetApiKeyModal from './GetApiKeyModal';
|
6 | 6 | import { membershipState, membershipStateToString } from '../../../Enums';
|
7 | 7 | import { useSCE } from '../../../Components/context/SceContext';
|
| 8 | +import { useBackgroundColor } from '../../../Components/context/BackgroundColorContext'; |
8 | 9 |
|
9 | 10 | export default function Profile() {
|
| 11 | + const defaultColor = '#2a323c'; |
10 | 12 | const { user } = useSCE();
|
| 13 | + const { setBackgroundColorVersion } = useBackgroundColor() || {}; |
11 | 14 | const [response, setResponse] = useState({});
|
12 | 15 | const [bannerMessage, setBannerMessage] = useState('');
|
13 | 16 | const [bannerColor, setBannerColor] = useState('');
|
| 17 | + const [backgroundColor, setBackgroundColor] = useState(defaultColor); |
| 18 | + const [savedBackgroundColor, setSavedBackgroundColor] = useState(defaultColor); |
| 19 | + const [saved, setSaved] = useState(false); |
14 | 20 |
|
15 |
| - async function getUserFromApi() { |
16 |
| - const response = await getUserById(user._id, user.token); |
17 |
| - if (response.responseData) { |
18 |
| - setResponse(response.responseData); |
| 21 | + useEffect(() => { |
| 22 | + async function getUserFromApi() { |
| 23 | + const response = await getUserById(user._id, user.token); |
| 24 | + const responseData = response.responseData; |
| 25 | + if (responseData) { |
| 26 | + setResponse(responseData); |
| 27 | + const bgColor = responseData.backgroundColor; |
| 28 | + if(bgColor) { |
| 29 | + setBackgroundColor(bgColor); |
| 30 | + setSavedBackgroundColor(bgColor); |
| 31 | + } |
| 32 | + } |
19 | 33 | }
|
20 |
| - } |
| 34 | + getUserFromApi(); |
| 35 | + }, []); |
21 | 36 |
|
22 |
| - useEffect(getUserFromApi, []); |
| 37 | + async function updateBackgroundColor() { |
| 38 | + if(saved) { |
| 39 | + return; |
| 40 | + } |
| 41 | + if(backgroundColor == savedBackgroundColor) { |
| 42 | + setSaved(true); |
| 43 | + return; |
| 44 | + } |
| 45 | + const response = await editUser({_id: user._id, backgroundColor}, user.token); |
| 46 | + if(!response.error) { |
| 47 | + setSavedBackgroundColor(backgroundColor); |
| 48 | + setSaved(true); |
| 49 | + setBackgroundColorVersion(v => v + 1); |
| 50 | + } |
| 51 | + } |
23 | 52 |
|
24 | 53 | function renderExpirationDate() {
|
25 | 54 | if (response.accessLevel >= membershipState.OFFICER) {
|
@@ -79,6 +108,24 @@ export default function Profile() {
|
79 | 108 | </button>
|
80 | 109 | </div>
|
81 | 110 | </div>
|
| 111 | + <div className="pb-6 px-4"> |
| 112 | + <div className="font-semibold text-gray-900">Profile Icon Color</div> |
| 113 | + <div className="py-2 flex gap-2"> |
| 114 | + <input className="cursor-pointer h-8 w-[60px] rounded-lg" type='color' value={backgroundColor} style={{backgroundColor: backgroundColor}} onChange={(e) => { |
| 115 | + setBackgroundColor(e.target.value); |
| 116 | + if(e.target.value != savedBackgroundColor) { |
| 117 | + setSaved(false); |
| 118 | + } |
| 119 | + }}/> |
| 120 | + <button className="btn btn-sm btn-primary w-[60px]" onClick={updateBackgroundColor}>{saved ? 'Saved!' : 'Save'}</button> |
| 121 | + </div> |
| 122 | + <button className="btn btn-sm btn-error w-32" onClick={() => { |
| 123 | + setBackgroundColor(defaultColor); |
| 124 | + if(savedBackgroundColor != defaultColor) { |
| 125 | + setSaved(false); |
| 126 | + } |
| 127 | + }}>Reset</button> |
| 128 | + </div> |
82 | 129 | <div className="text-gray-700">
|
83 | 130 | <div className="grid text-sm">
|
84 | 131 | <div className="grid grid-cols-2">
|
|
0 commit comments