NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import React, { useReducer } from "react";
import axios from "axios";
import CustomerContext from "./CustomerContext";
import CustomerReducer from "./CustomerReducer";
import {
CUSTOMER_LOADING,
CUSTOMER_LOADED,
CUSTOMER_ERROR,
CUSTOMER_RESET,
SET_CURRENT_CUSTOMER,
UPDATE_CUSTOMER,
CLEAR_CURRENT_CUSTOMER,
} from "../../data/constants";
import { Business } from "@mui/icons-material";
// import { env } from "process";

const API_BASE_URL = process.env.REACT_APP_TRKNW_API_BASE_URL;
// const API_HOST = "";
const CustomerState = (props) => {
const initialState = {
error: null,
customers: null,
changedCustomers: [],
customerLoading: false,
customerLoaded: false,
selectedCustomer: localStorage.getItem("selectedCustomer")
? JSON.parse(localStorage.getItem("selectedCustomer"))
: null,
savedCriteria: null,
};
const [state, dispatch] = useReducer(CustomerReducer, initialState);

//Reset Context State
const resetCustomerState = () => {
dispatch({
type: CUSTOMER_RESET,
payload: { ...initialState },
});
};

//select a customer for processing
const setCurrentCustomer = (customer) => {
dispatch({
type: SET_CURRENT_CUSTOMER,
payload: customer,
});
};

//Search All customers
const getCustomers = async (formData) => {
// const savedCriteria = { ...formData };
// const config = {
// headers: {
// Authorization: `Bearer ${token}`,
// "Access-Control-Allow-Credentials": "true",
// },
// };
// if (formData) {
// formData.action = "CUSTOMER_MANAGEMENT";
// formData.function = "get_customers";
// formData.version = "1";
// }
console.log("FETCHING Customers ");
return new Promise(async (resolve, reject) => {
try {
console.log("Payload", JSON.stringify(formData));
dispatch({
type: CUSTOMER_LOADING,
});
const res = await axios.get(`${API_BASE_URL}/Customer/CustomerList`);
console.log("Customers ", res);
dispatch({
type: CUSTOMER_LOADED,
payload: res.data,
});
resolve(res.data);
} catch (err) {
dispatch({
type: CUSTOMER_ERROR,
payload: err,
});
reject(err);
}
});
};
//Creat a customer
const createCustomer = async (formData, token) => {
return new Promise(async (resolve, reject) => {
try {
console.log("Payload", JSON.stringify(formData));
dispatch({
type: CUSTOMER_LOADING,
});
const res = await axios.post(
`${API_BASE_URL}/Customer/CreateCustomer`,
formData
);
console.log("Customers ", res);
dispatch({
type: UPDATE_CUSTOMER,
payload: res.data,
});
resolve(res.data);
} catch (err) {
dispatch({
type: CUSTOMER_ERROR,
payload: err,
});
reject(err);
}
});
};

//Create Filing master Businesss Categories
const createCustomerBusinessCategories = async (formData, token) => {
return new Promise(async (resolve, reject) => {
try {
console.log("Payload", JSON.stringify(formData));
// dispatch({
// type: CUSTOMER_LOADING,
// });
const res = await axios.post(
`${API_BASE_URL}/Customer/CreateCustomerBusinessCategory`,
formData
);
console.log("Customer Business Categories", res);
// dispatch({
// type: UPDATE_FILING_MASTER_CATEGORY,
// payload: res.data,
// });
resolve(res.data);
} catch (err) {
dispatch({
type: CUSTOMER_ERROR,
payload: err,
});
reject(err);
}
});
};
// Update an existing business category
const updateBusinessCategory = async (BusinessCategoryId, formData) => {
try {
dispatch({
type: CUSTOMER_LOADING,
});

const res = await axios.put(
`${API_BASE_URL}/Application/BusinessCategoryMasterUpdate1/${BusinessCategoryId}`,
formData
);

// Find the index of the updated category in the state array
const updatedCategoryIndex = state.selectedCustomer.businessCategories.findIndex(
(category) => category.BusinessCategoryId === BusinessCategoryId
);

// Update the category in the state array
const updatedCategories = [...state.selectedCustomer.businessCategories];
updatedCategories[updatedCategoryIndex] = res.data;

// Update the selected customer in the state
const updatedCustomer = {
...state.selectedCustomer,
businessCategories: updatedCategories,
};

dispatch({
type: UPDATE_CUSTOMER,
payload: updatedCustomer,
});

// Update the selected customer in local storage
localStorage.setItem("selectedCustomer", JSON.stringify(updatedCustomer));

console.log("Business category updated: ", res.data);
} catch (err) {
dispatch({
type: CUSTOMER_ERROR,
payload: "Failed to update business category",
});
}
};

//deteting customer
const clearCustomer = async (customerId) => {
try {
dispatch({
type: CUSTOMER_LOADING,
});

await axios.delete(
`${API_BASE_URL}/Customer/CustomerDelete${customerId}`
// " https://localhost:7005/api/Customer/CustomerDelete"
);
console.log("Customer deleted with ID: ", customerId);

dispatch({
type: CLEAR_CURRENT_CUSTOMER,
});

localStorage.removeItem("selectedCustomer");
} catch (err) {
dispatch({
type: CUSTOMER_ERROR,
payload: err,
});
}
};
//editing customer

// Update the selected customer
// const updateSelectedCustomer = async (formData) => {
// try {
// dispatch({
// type: CUSTOMER_LOADING,
// });

// const res = await axios.put(
// `${API_BASE_URL}/Customer/CustomerUpdate/${state.selectedCustomer.customerId}`,
// formData
// );

// dispatch({
// type: UPDATE_CUSTOMER,
// payload: res.data,
// });

// // Update the selected customer in local storage
// localStorage.setItem("selectedCustomer", JSON.stringify(res.data));

// console.log("Selected customer updated: ", res.data);
// } catch (err) {
// dispatch({
// type: CUSTOMER_ERROR,
// payload: err,
// });
// }
// };
// Update an existing customer
const updateCustomer = async (customerId, formData) => {
try {
dispatch({
type: CUSTOMER_LOADING,
});

const res =
await axios.put(
`${API_BASE_URL}/Customer/CustomerUpdate${customerId}`,
formData
);

dispatch({
type: UPDATE_CUSTOMER,
payload: res.data,
});

console.log("Customer updated: ", res.data);
} catch (err) {
dispatch({
type: CUSTOMER_ERROR,
payload: err,
});
}
};






return (
<CustomerContext.Provider
value={{
...state,
resetCustomerState,
setCurrentCustomer,
// clearCurrentCustomer,

clearCustomer,
getCustomers,
createCustomer,
createCustomerBusinessCategories,
updateCustomer,
// loadCustomer,
// customerDispatch: dispatch,
// removeCustomers,
updateBusinessCategory,
}}
>
{props.children}
</CustomerContext.Provider>
);
};

export default CustomerState;
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.