Skip to content

Commit

Permalink
разбил store на store для карты и модулей
Browse files Browse the repository at this point in the history
  • Loading branch information
semant1cs committed Nov 27, 2023
1 parent 9303056 commit 755ae1f
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 105 deletions.
3 changes: 2 additions & 1 deletion frontend/src/components/mapMenu/EmployeeMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import UserProfile from "./UIMapMenu/UserProfile/UserProfile.tsx";
import Level from "./UIMapMenu/Level/Level.tsx";
import {IUserType} from "../../types/UserType.ts";
import {observer} from "mobx-react-lite";
import moduleMenuStore from "../../store/moduleMenuStore.ts";

interface IEmployeeMap {
user?: IUserType,
Expand All @@ -16,7 +17,7 @@ const EmployeeMap: React.FC<IEmployeeMap> = observer(({user, formattedDate}) =>
return (
<div className="employee-interface">
<Coins coins={100} additionalClassname="coins"/>
<ChooseModuleWindow moduleName={mapMenuStore.currentModule?.title}/>
<ChooseModuleWindow moduleName={moduleMenuStore.currentModule?.title}/>
<UserProfile user={user} formattedDate={formattedDate}/>
<div className="geolocations">
<div className="geolocations__wrapper">
Expand Down
12 changes: 7 additions & 5 deletions frontend/src/components/mapMenu/MapMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {IUserType} from "../../types/UserType.ts";
import axios from "axios";
import EmployeeMap from "./EmployeeMap.tsx";
import SuperUserMap from "./SuperUserMap.tsx";
import superUserStore from "../../store/superUserStore.ts";
import moduleMenuStore from "../../store/moduleMenuStore.ts";

const MapMenu: React.FC = observer(() => {
const navigate = useNavigate()
Expand All @@ -24,7 +26,7 @@ const MapMenu: React.FC = observer(() => {
}
})

mapMenuStore.setAllUsers(response.data)
superUserStore.setAllUsers(response.data)


if (user) {
Expand All @@ -40,8 +42,8 @@ const MapMenu: React.FC = observer(() => {

mapMenuStore.fetchAvailableMaps().then(() => {
mapMenuStore.fetchMapById(mapMenuStore.availableMaps[0].id).then(() => {
mapMenuStore.fetchModules().then(() => {
mapMenuStore.fetchModuleById(mapMenuStore.availableModules[0].id).then(() => {
moduleMenuStore.fetchModules().then(() => {
moduleMenuStore.fetchModuleById(moduleMenuStore.availableModules[0].id).then(() => {
mapMenuStore.fetchLevels().then(() => {

}).catch(() => alert("Нет доступных уровней для данного модуля"))
Expand All @@ -53,8 +55,8 @@ const MapMenu: React.FC = observer(() => {

return (
<div>
{!user?.is_superuser
? <SuperUserMap allUsers={mapMenuStore.allUsers}/>
{user?.is_superuser
? <SuperUserMap allUsers={superUserStore.allUsers}/>
: <EmployeeMap user={user} formattedDate={formattedDate}/>
}
</div>
Expand Down
142 changes: 45 additions & 97 deletions frontend/src/store/mapMenuStore.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import {makeAutoObservable} from "mobx";
import axios from "axios";
import {IMapType} from "../types/MapType.ts";
import {IModuleType} from "../types/ModuleType.ts";
import {ILevelType} from "../types/LevelType/LevelType.ts";
import {IFetchLevelType} from "../types/LevelType/FetchLevelType.ts";
import {IUserType} from "../types/UserType.ts";
import moduleMenuStore from "./moduleMenuStore.ts";

class MapMenuStore {
mapMenu: IMapType | null = null
availableMaps: IMapType[] = []

currentMapId: string | null = null
currentModuleId: string | null = null
currentLevelId: string | null = null

modulesMap: string[] = []
availableModules: IModuleType[] = []
currentModule: IModuleType | null = null

availableLevels: ILevelType[] = []

constructor() {
Expand Down Expand Up @@ -51,13 +45,6 @@ class MapMenuStore {
})
}

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

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

setAvailableLevels(levels: IFetchLevelType[]) {
this.availableLevels = levels.map((level) => {
Expand All @@ -71,94 +58,55 @@ class MapMenuStore {
})
}

setCurrentModule(newModule: IModuleType) {
this.currentModule = newModule
}

async fetchModules() {
await axios.get("http://localhost:8000/maps/" + this.currentMapId + "/modules/")
.then((response) => this.setAvailableModules(response.data))
}

async createModule(mapId: string, title: string, previousModuleId: string, nextModuleId: string) {
axios.post("http://localhost:8000/maps/" + mapId + "/modules/", {
map_id: mapId,
title: title,
previous_module_id: previousModuleId,
next_module_id: nextModuleId,
})
}

async fetchModuleById(id: string) {
await axios.get("http://localhost:8000/maps/" + this.currentMapId + "/modules/" + id).then((response) => {
this.setCurrentModule(response.data)
this.currentModuleId = response.data.id
})
}

async fetchLevels() {
await axios.get("http://localhost:8000/maps/" + this.currentMapId + "/modules/" + this.currentModuleId + "/levels/")
await axios.get("http://localhost:8000/maps/" + this.currentMapId + "/modules/" + moduleMenuStore.currentModuleId + "/levels/")
.then((response) => this.setAvailableLevels(response.data))
}

async fetchLevelById(id: string) {
await axios.get("http://localhost:8000/maps/" + this.currentMapId + "/modules/" + this.currentModuleId + "/levels/" + id).then((response) => {
this.currentLevelId = response.data
})
}

async deleteModule(id: string) {
axios.delete("http://localhost:8000/modules/" + id)
}

updateModuleById(mapId: string, id: string, title?: string, previousModuleId?: string, nextModuleId?: string, levels?: string[]) {
axios.patch("http://localhost:8000/modules/" + id, {
id: id,
map_id: mapId,
title: title,
previous_module_id: previousModuleId,
next_module_id: nextModuleId,
levels_ids: levels
})
}

createTheoryUnit() {
axios.post("http://localhost:8000/maps/" + this.currentMapId + "/modules/" + this.currentModuleId + "/levels/" + this.currentLevelId + "/theory", {
title: "Заголовок 1",
content: "Контент"
})
}

createTaskUnit() {
axios.post("http://localhost:8000/maps/" + this.currentMapId + "/modules/" + this.currentModuleId + "/levels/" + this.currentLevelId + "/tasks", {
type: "test",
score_reward: 100,
requires_review: false
})
}

addQuestionToTaskUnit() {
axios.post("http://localhost:8000/maps/" + this.currentMapId + "/modules/" + this.currentModuleId
+ "/levels/" + this.currentLevelId + "/tasks/" + "b12de86e-fcd9-4761-89f0-b80d9583822d",
{
type: "singlechoice",
question: "Кто такой вопрос?",
possible_answers: [
{
answer: "Я",
is_correct: false
},
{
answer: "Не я",
is_correct: true
},
{
answer: "Не ты",
is_correct: false
}
]
})
}
// async fetchLevelById(id: string) {
// await axios.get("http://localhost:8000/maps/" + this.currentMapId + "/modules/" + this.currentModuleId + "/levels/" + id).then((response) => {
// this.currentLevelId = response.data
// })
// }

// createTheoryUnit() {
// axios.post("http://localhost:8000/maps/" + this.currentMapId + "/modules/" + this.currentModuleId + "/levels/" + this.currentLevelId + "/theory", {
// title: "Заголовок 1",
// content: "Контент"
// })
// }

// createTaskUnit() {
// axios.post("http://localhost:8000/maps/" + this.currentMapId + "/modules/" + this.currentModuleId + "/levels/" + this.currentLevelId + "/tasks", {
// type: "test",
// score_reward: 100,
// requires_review: false
// })
// }

// addQuestionToTaskUnit() {
// axios.post("http://localhost:8000/maps/" + this.currentMapId + "/modules/" + this.currentModuleId
// + "/levels/" + this.currentLevelId + "/tasks/" + "b12de86e-fcd9-4761-89f0-b80d9583822d",
// {
// type: "singlechoice",
// question: "Кто такой вопрос?",
// possible_answers: [
// {
// answer: "Я",
// is_correct: false
// },
// {
// answer: "Не я",
// is_correct: true
// },
// {
// answer: "Не ты",
// is_correct: false
// }
// ]
// })
// }
}

export default new MapMenuStore()
64 changes: 64 additions & 0 deletions frontend/src/store/moduleMenuStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {makeAutoObservable} from "mobx";
import {IModuleType} from "../types/ModuleType.ts";
import axios from "axios";
import mapMenuStore from "./mapMenuStore.ts";

class ModuleMenuStore {
currentModuleId: string | null = null
modulesMap: string[] = []
availableModules: IModuleType[] = []
currentModule: IModuleType | null = null
constructor() {
makeAutoObservable(this)
}

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

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

setCurrentModule(newModule: IModuleType) {
this.currentModule = newModule
}

async fetchModules() {
await axios.get("http://localhost:8000/maps/" + mapMenuStore.currentMapId + "/modules/")
.then((response) => this.setAvailableModules(response.data))
}

async createModule(mapId: string, title: string, previousModuleId: string, nextModuleId: string) {
axios.post("http://localhost:8000/maps/" + mapId + "/modules/", {
map_id: mapId,
title: title,
previous_module_id: previousModuleId,
next_module_id: nextModuleId,
})
}

async fetchModuleById(id: string) {
await axios.get("http://localhost:8000/maps/" + mapMenuStore.currentMapId + "/modules/" + id).then((response) => {
this.setCurrentModule(response.data)
this.currentModuleId = response.data.id
})
}

async deleteModule(id: string) {
axios.delete("http://localhost:8000/modules/" + id)
}

updateModuleById(mapId: string, id: string, title?: string, previousModuleId?: string, nextModuleId?: string, levels?: string[]) {
axios.patch("http://localhost:8000/modules/" + id, {
id: id,
map_id: mapId,
title: title,
previous_module_id: previousModuleId,
next_module_id: nextModuleId,
levels_ids: levels
})
}
}

export default new ModuleMenuStore()
9 changes: 7 additions & 2 deletions frontend/src/store/superUserStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ import {IUserType} from "../types/UserType.ts";


class SuperUserStore {
allUsers: IUserType[] = []

constructor() {
makeAutoObservable(this)
}

allUsers: IUserType = []

setAllUsers(fetchedUsers: IUserType[]) {
this.allUsers = fetchedUsers
}


}

export default new SuperUserStore()

0 comments on commit 755ae1f

Please sign in to comment.