Skip to content

Commit

Permalink
xceed-nitj#880 shareWith and deadline field added
Browse files Browse the repository at this point in the history
  • Loading branch information
SumitTeerthani committed Jul 26, 2024
1 parent 81a9e1f commit 136021b
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 35 deletions.
142 changes: 110 additions & 32 deletions client/src/reviewmodule/pages/Forms.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const Forms = () => {
const [section, setSection] = useState('');
const [accessRole, setAccessRole] = useState('');
const [show, setShow] = useState(false);
const [sharedWith, setSharedWith] = useState(['']);
const [deadline, setDeadline] = useState('');
const toast = useToast();
const { eventId } = useParams(); // Removed paperId from useParams
const [searchParams] = useSearchParams();
Expand Down Expand Up @@ -64,6 +66,21 @@ const Forms = () => {
setOptions(updatedOptions);
};

const handleSharedWithChange = (index, newValue) => {
const updatedSharedWith = sharedWith.map((email, i) => (i === index ? newValue : email));
setSharedWith(updatedSharedWith);
};

const handleAddSharedWith = () => {
setSharedWith([...sharedWith, '']);
};

const handleRemoveSharedWith = (index) => {
const updatedSharedWith = sharedWith.filter((_, i) => i !== index);
setSharedWith(updatedSharedWith);
};


const handleAddOption = () => {
setOptions([...options, '']);
};
Expand All @@ -90,9 +107,11 @@ const Forms = () => {
title,
section,
accessRole,
};

const editQuestion = {
sharedWith: sharedWith.filter(email => email.trim() !== ''),
deadline,
};

const editQuestion = {
question: [question],
show,
type: [type],
Expand All @@ -101,39 +120,63 @@ const Forms = () => {
title,
section,
accessRole,
}
sharedWith: sharedWith.filter(email => email.trim() !== ''),
deadline,
};


try {
if (edit) {
await axios.patch(`${apiUrl}/reviewmodule/reviewQuestion/${edit}`, editQuestion);
try {
if (edit) {
const response = await axios.patch(`${apiUrl}/reviewmodule/reviewQuestion/${edit}`, editQuestion);
if (response.status === 200 || response.status === 201) {
toast({
title: 'Question saved.',
description: 'Your question has been saved successfully.',
status: 'success',
duration: 5000,
isClosable: true,
});
} else {
await axios.post(`${apiUrl}/reviewmodule/forms/`, newQuestion);
throw new Error('Failed to update question');
}
} else {
const response = await axios.post(`${apiUrl}/reviewmodule/forms/`, newQuestion);
if (response.status === 200 || response.status === 201) {
toast({
title: 'Question saved.',
description: 'Your question has been saved successfully.',
status: 'success',
duration: 5000,
isClosable: true,
});
} else {
throw new Error('Failed to save new question');
}
toast({
title: 'Question saved.',
description: 'Your question has been saved successfully.',
status: 'success',
duration: 5000,
isClosable: true,
});
setQuestion('');
setType('');
setOptions(['']);
setOrder('');
setTitle('');
setSection('');
setAccessRole('');
setShow(false);
} catch (error) {
toast({
title: 'Error saving question.',
description: 'An error occurred while saving your question.',
status: 'error',
duration: 5000,
isClosable: true,
});
}
};

// Reset form fields
setQuestion('');
setType('');
setOptions(['']);
setOrder('');
setTitle('');
setSection('');
setAccessRole('');
setSharedWith(['']);
setDeadline('');
setShow(false);

} catch (error) {
console.error('Error saving question:', error);
toast({
title: 'Error saving question.',
description: 'An error occurred while saving your question.',
status: 'error',
duration: 5000,
isClosable: true,
});
}
};

const HeaderReviewQuestion = ({ title }) => {
const navigate = useNavigate();
Expand Down Expand Up @@ -274,6 +317,41 @@ const Forms = () => {
</Select>
</div>
<div className="form-group">
<Box bg={'#48835d'} style={{ fontWeight: '500', opacity: '100%', borderTopLeftRadius: '5px', borderTopRightRadius: '5px' }} p={2}>
<Text color="white" textAlign={'center'}>Share With (Emails)</Text>
</Box>
{sharedWith.map((email, index) => (
<div key={index} className="shared-with">
<Input
type="email"
value={email}
onChange={(e) => handleSharedWithChange(index, e.target.value)}
onKeyPress={handleKeyPress}
/>
{sharedWith.length > 1 && (
<Button type="button" onClick={() => handleRemoveSharedWith(index)}>
Remove
</Button>
)}
</div>
))}
<Button type="button" onClick={handleAddSharedWith}>
Add Email
</Button>
</div>
<div className="form-group">
<Box bg={'#48835d'} style={{ fontWeight: '500', opacity: '100%', borderTopLeftRadius: '5px', borderTopRightRadius: '5px' }} p={2}>
<Text color="white" textAlign={'center'}>Deadline</Text>
</Box>
<Input
type="datetime-local"
value={deadline}
onChange={(e) => setDeadline(e.target.value)}
onKeyPress={handleKeyPress}
/>
</div>

<div className="form-group">
<Checkbox isChecked={show} onChange={(e) => setShow(e.target.checked)}>Show</Checkbox>
</div>
<Button type="button" onClick={handleSubmit}>
Expand Down
3 changes: 2 additions & 1 deletion server/src/models/reviewModule/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const formSchema = new mongoose.Schema({
order: [{ type: Number }],
title: { type: String, required: true },
section: { type: String, required: true },

sharedWith: [{ type: String }], // Added field
deadline: { type: Date },
accessRole: { type: String, required: true }
}, { timestamps: true });

Expand Down
15 changes: 15 additions & 0 deletions server/src/modules/reviewModule/controller/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ const getFormsByEventId = async (req, res) => {
}
};

const getFormByEventIdAndFormId = async (req, res) => {
try {
const { eventId, formId } = req.params;
const form = await Form.findOne({ _id: formId, eventId });

if (!form) {
return res.status(404).json({ message: 'Form not found' });
}

res.status(200).json(form);
} catch (error) {
res.status(500).json({ message: 'Error fetching form', error });
}
};

module.exports = {
createForm,
Expand All @@ -82,4 +96,5 @@ module.exports = {
updateForm,
deleteForm,
getFormsByEventId,
getFormByEventIdAndFormId,
};
4 changes: 2 additions & 2 deletions server/src/modules/reviewModule/routes/forms.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const express = require("express");
const router = express.Router();
const {getFormsByEventId,createForm,getAllForms,getFormById,updateForm,deleteForm} = require('../controller/forms');
const {getFormByEventIdAndFormId,getFormsByEventId,createForm,getAllForms,getFormById,updateForm,deleteForm} = require('../controller/forms');

// Create a new form
router.post("/", createForm);

// Get all forms
router.get("/", getAllForms);
router.get('/:eventId', getFormsByEventId);

router.get('/:eventId/:formId',getFormByEventIdAndFormId);
// Get a form by ID
router.get("/:id", getFormById);

Expand Down

0 comments on commit 136021b

Please sign in to comment.