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