{"size": 5141, "ext": "js", "max_stars_repo_path": "src/contexts/JobsContext.js", "max_stars_repo_name": "Mengqi89/everest-client", "max_stars_repo_stars_event_min_datetime": "2019-07-31T22:17:30.000Z", "max_stars_repo_stars_event_max_datetime": "2019-07-31T22:17:30.000Z", "max_issues_repo_path": "src/contexts/JobsContext.js", "max_issues_repo_name": "Mengqi89/everest-client", "max_issues_repo_issues_event_min_datetime": "2020-04-28T19:19:44.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-18T10:00:58.000Z", "max_forks_repo_path": "src/contexts/JobsContext.js", "max_forks_repo_name": "Mengqi89/everest-client", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 5, "max_forks_count": null, "avg_line_length": 25.9646464646, "max_line_length": 93, "alphanum_fraction": 0.5008753161}
import React, { Component } from 'react'

const JobsContext = React.createContext({
    jobs: [],
    filteredJobs: [],
    filtered: false,
    keyword: '',
    salary: 0,
    gradeLevel: 0,
    location: '',
    error: null,
    setError: () => { },
    clearError: () => { },
    setJobsData: () => { },
    setFilter: () => { },
    filterJobs: () => { },
    handleChange: () => { },
    addJob: () => { },
    removeJob: () => { },
})

export default JobsContext

export class JobsProvider extends Component {
    state = {
        jobs: [],
        filteredJobs: [],
        filtered: false,
        keyword: '',
        salary: 0,
        gradeLevel: 0,
        location: '',
        error: null
    }

    filter = (salary, location, gradeLevel, keyword, tempJobs) => {
        return this.checkSalary(salary, location, gradeLevel, keyword, tempJobs)
    }

    checkKeyword = (keyword, tempJobs) => {
        if (keyword !== '') {
            let lowerCaseKeyword = keyword.toLowerCase()
            function includesStr(values, str) {
                return values.map(function (value) {
                    return String(value).toLowerCase()
                }).find(function (value) {
                    return value.includes(lowerCaseKeyword)
                })
            }

            tempJobs = tempJobs.filter(function (item) {
                return includesStr(Object.values(item), keyword)
            })
        }
        return tempJobs
    }

    checkGrade = (gradeLevel, keyword, tempJobs) => {
        if (gradeLevel !== 'all') {
            if (gradeLevel === 'kindergarten') {
                tempJobs = tempJobs.filter(jobs => jobs.grade_level.includes('kindergarten'))
            }

            if (gradeLevel === 'elementary') {
                tempJobs = tempJobs.filter(jobs => jobs.grade_level.includes('elementary'))
            }

            if (gradeLevel === 'middle') {
                tempJobs = tempJobs.filter(jobs => jobs.grade_level.includes('middle'))
            }

            if (gradeLevel === 'high') {
                tempJobs = tempJobs.filter(jobs => jobs.grade_level.includes('high'))
            }

            if (gradeLevel === 'college') {
                tempJobs = tempJobs.filter(jobs => jobs.grade_level.includes('college'))
            }
        }
        return this.checkKeyword(keyword, tempJobs)
    }

    checkLocation = (location, gradeLevel, keyword, tempJobs) => {
        if (location !== '') {
            tempJobs = tempJobs.filter(job => job.location.includes(location))
        }
        return this.checkGrade(gradeLevel, keyword, tempJobs)
    }

    checkSalary = (salary, location, gradeLevel, keyword, tempJobs) => {
        if (salary !== 'all') {
            tempJobs = this.state.jobs.filter(job => job.total_salary_lowest >= salary)
        }
        return this.checkLocation(location, gradeLevel, keyword, tempJobs)
    }

    setError = error => {
        console.log(error)
        this.setState({ error })
    }

    clearError = error => {
        this.setState({ error: null })
    }

    addJob = (job) => {
        this.setState({
            jobs: [
                ...this.state.jobs,
                job
            ]
        })
    }

    removeJob = (job_id) => {
        const newJobs = this.state.jobs.filter(j => j.id !== job_id)
        this.setState({
            jobs: newJobs
        })
    }

    setJobsData = jobs => {

        this.setState({
            jobs,
            filteredJobs: [...jobs]
        })
    }

    handleChange = (event) => {
        const { value, name } = event.target
        this.setState(
            {
                [name]: value
            }
        )
    }

    setFilter = () => {
        this.setState({
            filtered: false
        })

    }

    filterJobs = (event) => {
        event.preventDefault()
        const {
            jobs,
            keyword,
            salary,
            gradeLevel,
            location
        } = this.state

        //all jobs
        let tempJobs = [...jobs]

        //recursive filter
        tempJobs = this.filter(salary, location, gradeLevel, keyword, tempJobs)

        this.setState({
            filtered: true,
            filteredJobs: [...tempJobs]
        })


    }

    render() {
        const value = {
            jobs: this.state.jobs,
            filteredJobs: this.state.filteredJobs,
            filtered: this.state.filtered,
            keyword: this.state.keyword,
            salary: this.state.salary,
            gradeLevel: this.state.gradeLevel,
            location: this.state.location,
            error: this.state.error,
            setError: this.setError,
            clearError: this.clearError,
            setJobsData: this.setJobsData,
            filterJobs: this.filterJobs,
            handleChange: this.handleChange,
            addJob: this.addJob,
            removeJob: this.removeJob,
            setFilter: this.setFilter
        }
        return (
            <JobsContext.Provider value={value}>
                {this.props.children}
            </JobsContext.Provider>
        )
    }

}

