<commit_before>import * as React from 'react';
import * as PropTypes from 'prop-types';
import { Card, CardActions, CardTitle, CardText } from 'material-ui/Card';
import FlatButton from 'material-ui/FlatButton';
import Snackbar from 'material-ui/Snackbar';

import RoomLight from './RoomLight';

const getOneLightState = async id => fetch(`/api/lights/${id}`, {
    headers: {
        Authorization: `Bearer ${sessionStorage.getItem('userToken')}`,
        'Content-Type': 'application/json'
    }
});

const getAllLightStates = async (lightIds) => {
    const responses = await Promise.all(lightIds.map(i => getOneLightState(i)));
    const lightStates = await Promise.all(responses.map(r => r.json()));
    return lightStates.reduce((acc, { id, state: { on } }) => ({ ...acc, [id]: on }), {});
};

class Room extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            showSnackbar: false,
            expanded: false,
            snackbarMessage: '',
            lights: []
        };

        this.changeRoomState = this.changeRoomState.bind(this);
        this.turnLightsOn = this.turnLightsOn.bind(this);
        this.turnLightsOff = this.turnLightsOff.bind(this);
        this.handleRequestClose = this.handleRequestClose.bind(this);
        this.handleExpandChange = this.handleExpandChange.bind(this);
    }

    async componentWillMount() {
        try {
            const lightStates = await getAllLightStates(this.props.lightIds);
            this.setState({
                lights: lightStates
            });
        }
        catch (e) {
            console.log(e);
            this.setState({
                showSnackbar: true,
                snackbarMessage: 'Couldn\'t get the state of each light'
            });
        }
    }

    handleRequestClose() {
        this.setState({
            showSnackbar: false,
            snackbarMessage: ''
        });
    }

    handleExpandChange(newState) {
        this.setState({
            expanded: newState
        });
    }

    async changeRoomState(state) {
        try {
            await fetch(`/api/rooms/${this.props.id}`, {
                method: 'post',
                headers: {
                    Authorization: `Bearer ${sessionStorage.getItem('userToken')}`,
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(state)
            });
            const lightStates = await getAllLightStates(this.props.lightIds);
            this.setState({
                lights: lightStates
            });
        }
        catch (e) {
            this.setState({
                showSnackbar: true,
                snackbarMessage: `An error occured while turning ${state.on ? 'on' : 'off'} lights`
            });
        }
    }

    async changeOneLight(id) {
        console.log(this.state);
        console.log(id);
    }

    async turnLightsOn() {
        return this.changeRoomState({ on: true });
    }

    async turnLightsOff() {
        return this.changeRoomState({ on: false });
    }

    render() {
        return (
            <div>
                <Card
                    expanded={this.state.expanded}
                    initiallyExpanded={false}<commit_msg>when changing lights, update the state as well<commit_after>import * as React from 'react';
import * as PropTypes from 'prop-types';
import { Card, CardActions, CardTitle, CardText } from 'material-ui/Card';
import FlatButton from 'material-ui/FlatButton';
import Snackbar from 'material-ui/Snackbar';
import { assign } from 'lodash';

import RoomLight from './RoomLight';

const getOneLightState = async id => fetch(`/api/lights/${id}`, {
    headers: {
        Authorization: `Bearer ${sessionStorage.getItem('userToken')}`,
        'Content-Type': 'application/json'
    }
});

const getAllLightStates = async (lightIds) => {
    const responses = await Promise.all(lightIds.map(i => getOneLightState(i)));
    const lightStates = await Promise.all(responses.map(r => r.json()));
    return lightStates.reduce((acc, { id, state: { on } }) => ({ ...acc, [id]: on }), {});
};

class Room extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            showSnackbar: false,
            expanded: false,
            snackbarMessage: '',
            lights: []
        };

        this.changeRoomState = this.changeRoomState.bind(this);
        this.turnLightsOn = this.turnLightsOn.bind(this);
        this.turnLightsOff = this.turnLightsOff.bind(this);
        this.handleRequestClose = this.handleRequestClose.bind(this);
        this.handleExpandChange = this.handleExpandChange.bind(this);
    }

    async componentWillMount() {
        try {
            const lightStates = await getAllLightStates(this.props.lightIds);
            this.setState({
                lights: lightStates
            });
        }
        catch (e) {
            console.log(e);
            this.setState({
                showSnackbar: true,
                snackbarMessage: 'Couldn\'t get the state of each light'
            });
        }
    }

    handleRequestClose() {
        this.setState({
            showSnackbar: false,
            snackbarMessage: ''
        });
    }

    handleExpandChange(newState) {
        this.setState({
            expanded: newState
        });
    }

    async changeRoomState(state) {
        try {
            await fetch(`/api/rooms/${this.props.id}`, {
                method: 'post',
                headers: {
                    Authorization: `Bearer ${sessionStorage.getItem('userToken')}`,
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(state)
            });
            const lightStates = await getAllLightStates(this.props.lightIds);
            this.setState({
                lights: lightStates
            });
        }
        catch (e) {
            this.setState({
                showSnackbar: true,
                snackbarMessage: `An error occured while turning ${state.on ? 'on' : 'off'} lights`
            });
        }
    }

    async changeOneLight(id) {
        try {
            await fetch(`api/lights/${id}`, {
                method: 'post',
                headers: {
                    Authorization: `Bearer ${sessionStorage.getItem('userToken')}`,
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ on: !this.state.lights[id] })
            });
            const response = await getOneLightState(id);
            const { id: lightId, state: { on } } = await response.json();
            this.setState({
                lights: assign({}, this.state.lights, { [lightId]: on })
            });
        }
        catch (e) {
            this.setState({
                showSnackbar: true,
                snackbarMessage: 'Couldn\'t change light state'
            });
        }
    }

    async turnLightsOn() {
        return this.changeRoomState({ on: true });
    }

    async turnLightsOff() {
        return this.changeRoomState({ on: false });
    }

    render() {
        return (
            <div>
                <Card
                    expanded={this.state.expanded}
                    initiallyExpanded={false}
{'fraction_non_alphanumeric': 0.12482928161704453, 'fraction_numerical': 0.0, 'mean_word_length': 1.76131221719457, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '625035', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1990, 'n_words': 404}