<commit_before>import axios from 'axios';
import { pipe, pipeP, curry, tryCatch, path, ifElse, isEmpty } from 'ramda';
import { Either } from 'ramda-fantasy';
import { productApi } from 'utils/apiRest';

import { ToastSuccess, ToastDanger } from 'react-toastr-basic';

import  formStore from './formStore';
import { createProductApi } from '../apis';


const showError = (error) => ToastDanger(error.message);

const checkCategoryCode = (params) => 
	ifElse(
		() => isEmpty(path(['categoryCode'], params)),
		() => Either.Left(new Error('Category code is empty')),
		() => Either.Right(params)
	)(params);

const checkProduct = (params) => 
	ifElse(
		() => isEmpty(path(['product'], params)),
		() => Either.Left(new Error('Product is empty')),
		() => Either.Right(params)
	)(params);

const checkBasePrice = (params) => 
	ifElse(
		() => isEmpty(path(['product', 'basePrice'], params)),
		() => Either.Left(new Error('Please enter base price')),
		() => Either.Right(params)
	)(params);

const checkQuantity = (params) => 
	ifElse(
		() => isEmpty(path(['product', 'quantity'], params)),
		() => Either.Left(new Error('Please enter quantity')),
		() => Either.Right(params)
	)(params);

const callCreateProductApi = ({ categoryCode, product }) => 
	curry(createProductApi)(categoryCode)(product)
		.then((res) => Either.Right(res.data.result))
		.catch((err) => Either.Left(new Error('Create Product fail')))

const completeCreateProduct = () => 
	pipe(
		() => formStore.reset(),
		() => ToastSuccess('Create product success')
	)();

const eitherErrorOrCreateProduct = Either.either(showError, callCreateProductApi);

export const createProduct = (categoryCode, product) => {
	const params = { categoryCode, product };
	pipeP(
		() => eitherErrorOrCreateProduct(
			checkCategoryCode(params)
				.chain(checkProduct)
				.chain(checkBasePrice)
				.chain(checkQuantity)
		),
		completeCreateProduct
	)()
}
<commit_msg>apply functional programming -<commit_after>import axios from 'axios';
import { pipe, pipeP, curry, tryCatch, path, ifElse, isEmpty, defaultTo } from 'ramda';
import { Either } from 'ramda-fantasy';
import { productApi } from 'utils/apiRest';

import { ToastSuccess, ToastDanger } from 'react-toastr-basic';

import  formStore from './formStore';
import { createProductApi } from '../apis';


const showError = (error) => ToastDanger(error.message);

const checkCategoryCode = (params) => 
	ifElse(
		() => isEmpty(path(['categoryCode'], params)),
		() => Either.Left(new Error('Category code is empty')),
		() => Either.Right(params)
	)(params);

const checkProduct = (params) => 
	ifElse(
		() => isEmpty(path(['product'], params)),
		() => Either.Left(new Error('Product is empty')),
		() => Either.Right(params)
	)(params);

const checkBasePrice = (params) => 
	ifElse(
		() => isEmpty(path(['product', 'basePrice'], params)),
		() => Either.Left(new Error('Please enter base price')),
		() => Either.Right(params)
	)(params);

const checkQuantity = (params) => 
	ifElse(
		() => isEmpty(path(['product', 'quantity'], params)),
		() => Either.Left(new Error('Please enter quantity')),
		() => Either.Right(params)
	)(params);

const completeCreateProduct = () => 
	pipe(
		() => formStore.reset(),
		() => ToastSuccess('Create product success')
	)('');

const callCreateProductApi = ({ categoryCode, product }) => pipeP(
	() => curry(createProductApi)(categoryCode)(product)
		.then((res) => Either.Right(res.data.result))
		.catch((err) => Either.Left(new Error('Create Product fail'))),
	Either.either(showError, completeCreateProduct),
)({ categoryCode, product})

const eitherErrorOrCreateProduct = Either.either(showError, callCreateProductApi);

export const createProduct = (categoryCode, product) => {
	const params = { categoryCode, product };
	eitherErrorOrCreateProduct(
		checkCategoryCode(params)
			.chain(checkProduct)
			.chain(checkQuantity)
	);
}

{'fraction_non_alphanumeric': 0.17934224049331962, 'fraction_numerical': 0.0, 'mean_word_length': 5.2288, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 53, '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': '6643765', 'n_tokens_mistral': 1288, 'n_tokens_neox': 1191, 'n_words': 266}