<commit_before>import {List} from "immutable";
import "./../flashMessage/store";
import "./../item/store";
import "./../addItem/store";
import "./../addTemplate/store";
import api from "./../service/api";
import dispatcher from "./../lib/dispatcher";
import * as actions from "./actions";
import {addFlashMessage} from "./../flashMessage/actions";
import FlashMessageRecord from "./../flashMessage/flashMessageRecord";
import {addItem} from "./../addItem/actions";
import {saveItem} from "./../item/actions";
import {addTemplate} from "./../addTemplate/actions";
import {dashboardCursor} from "./state";
import TemplateRecord from "./../template/templateRecord";
import ItemRecord from "./../item/itemRecord";

export const dispatchToken = dispatcher.register(({action, data}) => {
    switch (action) {
        case actions.setSelectedTemplate:
            setToDashboard('selectedTemplate', data);
            break;

        case actions.setTemplateName:
            setToDashboard('templateName', data);
            break;

        case actions.setItems:
            _setItems(data);
            break;

        case actions.setTemplates:
            _setTemplates(data);
            break;

        case actions.showAddItem:
            setToDashboard('showAddItem', data ? true : false);
            break;

        case actions.showAddTemplate:
            setToDashboard('showAddTemplate', data ? true : false);
            break;

        case addItem:
            _addItem();
            break;

        case saveItem:
            _saveItem(data);
            break;

        case addTemplate:
            _addTemplate();
            break;
    }
});

function _setItems(data) {
    let items = new List();

    data.forEach((item) => {
        items = items.push(new ItemRecord(item));
    });

    setToDashboard('items', items);
}

function _setTemplates(data) {
    let templates = new List();

    data.forEach((template) => {
        templates = templates.push(new TemplateRecord(template));
    });

    setToDashboard('templates', templates);
}

function _addItem() {
    let newItem = getAddItem();

    api.saveItem(getSelectedTemplate(), newItem).then((newItemId) => {
        newItem = newItem.set('id', newItemId);

        let items = new List(getItems());
        items = items.push(newItem);

        setToDashboard('items', items);
        addFlashMessage(new FlashMessageRecord({message: 'New item successfully saved!'}));

        const defaultItem = new ItemRecord();
        setToDashboard('addItemName', defaultItem.name);
        setToDashboard('addItemUrl', defaultItem.url);
        setToDashboard('addItemRefreshRate', defaultItem.refreshRate);
        setToDashboard('addItemHeight', defaultItem.height);
        setToDashboard('addItemWidth', defaultItem.width);
    });
}

function _addTemplate() {
    let newTemplate = getAddTemplate();

    api.saveTemplate(newTemplate).then((newTemplateId) => {
        newTemplate = newTemplate.set('id', newTemplateId);

        let templates = new List(getTemplates());
        templates = templates.push(newTemplate);

        setToDashboard('templates', templates);
        addFlashMessage(new FlashMessageRecord({message: 'New template successfully saved!'}));

        const defaultTemplate = new TemplateRecord();
        setToDashboard('addTemplateName', defaultTemplate.name);
    });
}

function _saveItem(item) {
    api.saveItem(getSelectedTemplate(), item);
}

export function getApiUrl() {
    return dashboardCursor().get('apiUrl');
}

export function getApiVersion() {
    return dashboardCursor().get('apiVersion');
}

export function setToDashboard(name, value) {
    dashboardCursor((dashboard) => dashboard.set(name, value));
}

export function getSelectedTemplate() {
    return dashboardCursor().get('selectedTemplate');
}

export function getTemplateName() {<commit_msg>Flash message after item updated<commit_after>import React from "react";
import {List} from "immutable";
import "./../flashMessage/store";
import "./../item/store";
import "./../addItem/store";
import "./../addTemplate/store";
import api from "./../service/api";
import dispatcher from "./../lib/dispatcher";
import * as actions from "./actions";
import {addFlashMessage} from "./../flashMessage/actions";
import FlashMessageRecord from "./../flashMessage/flashMessageRecord";
import {addItem} from "./../addItem/actions";
import {saveItem} from "./../item/actions";
import {addTemplate} from "./../addTemplate/actions";
import {dashboardCursor} from "./state";
import TemplateRecord from "./../template/templateRecord";
import ItemRecord from "./../item/itemRecord";

export const dispatchToken = dispatcher.register(({action, data}) => {
    switch (action) {
        case actions.setSelectedTemplate:
            setToDashboard('selectedTemplate', data);
            break;

        case actions.setTemplateName:
            setToDashboard('templateName', data);
            break;

        case actions.setItems:
            _setItems(data);
            break;

        case actions.setTemplates:
            _setTemplates(data);
            break;

        case actions.showAddItem:
            setToDashboard('showAddItem', data ? true : false);
            break;

        case actions.showAddTemplate:
            setToDashboard('showAddTemplate', data ? true : false);
            break;

        case addItem:
            _addItem();
            break;

        case saveItem:
            _saveItem(data);
            break;

        case addTemplate:
            _addTemplate();
            break;
    }
});

function _setItems(data) {
    let items = new List();

    data.forEach((item) => {
        items = items.push(new ItemRecord(item));
    });

    setToDashboard('items', items);
}

function _setTemplates(data) {
    let templates = new List();

    data.forEach((template) => {
        templates = templates.push(new TemplateRecord(template));
    });

    setToDashboard('templates', templates);
}

function _addItem() {
    let newItem = getAddItem();

    api.saveItem(getSelectedTemplate(), newItem).then((newItemId) => {
        newItem = newItem.set('id', newItemId);

        let items = new List(getItems());
        items = items.push(newItem);

        setToDashboard('items', items);
        addFlashMessage(new FlashMessageRecord({message: 'New item successfully saved!'}));

        const defaultItem = new ItemRecord();
        setToDashboard('addItemName', defaultItem.name);
        setToDashboard('addItemUrl', defaultItem.url);
        setToDashboard('addItemRefreshRate', defaultItem.refreshRate);
        setToDashboard('addItemHeight', defaultItem.height);
        setToDashboard('addItemWidth', defaultItem.width);
    });
}

function _addTemplate() {
    let newTemplate = getAddTemplate();

    api.saveTemplate(newTemplate).then((newTemplateId) => {
        newTemplate = newTemplate.set('id', newTemplateId);

        let templates = new List(getTemplates());
        templates = templates.push(newTemplate);

        setToDashboard('templates', templates);
        addFlashMessage(new FlashMessageRecord({message: 'New template successfully saved!'}));

        const defaultTemplate = new TemplateRecord();
        setToDashboard('addTemplateName', defaultTemplate.name);
    });
}

function _saveItem(item) {
    api.saveItem(getSelectedTemplate(), item)
        .then(() => {
            // todo check React warning with `keys` for items (try new version of React?)
            const message = [
                'Item ',
                <strong>{item.name}</strong>,
                ' was updated successfully.'
            ];

            addFlashMessage(new FlashMessageRecord({message}));
        });
}

export function getApiUrl() {
    return dashboardCursor().get('apiUrl');
}

export function getApiVersion() {
    return dashboardCursor().get('apiVersion');
}

export function setToDashboard(name, value) {
    dashboardCursor((dashboard) => dashboard.set(name, value));
}

export function getSelectedTemplate() {
    return dashboardCursor().get('selectedTemplate');
}

export function getTemplateName() {
{'fraction_non_alphanumeric': 0.13481627135307853, 'fraction_numerical': 0.0, 'mean_word_length': 2.9162656400384988, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, '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': '3069507', 'n_tokens_mistral': 2180, 'n_tokens_neox': 2155, 'n_words': 487}