Skip to content

Commit c174e5d

Browse files
committed
Create reusable Modal component
1 parent ca2b14e commit c174e5d

File tree

4 files changed

+74
-53
lines changed

4 files changed

+74
-53
lines changed

frontend/package-lock.json

Lines changed: 0 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"react-icons": "^5.3.0",
1717
"react-router-dom": "^6.26.2",
1818
"react-scripts": "5.0.1",
19-
"reactjs-popup": "^2.0.6",
2019
"web-vitals": "^2.1.4"
2120
},
2221
"scripts": {

frontend/src/components/Modal.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import React, {useState} from 'react';
2+
3+
/**
4+
* Modal component
5+
* @param {string} title - A string which is the title at the top of the modal.
6+
* @param {React.JSX.Element} trigger - React.JSX.Element that when clicked will open the modal.
7+
* @param {Map} inputFields - {var : "Input Var Here", ...} A map of variables where things typed into a text box will be stored to the string that the user will see above that text box.
8+
* @param {(e: any) => void} changeHandler - The handler that handles changes in text and assigns them to a variable.
9+
* @param {(e: any) => Promise<void>} confirmHandler - The handler that calls an API upon clicking confirm
10+
*/
11+
export const Modal = ({title, trigger, inputFields, changeHandler, confirmHandler}) => {
12+
13+
const [showModal, setShowModal] = useState(false);
14+
15+
const close = () => {
16+
setShowModal(false);
17+
};
18+
19+
const open = () => {
20+
setShowModal(true);
21+
};
22+
23+
var fieldList = []
24+
Object.entries(inputFields).map(([name, text]) => (
25+
fieldList.push(
26+
<div className="p-2">
27+
<label>{text+ ": "} </label>
28+
<input className="border-2 border-black rounded p-2 size-11/12"
29+
type="text"
30+
name={name}
31+
onChange={changeHandler}
32+
/>
33+
</div>
34+
)
35+
));
36+
37+
return (
38+
<div>
39+
<div onClick={open}>{trigger}</div>
40+
{showModal ?
41+
<div>
42+
<div className='opacity-50 bg-black fixed top-0 left-0 h-full w-full' onClick={close}/>
43+
<div className="flex-auto bg-white rounded-lg border-2 border-black absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 p-3">
44+
<h3 className="font-semibold">
45+
{title}
46+
</h3>
47+
{fieldList}
48+
<div className="grid grid-rows-1 grid-cols-2 p-2">
49+
<button className="justify-self-start border-2 border-black p-1 rounded" onClick=
50+
{() => close()}>
51+
Cancel
52+
</button>
53+
<button className="justify-self-end border-2 border-black p-1 rounded bg-green-400" onClick={confirmHandler}>
54+
Confirm
55+
</button>
56+
</div>
57+
</div>
58+
</div>:null}
59+
</div>
60+
);
61+
}

frontend/src/components/pages/CourseDashboard.js

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React, { useEffect, useState } from 'react';
22
import Cookies from 'js-cookie';
3-
import Popup from 'reactjs-popup';
4-
import 'reactjs-popup/dist/index.css';
53
import Notiflix from 'notiflix';
4+
import {Modal} from '../Modal.js';
65

76
export function CourseDashboard() {
87
const [courseInfo, setCourseInfo] = useState(null);
@@ -94,45 +93,20 @@ export function CourseDashboard() {
9493

9594
let createButton = null;
9695
if (userInfo.role === "educator"){
97-
createButton =
98-
<Popup trigger={<div className="bg-blue-500 p-2 rounded shadow hover:bg-blue-700 text-center text-sm text-white font-semibold hover:cursor-pointer" >
99-
<div>+ New Course</div> </div>} modal nested>
100-
{close => (
101-
<div className="flex-auto">
102-
<h3 className="font-semibold">
103-
Create New Course
104-
</h3>
105-
<div className="p-2">
106-
<label>Course Name: </label>
107-
<input className="border-2 border-black rounded p-2 size-11/12"
108-
type="text"
109-
name="title"
110-
onChange={handleChange}
111-
/>
112-
</div>
113-
<div className="p-2">
114-
<label>Course Description: </label>
115-
<input className="border-2 border-black rounded p-2 size-11/12"
116-
type="text"
117-
name="description"
118-
onChange={handleChange}
119-
/>
120-
</div>
121-
<div className="grid grid-rows-1 grid-cols-2">
122-
<button className="justify-self-start border-2 border-black p-1 rounded" onClick=
123-
{() => close()}>
124-
Cancel
125-
</button>
126-
<button className="justify-self-end border-2 border-black p-1 rounded bg-green-400"
127-
onClick={handleCreateCourse}>
128-
Create
129-
</button>
130-
</div>
96+
createButton = <Modal
97+
title={"Create New Course"}
98+
trigger={
99+
<div className="bg-blue-500 p-2 rounded shadow hover:bg-blue-700 m-auto text-center text-sm text-white font-semibold hover:cursor-pointer" >
100+
<div>+ New Course</div>
131101
</div>
132-
)
133102
}
134-
</Popup>
135-
103+
inputFields={{
104+
title : "Course Name",
105+
description : "Course Description"
106+
}}
107+
changeHandler={handleChange}
108+
confirmHandler={handleCreateCourse}
109+
/>
136110
}
137111

138112
var courseList = [];

0 commit comments

Comments
 (0)