import actionPromise from "./action-promise"
import { gql } from '../../graphQl/getGQL'
import { useParams } from "react-router-dom"



export const actionCreateMessage = (text, _id, chats) =>  {
    const windowId = window.location.pathname
    const windowSplit = windowId.split('/')
    const chatId = windowSplit[2]

    return (
        actionPromise(
            'updateMsg',
            gql(
                `mutation newMsg($text: String, $id: ID) {
                    MessageUpsert(message: {chat: {_id: $id} text: $text}){
                        _id owner {
                        _id
                        createdAt
                        login
                        nick
                        } chat {
                        _id
                        createdAt
                        lastModified
                        title
                        } text media {
                        _id
                        createdAt
                        text
                        url
                        originalFileName
                        type
                        } createdAt
                    }
                }`, { id: chatId, text: text }))
    )
}