Listing_SuggestPrice/frontend/src/store/uiSlice.js

54 lines
1.4 KiB
JavaScript

import { createSlice } from '@reduxjs/toolkit';
// Form các trường hiển thị: sku, condition, qty, price, warehouse, packageContain, noListing.
// aiPrice là giá do AI gợi ý (chỉ hiển thị / cập nhật qua nút Suggest, không nhập tay).
export const emptyForm = {
sku: '',
condition: 'NEW',
qty: 0,
price: 0,
warehouse: '',
packageContain: '',
noListing: false,
aiPrice: null,
};
const uiSlice = createSlice({
name: 'ui',
initialState: {
editingId: null, // null = thêm mới, số = đang sửa
form: { ...emptyForm },
},
reducers: {
updateForm(state, action) {
const { name, value } = action.payload;
state.form[name] = value;
},
startAdd(state) {
state.editingId = null;
state.form = { ...emptyForm };
},
startEdit(state, action) {
const p = action.payload;
state.editingId = p.id;
state.form = {
sku: p.sku ?? '',
condition: p.condition ?? 'NEW',
qty: p.qty ?? 0,
price: p.price ?? 0,
warehouse: p.warehouse ?? '',
packageContain: p.packageContain ?? '',
noListing: p.noListing ?? false,
aiPrice: p.aiPrice ?? null,
};
},
resetForm(state) {
state.editingId = null;
state.form = { ...emptyForm };
},
},
});
export const { updateForm, startAdd, startEdit, resetForm } = uiSlice.actions;
export default uiSlice.reducer;