Skip to content

Instantly share code, notes, and snippets.

View jaunesarmiento's full-sized avatar

Jaune Carlo Sarmiento jaunesarmiento

View GitHub Profile
// useEffect(..., []);
useEffect(() => {
const subscription = API
.graphql(graphqlOperation(onCreateMessage))
.subscribe({
next: (event) => {
setMessages([...messages, event.value.data.onCreateMessage]);
}
});
import '@aws-amplify/pubsub';
import { onCreateMessage } from '../graphql/subscriptions';
export const onCreateMessage = /* GraphQL */ `
subscription OnCreateMessage {
onCreateMessage {
id
channelID
author
body
createdAt
updatedAt
}
mutation createMessage1 {
createMessage(input: {
author: "Monica",
body: "Hi there Dave!"
}) {
id
author
body
createdAt
updatedAt
.message {
align-self: flex-start;
...
}
.message.me {
align-self: flex-end;
...
}
{messages.map((message) => (
<div
key={message.id}
className={message.author === 'Dave' ? 'message me' : 'message'}>{message.body}</div>
))}
const [messages, setMessages] = useState([]);
useEffect(() => {
API
.graphql(graphqlOperation(listMessages))
.then((response) => {
const items = response.data?.listMessages?.items;
if (items) {
setMessages(items);
import React, { useEffect, useState } from 'react';
import API, { graphqlOperation } from '@aws-amplify/api';
import { listMessages } from './graphql/queries';
// ... rest of the code here
* {
box-sizing: border-box;
}
html, body {
height: 100%;
}
#root {
display: flex;
// ... import statements here
function App() {
// Placeholder function for handling changes to our chat bar
const handleChange = () => {};
// Placeholder function for handling the form submission
const handleSubmit = () => {};