Skip to content

Commit

Permalink
чистка глобального стейтменеджера
Browse files Browse the repository at this point in the history
  • Loading branch information
semant1cs committed Dec 15, 2023
1 parent 49f5a2f commit 6a6343e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 45 deletions.
49 changes: 24 additions & 25 deletions frontend/src/components/mapMenu/SuperUserMap/SuperUserMap.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
import React, {useCallback, useEffect, useState} from 'react';

import CustomButton from "../../../UIComponents/customButton/CustomButton.tsx";
import CustomInput from "../../../UIComponents/customInput/CustomInput.tsx";
import CustomAddButton from "../../../UIComponents/customAddButton/CustomAddButton.tsx";
import ModalWindow from "../../../UIComponents/modalWindow/ModalWindow.tsx";
import UsersListModalBody from "./UsersListModalBody.tsx";
import CreateUnit from "./superUserControlPanel/CreateUnit.tsx";

import {observer} from "mobx-react-lite";
import mapMenuStore from "../../../store/mapMenuStore.ts";
import superUserStore from "../../../store/superUserStore.ts";
import moduleMenuStore from "../../../store/moduleMenuStore.ts";

import moduleMenuStore from "../../../store/moduleMenuStore.ts";
import {IMapType} from "../../../types/MapType.ts";
import {IModuleType} from "../../../types/ModuleType.ts";
import CreateUnit from "./superUserControlPanel/CreateUnit.tsx";
import {ILevelType} from "../../../types/LevelType.ts";
import {map} from "mobx-state-tree/dist/types/complex-types/map";

interface ISuperUserMap {

}

const SuperUserMap: React.FC<ISuperUserMap> = observer(() => {
const SuperUserMap: React.FC = observer(() => {
const [isUsersListModalOpen, setIsUserListModalOpen] = useState<boolean>(false)

const [mapName, setMapName] = useState<string>("")
Expand All @@ -39,36 +32,46 @@ const SuperUserMap: React.FC<ISuperUserMap> = observer(() => {
const [currentLevelId, setCurrentLevelId] = useState<string>("")
const [currentLevelIndex, setCurrentLevelIndex] = useState<number>(0)


// Загрузка карт модулей
useEffect(() => {
mapMenuStore.fetchAvailableMaps()
.then(() => mapMenuStore.fetchMapById(mapMenuStore.availableMaps[currentMapIndex].id)
.then(() => moduleMenuStore.fetchModules()))
}, []);

const handleOnClickChangeIsModalOpen = useCallback(() => {
setIsUserListModalOpen(!isUsersListModalOpen)
}, [isUsersListModalOpen])

// Работа с картами

const handleOnClickOptionMap = useCallback((map: IMapType, indexMap: number) => {
setCurrentMap(map)
setCurrentMapIndex(indexMap)
mapMenuStore.fetchMapById(map.id).then()
}, [])

const handleOnClickOptionModule = useCallback((module: IModuleType, index: number) => {
console.log(module)
moduleMenuStore.selectModule(module).then(() => moduleMenuStore.changeCurrentModuleIndex(index))
}, [])

const handleOnClickChangeIsModalOpen = useCallback(() => {
setIsUserListModalOpen(!isUsersListModalOpen)
}, [isUsersListModalOpen])


const handleOnChangeMapName = useCallback((e: React.FormEvent<HTMLInputElement>) => {
setMapName(e.currentTarget.value)
}, [])


const handleOnClickCreateMap = useCallback((mapName: string) => {
mapName !== "" ? mapMenuStore.createMap(mapName).then(() => currentMap ? mapMenuStore.fetchMapById(currentMap?.id).then() : undefined) : alert("Введите название карты")
mapName !== "" ? mapMenuStore.createMap(mapName).then(() => currentMap ? mapMenuStore.fetchAvailableMaps().then() : undefined) : alert("Введите название карты")
}, [])

const handleOnClickDeleteMap = useCallback((mapId: string) => {
mapMenuStore.deleteMap(mapId).then(() => mapMenuStore.fetchAvailableMaps())
}, [])


// Работа с модулями

const handleOnClickOptionModule = useCallback((module: IModuleType, index: number) => {
console.log(module)
moduleMenuStore.selectModule(module).then(() => moduleMenuStore.changeCurrentModuleIndex(index))
}, [])

const handleOnChangeModuleName = useCallback((e: React.FormEvent<HTMLInputElement>) => {
setModuleName(e.currentTarget.value)
Expand All @@ -79,10 +82,6 @@ const SuperUserMap: React.FC<ISuperUserMap> = observer(() => {
moduleMenuStore.fetchModules().then()
}, [])

const handleOnClickDeleteMap = useCallback((mapId: string) => {
mapMenuStore.deleteMap(mapId).then(() => mapMenuStore.fetchAvailableMaps())
}, [])

const handleOnClickDeleteModule = useCallback((moduleId: string) => {
moduleMenuStore.deleteModule(moduleId).then(() => moduleMenuStore.fetchModules())
}, [])
Expand Down
20 changes: 5 additions & 15 deletions frontend/src/store/mapMenuStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ class MapMenuStore {
}

async fetchAvailableMaps() {
await axios.get("http://localhost:8000/maps/").then((response) => this.availableMaps = response.data)
await axios.get("http://localhost:8000/maps/").then((response) => this.setMaps(response.data))
}

setMaps(maps: IMapType[]) {
this.availableMaps = maps
}

async fetchMapById(id: string) {
Expand Down Expand Up @@ -111,20 +115,6 @@ class MapMenuStore {
this.newNameMap = ""
}

changeNewMapName(newName: string) {
this.newNameMap = newName
}

changeCurrentMapIndex(newIndex: number) {
this.currentMapIndex = newIndex
this.fetchMapById(this.availableMaps[this.currentMapIndex].id).then(() => moduleMenuStore.fetchModules())
}

async selectMap(newMap: IMapType) {
this.mapMenu = newMap
this.currentMapId = newMap.id
}

async deleteMap(mapId?: string) {
await axios.delete("http://localhost:8000/maps/" + mapId)
}
Expand Down
5 changes: 0 additions & 5 deletions frontend/src/store/moduleMenuStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {makeAutoObservable} from "mobx";
import {IModuleType} from "../types/ModuleType.ts";
import axios from "axios";
import mapMenuStore from "./mapMenuStore.ts";
import {v4 as uuidv4} from 'uuid';
import levelStore from "./levelStore.ts";

class ModuleMenuStore {
Expand All @@ -16,10 +15,6 @@ class ModuleMenuStore {
makeAutoObservable(this)
}

setModulesMap(newModulesMap: string[]) {
this.modulesMap = newModulesMap
}

setAvailableModules(modules: IModuleType[]) {
this.availableModules = modules
}
Expand Down

0 comments on commit 6a6343e

Please sign in to comment.