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

news add modal ui (fixes: #342) #410

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
4fb217a
news add modal ui
SujalLama Jul 29, 2021
975df15
scss lint fix
SujalLama Jul 29, 2021
0195fb2
scss fix part 2
SujalLama Jul 29, 2021
0ca05c0
news modal: same component for course and news
SujalLama Aug 2, 2021
c1298f1
video feature added in news
SujalLama Aug 3, 2021
a99c995
video feature added in the news add
SujalLama Aug 3, 2021
49e04dc
js lint fix in content
SujalLama Aug 3, 2021
c6f4c1d
news table linked to photos, videos and texts table
SujalLama Aug 5, 2021
08e7192
js lint fix of api
SujalLama Aug 5, 2021
0885ea5
news view and news create
SujalLama Aug 5, 2021
8e2b7ac
news edit and delete feature
SujalLama Aug 6, 2021
1cb7e0d
edit content component added
SujalLama Aug 6, 2021
2f4d815
importing necessary files
SujalLama Aug 6, 2021
01a405f
changes to the text, video and image
SujalLama Aug 6, 2021
aeddcce
Action file created
SujalLama Aug 6, 2021
3eae392
changes to edit content
SujalLama Aug 6, 2021
9d05d4e
styling Image, Text and Video
SujalLama Aug 6, 2021
723caf2
delete individual text, image and video in news
SujalLama Aug 6, 2021
ad2a34e
news add configuration
SujalLama Aug 6, 2021
3adba5a
adding cover image functionality to the news-component
SujalLama Aug 9, 2021
5430f84
video actions
SujalLama Aug 9, 2021
d4a2fa3
photo action and text action added
SujalLama Aug 9, 2021
c860b62
video actions is generic
SujalLama Aug 9, 2021
956ab91
updated photo of the news
SujalLama Aug 9, 2021
0bf047c
photo edit controller configured
SujalLama Aug 9, 2021
6170eac
photo, video and text refresh on delete and update
SujalLama Aug 9, 2021
309b353
add modal error fix
SujalLama Aug 9, 2021
f6b1cd7
Merge branch 'main' into news-modal-ui
lmmrssa Aug 11, 2021
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
16 changes: 13 additions & 3 deletions api/src/controllers/newsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ const addNews = (req, res) => {
if (req.file) {
filename = req.file.filename
}

const {
title, message, docType, readTime, language, creator, textDetail, imageDetail, videoDetail, category
} = req.body
db.News.create({
// _attachments: 'uploads/' + req.file.filename,
_attachments: 'news/' + filename,
title,
message,
Expand All @@ -62,7 +62,7 @@ const addNews = (req, res) => {
category,
communityId: req.params.id
})
.then(() => res.json({ message: 'News Created !!!' }).status(200))
.then((data) => res.json({ data }).status(200))
.catch((err) => res.json({ error: err.message }).status(400))
}

Expand Down Expand Up @@ -140,7 +140,17 @@ const getNewsById = (req, res) => {
model: db.Community,
attributes: [],
where: { id: req.params.id }
}]
},
{
model: db.Text,
},
{
model: db.Photo,
},
{
model: db.Video,
}
]
})
.then(news => {
if (news) {
Expand Down
27 changes: 15 additions & 12 deletions api/src/controllers/photoController.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,33 @@ const addphoto = async (req, res) => {
})
}

const deletePhoto = async (req, res) => {
const { id } = req.params
const photo = await db.Photo.destroy({ where: { id } })
if (!photo) {
throw new NotFoundError()
const updatePhoto = async (req, res) => {
let lessonImg = ''

const singlePhoto = await db.Photo.findOne({where: {id: req.params.id}});
if (req.file) {
lessonImg = req.file.filename
} else {
lessonImg = singlePhoto.dataValues.lessonImg
}
res.status(202).json({

const photo = await db.Photo.update({ ...req.body, lessonImg }, {where: {id: req.params.id}})
res.status(201).json({
status: true,
message: 'Lesson photo deleted successfully',
message: 'photo updated successfully',
data: photo
})
}

const updatePhoto = async (req, res) => {
const deletePhoto = async (req, res) => {
const { id } = req.params
const photo = await db.Photo.update(req.body, {
where: { id }
})
const photo = await db.Photo.destroy({ where: { id } })
if (!photo) {
throw new NotFoundError()
}
res.status(202).json({
status: true,
message: 'Lesson photo updated successfully',
message: 'Lesson photo deleted successfully',
data: photo
})
}
Expand Down
6 changes: 1 addition & 5 deletions api/src/controllers/textController.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,10 @@ const addText = async (req, res) => {

const deleteText = async (req, res) => {
const { id } = req.params
const text = await db.Text.destroy({ where: { id } })
if (!text) {
throw new NotFoundError()
}
await db.Text.destroy({ where: { id } })
res.status(202).json({
status: true,
message: 'Lesson text deleted successfully',
data: text
})
}

Expand Down
23 changes: 20 additions & 3 deletions api/src/controllers/videoController.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const addVideo = async (req, res) => {
videoResource = req.files.videoResource[0].filename
}
}
// console.log(req)

const video = await db.Video.create({
...req.body,
videoCover,
Expand All @@ -78,13 +78,30 @@ const deleteVideo = async (req, res) => {

const updateVideo = async (req, res) => {
const { id } = req.params
const video = await db.Video.update(req.body, {
const video = await db.Video.findOne({
where: { id }
})

let videoCover = ''
let videoResource = ''

if(req.files.videoCover === undefined) {
videoCover = video.dataValues.videoCover
videoResource = video.dataValues.videoResource
} else {
if (req.files.videoCover) {
videoCover = req.files.videoCover[0].filename
}
if (req.files.videoResource) {
videoResource = req.files.videoResource[0].filename
}
}

await db.Video.update({...req.body, videoCover, videoResource}, {where: {id}})

res.status(202).json({
status: true,
message: 'Video updated successfully',
data: video
})
}

Expand Down
62 changes: 33 additions & 29 deletions api/src/helpers/filehelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,38 +53,42 @@ const resizeImage = (req, res, next) => {
const { format, height, width } = { format: 'webp', ...req.body }
try {
// user might not send image sometimes
if (!req.file) return next()
if (!req.file) {
return next()

const filename = path
.basename(req.file.path)
.split('.')
.slice(0, -1)
.join('.')
let dir = path.join(
path.dirname(__dirname),
'..',
'files',
`${req.file.fieldname}`,
filename
)
let newImage = sharp(req.file.path)
if (width) {
newImage = newImage.resize(parseInt(width))
dir = dir + '-' + width + 'x' + height
}
if (req.body.render) {
newImage.toBuffer().then((data) => {
// To display the image
res.writeHead(200, {
'Content-Type': 'image/webp',
'Content-Length': data.length
else {
const filename = path
.basename(req.file.path)
.split('.')
.slice(0, -1)
.join('.')
let dir = path.join(
path.dirname(__dirname),
'..',
'files',
`${req.file.fieldname}`,
filename
)
let newImage = sharp(req.file.path)
if (width) {
newImage = newImage.resize(parseInt(width))
dir = dir + '-' + width + 'x' + height
}
if (req.body.render) {
newImage.toBuffer().then((data) => {
// To display the image
res.writeHead(200, {
'Content-Type': 'image/webp',
'Content-Length': data.length
})
return res.end(data)
})
return res.end(data)
})
} else {
const savePath = dir + '.' + format
newImage = newImage.toFile(savePath)
return next(null, true)
} else {
const savePath = dir + '.' + format
newImage = newImage.toFile(savePath)
return next(null, true)
}
}
} catch (error) {
throw error
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.addColumn('photos', 'newsId', {
type: Sequelize.INTEGER,
references: {
model: 'news',
key: 'id'
}
})
await queryInterface.addColumn('videos', 'newsId', {
type: Sequelize.INTEGER,
references: {
model: 'news',
key: 'id'
}
})
await queryInterface.addColumn('texts', 'newsId', {
type: Sequelize.INTEGER,
references: {
model: 'news',
key: 'id'
}
})
},

down: async (queryInterface, Sequelize) => {
await queryInterface.removeColumn('photos', 'newsId')
await queryInterface.removeColumn('videos', 'newsId')
await queryInterface.removeColumn('texts', 'newsId')
}
}
3 changes: 3 additions & 0 deletions api/src/models/newsModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ module.exports = (sequelize, DataTypes) => {
// association
News.associate = (models) => {
News.belongsTo(models.Community, { foreignKey: 'communityId' })
News.hasMany(models.Video, { foreignKey: 'newsId' })
News.hasMany(models.Text, { foreignKey: 'newsId' })
News.hasMany(models.Photo, { foreignKey: 'newsId' })
}
return News
}
4 changes: 4 additions & 0 deletions api/src/models/photoModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ module.exports = (sequelize, DataTypes) => {
isImgDesc: {
type: DataTypes.BOOLEAN
},
newsId: {
type: DataTypes.INTEGER
},
createdAt: {
type: DataTypes.DATE
},
Expand All @@ -24,6 +27,7 @@ module.exports = (sequelize, DataTypes) => {
{ timestamps: true })
Photo.associate = (models) => {
Photo.belongsTo(models.Lesson, { constraints: true, foreignKey: 'lessonId' })
Photo.belongsTo(models.News, { constraints: true, foreignKey: 'newsId' })
}
return Photo
}
7 changes: 7 additions & 0 deletions api/src/models/textModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ module.exports = (sequelize, DataTypes) => {
textDescription: {
type: DataTypes.TEXT
},
newsId: {
type: DataTypes.INTEGER
},
createdAt: {
type: DataTypes.DATE
},
Expand All @@ -27,6 +30,10 @@ module.exports = (sequelize, DataTypes) => {
constraints: true,
foreignKey: 'lessonId'
})
Text.belongsTo(models.News, {
constraints: true,
foreignKey: 'newsId'
})
}
return Text
}
4 changes: 4 additions & 0 deletions api/src/models/videoModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ module.exports = (sequelize, DataTypes) => {
videoResource: {
type: DataTypes.STRING
},
newsId: {
type: DataTypes.INTEGER
},
createdAt: {
type: DataTypes.DATE
},
Expand All @@ -30,6 +33,7 @@ module.exports = (sequelize, DataTypes) => {
{ timestamps: true })
Video.associate = (models) => {
Video.belongsTo(models.Lesson, { constraints: true, foreignKey: 'lessonId' })
Video.belongsTo(models.News, { constraints: true, foreignKey: 'newsId' })
}

return Video
Expand Down
4 changes: 2 additions & 2 deletions api/src/routes/photoRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const {
} = require('../controllers/photoController')

router.route('/').get(getPhotos)
router.route('/add').post(upload.single('lessonImg'), resizeImage, addphoto)
router.route('/:id').get(getPhotoById).delete(deletePhoto).put(updatePhoto)
router.route('/add').post(upload.single('img'), resizeImage, addphoto)
router.route('/:id').get(getPhotoById).delete(deletePhoto).put(upload.single('img'), resizeImage, updatePhoto)

module.exports = router
15 changes: 12 additions & 3 deletions api/src/routes/videoRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ const {

// get image file middleware
const fileChanger = (req, _res, next) => {
req.file = req.files.videoCover[0]
next()
if(req.files.videoCover === undefined) {
req.file = ''
next()
} else {
req.file = req.files.videoCover[0]
next()
}
}

router.route('/').get(getVideos)
Expand All @@ -26,6 +31,10 @@ router
resizeImage,
addVideo
)
router.route('/:id').get(getVideosById).delete(deleteVideo).put(updateVideo)
router.route('/:id').get(getVideosById).delete(deleteVideo).put(
upload.fields([{ name: 'videoCover' }, { name: 'videoResource' }]),
fileChanger,
resizeImage,
updateVideo)

module.exports = router
4 changes: 2 additions & 2 deletions api/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ app.use('/api/groups-users', groupUsersRoutes)
app.use('/api/enterprises-users', enterpriseUsersRoutes)
app.use('/api/resizer', resizerRoutes)
app.use('/api/videos', videoRouter)
app.use('/api/lesson-photos', photoRouter)
app.use('/api/lesson-text', textRouter)
app.use('/api/photos', photoRouter)
app.use('/api/texts', textRouter)
app.use('/api/materials', materialRouter)
app.use('/api/tests', testRouter)
app.use('/api/questions', questionRouter)
Expand Down
5 changes: 3 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ function App () {
<PrivateRoute component={Messenger} exact path='/messenger' />
<PrivateRoute component={MobileMessage} path='/messenger/:id' exact />
<PrivateRoute component={CommunityPagenews} exact path='/community-page-news/:id' />
<PrivateRoute component={NewsAdd} path='/community-page-news/:title/:category' exact />
<PrivateRoute component={CommunityNewsViewPage} path='/community-page-news-view' exact />
<PrivateRoute component={NewsAdd} path='/community-news/:title' exact />
<PrivateRoute component={NewsAdd} path='/community-news/edit/:id' exact />
<PrivateRoute component={CommunityNewsViewPage} path='/community-news-view-page/:id' exact />
<PrivateRoute component={AllCommunitiesCard} path='/community-switching' exact />
<PrivateRoute component={CommunityMembers} path='/community-members/:id' exact />
<PrivateRoute component={CommunityMembersProfile} path='/community-members-profile/:id' exact />
Expand Down
8 changes: 5 additions & 3 deletions src/actions/lessonActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ export const createLesson =
const lessonId = data?.data?.id
for (let i = 0; i < lessonData.length; i++) {
if (lessonData[i]?.videoLink || lessonData[i]?.videoResource) {
await addVideo({ lessonData: lessonData[i], lessonId, dispatch })
await addVideo({ data: lessonData[i], lessonId, dispatch })
}
if (lessonData[i]?.lessonImg) {
await addImage({ lessonData: lessonData[i], lessonId, dispatch })
console.log('hi')
await addImage({ data: lessonData[i], lessonId, dispatch })
}
if (lessonData[i]?.textHeading || lessonData[i]?.textDescription) {
await addText({ lessonData: lessonData[i], lessonId, dispatch })
console.log('hello')
await addText({ data: lessonData[i], lessonId, dispatch })
}
}
for (let i = 0; i < material.length; i++) {
Expand Down
Loading