Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jello] Week4 #55

Open
wants to merge 12 commits into
base: jello
Choose a base branch
from
Prev Previous commit
Next Next commit
chore: rename properties of state for clarity
hjsong333 committed Jun 16, 2023
commit 5aced239f79dbdf5d45ff110ecbd03a3d59afc70
7 changes: 2 additions & 5 deletions src/components/NewsStand.ts
Original file line number Diff line number Diff line change
@@ -6,12 +6,9 @@ import style from '@components/NewsStand.module.css';

type NewsStandProps = {
dateInfo: Date;
gridInfo: GridInfo;
gridViewInfo: GridViewInfo;
subscriptionInfo: string[];
mainViewerInfo: {
targetMedia: 'total' | 'subscribed';
viewer: 'listView' | 'gridView';
};
mainViewerInfo: MainViewerInfo;
news: NewsData | null;
fields: FieldData[];
listIndex: number;
2 changes: 1 addition & 1 deletion src/components/main/Main.ts
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import GridView from '@components/main/gridView/GridView';
import style from '@components/main/Main.module.css';

type MainProps = {
gridInfo: GridInfo;
gridViewInfo: GridViewInfo;
subscriptionInfo: string[];
mainViewerInfo: {
targetMedia: 'total' | 'subscribed';
14 changes: 7 additions & 7 deletions src/components/main/gridView/GridView.ts
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import SubscriptionCover from '@components/main/gridView/SubscriptionCover';
import style from '@components/main/gridView/GridView.module.css';

type GridViewProps = {
gridInfo: GridInfo;
gridViewInfo: GridViewInfo;
subscriptionInfo: string[];
};

@@ -40,7 +40,7 @@ export default class GridView {
images: shuffleArray(await getGridImgs())
}
});
};
}

