Skip to content
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

feat: save settings to the backend #395

Merged
merged 2 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion client/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { KindeProvider } from "@kinde-oss/kinde-auth-react"

// Store
import { useAuth } from 'context/AuthContext';
import { useApp } from 'src/context/AppContext';

// Components
import NavBar from 'components/NavBar';
Expand All @@ -23,7 +24,7 @@ import Profile from './pages/Profile';

function App() {
const { isLoggedIn, dispatchAuth } = useAuth();

const { loadUserSettings } = useApp();

async function loginWithEmail(email) {
try {
Expand All @@ -43,6 +44,12 @@ function App() {
email
},
})
try {
const userData = await api.get(`/profile/${email}`);
loadUserSettings(userData.data?.settings);
} catch (error) {
console.error('Error loading user data:', error);
}
} else {
throw new Error('Login failed');
}
Expand Down
11 changes: 11 additions & 0 deletions client/src/context/AppContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ export const AppProvider = ({ children }) => {
});
}

function loadUserSettings(settings) {
if (!settings) {
return;
}
dispatch({
type: 'UPDATE_SETTINGS',
payload: settings,
});
}

function updateTmpSettings(newSettings) {
dispatch({
type: 'UPDATE_TMP_SETTINGS',
Expand Down Expand Up @@ -104,6 +114,7 @@ export const AppProvider = ({ children }) => {
cancelSettingsUpdate,
startSearch,
endSearch,
loadUserSettings
}}
>
{children}
Expand Down
20 changes: 19 additions & 1 deletion client/src/pages/Settings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
} from 'rsuite';

import { useApp } from 'src/context/AppContext';
import { useAuth } from 'src/context/AuthContext';
import { api } from 'src/lib/axios';

const Searching = () => {
const {
Expand All @@ -20,18 +22,34 @@ const Searching = () => {
updateTmpSettings,
cancelSettingsUpdate,
} = useApp();
const { authState } = useAuth();

const settings = useMemo(() => {
return app.tmpSettings
? { ...app.settings, ...app.tmpSettings }
: app.settings;
});

const updateUserSettings = async () => {
const data = {
email: authState?.email,
settings
};
try {
const response = await api.post('/profile', data);
console.log(response.data.message);
} catch (error) {
console.error(error);
}
};

/**
*
* @param {Event | SubmitEvent} e
*/
const handleSubmit = () => {
const handleSubmit = async () => {
updateSettings();
await updateUserSettings();
};

const handleChange = (newSettings) => {
Expand Down
13 changes: 7 additions & 6 deletions server/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const getProfile = async (req, res) => {
};

const updateProfile = async (req, res) => {
const { username, aboutMe, gender, age, email } = req.body;
const { username, aboutMe, gender, age, email, settings } = req.body;

try {
// Find the user by email
Expand All @@ -72,11 +72,12 @@ const updateProfile = async (req, res) => {
return res.status(404).json({ error: 'User not found' });
}

// Update user's profile with provided fields or defaults
user.username = username || 'Anonymous';
user.aboutMe = aboutMe || null;
user.gender = gender || 'Unknown';
user.age = age || null;
// Update user's profile with provided fields or the User fields or defaults
user.username = username || user.username || 'Anonymous';
user.aboutMe = aboutMe || user.aboutMe || null;
user.gender = gender || user.gender || 'Unknown';
user.age = age || user.age || null;
Comment on lines +75 to +79
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next time if you want to make a change that isn't part of your PR, create a new issue, will let this slide for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will keep that in mind.

user.settings = settings || user.settings;

// Save the updated user profile
await user.save();
Expand Down
4 changes: 4 additions & 0 deletions server/models/UserSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const UserSchema = new mongoose.Schema({
type: String,
required: false,
},
settings: {
type: Object,
required: false
},
});

UserSchema.index({ email: 1 }, { unique: true });
Expand Down