Facebook
From Tacky Iguana, 3 Years ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 68
  1. import User from "../Users/User";
  2. import React from 'react';
  3.  
  4. function sendMessage(message: string, sendPath: string, user: User, channel: number): string | null {
  5.     let body = {
  6.         channel: channel,
  7.         text: message
  8.     };
  9.  
  10.     async function postData(url = '', data = {}) {
  11.         const response = await fetch(url, {
  12.             method: 'POST',
  13.             mode: 'cors',
  14.             headers: {
  15.                 'Content-Type': 'application/json'
  16.             },
  17.             body: JSON.stringify(data)
  18.         });
  19.         return response.json();
  20.     }
  21.  
  22.     postData(sendPath, body)
  23.         .then(data => {
  24.             if (data.errorMessage !== undefined) {
  25.                 return data.errorMessage;
  26.             }
  27.         });
  28.     return null;
  29. }
  30.  
  31. export default sendMessage;