private createCells() {
return [...Array(this.numberOfCells)].map((_, index) => {
@@ -88,10 +88,10 @@ export default class GridView {

private updateProps(props: GridViewProps) {
return {
imgs: [...props.gridInfo.imgs],
page: props.gridInfo.page,
isHover: props.gridInfo.isHover,
hoverIndex: props.gridInfo.hoverIndex,
imgs: [...props.gridViewInfo.imgs],
page: props.gridViewInfo.page,
isHover: props.gridViewInfo.isHover,
hoverIndex: props.gridViewInfo.hoverIndex,
subscribedIds: [...props.subscriptionInfo]
};
}
@@ -130,7 +130,7 @@ export default class GridView {
}

updateView(props: GridViewProps) {
const { imgs, page, isHover, hoverIndex } = props.gridInfo;
const { imgs, page, isHover, hoverIndex } = props.gridViewInfo;
if (
this.props.isHover !== isHover ||
this.props.hoverIndex !== hoverIndex ||
2 changes: 1 addition & 1 deletion src/dispatch.ts
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ const subscribers: Function[] = [];
export const subscribe = (
subscriber: (state: {
dateInfo: Date;
gridInfo: GridInfo;
gridViewInfo: GridViewInfo;
subscriptionInfo: string[];
mainViewerInfo: {
targetMedia: 'total' | 'subscribed';
15 changes: 1 addition & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -4,19 +4,6 @@ import '@/styles/main.css';

const app = document.querySelector('#app')!;
const state = getState();

const newsStand = new NewsStand({
dateInfo: state.dateInfo,
gridInfo: state.gridInfo,
subscriptionInfo: state.subscribedMedias,
mainViewerInfo: {
targetMedia: state.targetMedia,
viewer: state.viewer
},
news: state.news,
fields: state.fields,
listIndex: state.listIndex,
arrowInfo: state.arrowInfo
});
const newsStand = new NewsStand(state);

app.append(newsStand.element);
59 changes: 31 additions & 28 deletions src/store.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
import { getNewsList, getSubscribedMedias, setSubscribedMedias } from '@utils/dataUtils';
import { GRID_PAGE_LIMIT } from '@/constants';

const state: {
type State = {
dateInfo: Date;
gridInfo: GridInfo;
subscribedMedias: string[];
targetMedia: 'total' | 'subscribed';
viewer: 'gridView' | 'listView';
gridViewInfo: GridViewInfo;
subscriptionInfo: string[];
mainViewerInfo: MainViewerInfo;
news: NewsData | null;
fields: FieldData[];
listIndex: number;
arrowInfo: {
left: boolean;
right: boolean;
};
} = {
}

const state: State = {
dateInfo: new Date(),
gridInfo: {
gridViewInfo: {
imgs: [],
page: 0,
isHover: false,
hoverIndex: -1
},
subscribedMedias: getSubscribedMedias(),
targetMedia: 'total',
viewer: 'gridView',
subscriptionInfo: getSubscribedMedias(),
mainViewerInfo: {
targetMedia: 'total',
viewer: 'gridView',
},
news: null,
fields: [],
listIndex: 0,
@@ -41,31 +44,31 @@ export const getState = () => {
export const invoke = (action: Action) => {
switch (action.type) {
case 'turnOnSubscriptionCover':
state.gridInfo.isHover = action.payload.hoverOnGrid;
state.gridInfo.hoverIndex = action.payload.hoveredCellIndex;
state.gridViewInfo.isHover = action.payload.hoverOnGrid;
state.gridViewInfo.hoverIndex = action.payload.hoveredCellIndex;

break;
case 'turnOffSubscriptionCover':
state.gridInfo.isHover = action.payload.hoverOnGrid;
state.gridInfo.hoverIndex = -1;
state.gridViewInfo.isHover = action.payload.hoverOnGrid;
state.gridViewInfo.hoverIndex = -1;

break;
case 'initGridImages':
state.gridInfo.imgs = action.payload.images;
state.gridViewInfo.imgs = action.payload.images;

changeArrowStates();
break;
case 'updateSubscribedMedia':
if (action.payload.mode === 'add') {
state.subscribedMedias.push(action.payload.name);
state.subscriptionInfo.push(action.payload.name);
} else {
state.subscribedMedias = state.subscribedMedias.filter(
state.subscriptionInfo = state.subscriptionInfo.filter(
(name) => name !== action.payload.name
);
}

setSubscribedMedias(state.subscribedMedias);
state.subscribedMedias = getSubscribedMedias();
setSubscribedMedias(state.subscriptionInfo);
state.subscriptionInfo = getSubscribedMedias();
break;
case 'initNewsData':
const news = action.payload.news;
@@ -93,7 +96,7 @@ export const invoke = (action: Action) => {

break;
case 'onClickLeftArrow':
if (state.viewer === 'gridView') {
if (state.mainViewerInfo.viewer === 'gridView') {
decreaseGridPage();
changeArrowStates();
} else {
@@ -103,7 +106,7 @@ export const invoke = (action: Action) => {

break;
case 'onClickRightArrow':
if (state.viewer === 'gridView') {
if (state.mainViewerInfo.viewer === 'gridView') {
increaseGridPage();
changeArrowStates();
} else {
@@ -113,7 +116,7 @@ export const invoke = (action: Action) => {

break;
case 'changeViewer':
state.viewer = action.payload.viewer;
state.mainViewerInfo.viewer = action.payload.viewer;

resetViewerStates();
changeArrowStates();
@@ -125,11 +128,11 @@ export const invoke = (action: Action) => {
};

const increaseGridPage = () => {
state.gridInfo.page = (state.gridInfo.page + 1 + GRID_PAGE_LIMIT) % GRID_PAGE_LIMIT;
state.gridViewInfo.page = (state.gridViewInfo.page + 1 + GRID_PAGE_LIMIT) % GRID_PAGE_LIMIT;
};

const decreaseGridPage = () => {
state.gridInfo.page = (state.gridInfo.page - 1 + GRID_PAGE_LIMIT) % GRID_PAGE_LIMIT;
state.gridViewInfo.page = (state.gridViewInfo.page - 1 + GRID_PAGE_LIMIT) % GRID_PAGE_LIMIT;
};

const increaseListIndex = () => {
@@ -143,13 +146,13 @@ const decreaseListIndex = () => {
};

const changeArrowStates = () => {
if (state.viewer === 'listView') {
if (state.mainViewerInfo.viewer === 'listView') {
state.arrowInfo.left = true;
state.arrowInfo.right = true;
return;
}
state.arrowInfo.left = state.gridInfo.page !== 0;
state.arrowInfo.right = state.gridInfo.page !== 3;
state.arrowInfo.left = state.gridViewInfo.page !== 0;
state.arrowInfo.right = state.gridViewInfo.page !== 3;
};

export const fetchNewsData = async (index: number = 0, category: string = '') => {
@@ -167,7 +170,7 @@ export const resetViewerStates = () => {
};

export const resetGridViewStates = () => {
state.gridInfo = {
state.gridViewInfo = {
imgs: [],
page: 0,
isHover: false,
7 changes: 6 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -74,7 +74,7 @@ type GridImg = {
alt: string;
};

type GridInfo = {
type GridViewInfo = {
imgs: GridImg[];
page: number;
isHover: boolean;
@@ -123,3 +123,8 @@ type FieldData = {
name: string;
active: boolean;
};

type MainViewerInfo = {
targetMedia: 'total' | 'subscribed';
viewer: 'listView' | 'gridView';
};