-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
340 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { CooperationData } from '@/shared/types/validation/cooperationData.ts'; | ||
|
||
import getStore from '@/shared/Store/Store.ts'; | ||
import { setCurrentPage } from '@/shared/Store/actions.ts'; | ||
import observeStore, { selectCurrentLanguage } from '@/shared/Store/observer.ts'; | ||
import { PAGE_ID } from '@/shared/constants/pages.ts'; | ||
import isCooperationData from '@/shared/types/validation/cooperationData.ts'; | ||
import getCooperationData from '@/shared/utils/getCooperationData.ts'; | ||
import showErrorMessage from '@/shared/utils/userMessage.ts'; | ||
|
||
import CooperationPageView from '../view/CooperationPageView.ts'; | ||
|
||
class CooperationPageModel { | ||
private view: CooperationPageView; | ||
|
||
constructor(parent: HTMLDivElement) { | ||
this.view = new CooperationPageView(parent); | ||
this.init(); | ||
} | ||
|
||
private init(): void { | ||
getCooperationData() | ||
.then((data) => { | ||
if (isCooperationData(data)) { | ||
this.view.drawCooperationInfo(data); | ||
this.observeState(data); | ||
} | ||
}) | ||
.catch(showErrorMessage); | ||
getStore().dispatch(setCurrentPage(PAGE_ID.COOPERATION_PAGE)); | ||
} | ||
|
||
private observeState(data: CooperationData[]): void { | ||
observeStore(selectCurrentLanguage, () => { | ||
this.view.redrawCooperationInfo(data); | ||
}); | ||
} | ||
|
||
public getHTML(): HTMLDivElement { | ||
return this.view.getHTML(); | ||
} | ||
} | ||
|
||
export default CooperationPageModel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import type { CooperationData } from '@/shared/types/validation/cooperationData'; | ||
|
||
import getStore from '@/shared/Store/Store.ts'; | ||
import createBaseElement from '@/shared/utils/createBaseElement.ts'; | ||
|
||
import styles from './cooperationPageView.module.scss'; | ||
|
||
class CooperationPageView { | ||
private page: HTMLDivElement; | ||
|
||
private parent: HTMLDivElement; | ||
|
||
private wrapper: HTMLDivElement; | ||
|
||
constructor(parent: HTMLDivElement) { | ||
this.parent = parent; | ||
this.parent.innerHTML = ''; | ||
this.wrapper = this.createCooperationWrapper(); | ||
this.page = this.createHTML(); | ||
window.scrollTo(0, 0); | ||
} | ||
|
||
private createCooperationWrapper(): HTMLDivElement { | ||
this.wrapper = createBaseElement({ | ||
cssClasses: [styles.cooperationWrapper], | ||
tag: 'div', | ||
}); | ||
|
||
return this.wrapper; | ||
} | ||
|
||
private createDescription(description: string): HTMLParagraphElement { | ||
const descriptionElement = createBaseElement({ | ||
cssClasses: [styles.cooperationDescription], | ||
tag: 'p', | ||
}); | ||
descriptionElement.textContent = description; | ||
return descriptionElement; | ||
} | ||
|
||
private createHTML(): HTMLDivElement { | ||
this.page = createBaseElement({ | ||
cssClasses: [styles.cooperationPage], | ||
tag: 'div', | ||
}); | ||
|
||
this.page.append(this.wrapper); | ||
this.parent.append(this.page); | ||
|
||
return this.page; | ||
} | ||
|
||
private createItem(text: string): HTMLLIElement { | ||
const listItem = createBaseElement({ | ||
cssClasses: [styles.cooperationListItem], | ||
innerContent: text, | ||
tag: 'li', | ||
}); | ||
return listItem; | ||
} | ||
|
||
private createItemList(): HTMLUListElement { | ||
const itemList = createBaseElement({ | ||
cssClasses: [styles.cooperationItemList], | ||
tag: 'ul', | ||
}); | ||
return itemList; | ||
} | ||
|
||
private createSubtitle(subtitle: string): HTMLHeadingElement { | ||
const subtitleElement = createBaseElement({ | ||
cssClasses: [styles.cooperationSubtitle], | ||
tag: 'h2', | ||
}); | ||
subtitleElement.textContent = subtitle; | ||
return subtitleElement; | ||
} | ||
|
||
private createTitle(title: string): HTMLHeadingElement { | ||
const titleElement = createBaseElement({ | ||
cssClasses: [styles.cooperationTitle], | ||
tag: 'h2', | ||
}); | ||
titleElement.textContent = title; | ||
return titleElement; | ||
} | ||
|
||
public drawCooperationInfo(data: CooperationData[]): void { | ||
data.forEach((item) => { | ||
const section = createBaseElement({ | ||
cssClasses: [styles.cooperationSection], | ||
tag: 'div', | ||
}); | ||
const currentTitle = item[getStore().getState().currentLanguage].title; | ||
const currentDescription = item[getStore().getState().currentLanguage].description; | ||
const currentSubtitle = item[getStore().getState().currentLanguage].subtitle; | ||
const currentItems = item[getStore().getState().currentLanguage].items; | ||
if (currentTitle) { | ||
const title = this.createTitle(currentTitle); | ||
section.append(title); | ||
} | ||
|
||
if (currentDescription) { | ||
const title = this.createDescription(currentDescription); | ||
section.append(title); | ||
} | ||
|
||
if (currentSubtitle) { | ||
const title = this.createSubtitle(currentSubtitle); | ||
section.append(title); | ||
} | ||
|
||
if (currentItems) { | ||
const createItemList = this.createItemList(); | ||
currentItems.forEach((item) => { | ||
const listItem = this.createItem(item.text); | ||
createItemList.append(listItem); | ||
}); | ||
section.append(createItemList); | ||
} | ||
|
||
this.wrapper.append(section); | ||
}); | ||
} | ||
|
||
public getHTML(): HTMLDivElement { | ||
return this.page; | ||
} | ||
|
||
public redrawCooperationInfo(data: CooperationData[]): void { | ||
this.wrapper.innerHTML = ''; | ||
this.drawCooperationInfo(data); | ||
} | ||
} | ||
|
||
export default CooperationPageView; |
79 changes: 79 additions & 0 deletions
79
src/pages/CooperationPage/view/cooperationPageView.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
.cooperationPage { | ||
position: relative; | ||
display: block; | ||
padding: 0 var(--small-offset); | ||
animation: show 0.2s ease-out forwards; | ||
} | ||
|
||
@keyframes show { | ||
0% { | ||
opacity: 0; | ||
} | ||
|
||
100% { | ||
display: block; | ||
opacity: 1; | ||
} | ||
} | ||
|
||
.cooperationWrapper { | ||
display: flex; | ||
flex-direction: column; | ||
margin: 0 auto; | ||
max-width: 80%; | ||
gap: var(--extra-small-offset); | ||
|
||
@media (max-width: 768px) { | ||
max-width: 100%; | ||
} | ||
} | ||
|
||
.cooperationSection { | ||
display: flex; | ||
flex-direction: column; | ||
border-radius: var(--medium-br); | ||
padding: var(--small-offset); | ||
background-color: var(--steam-green-1000); | ||
gap: var(--extra-small-offset); | ||
} | ||
|
||
.cooperationTitle { | ||
font: var(--medium-font); | ||
letter-spacing: var(--one); | ||
color: var(--steam-green-800); | ||
} | ||
|
||
.cooperationSubtitle { | ||
font: var(--medium-bold-font); | ||
letter-spacing: var(--one); | ||
color: var(--noble-gray-1000); | ||
} | ||
|
||
.cooperationDescription { | ||
font: var(--regular-font); | ||
line-height: 170%; | ||
letter-spacing: var(--one); | ||
color: var(--noble-gray-800); | ||
} | ||
|
||
.cooperationItemList { | ||
display: flex; | ||
flex-direction: column; | ||
gap: var(--tiny-offset); | ||
} | ||
|
||
.cooperationListItem { | ||
position: relative; | ||
margin-left: var(--extra-small-offset); | ||
font: var(--regular-font); | ||
letter-spacing: var(--one); | ||
color: var(--noble-gray-700); | ||
|
||
&::after { | ||
content: '✔'; | ||
position: absolute; | ||
left: -1.7rem; | ||
top: 0; | ||
padding: 0.15rem 0.3rem; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
interface CooperationListItem { | ||
text: string; | ||
} | ||
|
||
interface CooperationItem { | ||
description?: string; | ||
items?: CooperationListItem[]; | ||
subtitle?: string; | ||
title?: string; | ||
} | ||
|
||
export interface CooperationData { | ||
en: CooperationItem; | ||
ru: CooperationItem; | ||
} | ||
|
||
const isCooperationItem = (data: unknown): data is CooperationItem => { | ||
let result = true; | ||
if (data === null || typeof data !== 'object') { | ||
result = false; | ||
return result; | ||
} | ||
|
||
if ('description' in data && typeof data.description === 'string') { | ||
result = true; | ||
} | ||
|
||
if ('title' in data && typeof data.title === 'string') { | ||
result = true; | ||
} | ||
|
||
if ('subtitle' in data && typeof data.subtitle === 'string') { | ||
result = true; | ||
} | ||
|
||
if ('items' in data && Array.isArray(data.items)) { | ||
data.items.forEach((item: CooperationListItem) => { | ||
result = 'text' in item && typeof item.text === 'string'; | ||
}); | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
const isCooperationData = (data: unknown): data is CooperationData[] => { | ||
let result = true; | ||
if (!Array.isArray(data)) { | ||
return false; | ||
} | ||
data.forEach((item: CooperationData) => { | ||
if ('en' in item && 'ru' in item) { | ||
result = isCooperationItem(item.en) && isCooperationItem(item.ru); | ||
} else { | ||
result = false; | ||
} | ||
}); | ||
|
||
return result; | ||
}; | ||
|
||
export default isCooperationData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const COOPERATION_URL = 'https://raw.githubusercontent.com/stardustmeg/greenshop-db/main/cooperation/cooperation.json'; | ||
|
||
const getCooperationData = async (): Promise<unknown> => { | ||
const response = await fetch(COOPERATION_URL); | ||
const data: unknown = await response.json(); | ||
return data; | ||
}; | ||
|
||
export default getCooperationData; |