Skip to content

Commit

Permalink
Created functions for actions
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebrownlee committed May 21, 2022
1 parent 8ae5487 commit 8ab29bb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 90 deletions.
66 changes: 26 additions & 40 deletions src/components/dashboard/CohortDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const CohortDialog = ({ toggleCohorts }) => {
const [message, setMessage] = useState("")
const { getCohorts, cohorts } = useContext(CohortContext)
const [cohortIds, setCohortIds] = useState([])
// let { toggleDialog: toggleCohorts } = useModal("#dialog--cohorts")

useEffect(
() => {
Expand All @@ -27,55 +26,42 @@ export const CohortDialog = ({ toggleCohorts }) => {
}
}, [activeStudent])

const addStudentToCohort = (e) => {
// return fetchIt(`${Settings.apiHost}/students/${activeStudent.id}/status`, {
// method: "POST",
// body: JSON.stringify({ status: e.target.value })
// }).then(reset)
const removeStudent = (cohort) => {
return fetchIt(`${Settings.apiHost}/cohorts/${cohort.id}/assign`, {
method: "DELETE",
body: JSON.stringify({
student_id: activeStudent.id
})
})
}

const assignStudent = (cohort) => {
return fetchIt(`${Settings.apiHost}/cohorts/${cohort.id}/assign`, {
method: "POST",
body: JSON.stringify({
person_id: activeStudent.id
})
})
}

return <dialog id="dialog--cohorts" className="dialog--cohorts">
{
cohorts.map(cohort => <div key={`cohort--${cohort.id}`}>
<input type="checkbox"
onChange={(changeEvent) => {
// If true, POST to /cohorts/n with `person_id: n` in body
if (changeEvent.target.checked) {
fetchIt(`${Settings.apiHost}/cohorts/${cohort.id}/assign`, {
method: "POST",
body: JSON.stringify({
person_id: activeStudent.id
})
})
.then((data) => {
getStudent()
})
}
// If false, DELETE to /cohorts/n with `student_id: n` in body
else {
fetchIt(`${Settings.apiHost}/cohorts/${cohort.id}/assign`, {
method: "DELETE",
body: JSON.stringify({
student_id: activeStudent.id
})
})
.then((data) => {
getStudent()
})
}


const action = changeEvent.target.checked ? assignStudent : removeStudent
action(cohort).then(getStudent)
}}
checked={cohortIds.includes(cohort.id) ? true : false} value={cohort.id} /> {cohort.name}
checked={cohortIds.includes(cohort.id)} value={cohort.id} /> {cohort.name}
</div>)
}
<button className="fakeLink" style={{
position: "absolute",
top: "0.33em",
right: "0.5em",
fontSize: "0.75rem"
}}
id="closeBtn"
onClick={toggleCohorts}>[ close ]</button>
position: "absolute",
top: "0.33em",
right: "0.5em",
fontSize: "0.75rem"
}}
id="closeBtn"
onClick={toggleCohorts}>[ close ]</button>
</dialog>
}
50 changes: 0 additions & 50 deletions src/components/utils/Fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,53 +48,3 @@ export const fetchIt = (url, kwargs = { method: "GET", body: null, token: null }

return theFetch
}

export const request = {
init(url) {
this.options = {}
this.options.headers = {}
this.url = url
},

get(url) {
this.init(url)
this.options.method = "GET"
return this.send()
},

post(url) {
this.init(url)
this.options.method = "POST"
this.options.headers["Content-Type"] = "application/json"
this.options.headers["Accept"] = "application/json"
return this
},

put(url) {
this.init(url)
this.options.method = "PUT"
this.options.headers = {
"Content-Type": "application/json"
}
return this
},

delete(url) {
this.init(url)
this.options.method = "DELETE"
return this.send()
},

withBody(body) {
if (this.options.method === "POST" || this.options.method === "PUT") {
this.options.body = JSON.stringify(body)
}
return this
},

async send() {
const req = await fetch(this.url, this.options)
const parsed = await req.json()
return parsed
}
}

0 comments on commit 8ab29bb

Please sign in to comment.