{"size": 991, "ext": "js", "max_stars_repo_path": "src/client/src/hooks/api/useChatUpload.js", "max_stars_repo_name": "UP933287/cw-proj", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/client/src/hooks/api/useChatUpload.js", "max_issues_repo_name": "UP933287/cw-proj", "max_issues_repo_issues_event_min_datetime": "2021-06-22T08:36:33.000Z", "max_issues_repo_issues_event_max_datetime": "2021-06-22T08:36:33.000Z", "max_forks_repo_path": "src/client/src/hooks/api/useChatUpload.js", "max_forks_repo_name": "UP933287/cw-proj", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 30.96875, "max_line_length": 60, "alphanum_fraction": 0.6024217962}
import { useState, useCallback } from 'react';
import useLocationId from '../other/useLocationId';
import useChat from './useChat';
import ENDPOINTS from '../../constants/endpoints';

const useChatUpload = () => {
    const { locationId: chatId } = useLocationId();
    const { sendFile } = useChat(chatId);
    const [filePayload, setFilePayload] = useState();

    const uploadFile = useCallback(async () => {
        if (!chatId || !filePayload) {
            return;
        }
        const formData = new FormData();
        formData.append('chatId', chatId);
        formData.append('file', filePayload);
        const response = await fetch(`${ENDPOINTS.files}`, {
            method: 'POST',
            body: formData,
        });
        const payload = await response.json();
        if (response.ok) {
            sendFile(filePayload, payload.uri);
        }
    }, [chatId, filePayload, sendFile]);

    return { uploadFile, setFilePayload };
};

export default useChatUpload;
