Skip to content

Commit

Permalink
collection file attachments and others
Browse files Browse the repository at this point in the history
  • Loading branch information
dixitsigdel8 authored and lmmrssa committed Jun 12, 2021
1 parent 16da29c commit 248accc
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 23 deletions.
24 changes: 17 additions & 7 deletions api/controllers/collectionUserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,25 @@ const getControllerUser = (req, res) => {
.catch((err) => res.json({ err }).status(400))
}
// @desc Add individual collection user
const addCollectionUser = (req, res) => {
const addCollectionUser = async (req, res) => {
const { userId, collectionId } = req.body
CollectionUser.create({
userId,
collectionId
const userExists = await User.findOne({ where: { id: userId } })
const collectionExits = await Collection.findOne({ where: { id: collectionId } })
const collectionUserExists = await CollectionUser.findOne({ where: { collectionId, userId } })
if (collectionUserExists) {
res.status(400).json({ error: 'User Already added collection' })
}
if (userExists && collectionExits) {
CollectionUser.create({
userId,
collectionId

})
.then(() => res.json({ message: 'Collection User Created !!!' }).status(200))
.catch((err) => res.json({ error: err.message }).status(400))
})
.then(() => res.json({ message: 'Collection User Created !!!' }).status(200))
.catch((err) => res.json({ error: err.message }).status(400))
} else {
res.status(400).json({ error: 'User Not Found' })
}
}

// @desc Update a collection user
Expand Down
23 changes: 17 additions & 6 deletions api/controllers/resourceUserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,33 @@ const getResourceUser = (req, res) => {
// @route POST /api/resourceUser/add
// @access Private

const addResourceUser = (req, res) => {
const addResourceUser = async (req, res) => {
// const user = User.findOne({ where: { id: req.body.id } })

const { userId, resourceId } = req.body
ResourceUser.create({
userId, resourceId
})
.then(() => res.json({ message: 'Resource User Created !!!' }).this.status(200))
.catch((err) => res.json({ error: err.message }).status(400))
const userExists = await User.findOne({ where: { id: userId } })
const resourceExists = await Resource.findOne({ where: { id: resourceId } })
const resourceUserExists = await ResourceUser.findOne({ where: { resourceId, userId } })
if (resourceUserExists) {
res.status(400).json({ error: 'User Already added resource' })
}
if (userExists && resourceExists) {
ResourceUser.create({
userId, resourceId
})
.then(() => res.json({ message: 'Resource User Created !!!' }).this.status(200))
.catch((err) => res.json({ error: err.message }).status(400))
} else {
res.status(400).json({ error: 'User Not Found' })
}
}

// @desc Update a ResourceUser
// @route PUT /api/ResourceUser/:id
// @access Public

const updateResourceUser = (req, res) => {
const id = req.params.id
ResourceUser.findByPk(id).then(resourceUser => {
if (resourceUser) {
const { id } = resourceUser
Expand Down
2 changes: 1 addition & 1 deletion api/routes/collectionUserRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const router = express.Router()
const { protect } = require('../middleware/authMiddleware')
const { addCollectionUser, getControllerUser } = require('../controllers/collectionUserController')

router.route('/add').post(protect, addCollectionUser)
router.route('/add').post(addCollectionUser)
router.route('/').get(getControllerUser)

module.exports = router
2 changes: 1 addition & 1 deletion api/src/helpers/filehelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const upload = multer({
}
})

const multipleUpload = upload.fields([{ name: 'avatar' }, { name: 'attachment' }])
const multipleUpload = upload.fields([{ name: 'file' }, { name: 'attachment' }])

const uploadArray = multer({ storage }).array('files')

Expand Down
2 changes: 1 addition & 1 deletion api/src/routes/resourceRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const multer = require('multer')
const shortid = require('shortid')
const path = require('path')
const { protect } = require('../middleware/authMiddleware')
const { uploadArray, upload } = require('../helpers/filehelpers')
const { uploadArray, multipleUpload, upload } = require('../helpers/filehelpers')

const { getResources, addResource, getResourcesById, deleteResources, updateResources, searchResourcesTitle } = require('../controllers/resourceController.js')

Expand Down
4 changes: 2 additions & 2 deletions src/Components/listView/ListView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const ListView = ({ data, title, setNewCollection, setModalActive, modalActive }
dispatch(
createResourceUser({ userId: userInfo, resourceId: id })
)
setIsAdded(isAdded => ({ [id]: !isAdded[id] }))
setIsAdded(id)
}

return (
Expand All @@ -34,7 +34,7 @@ const ListView = ({ data, title, setNewCollection, setModalActive, modalActive }
<div className='list-btn-wrapper'>
<span>Add to</span>
<button className='secondary-btn-border btn-img-wrapper' onClick={(id) => clickHandle(item.id)}>
{isAdded[item.id] ? (<><img src='./img/checkmark-outline.svg' alt='Added' /> <span>Added</span></>) : (<><img src='./img/book.svg' alt='My library' /> <span>My library</span></>)}
{isAdded === item.id ? (<><img src='./img/checkmark-outline.svg' alt='Added' /> <span>Added</span></>) : (<><img src='./img/book.svg' alt='My library' /> <span>My library</span></>)}
</button>
<button className='secondary-btn-border' onClick={() => setModalActive(!modalActive)}>Collections</button>
</div>
Expand Down
17 changes: 13 additions & 4 deletions src/Screens/Library/userCollection/UserCollection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,31 @@ import GroupModal from '../../../Components/GroupModal/GroupModal'
import { useSelector, useDispatch } from 'react-redux'
import { listCollections, updateCollection } from '../../../actions/collectionActions'
import { createCollectionUser } from '../../../actions/collectionUserActions'
import Pagination from '../../../Components/Paginations/Paginations'

const UserCollection = () => {
const [active, setActive] = useState(true)
const [modalActive, setModalActive] = useState(false)
const [isAdded, setIsAdded] = useState()
const [pageNumber, setPageNumber] = useState(1)

const [groupModal, setGroupModal] = useState(false)
const [newCollection, setNewCollection] = useState(false)

const data = useSelector(
(state) => state.listCollection.collections.collection
)

const dataCollection = useSelector(
(state) => state.listCollection
)
const userInfo = useSelector((state) => state.userLogin.userInfo.id)

console.log('collection', data)
const dispatch = useDispatch()

useEffect(() => {
dispatch(listCollections())
}, [dispatch])
dispatch(listCollections({ pageNumber }))
}, [pageNumber, dispatch])

function openAddCollection () {
setGroupModal(true)
Expand All @@ -39,6 +45,7 @@ const UserCollection = () => {
dispatch(
createCollectionUser({ userId: userInfo, collectionId: id })
)
setIsAdded(id)
}

return (
Expand Down Expand Up @@ -74,14 +81,16 @@ const UserCollection = () => {
<h4>{item.name}</h4>

<button className='trasnsparent-btn fixed-width' value={active} onClick={(id) => handleClick(item.id)}>
{active ? 'Save Collection' : <><img src='/img/check-circle.svg' alt='circle-icon' /> <span>Saved</span></>}
{isAdded === item.id ? <><img src='/img/check-circle.svg' alt='circle-icon' /> <span>Saved</span></> : 'Save Collection'}
</button>
</div>
</div>
)
})
}
</div>
<Pagination pageNumber={pageNumber} setPageNumber={setPageNumber} resourceList={dataCollection} />

</DashboardLayout>
</>
)
Expand Down
2 changes: 1 addition & 1 deletion src/actions/collectionUserActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const createCollectionUser = (newCollectionUser) => async (dispatch) => {
type: COLLECTION_USER_CREATE_REQUEST
})

const { data } = await axios.post(`${process.env.REACT_APP_API_BASE_URL}/api/resourceUser/add`, newCollectionUser)
const { data } = await axios.post(`${process.env.REACT_APP_API_BASE_URL}/api/collectionUser/add`, newCollectionUser)
dispatch({
type: COLLECTION_USER_CREATE_SUCCESS,
payload: data
Expand Down

0 comments on commit 248accc

Please sign in to comment.