refactor: re-structure logical structure

This commit is contained in:
sirlolcat 2022-06-04 17:58:25 +04:30
parent 8c090978fd
commit 528057894b
9 changed files with 189 additions and 0 deletions

View File

@ -0,0 +1,22 @@
import { useStore } from "../model";
import EEvents from "../model/EEvents";
import Item from "../types"
function useItem() {
const { state, dispatch } = useStore();
return {
items: state,
updateItem: (item: Item):void => {
dispatch({
type: EEvents.SOLD_ITEM,
payload: {
name: item.name
}
});
}
};
}
export default useItem;

View File

@ -0,0 +1,5 @@
const enum EEvents {
SOLD_ITEM = 'SOLD_ITEM',
}
export default EEvents;

View File

@ -0,0 +1,3 @@
import useStore from './useStore';
export { useStore };

View File

@ -0,0 +1,42 @@
const GildedRose = {
items: [
{
name: '+5 Dexterity Vest',
sellIn: 10,
quality: 20,
isConjured: false
},
{
name: 'Aged Brie',
sellIn: 2,
quality: 0,
isConjured: false
},
{
name: 'Elixir of the Mongoose',
sellIn: 5,
quality: 7,
isConjured: false
},
{
name: 'Sulfuras, Hand of Ragnaros',
sellIn: 0,
quality: 80,
isConjured: false
},
{
name: 'Backstage passes to a TAFKAL80ETC concert',
sellIn: 15,
quality: 20,
isConjured: false
},
{
name: 'Conjured Mana Cake',
sellIn: 3,
quality: 6,
isConjured: true
},
]
}
export default GildedRose;

View File

@ -0,0 +1,52 @@
import Item from "../../types"
function calculateQuality(state: Item): number {
const sellInAmount = state.sellIn;
let calculatedQuality = state.quality;
let degradeRate = 1;
let qualityIdentifier = 1;
if(state.name.includes("Sulfuras")) {
return 80;
}
if(state.name.includes("Aged Brie")) {
qualityIdentifier = -1;
}
if(state.name.includes("Backstage passes")) {
qualityIdentifier = -1;
if(sellInAmount <= 10 && sellInAmount > 6) {
degradeRate = 2;
}
if(sellInAmount <= 6 && sellInAmount > 0) {
degradeRate = 3;
}
if(sellInAmount <= 0) {
return 0;
}
}
if(state.isConjured) {
degradeRate = 2;
}
if(state.sellIn <= 0) {
return calculatedQuality;
}
calculatedQuality = calculatedQuality - (qualityIdentifier * degradeRate);
if(calculatedQuality > 50) {
return 50;
}
if(calculatedQuality < 0) {
return 0;
}
return calculatedQuality;
}
export default calculateQuality;

View File

@ -0,0 +1,17 @@
function updateSellIn (state: any): number {
let calculatedSellIn = state.sellIn;
if(state.name.includes("Sulfuras")) {
return calculatedSellIn;
}
calculatedSellIn = calculatedSellIn - 1;
if(calculatedSellIn < 0) {
return 0;
}
return calculatedSellIn;
}
export default updateSellIn;

View File

@ -0,0 +1,28 @@
import Item from '../../types';
import EEvents from '../EEvents';
import calculateQuality from '././calculateQuality';
import calculateSellIn from '././calculateSellIn';
function updateQuality(item: Item) {
const calculatedQuality = calculateQuality(item);
return { ...item, quality: calculatedQuality };
}
function updateSellIn(item: Item) {
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];
}
}
return state;
}
export default reducer;

View File

@ -0,0 +1,12 @@
import { useReducer } from "react";
import initialState from "./initialState";
import reducer from "./reducer";
export default function useStore() {
const [state, dispatch] = useReducer(reducer, initialState.items);
return {
state,
dispatch
}
}

View File

@ -0,0 +1,8 @@
type TItem = {
name: string,
sellIn: number,
quality: number,
isConjured: boolean
}
export default TItem;