Created
June 28, 2020 17:16
-
-
Save kirasiris/c4af5486c6e5ec4e48f8430be83cb9e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const addPost = (formData, files, setUploadPercentage) => async dispatch => { | |
try { | |
// ATTACH FILES | |
let fileKeys = []; | |
for(let file of files) { | |
const uploadConfig = await axios.get(`${API}/api/v1/uploads/getS3url?type=${file.type}`); | |
await axios.put(uploadConfig.data.url, file, { | |
headers: { | |
'Content-Type': file.type | |
} | |
}); | |
fileKeys.push(uploadConfig.data.key); | |
} | |
// INSERT NEW BLOG | |
const config = { | |
headers: { | |
'Content-Type': 'multipart/form-data; application/json' | |
}, | |
onUploadProgress: ProgressEvent => { | |
setUploadPercentage( | |
parseInt(Math.round(ProgressEvent.loaded * 100) / ProgressEvent.total) | |
); | |
// Clear percentage | |
setTimeout(() => setUploadPercentage(0), 10000); | |
} | |
}; | |
const res = await axios.post(`${API}/api/v1/posts`, { ...formData, images: fileKeys}, config); | |
dispatch({ | |
type: ADD_POST, | |
payload: res.data | |
}); | |
dispatch(setAlert('Post Created', 'success')); | |
} catch (err) { | |
const errors = err.response && err.response.data.errors; | |
if (errors) { | |
errors.forEach(error => dispatch(setAlert(error.msg, 'danger'))); | |
} | |
dispatch({ | |
type: POST_ERROR, | |
payload: { msg: err.response && err.response.statusText, status: err.response && err.response.status } | |
}); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* HANLDE FORM AND INPUT | |
* | |
*/ | |
const [uploadPercentage, setUploadPercentage] = useState(0); | |
const [postData, setPostData] = useState({ | |
text: '', | |
images: null, | |
postedto: auth && auth.user.data._id === userId ? null : userId | |
}); | |
const { text, images, postedto } = postData; | |
const handleChange = name => e => { | |
setPostData({ ...postData, [name]: e.target.value, images: e.target.files }); | |
}; | |
const createPost = async e => { | |
e.preventDefault(); | |
await addPost(postData, images, setUploadPercentage); | |
}; | |
//// INPUT | |
<input | |
className={`custom-file-input`} | |
type={`file`} | |
aria-describedby={`avatar-text`} | |
name={`images`} | |
id={`images`} | |
onChange={handleChange('images')} | |
multiple | |
accept={`image/*`} | |
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.createPost = asyncHandler(async (req, res, next) => { | |
// Add user to req,body | |
req.body.user = req.user._id; | |
if (req.body.postedto === req.user._id) { | |
req.body.postedto = null; | |
} | |
// Bring files | |
// if (req.files) { | |
// req.body.images = req.files.map((image) => image.location); | |
// } | |
req.body.images = req.body.images.map((image) => image.data.key); | |
console.log('NOT GETTING HERE'); | |
console.log(req.body); | |
// Create document | |
const post = await Post.create(req.body); | |
// Send response | |
res.status(201).json({ success: true, data: post }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment