diff --git a/api/src/controllers/collectionController.js b/api/src/controllers/collectionController.js index 9a5fb4c8a..90380d92d 100644 --- a/api/src/controllers/collectionController.js +++ b/api/src/controllers/collectionController.js @@ -53,12 +53,20 @@ const getCollectionById = (req, res) => { // @todo we have to work on relation mapping betweenn collection and resources const addcollection = (req, res) => { - const { name, docType, resourceType, linkId } = req.body + const { name, docType, resourceType, linkId, description, category, status } = req.body + let filename = '' + if (req.file) { + filename = req.file.filename + } Collection.create({ name, docType, resourceType, - linkId + linkId, + description, + filename, + category, + status }) .then(() => res.json({ message: 'Collection Created !!!' }).status(200)) .catch((err) => res.json({ error: err.message }).status(400)) @@ -69,20 +77,17 @@ const addcollection = (req, res) => { // @access Public const updateCollection = (req, res) => { - const { - name, docType, resourceType, linkId - } = req.body + // const { + // name, docType, resourceType, linkId,description,category,status + // } = req.body const id = req.params.id Collection.findByPk(id).then(collection => { if (collection) { const { id } = collection - Collection.update({ - name, - docType, - resourceType, - linkId - }, - { where: { id } }) + Collection.update( + req.body + , + { where: { id } }) .then(() => res.json({ message: 'Collection Updated !!!' }).status(200)) .catch((err) => res.json({ error: err.message }).status(400)) } diff --git a/api/src/controllers/collectionUserController.js b/api/src/controllers/collectionUserController.js new file mode 100644 index 000000000..2e7d34aed --- /dev/null +++ b/api/src/controllers/collectionUserController.js @@ -0,0 +1,73 @@ +const CollectionUser = require('../models/collectionUserModel') +const Collection = require('../models/collectionModel') +const Sequelize = require('sequelize') +const User = require('../models/userModel') +const Op = Sequelize.Op + +// @desc Fetch all collection_user +// @route Get/api/collection_user +// @access Private +const getControllerUser = (req, res) => { + CollectionUser.findAll({ include: [{ model: Collection, as: 'collectionInfo' }, { model: User, as: 'user' }] }) + .then(items => { + res.json({ items }).status(200) + }) + .catch((err) => res.json({ err }).status(400)) +} + +// @desc Add individual collection user +const addCollectionUser = async (req, res) => { + const { userId, collectionId } = req.body + 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)) + } else { + res.status(400).json({ error: 'User Not Found' }) + } +} + +// @desc Update a collection user + +const updateCollectionUser = (req, res) => { + CollectionUser.findByPk(id).then(collectionUser => { + if (collectionUser) { + const { id } = collectionUser + CollectionUser.update( + req.body, + { where: { id } }) + .then(() => res.json({ message: 'Collection user updated !!!' })) + .catch((err) => res.json({ error: err.message }).status(400)) + } + res.status(400) + throw new Error('Collection User not found') + }) +} + +// @desc Delete a collectionUser +const deleteController = (req, res) => { + const id = req.params.id + CollectionUser.findByPk(id).then(controllerUser => { + if (controllerUser) { + const { id } = controllerUser + CollectionUser.destroy({ where: { id } }) + .then(() => res.json({ message: 'Controller User Delete!!!' })) + .catch((err) => res.json({ err: err.message }).status(400)) + } else { + res.status(404) + throw new Error('Controller User not found') + } + }) +} + +module.exports = { addCollectionUser, getControllerUser, updateCollectionUser, deleteController } diff --git a/api/src/controllers/resourceUserController.js b/api/src/controllers/resourceUserController.js new file mode 100644 index 000000000..495096395 --- /dev/null +++ b/api/src/controllers/resourceUserController.js @@ -0,0 +1,85 @@ +const ResourceUser = require('../models/resourceUserModel') +const Resource = require('../models/resourceModel') +const Sequelize = require('sequelize') +const User = require('../models/userModel') +const Op = Sequelize.Op + +// @desc Fetch all course +// @route GET/api/courses +// @access Public + +const getResourceUser = (req, res) => { + const pageSize = 10 + const page = Number(req.query.pageNumber) || 1 + const order = req.query.order || 'ASC' + ResourceUser.findAll({ include: [{ model: User, as: 'userfollower' }, { model: Resource, as: 'resourceInfo' }] }) + .then(items => { + res.json({ items }).status(200) + }) + .catch((error) => res.json({ error }).status(400)) +} + +// @desc Add individual resource User +// @route POST /api/resourceUser/add +// @access Private + +const addResourceUser = async (req, res) => { + // const user = User.findOne({ where: { id: req.body.id } }) + + const { userId, resourceId } = req.body + 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 + ResourceUser.update( + req.body, + { where: { id } }) + .then(() => res.json({ message: 'Resource User updated !!' })) + .catch((err) => res.json({ error: err.message }).status(400)) + } + res.status(404) + throw new Error('Resource User not found') + }) +} + +// @desc Delete a resourceUser +// @route Delete /api/resourceUser/:id +// @access Public +const deleteResourceUser = (req, res) => { + const id = req.params.id + ResourceUser.findByPk(id).then(resourceUser => { + if (resourceUser) { + const { id } = resourceUser + ResourceUser.destroy({ where: { id } }) + .then(() => res.json({ message: 'Resource User Deleted !!!' })) + .catch((err) => res.json({ err: err.message }).status(400)) + } else { + res.status(404) + throw new Error('Resource User not found') + } + }) +} + +module.exports = { addResourceUser, getResourceUser, updateResourceUser, deleteResourceUser } diff --git a/api/src/helpers/filehelpers.js b/api/src/helpers/filehelpers.js index c99a0d2f9..5c2000483 100644 --- a/api/src/helpers/filehelpers.js +++ b/api/src/helpers/filehelpers.js @@ -34,7 +34,7 @@ const upload = multer({ } }) -const multipleUpload = upload.fields([{ name: 'avatar' }, { name: 'attachment' }]) +const multipleUpload = upload.fields([{ name: 'file' }, { name: 'attachment' }, { name: 'avatar' }]) const uploadArray = multer({ storage }).array('files') diff --git a/api/src/migrations/20210530092345-alter_collections.js b/api/src/migrations/20210530092345-alter_collections.js new file mode 100644 index 000000000..5ad894a4a --- /dev/null +++ b/api/src/migrations/20210530092345-alter_collections.js @@ -0,0 +1,12 @@ + +module.exports = { + up: async (queryInterface, Sequelize) => { + queryInterface.addColumn('collections', 'filename', Sequelize.STRING) + queryInterface.addColumn('collections', 'description', Sequelize.STRING) + }, + + down: async (queryInterface, Sequelize) => { + queryInterface.removeColumn('collections', 'filename', Sequelize.STRING) + queryInterface.removeColumn('collections', 'description', Sequelize.STRING) + } +} diff --git a/api/src/migrations/20210531094440-alter_collections.js b/api/src/migrations/20210531094440-alter_collections.js new file mode 100644 index 000000000..dc49a5d01 --- /dev/null +++ b/api/src/migrations/20210531094440-alter_collections.js @@ -0,0 +1,10 @@ + +module.exports = { + up: async (queryInterface, Sequelize) => { + queryInterface.addColumn('collections', 'category', Sequelize.STRING) + }, + + down: async (queryInterface, Sequelize) => { + queryInterface.removeColumn('collections', 'category', Sequelize.STRING) + } +} diff --git a/api/src/migrations/20210601084926-alternate_collection_table.js b/api/src/migrations/20210601084926-alternate_collection_table.js new file mode 100644 index 000000000..46c996cec --- /dev/null +++ b/api/src/migrations/20210601084926-alternate_collection_table.js @@ -0,0 +1,13 @@ + +module.exports = { + up: async (queryInterface, Sequelize) => { + queryInterface.addColumn('collections', 'status', { + type: Sequelize.BOOLEAN, + defaultValue: false + }) + }, + + down: async (queryInterface, Sequelize) => { + await queryInterface.dropTable('collections') + } +} diff --git a/api/src/migrations/20210602093915-resource_users.js b/api/src/migrations/20210602093915-resource_users.js new file mode 100644 index 000000000..e4bc9819e --- /dev/null +++ b/api/src/migrations/20210602093915-resource_users.js @@ -0,0 +1,40 @@ + +module.exports = { + up: async (queryInterface, Sequelize) => { + queryInterface.createTable('resource_users', + { + id: { + type: Sequelize.INTEGER, + primaryKey: true, + autoIncrement: true + }, + userId: { + type: Sequelize.INTEGER, + allowNull: false, + references: { + model: 'users', + key: 'id' + } + }, + resourceId: { + type: Sequelize.INTEGER, + allowNull: false, + references: { + model: 'resources', + key: 'id' + } + }, + createdAt: { + type: Sequelize.DATE + }, + updatedAt: { + type: Sequelize.DATE + } + } + ) + }, + + down: async (queryInterface, Sequelize) => { + queryInterface.dropTable('resource_users', {}) + } +} diff --git a/api/src/migrations/20210603071204-collection_users.js b/api/src/migrations/20210603071204-collection_users.js new file mode 100644 index 000000000..9debf582e --- /dev/null +++ b/api/src/migrations/20210603071204-collection_users.js @@ -0,0 +1,33 @@ + +module.exports = { + up: async (queryInterface, Sequelize) => { + queryInterface.createTable('collection_users', + { + id: { + type: Sequelize.INTEGER, + primaryKey: true, + autoIncrement: true + }, + userId: { + type: Sequelize.INTEGER, + allowNull: false + }, + collectionId: { + type: Sequelize.INTEGER, + allowNull: false + }, + createdAt: { + type: Sequelize.DATE + }, + updatedAt: { + type: Sequelize.DATE + } + + } + ) + }, + + down: async (queryInterface, Sequelize) => { + queryInterface.dropTable('collection_users',{}) + } +}; diff --git a/api/src/models/collectionModel.js b/api/src/models/collectionModel.js index 50adce827..5975e68cc 100644 --- a/api/src/models/collectionModel.js +++ b/api/src/models/collectionModel.js @@ -20,9 +20,27 @@ const Collection = db.define( }, linkId: { type: Sequelize.INTEGER + }, + filename: { + type: Sequelize.STRING + }, + description: { + type: Sequelize.STRING + }, + category: { + type: Sequelize.STRING + }, + status: { + type: Sequelize.BOOLEAN } }, { timestamps: true } ) +Collection.associate = function (models) { + Collection.hasMany(models.CollectionUser, { + foreignKey: 'collectionId' + }) +} + module.exports = Collection diff --git a/api/src/models/collectionUserModel.js b/api/src/models/collectionUserModel.js new file mode 100644 index 000000000..64b15ad46 --- /dev/null +++ b/api/src/models/collectionUserModel.js @@ -0,0 +1,31 @@ +const Sequelize = require('sequelize') +const db = require('../config/database.js') + +const CollectionUser = db.define('collection_user', + { + id: { + type: Sequelize.INTEGER, + primaryKey: true, + autoIncrement: true + }, + userId: { + type: Sequelize.INTEGER + }, + collectionId: { + type: Sequelize.INTEGER + } + + }, + { timestamps: true } +) + +CollectionUser.associate = (models) => { + CollectionUser.belongsTo(models.User, { + foreignKey: 'userId' + }) + CollectionUser.belongsTo(models.Collection, { + foreignKey: 'collectionId' + }) +} + +module.exports = CollectionUser diff --git a/api/src/models/resourceModel.js b/api/src/models/resourceModel.js index c03a96193..796bc28d4 100644 --- a/api/src/models/resourceModel.js +++ b/api/src/models/resourceModel.js @@ -1,5 +1,6 @@ const Sequelize = require('sequelize') const db = require('../config/database.js') +const ResourceUser = require('./resourceUserModel.js') const Resources = db.define('resources', { @@ -91,4 +92,10 @@ const Resources = db.define('resources', { timestamps: false } ) +Resources.associate = function (models) { + Resources.hasMany(models.ResourceUser, { + foreignKey: 'resourceId' + }) +} + module.exports = Resources diff --git a/api/src/models/resourceUserModel.js b/api/src/models/resourceUserModel.js new file mode 100644 index 000000000..9b30cf955 --- /dev/null +++ b/api/src/models/resourceUserModel.js @@ -0,0 +1,31 @@ +const Sequelize = require('sequelize') +const User = require('./userModel') +const db = require('../config/database.js') + +const ResourceUser = db.define('resource_users', + { + id: { + type: Sequelize.INTEGER, + primaryKey: true, + autoIncrement: true + }, + userId: { + type: Sequelize.INTEGER + }, + resourceId: { + type: Sequelize.INTEGER + } + }, + { timestamps: true } +) + +ResourceUser.associate = (models) => { + ResourceUser.belongsTo(User) + + ResourceUser.belongsTo(models.Resource, { + foreignKey: 'resourceId' + + }) +} + +module.exports = ResourceUser diff --git a/api/src/models/userModel.js b/api/src/models/userModel.js index e01e0a663..d8c0dea66 100644 --- a/api/src/models/userModel.js +++ b/api/src/models/userModel.js @@ -13,4 +13,15 @@ const User = db.define('users', { } }, { timestamps: false }) +User.associate = function (models) { + User.hasMany(models.CollectionUser, { + foreignKey: 'userId' + }) +} +User.associate = function (models) { + User.hasMany(models.ResourceUser, { + foreignKey: 'userId' + }) +} + module.exports = User diff --git a/api/src/routes/collectionRouter.js b/api/src/routes/collectionRouter.js index e553ff28e..090e7736d 100644 --- a/api/src/routes/collectionRouter.js +++ b/api/src/routes/collectionRouter.js @@ -1,9 +1,10 @@ const express = require('express') const router = express.Router() const { addcollection, getCollection, deleteCollection, getCollectionById, updateCollection, searchCollectionTitle } = require('../controllers/collectionController') +const { upload } = require('../helpers/filehelpers') router.route('/').get(getCollection) -router.route('/add').post(addcollection) +router.route('/add').post(upload.single('collection'), addcollection) router.route('/search').get(searchCollectionTitle) router.route('/:id').get(getCollectionById).delete(deleteCollection).put(updateCollection) diff --git a/api/src/routes/collectionUserRouter.js b/api/src/routes/collectionUserRouter.js new file mode 100644 index 000000000..e2ae1eb39 --- /dev/null +++ b/api/src/routes/collectionUserRouter.js @@ -0,0 +1,9 @@ +const express = require('express') +const router = express.Router() +const { protect } = require('../middleware/authMiddleware') +const { addCollectionUser, getControllerUser } = require('../controllers/collectionUserController') + +router.route('/add').post(addCollectionUser) +router.route('/').get(getControllerUser) + +module.exports = router diff --git a/api/src/routes/resourceRouter.js b/api/src/routes/resourceRouter.js index c89ffb6a9..ff4625082 100644 --- a/api/src/routes/resourceRouter.js +++ b/api/src/routes/resourceRouter.js @@ -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') diff --git a/api/src/routes/resourceUserRouter.js b/api/src/routes/resourceUserRouter.js new file mode 100644 index 000000000..c565600de --- /dev/null +++ b/api/src/routes/resourceUserRouter.js @@ -0,0 +1,9 @@ +const express = require('express') +const router = express.Router() + +const { addResourceUser, getResourceUser } = require('../controllers/resourceUserController') + +router.route('/').get(getResourceUser) +router.route('/add').post(addResourceUser) + +module.exports = router diff --git a/api/src/server.js b/api/src/server.js index 9f5773194..4d3711852 100644 --- a/api/src/server.js +++ b/api/src/server.js @@ -9,6 +9,8 @@ const newsRoutes = require('./routes/newsRouter') const enterprisesRoutes = require('./routes/enterprisesRouter') const communityGroupsRoutes = require('./routes/communityGroupRouter') const calendarRoutes = require('./routes/calendarEventsRouter') +const collectionUserRoutes = require('./routes/collectionUserRouter') +const resourceUserRoutes = require('./routes/resourceUserRouter') const sequelize = require('./config/database.js') const cors = require('cors') const dotenv = require('dotenv') @@ -24,6 +26,8 @@ dotenv.config() app.use('/api/users', userRoutes) app.use('/api/resources', resourceRoutes) app.use('/api/collection', collectionRoutes) +app.use('/api/collectionUser', collectionUserRoutes) +app.use('/api/resourceUser', resourceUserRoutes) app.use('/api/enterprises', enterprisesRoutes) app.use('/api/groups', communityGroupsRoutes) app.use('/api/calendar', calendarRoutes) diff --git a/src/actions/collectionActions.js b/src/actions/collectionActions.js new file mode 100644 index 000000000..2e89dd34e --- /dev/null +++ b/src/actions/collectionActions.js @@ -0,0 +1,111 @@ +import axios from 'axios' +import { + COLLECTION_LIST_REQUEST, + COLLECTION_LIST_SUCCESS, + COLLECTION_LIST_FAIL, + COLLECTION_SEARCH_REQUEST, + COLLECTION_SEARCH_SUCCESS, + COLLECTION_SEARCH_FAIL, + COLLECTION_CREATE_REQUEST, + COLLECTION_CREATE_SUCCESS, + COLLECTION_CREATE_FAIL, + COLLECTION_UPDATE_REQUEST, + COLLECTION_UPDATE_SUCCESS, + COLLECTION_UPDATE_FAIL +} from '../constants/collectionConstants' + +export const listCollections = (sort = '', pageNumber = '') => async ( + dispatch +) => { + try { + dispatch({ type: COLLECTION_LIST_REQUEST }) + const { data } = await axios.get( + `${process.env.REACT_APP_API_BASE_URL}/api/collection` + ) + console.log('data', data) + dispatch({ + type: COLLECTION_LIST_SUCCESS, + payload: data + }) + } catch (error) { + dispatch({ + type: COLLECTION_LIST_FAIL, + payload: + error.response && error.response.data.message + ? error.response.data.message + : error.message + }) + } +} + +export const searchCollections = (search) => async ( + dispatch +) => { + try { + dispatch({ type: COLLECTION_SEARCH_REQUEST }) + const { data } = await axios.get(`${process.env.REACT_APP_API_BASE_URL}/api/collection/search?title=${search}`) + dispatch({ + type: COLLECTION_SEARCH_SUCCESS, + payload: data + }) + } catch (error) { + dispatch({ + type: COLLECTION_SEARCH_FAIL, + payload: + error.response && error.response.data.message + ? error.response.data.message + : error.message + }) + } +} + +export const createCollection = (newCollection) => async (dispatch, getState) => { + const formData = new FormData() + formData.append('name', newCollection.name) + formData.append('docType', newCollection.docType) + formData.append('resourceType', newCollection.resourceType) + formData.append('linkId', newCollection.linkId) + try { + dispatch({ type: COLLECTION_CREATE_REQUEST }) + const { userLogin: { userInfo } } = getState() + const config = { + headers: { + // Authorization : `Bearer ${userInfo.token}`, + 'Content-Type': 'multipart/form-data' + } + } + const { data } = await axios.post(`${process.env.REACT_APP_API_BASE_URL}/api/collection/add`, formData) + dispatch({ type: COLLECTION_CREATE_SUCCESS, payload: data }) + } catch (error) { + const message = + error.response && error.response.data.message + ? error.response.data.message + : error.message + dispatch({ type: COLLECTION_CREATE_FAIL, payload: message }) + } +} + +export const updateCollection = (updateCollection, id) => async (dispatch) => { + try { + dispatch({ + type: COLLECTION_UPDATE_REQUEST + }) + + const { data } = await axios.put( + `${process.env.REACT_APP_API_BASE_URL}/api/collection/${id}`, updateCollection + ) + dispatch({ + type: COLLECTION_UPDATE_SUCCESS, + payload: data + }) + } catch (error) { + const message = + error.response && error.response.data.message + ? error.response.data.message + : error.message + dispatch({ + type: COLLECTION_UPDATE_FAIL, + payload: message + }) + } +} diff --git a/src/actions/collectionUserActions.js b/src/actions/collectionUserActions.js new file mode 100644 index 000000000..0aa5c60fb --- /dev/null +++ b/src/actions/collectionUserActions.js @@ -0,0 +1,32 @@ +import axios from 'axios' +import { + COLLECTION_USER_LIST_REQUEST, + COLLECTION_USER_LIST_SUCCESS, + COLLECTION_USER_LIST_FAIL, + COLLECTION_USER_CREATE_REQUEST, + COLLECTION_USER_CREATE_SUCCESS, + COLLECTION_USER_CREATE_FAIL +} from '../constants/collectionUserConstants' + +export const createCollectionUser = (newCollectionUser) => async (dispatch) => { + try { + dispatch({ + type: COLLECTION_USER_CREATE_REQUEST + }) + + const { data } = await axios.post(`${process.env.REACT_APP_API_BASE_URL}/api/collectionUser/add`, newCollectionUser) + dispatch({ + type: COLLECTION_USER_CREATE_SUCCESS, + payload: data + }) + } catch (error) { + const message = + error.response && error.response.data.message + ? error.response.data.message + : error.message + dispatch({ + type: COLLECTION_USER_CREATE_FAIL, + payload: message + }) + } +} diff --git a/src/actions/resourceUserAction.js b/src/actions/resourceUserAction.js new file mode 100644 index 000000000..c39af79aa --- /dev/null +++ b/src/actions/resourceUserAction.js @@ -0,0 +1,57 @@ +import axios from 'axios' +import { + RESOURCE_USER_LIST_REQUEST, + RESOURCE_USER_LIST_SUCCESS, + RESOURCE_USER_LIST_FAIL, + RESOURCE_USER_CREATE_REQUEST, + RESOURCE_USER_CREATE_SUCCESS, + RESOURCE_USER_CREATE_FAIL +} from '../constants/resourceuserConstants' + +export const listResourceUser = (sort = '', pageNumber = '') => async ( + dispatch +) => { + try { + dispatch({ type: RESOURCE_USER_LIST_REQUEST }) + const { data } = await axios.get( + `${process.env.REACT_APP_API_BASE_URL}/api/resourceUser` + ) + console.log('data', data) + dispatch({ + type: RESOURCE_USER_LIST_SUCCESS, + payload: data + }) + } catch (error) { + dispatch({ + type: RESOURCE_USER_LIST_FAIL, + payload: + error.response && error.response.data.message + ? error.response.data.message + : error.message + + }) + } +} + +export const createResourceUser = (newResourceUser) => async (dispatch) => { + try { + dispatch({ + type: RESOURCE_USER_CREATE_REQUEST + }) + + const { data } = await axios.post(`${process.env.REACT_APP_API_BASE_URL}/api/resourceUser/add`, newResourceUser) + dispatch({ + type: RESOURCE_USER_CREATE_SUCCESS, + payload: data + }) + } catch (error) { + const message = + error.response && error.response.data.message + ? error.response.data.message + : error.message + dispatch({ + type: RESOURCE_USER_CREATE_FAIL, + payload: message + }) + } +} diff --git a/src/components/collectionModal/CollectionModal.jsx b/src/components/collectionModal/CollectionModal.jsx index dfabdc3da..b58a0cee3 100644 --- a/src/components/collectionModal/CollectionModal.jsx +++ b/src/components/collectionModal/CollectionModal.jsx @@ -4,6 +4,7 @@ import { useLocation } from 'react-router-dom' import { useDispatch } from 'react-redux' import { createResource } from '../../actions/resourceActions' import SimpleFilter from '../simpleFilter/SimpleFilter' +import { createCollection } from '../../actions/collectionActions' import { collectionFilterData } from '../../constants/sampleData' import CollectionModalHeader from '../newsCreateModal/CollectionModalHeader' import DragDrop from '../dragDrop/DragDrop' @@ -15,6 +16,9 @@ const CollectionModal = ({ setActive, openAddCollection, name }) => { const dispatch = useDispatch() const [resourceTitleError, setResourceTitleError] = useState(false) const [resourceDescriptionError, setResourceDescriptionError] = useState(false) + const [collectionTitle, setCollectionTitle] = useState('') + const [collectionTitleError, setCollectionTitleError] = useState(false) + const { pathname } = useLocation() const resourceTitleChange = (e) => { @@ -36,6 +40,19 @@ const CollectionModal = ({ setActive, openAddCollection, name }) => { setActive(false) } } + const collectionTitleChange = (e) => { + setCollectionTitle(e.target.value) + setCollectionTitleError(false) + } + const handleAddCollection = async (e) => { + e.preventDefault() + if (!collectionTitle) setCollectionTitleError(true) + + if (collectionTitle) { + dispatch(createCollection({ name: collectionTitle })) + setActive(false) + } + } return ( <> @@ -54,11 +71,11 @@ const CollectionModal = ({ setActive, openAddCollection, name }) => { : <>
-
+ collectionTitleChange(e)} placeholder='Collection title' />
openAddCollection()}>Add Files
- + } diff --git a/src/components/groupModal/GroupModal.jsx b/src/components/groupModal/GroupModal.jsx index a30826e80..fc03ae18f 100644 --- a/src/components/groupModal/GroupModal.jsx +++ b/src/components/groupModal/GroupModal.jsx @@ -1,22 +1,40 @@ - +import { useEffect } from 'react' +import { useSelector, useDispatch } from 'react-redux' import GroupUsers from '../groupUsers/GroupUsers' import SearchComponent from '../searchComponent/SearchComponent' import './GroupModal.scss' +import { listCollections } from '../../actions/collectionActions' -const GroupModal = ({ clickHandler, setNewCollection, data, btnName, name }) => { +const GroupModal = ({ + clickHandler, + setNewCollection, + btnName, + name, + title +}) => { function collectionAdded () { setNewCollection(true) clickHandler(false) } + const data = useSelector( + (state) => state.listCollection.collections.collection + ) + console.log('collection', data) + const dispatch = useDispatch() + + useEffect(() => { + dispatch(listCollections()) + }, [dispatch]) return ( <>
-
-

{name || 'Add members'}

- +

{title}

+
@@ -28,9 +46,12 @@ const GroupModal = ({ clickHandler, setNewCollection, data, btnName, name }) =>
- {btnName === 'add to collections' &&
-
} + {btnName === 'add to collections' && ( +
+ + +
+ )} diff --git a/src/components/groupUsers/GroupUsers.jsx b/src/components/groupUsers/GroupUsers.jsx index 6f3d4a861..950a02b77 100644 --- a/src/components/groupUsers/GroupUsers.jsx +++ b/src/components/groupUsers/GroupUsers.jsx @@ -3,22 +3,24 @@ import Radiobox from '../radioBox/Radiobox' import './GroupUsers.scss' import { groupUsersData } from '../../constants/sampleData' -const GroupUsers = () => { +const GroupUsers = ({ data }) => { return ( <> { - groupUsersData.map(user => ( -
-
-
- users -
-

{user.name}

-
- -
- )) + + data && data.map(user => ( +
+
+
+ users +
+

{user.name && user.name}

+
+ +
+ )) } + ) } diff --git a/src/components/libraryCard/LibraryCard.jsx b/src/components/libraryCard/LibraryCard.jsx index 092b907f2..dd8ac210e 100644 --- a/src/components/libraryCard/LibraryCard.jsx +++ b/src/components/libraryCard/LibraryCard.jsx @@ -1,45 +1,35 @@ -import React from 'react' +import React, { useEffect } from 'react' import CardLayout from '../../layout/cardLayout/CardLayout' import './LibraryCard.css' +import { listResourceUser } from '../../actions/resourceUserAction' +import { useSelector, useDispatch } from 'react-redux' const LibraryCard = ({ data }) => { return ( <> - +
{ - data.map(item => { - return ( -
- -
- ) - }) + data && data.map((item) => { + return ( +
+ +
+
{item.category}
+

{item.title}

+
+
+ ) + }) } - - - ) -} - -const LibraryBackgroundCard = ({ item }) => { - return ( -
-
-
- -
-
-
{item.category}
-
{item.title}
-
-
+ ) } diff --git a/src/components/libraryHeader/LibraryHeader.jsx b/src/components/libraryHeader/LibraryHeader.jsx index e3ce525a9..17dbf4233 100644 --- a/src/components/libraryHeader/LibraryHeader.jsx +++ b/src/components/libraryHeader/LibraryHeader.jsx @@ -20,7 +20,7 @@ const data = [{ link: '/library/collection/saved' }] -const LibraryHeader = ({ setActive }) => { +const LibraryHeader = ({ setActive, btnName }) => { const [windowWidth, setWindowWidth] = useState(window.innerWidth) const userLogin = useSelector((state) => state.userLogin) const { userInfo } = userLogin @@ -70,7 +70,7 @@ const LibraryHeader = ({ setActive }) => {
-
+
diff --git a/src/components/listView/ListView.jsx b/src/components/listView/ListView.jsx index dbacf0aad..22eb8ab28 100644 --- a/src/components/listView/ListView.jsx +++ b/src/components/listView/ListView.jsx @@ -2,55 +2,50 @@ import { useState } from 'react' import CardLayout from '../../layout/cardLayout/CardLayout' import useSizeFinder from '../../utils/sizeFinder' import './ListView.css' +import { useParams } from 'react-router-dom' +import { useDispatch, useSelector } from 'react-redux' +import { createResourceUser } from '../../actions/resourceUserAction' const ListView = ({ data, title, setNewCollection, setModalActive, modalActive }) => { - const windowWidth = useSizeFinder() + const [active, setActive] = useState('') + + const dispatch = useDispatch() + const userInfo = useSelector((state) => state.userLogin.userInfo.id) + const [isAdded, setIsAdded] = useState() + function clickHandle (id) { + dispatch( + createResourceUser({ userId: userInfo, resourceId: id }) + ) + setIsAdded(id) + } + return ( <>

{title}

- { - windowWidth > 1000 - ? data && data.map(item => { - return ( - - ) - }) - : - {data && data.map(item => { - return ( - - ) - })} - - } + {data && data.map((item, index) => { + return ( +
+
+ item.title +
+
+

{item.title}

+

{item.category}

+
+
+ Add to + + +
+
+ ) + })}
) } -const ListViewCardComponent = ({ item, modalActive, setModalActive }) => { - const [active, setActive] = useState(false) - return ( -
-
-
- item.title -
-
-

{item.title}

-

{item.category}jelroro

-
-
-
- Add to - - -
-
- ) -} - export default ListView diff --git a/src/constants/collectionConstants.js b/src/constants/collectionConstants.js new file mode 100644 index 000000000..6fb836fb2 --- /dev/null +++ b/src/constants/collectionConstants.js @@ -0,0 +1,16 @@ +export const COLLECTION_LIST_REQUEST = 'COLLECTION_LIST_REQUEST' +export const COLLECTION_LIST_SUCCESS = 'COLLECTION_LIST_SUCCESS' +export const COLLECTION_LIST_FAIL = 'COLLECTION_LIST_FAIL' + +export const COLLECTION_SEARCH_REQUEST = 'COLLECTION_SEARCH_REQUEST' +export const COLLECTION_SEARCH_SUCCESS = 'COLLECTION_SEARCH_SUCCESS' +export const COLLECTION_SEARCH_FAIL = 'COLLECTION_SEARCH_FAIL' + +export const COLLECTION_CREATE_REQUEST = 'COLLECTION_CREATE_REQUEST' +export const COLLECTION_CREATE_SUCCESS = 'COLLECTION_CREATE_SUCCESS' +export const COLLECTION_CREATE_FAIL = 'COLLECTION_CREATE_FAIL' +export const COLLECTION_CREATE_RESET = 'COLLECTION_CREATE_RESET' + +export const COLLECTION_UPDATE_REQUEST = 'COLLECTION_UPDATE_REQUEST' +export const COLLECTION_UPDATE_SUCCESS = 'COLLECTION_UPDATE_SUCCESS' +export const COLLECTION_UPDATE_FAIL = 'COLLECTION_UPDATE_FAIL' diff --git a/src/constants/collectionUserConstants.js b/src/constants/collectionUserConstants.js new file mode 100644 index 000000000..f5d58e57a --- /dev/null +++ b/src/constants/collectionUserConstants.js @@ -0,0 +1,7 @@ +export const COLLECTION_USER_LIST_REQUEST = 'COLLECTION_USER_LIST_REQUEST' +export const COLLECTION_USER_LIST_SUCCESS = 'COLLECTION_USER_LIST_SUCCESS' +export const COLLECTION_USER_LIST_FAIL = 'COLLECTION_USER_LIST_FAIL' + +export const COLLECTION_USER_CREATE_REQUEST = 'COLLECTION_USER_CREATE_REQUEST' +export const COLLECTION_USER_CREATE_SUCCESS = 'COLLECTION_USER_CREATE_SUCCESS' +export const COLLECTION_USER_CREATE_FAIL = 'COLLECTION_USER_CREATE_FAIL' diff --git a/src/constants/resourceuserConstants.js b/src/constants/resourceuserConstants.js new file mode 100644 index 000000000..a314ab0db --- /dev/null +++ b/src/constants/resourceuserConstants.js @@ -0,0 +1,7 @@ +export const RESOURCE_USER_LIST_REQUEST = 'RESOURCE_USER_LIST_REQUEST' +export const RESOURCE_USER_LIST_SUCCESS = 'RESOURCE_USER_LIST_SUCCESS' +export const RESOURCE_USER_LIST_FAIL = 'RESOURCE_USER_LIST_FAIL' + +export const RESOURCE_USER_CREATE_REQUEST = 'RESOURCE_USER_CREATE_REQUEST' +export const RESOURCE_USER_CREATE_SUCCESS = 'RESOURCE_USER_CREATE_SUCCESS' +export const RESOURCE_USER_CREATE_FAIL = 'RESOURCE_USER_CREATE_FAIL' diff --git a/src/reducers/collectionReducres.js b/src/reducers/collectionReducres.js new file mode 100644 index 000000000..c6ee33261 --- /dev/null +++ b/src/reducers/collectionReducres.js @@ -0,0 +1,57 @@ +import { + COLLECTION_LIST_REQUEST, + COLLECTION_LIST_SUCCESS, + COLLECTION_LIST_FAIL, + COLLECTION_SEARCH_REQUEST, + COLLECTION_SEARCH_SUCCESS, + COLLECTION_SEARCH_FAIL, + COLLECTION_CREATE_REQUEST, + COLLECTION_CREATE_SUCCESS, + COLLECTION_CREATE_FAIL, + COLLECTION_CREATE_RESET +} from '../constants/collectionConstants' + +export const collectionListReducer = (state = { collections: [] }, action) => { + switch (action.type) { + case COLLECTION_LIST_REQUEST: + return { loading: true, collections: [] } + case COLLECTION_LIST_SUCCESS: + return { + loading: false, + collections: action.payload, + pages: action.payload.pages, + page: action.payload.page + + } + case COLLECTION_LIST_FAIL: + return { loading: false, error: action.payload } + case COLLECTION_SEARCH_REQUEST: + return { loading: true, collections: [] } + case COLLECTION_SEARCH_SUCCESS: + return { + loading: false, + searchCollections: action.payload.collections, + order: action.payload.order + } + case COLLECTION_SEARCH_FAIL: + return { loading: false, error: action.payload } + + default: + return state + } +} + +export const collectionCreateReducer = (state = {}, action) => { + switch (action.type) { + case COLLECTION_CREATE_REQUEST: + return { loading: true } + case COLLECTION_CREATE_SUCCESS: + return { loading: false, success: true, collection: action.payload } + case COLLECTION_CREATE_FAIL: + return { loading: false, error: action.payload } + case COLLECTION_CREATE_RESET: + return {} + default: + return state + } +} diff --git a/src/reducers/resourceUserReducers.js b/src/reducers/resourceUserReducers.js new file mode 100644 index 000000000..043a43a05 --- /dev/null +++ b/src/reducers/resourceUserReducers.js @@ -0,0 +1,24 @@ +import { + RESOURCE_USER_LIST_REQUEST, + RESOURCE_USER_LIST_SUCCESS, + RESOURCE_USER_LIST_FAIL + +} from '../constants/resourceuserConstants' + +export const resourceUserListReducer = (state = { resourceUsers: [] }, action) => { + switch (action.type) { + case RESOURCE_USER_LIST_REQUEST: + return { loading: true, resourceUsers: [] } + case RESOURCE_USER_LIST_SUCCESS: + return { + loading: false, + resourceUsers: action.payload, + pages: action.payload.pages, + page: action.payload.page + } + case RESOURCE_USER_LIST_FAIL: + return { loading: false, error: action.payload } + default: + return state + } +} diff --git a/src/screens/library/Library.jsx b/src/screens/library/Library.jsx index de473ae53..d8d27f302 100644 --- a/src/screens/library/Library.jsx +++ b/src/screens/library/Library.jsx @@ -37,6 +37,7 @@ const Library = () => { clickHandler={setModalActive} data={groupCollection} btnName='add to collections' setNewCollection={setNewCollection} + title='Collections' />} {newCollection && } @@ -45,7 +46,7 @@ const Library = () => {
- + {['Articles', 'Videos'].map(type => (
{ const [newCollection, setNewCollection] = useState(false) const [active, setActive] = useState(false) const [modalActive, setModalActive] = useState(false) + // const [pageNumber, setPageNumber] = useState(1) + const dispatch = useDispatch() + // const collectiondata = useSelector( + // (state) => state.listCollection.collections.collection + // ) + // const resourceList = useSelector((state) => state.listResources) + // const data = useSelector((state) => state.listResources) + // let resources = resourceList.searchResources ? resourceList.searchResources : resourceList.resources + // if (data) resources = data.resources + + // const userInfo = useSelector((state) => state.userLogin) + + // const resourceUser = useSelector((state)=>state) + // console.log("uuuu",resourceUser) + // useEffect(() =>{ + // dispatch(listResourceUser()) + // },[dispatch]) + + // useEffect(() => { + // dispatch(listResources({ pageNumber })) + // }, [pageNumber, dispatch]) + + // console.log('userinfo', userInfo) + function openAddCollection () { setModalActive(true) setActive(false) } return ( <> - {modalActive && } + {modalActive && } {newCollection && } + {active && }
- +

My library (files)

+ + + {/* */}

My Collections

- +
diff --git a/src/screens/library/savedCollection/SavedCollection.jsx b/src/screens/library/savedCollection/SavedCollection.jsx index 76441d207..92049b995 100644 --- a/src/screens/library/savedCollection/SavedCollection.jsx +++ b/src/screens/library/savedCollection/SavedCollection.jsx @@ -1,4 +1,4 @@ -import { useState } from 'react' +import { useState, useEffect } from 'react' import CollectionModal from '../../../components/collectionModal/CollectionModal' import GroupModal from '../../../components/groupModal/GroupModal' import LibraryCard from '../../../components/libraryCard/LibraryCard' @@ -7,6 +7,8 @@ import DashboardLayout from '../../../layout/dashboardLayout/DashboardLayout' import './SavedCollection.css' import { groupCollection, collections, library } from '../CollectionData' import SimpleModal from '../../../components/simpleModal/SimpleModal' +import { useSelector, useDispatch } from 'react-redux' +import { listCollections } from '../../../actions/collectionActions' const SavedCollection = () => { const [newCollection, setNewCollection] = useState(false) @@ -18,21 +20,39 @@ const SavedCollection = () => { setActive(false) } + // const data = useSelector( + // (state) => state.listCollection.collections.collection + // ) + + // const dispatch = useDispatch() + + // useEffect(() => { + // dispatch(listCollections()) + // }, [dispatch]) + return ( <> - {modalActive && } + {modalActive && } {newCollection && } + {active && }
- {['Farming', 'Branding'].map((category) => -
-

{category} Collections

- -
- )} +
+

Farming Collections

+ +
+
+

Branding Collections

+ +
) diff --git a/src/screens/library/userCollection/UserCollection.jsx b/src/screens/library/userCollection/UserCollection.jsx index 60a619630..1f52aa8f5 100644 --- a/src/screens/library/userCollection/UserCollection.jsx +++ b/src/screens/library/userCollection/UserCollection.jsx @@ -1,4 +1,5 @@ -import { useState } from 'react' +import { useState, useEffect } from 'react' +import { useSelector, useDispatch } from 'react-redux' import './UserCollection.css' import LibraryHeader from '../../../components/libraryHeader/LibraryHeader' @@ -8,19 +9,46 @@ import SimpleModal from '../../../components/simpleModal/SimpleModal' import { farming, groupCollection } from '../CollectionData' import GroupModal from '../../../components/groupModal/GroupModal' import CardLayout from '../../../layout/cardLayout/CardLayout' +import { listCollections, updateCollection } from '../../../actions/collectionActions' +import { createCollectionUser } from '../../../actions/collectionUserActions' +import Pagination from '../../../components/pagination/Pagination' const UserCollection = () => { - const [active, setActive] = useState(false) + 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) + + const dispatch = useDispatch() + + useEffect(() => { + dispatch(listCollections({ pageNumber })) + }, [pageNumber, dispatch]) + function openAddCollection () { setGroupModal(true) setModalActive(false) } + function handleClick (id) { + dispatch( + createCollectionUser({ userId: userInfo, collectionId: id }) + ) + setIsAdded(id) + } + return ( <> {groupModal && {

Farming Collections

+
- { - farming.map(item => { - return ( -
- + data && data.map(item => { + return ( +
+ -
-
{item.category}
-

{item.title}

+
+
{item.category}
+

{item.name}

- -
+
- ) - }) +
+ ) + }) } - +
+ + ) diff --git a/src/store.js b/src/store.js index 203428a05..beaae805d 100644 --- a/src/store.js +++ b/src/store.js @@ -7,9 +7,13 @@ import { enterpriseListReducer } from './reducers/enterpriseReducers' import { groupListReducer, groupViewReducer } from './reducers/communityGroupReducers' import { userLoginReducer, userRegisterReducer } from './reducers/userReducers' import { eventListReducer } from './reducers/calendarEventReducer' +import { collectionListReducer } from './reducers/collectionReducres' +import { resourceUserListReducer } from './reducers/resourceUserReducers' const reducer = combineReducers({ listEvents: eventListReducer, + listResourceUser: resourceUserListReducer, + listCollection: collectionListReducer, groupView: groupViewReducer, listGroups: groupListReducer, listEnterprises: enterpriseListReducer,