mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 06:51:27 +00:00
fix: fix logic of quality and sellIn
This commit is contained in:
parent
d658a4d7ff
commit
58f35a4fd5
@ -1,12 +1,12 @@
|
||||
import useItem from "../hooks/useItem";
|
||||
import Item from "../types"
|
||||
import { TItem } from "../types"
|
||||
|
||||
function Items() {
|
||||
const { items, updateItem } = useItem();
|
||||
return (
|
||||
<div className="Item">
|
||||
<ul>
|
||||
{items.map((item: Item) => {
|
||||
{items.map((item: TItem) => {
|
||||
return (
|
||||
<li key={item.name}>
|
||||
Name: {item.name} Quality: {item.quality} SellIn: {item.sellIn} <button onClick={() => updateItem(item)}></button>
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { useStore } from "../model";
|
||||
import EEvents from "../model/EEvents";
|
||||
import Item from "../types"
|
||||
import { TItem } from "../types"
|
||||
|
||||
function useItem() {
|
||||
const { state, dispatch } = useStore();
|
||||
@ -8,9 +8,9 @@ function useItem() {
|
||||
return {
|
||||
items: state,
|
||||
|
||||
updateItem: (item: Item):void => {
|
||||
updateItem: (item: TItem):void => {
|
||||
dispatch({
|
||||
type: EEvents.SOLD_ITEM,
|
||||
type: EEvents.NEXT_DAY,
|
||||
payload: {
|
||||
name: item.name
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
const enum EEvents {
|
||||
SOLD_ITEM = 'SOLD_ITEM',
|
||||
NEXT_DAY = 'NEXT_DAY',
|
||||
}
|
||||
|
||||
export default EEvents;
|
||||
@ -1,4 +1,4 @@
|
||||
const GildedRose = {
|
||||
const initialState = {
|
||||
items: [
|
||||
{
|
||||
name: '+5 Dexterity Vest',
|
||||
@ -9,7 +9,7 @@
|
||||
{
|
||||
name: 'Aged Brie',
|
||||
sellIn: 2,
|
||||
quality: 0,
|
||||
quality: 5,
|
||||
isConjured: false
|
||||
},
|
||||
{
|
||||
@ -36,7 +36,7 @@
|
||||
quality: 6,
|
||||
isConjured: true
|
||||
},
|
||||
]
|
||||
],
|
||||
}
|
||||
|
||||
export default GildedRose;
|
||||
export default initialState;
|
||||
@ -1,6 +1,6 @@
|
||||
import Item from "../../types"
|
||||
import { TItem } from "../../types"
|
||||
|
||||
function calculateQuality(state: Item): number {
|
||||
function calculateQuality(state: TItem): number {
|
||||
const sellInAmount = state.sellIn;
|
||||
|
||||
let calculatedQuality = state.quality;
|
||||
|
||||
@ -1,26 +1,26 @@
|
||||
import Item from '../../types';
|
||||
import { TItem } from '../../types';
|
||||
import EEvents from '../EEvents';
|
||||
import calculateQuality from '././calculateQuality';
|
||||
import calculateSellIn from '././calculateSellIn';
|
||||
|
||||
function updateQuality(item: Item) {
|
||||
function updateQuality(item: TItem): TItem {
|
||||
const calculatedQuality = calculateQuality(item);
|
||||
return { ...item, quality: calculatedQuality };
|
||||
}
|
||||
|
||||
function updateSellIn(item: Item) {
|
||||
function updateSellIn(item: TItem): TItem {
|
||||
const calculatedSellIn = calculateSellIn(item);
|
||||
return { ...item, sellIn: calculatedSellIn };
|
||||
}
|
||||
|
||||
function reducer(state: Item[], action: any): any {
|
||||
if(action.type === EEvents.SOLD_ITEM) {
|
||||
let targetItem = state.find((item) => (item.name.includes(action.payload.name)));
|
||||
if (targetItem !== undefined) {
|
||||
const calculatedItems = [...state];
|
||||
|
||||
|
||||
}
|
||||
function reducer(state: TItem[], action: any): TItem[] {
|
||||
if(action.type === EEvents.NEXT_DAY) {
|
||||
return state.map(item => {
|
||||
if(item.name.includes(action.payload.name)) {
|
||||
return updateQuality(updateSellIn(item));
|
||||
}
|
||||
return item;
|
||||
});
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
@ -5,4 +5,11 @@ type TItem = {
|
||||
isConjured: boolean
|
||||
}
|
||||
|
||||
export default TItem;
|
||||
type TGildedRose = {
|
||||
items: TItem[]
|
||||
}
|
||||
|
||||
export type {
|
||||
TItem,
|
||||
TGildedRose
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user