{"size": 854, "ext": "js", "max_stars_repo_path": "src/hooks/useDownload.js", "max_stars_repo_name": "PhotonQuantum/sjtu-schedule-exporter", "max_stars_repo_stars_event_min_datetime": "2020-08-19T17:50:30.000Z", "max_stars_repo_stars_event_max_datetime": "2020-08-19T17:50:30.000Z", "max_issues_repo_path": "src/hooks/useDownload.js", "max_issues_repo_name": "PhotonQuantum/sjtu-schedule-exporter", "max_issues_repo_issues_event_min_datetime": "2022-02-27T09:45:01.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-27T09:45:01.000Z", "max_forks_repo_path": "src/hooks/useDownload.js", "max_forks_repo_name": "PhotonQuantum/sjtu-schedule-exporter", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 30.5, "max_line_length": 94, "alphanum_fraction": 0.5854800937}
import fileDownload from "js-file-download";

import {useRequest, useSessionStorageState} from "@umijs/hooks";
import {BACKEND_URL} from "../config";

import {formatISO} from "date-fns";

export const useDownload = (session) => {
    const {loading, error, run: download} = useRequest(({year, term, termStart}) => ({
        url: `${BACKEND_URL}/schedule_ics`,
        params: {
            year: year,
            term: term,
            term_start_date: formatISO(termStart, {representation: "date"})
        },
        method: "post",
        body: JSON.stringify({"session": session})
    }), {
        manual: true,
        onSuccess: (result, params) => {
            const param = params[0];
            fileDownload(result, `schedule_${param.year}_${param.term}.ics`, "text/calendar");
        }
    });
    return {loading, error, download};
}

