diff --git a/react-typescript/src/components/Item.tsx b/react-typescript/src/components/Item.tsx
index 79d0c13a..448c1862 100644
--- a/react-typescript/src/components/Item.tsx
+++ b/react-typescript/src/components/Item.tsx
@@ -1,12 +1,12 @@
import useItem from "../hooks/useItem";
-import Item from "../types"
+import { TItem } from "../types"
function Items() {
const { items, updateItem } = useItem();
return (
- {items.map((item: Item) => {
+ {items.map((item: TItem) => {
return (
-
Name: {item.name} Quality: {item.quality} SellIn: {item.sellIn}
diff --git a/react-typescript/src/hooks/useItem.ts b/react-typescript/src/hooks/useItem.ts
index 3e4b549f..fe76dbe1 100644
--- a/react-typescript/src/hooks/useItem.ts
+++ b/react-typescript/src/hooks/useItem.ts
@@ -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
}
diff --git a/react-typescript/src/model/EEvents.ts b/react-typescript/src/model/EEvents.ts
index 35c396a7..65bf67ef 100644
--- a/react-typescript/src/model/EEvents.ts
+++ b/react-typescript/src/model/EEvents.ts
@@ -1,5 +1,5 @@
const enum EEvents {
- SOLD_ITEM = 'SOLD_ITEM',
+ NEXT_DAY = 'NEXT_DAY',
}
export default EEvents;
\ No newline at end of file
diff --git a/react-typescript/src/model/initialState.ts b/react-typescript/src/model/initialState.ts
index ef455151..6d45883e 100644
--- a/react-typescript/src/model/initialState.ts
+++ b/react-typescript/src/model/initialState.ts
@@ -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;
\ No newline at end of file
+export default initialState;
\ No newline at end of file
diff --git a/react-typescript/src/model/reducer/calculateQuality.ts b/react-typescript/src/model/reducer/calculateQuality.ts
index 4ecdd754..27d77e67 100644
--- a/react-typescript/src/model/reducer/calculateQuality.ts
+++ b/react-typescript/src/model/reducer/calculateQuality.ts
@@ -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;
diff --git a/react-typescript/src/model/reducer/index.ts b/react-typescript/src/model/reducer/index.ts
index d9dfd04b..84b75cea 100644
--- a/react-typescript/src/model/reducer/index.ts
+++ b/react-typescript/src/model/reducer/index.ts
@@ -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;
}
diff --git a/react-typescript/src/types/index.ts b/react-typescript/src/types/index.ts
index e9b9ce43..11261b68 100644
--- a/react-typescript/src/types/index.ts
+++ b/react-typescript/src/types/index.ts
@@ -5,4 +5,11 @@ type TItem = {
isConjured: boolean
}
-export default TItem;
\ No newline at end of file
+type TGildedRose = {
+ items: TItem[]
+}
+
+export type {
+ TItem,
+ TGildedRose
+}
\ No newline at end of file