Skip to content

Commit

Permalink
Final: App Working & React FE Done
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangsonww committed Oct 9, 2024
1 parent 530b3d7 commit 97e45d2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
66 changes: 66 additions & 0 deletions backend/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,3 +813,69 @@ exports.getUserEmail = async (req, res) => {
sendErrorResponse(res, 500, 'Failed to retrieve user email', error.message);
}
};

/**
* @swagger
* /update-document-title:
* post:
* summary: Update the title of a document
* description: Updates the title of a document associated with a given user and document ID in Firestore.
* tags:
* - Documents
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - userId
* - docId
* - newTitle
* properties:
* userId:
* type: string
* description: The userId of the user
* docId:
* type: string
* description: The ID of the document
* newTitle:
* type: string
* description: The new title for the document
* responses:
* 200:
* description: Document title updated successfully
* 404:
* description: User or document not found
* 500:
* description: Failed to update document title
*/
exports.updateDocumentTitle = async (req, res) => {
const { userId, docId, newTitle } = req.body;

try {
const userDoc = await firestore.collection('users').doc(userId).get();
if (!userDoc.exists) {
return sendErrorResponse(res, 404, 'User not found');
}

const userData = userDoc.data();
const documentIndex = userData.documents.findIndex((doc) => doc.id === docId);

if (documentIndex === -1) {
return sendErrorResponse(res, 404, 'Document not found');
}

// Update the title of the specific document
userData.documents[documentIndex].title = newTitle;

// Save the updated user document back to Firestore
await firestore.collection('users').doc(userId).update({
documents: userData.documents,
});

sendSuccessResponse(res, 200, 'Document title updated successfully');
} catch (error) {
sendErrorResponse(res, 500, 'Failed to update document title', error.message);
}
};
3 changes: 2 additions & 1 deletion backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const swaggerUi = require('swagger-ui-express');
const path = require('path');
const { registerUser, loginUser, uploadDocument, generateKeyIdeas, generateDiscussionPoints, chatWithAI, forgotPassword,
verifyEmail, getAllDocuments, getDocumentById, getDocumentDetails, deleteAllDocuments, deleteDocument,
getDaysSinceJoined, getDocumentCount, updateUserEmail, updateUserPassword, getUserEmail
getDaysSinceJoined, getDocumentCount, updateUserEmail, updateUserPassword, getUserEmail, updateDocumentTitle
} = require('./controllers');

const app = express();
Expand Down Expand Up @@ -103,6 +103,7 @@ app.post('/update-password', updateUserPassword); // Update password
app.get('/days-since-joined/:userId', getDaysSinceJoined); // Retrieve days since joined
app.get('/document-count/:userId', getDocumentCount); // Retrieve document count
app.get('/users/:userId', getUserEmail);
app.post('/update-document-title', updateDocumentTitle);

// Error handling for unsupported routes
app.use((req, res) => {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/pages/DocumentsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { Delete, Visibility } from '@mui/icons-material';
import { useNavigate } from 'react-router-dom';

const DocumentsPage = (theme) => {
const DocumentsPage = ({ theme }) => {
const [documents, setDocuments] = useState([]);
const [loading, setLoading] = useState(true);
const userId = localStorage.getItem('userId');
Expand Down Expand Up @@ -79,7 +79,7 @@ const DocumentsPage = (theme) => {
if (!userId) {
return (
<Box p={4} sx={{ textAlign: 'center' }}>
<Typography variant="h5" color="error" sx={{ font: 'inherit', fontSize: '24px', fontWeight: 'bold', color: theme === 'dark' ? 'red' : 'white' }}>
<Typography variant="h5" sx={{ font: 'inherit', fontSize: '24px', fontWeight: 'bold', color: theme === 'dark' ? 'white' : 'black' }}>
You are not logged in. Please log in to view your documents.
</Typography>
</Box>
Expand Down

0 comments on commit 97e45d2

Please sign in to comment.