Skip to content

Instantly share code, notes, and snippets.

View DJanoskova's full-sized avatar
🐱

Dana Janoskova DJanoskova

🐱
View GitHub Profile
const MaterialForm = () => {
const { values, useInput, isValid } = useForm({
username: '',
email: ''
});
const handleSubmit = e => {
e.preventDefault();
console.log(values)
};
export function useForm (defaultValues, invalidAttr = { error: true }) {
const [values, setValues] = useState(defaultValues);
const [mounted, setMounted] = useState(false);
const [formErrors, setFormErrors] = useState([]);
const handleError = useCallback((name, isValid) => {
let errors = formErrors;
const index = errors.findIndex(error => error === name);
if (!isValid) {
export function useFormInput ({
name,
validation = '',
values: formData,
setValues: setFormData,
defaultInvalidAttr,
handleError
}) {
const formValue = dot.pick(name, formData) || '';
const CustomForm = () => {
const defaultValues = {
username: '',
email: '',
age: ''
};
const customErrorAttribute = {
className: 'has-error',
'another-attr': 'look-at-me'
};
'use strict'
class RoomUpdateController {
constructor (params) {
const { socket, request } = params
this.socket = socket
this.request = request
console.log('A new subscription for room topic', socket.topic)
}
const Room = use('App/Models/Room');
const { broadcast } = require('../../utils/socket.utils');
async createMessage ({ params, request, response }) {
const room = await Room.find(params.id);
if (!room) {
return response.notFound(`The room doesn't exist`)
}
const Ws = use('Ws')
function broadcast (id, type, data) {
const channel = Ws.getChannel('room:*')
if (!channel) return
const topic = channel.topic(`room:${id}`)
if (!topic) {
console.error('Has no topic')
return
{
"type": "room:newMessage",
"data": {
"created_at": "2019-09-01 07:47:50",
"id": 1,
"message": "Hello world!",
"name": "Dana",
"room_id": "a2451846-fa6a-2cb2-9a9e-96ff1bc54af9",
"updated_at": "2019-09-01 07:47:50"
}
import React, { Component } from 'react';
import { ROOM_FETCH } from '../actions';
import connection from '../lib/socket';
import Messages from '../components/Messages';
import AddMessage from '../components/AddMessage';
// a global variable so we can disconnect once we unmount
let subscription;
import Ws from '@adonisjs/websocket-client';
import { getSocketProtocol } from '../utils/data';
export class SocketConnection {
connect () {
this.ws = Ws(`${getSocketProtocol()}${process.env.REACT_APP_API_URL}`)
// .withApiToken(token)
.connect();