diff --git a/src/containers/ManagementPage/index.js b/src/containers/ManagementPage/index.js index c607495ef48a808d3075d55fd949cecdad6447e3..35f9cc0e6806cb40c64982cae9bbb36e3df0ec2a 100644 --- a/src/containers/ManagementPage/index.js +++ b/src/containers/ManagementPage/index.js @@ -23,7 +23,7 @@ import TableCell from '@material-ui/core/TableCell'; import TableHead from '@material-ui/core/TableHead'; import TableRow from '@material-ui/core/TableRow'; -import { getAllVideos, getAllCourse, getAllSection, getCourse, getToken, postCourse, BASE_URL, BASE_URL_API, postSection } from "../../services"; +import { getAllVideos, getAllCourse, getAllSection, getCourse, postCourse, BASE_URL, BASE_URL_API, postSection, postVideo } from "../../services"; import './style.css'; @@ -76,6 +76,7 @@ const Management = props => { const [videos, setVideos] = useState([]); const [videoTitle, setVideoTitle] = React.useState(''); const [videoDesc, setVideoDesc] = React.useState(''); + const [videoFile, setVideoFile] = React.useState(null); const [openCourse, setOpenCourse] = React.useState(false); const [openSection, setOpenSection] = React.useState(false); @@ -104,7 +105,7 @@ const Management = props => { } const handleSubmitVideo = () => { - // postCourse(courseName, courseDesc); + postVideo(videoTitle, videoDesc, videoFile, videoSection); setOpenVideo(false); }; @@ -171,6 +172,10 @@ const Management = props => { setVideoSection(Number(event.target.value) || ''); }; + const handleChangeUploadVideo = event => { + setVideoFile(event.target.files[0].name); + } + useEffect(() => { getAllVideos().then(res => { if (res.status === 200) setVideos(res.data); @@ -402,6 +407,7 @@ const Management = props => { <input accept="video/*" className={classes.input} + onChange={handleChangeUploadVideo} // style={{ display: 'none' }} id="raised-button-file" type="file" diff --git a/src/services/index.js b/src/services/index.js index de045779b651e390f812973acf3ff384ae20ef7e..6d0d0dae9b67716ad8d8dd515befc3e3ceff52d9 100644 --- a/src/services/index.js +++ b/src/services/index.js @@ -142,4 +142,31 @@ export const postSection = async (sectionName, sectionDesc, courseObject) => { if (err.response === undefined) return { status: 401 }; return { status: err.response.status }; } +}; + +export const postVideo = async (videoTitle, videoDesc, videoFile, sectionObject) => { + try { + const token = getToken(); + if (!token) throw new Error({ response: { status: 401 } }); + const res = await fetch(BASE_URL_API + "/video/", { + method: 'POST', + headers: { + 'Authorization': `jwt ${token}`, + 'Content-Type': `application/json` + }, + body: JSON.stringify({ + 'video_title': videoTitle, + 'video_description': videoDesc, + 'video_file': videoFile, + 'section_object': sectionObject + }) + }); + return { + status: res.status, + data: res.data.videos + }; + } catch (err) { + if (err.response === undefined) return { status: 401 }; + return { status: err.response.status }; + } }; \ No newline at end of file