<commit_before>import api from '../api';
import history from '../components/history';
import Noty from '../components/Noty';
import * as Errors from './Errors';

export function addBook(book) {
	return {
		type: 'BOOK_ADD',
		book: book,
	};
}

export function replaceBook(book) {
	return {
		type: 'BOOK_UPDATE',
		book: book,
	};
}

export function setBooks(books, mode = null) {
	return {
		type: 'BOOKS_FETCH',
		books: books,
		mode: mode
	};
}

export function setSelectedBook(bookId) {
	return function(dispatch, getState) {
		if (null == bookId) {
			return dispatch(setBookInfo(null));
		}
		let books = getState().books.books.filter(book => book.id == bookId);
		let selectedBook = null;

		if (books.count() == 1) {
			selectedBook = books.first();
			dispatch(setBookInfo(selectedBook));
		} else {
			dispatch(fetchBookInfo(bookId));
		}
	};
}

export function setBookInfo(book) {
	return {
		type: 'SET_BOOK_INFO',
		book: book
	};
}

export function fetchBookInfo(bookId) {
	return function(dispatch, getState) {
		api.getBook(bookId).then(book => {
			dispatch(setBookInfo(book));
		}).catch(Errors.handleError);
	};
}

export function fetchBooks(mode = null) {
	return function(dispatch, getState) {
		let books = getState().books.books;

		if (!books || books.length == 0) {
			api.getBooks(null).then(books => {
				dispatch(setBooks(books, mode));
			});	
		} else {
			dispatch(setBooks(books, mode));		
		}
	};
}

export function createBook(book) {
	return function(dispatch, getState) {

		api.saveBook(book).then(book => {
			dispatch(addBook(book));
			history.pushState(null, '/book/' + book.id);
		}).catch(Errors.handleError);
	};
}

export function updateBook(book) {

	return function(dispatch, getState) {
		api.updateBook(book).then(book => {
			dispatch(replaceBook(book));
			Noty.info('Book updated');
		}).catch(Errors.handleError);
	}
}<commit_msg>sdffg<commit_after>import api from '../api';
import history from '../components/history';
import Noty from '../components/Noty';
import * as Errors from './Errors';

export function addBook(book) {
	return {
		type: 'BOOK_ADD',
		book: book,
	};
}

export function replaceBook(book) {
	return {
		type: 'BOOK_UPDATE',
		book: book,
	};
}

export function setBooks(books, mode = null) {
	return {
		type: 'BOOKS_FETCH',
		books: books,
		mode: mode
	};
}

export function setSelectedBook(bookId) {
	return function(dispatch, getState) {
		if (null == bookId) {
			return dispatch(setBookInfo(null));
		}
		let books = getState().books.books.filter(book => book.id == bookId);
		let selectedBook = null;

		if (books.count() == 1) {
			selectedBook = books.first();
			dispatch(setBookInfo(selectedBook));
		} else {
			dispatch(fetchBookInfo(bookId));
		}
	};
}

export function setBookInfo(book) {
	return {
		type: 'SET_BOOK_INFO',
		book: book
	};
}

export function fetchBookInfo(bookId) {
	return function(dispatch, getState) {
		api.getBook(bookId).then(book => {
			dispatch(setBookInfo(book));
		}).catch(Errors.handleError);
	};
}

export function fetchBooks(mode = null) {
	return function(dispatch, getState) {
		let books = getState().books.books;

		if (!books || books.length == 0) {
			api.getBooks(null).then(books => {
				dispatch(setBooks(books, mode));
			});	
		} else {
			dispatch(setBooks(books, mode));		
		}
	};
}

export function createBook(book) {
	return function(dispatch, getState) {
		console.log("CREATING BOOK...");
		api.saveBook(book).then(book => {
			console.log("CREATED	 BOOK 1...");
			//dispatch(addBook(book));
			console.log("CREATED	 BOOK 2...");
			history.pushState(null, '/book/' + book.id);
		}).catch(Errors.handleError);
	};
}

export function updateBook(book) {

	return function(dispatch, getState) {
		api.updateBook(book).then(book => {
			dispatch(replaceBook(book));
			Noty.info('Book updated');
		}).catch(Errors.handleError);
	}
}
{'fraction_non_alphanumeric': 0.1644295302013423, 'fraction_numerical': 0.0015487867836861124, 'mean_word_length': 4.058746736292428, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '2981966', 'n_tokens_mistral': 1465, 'n_tokens_neox': 1324, 'n_words': 272}