Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Feature/sc 3034 necassery channels #234

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
100 changes: 83 additions & 17 deletions src/Contexts/lesson.actions.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { editorWS } from '~/utils/socket'
import { setSections , createSection } from './section.actions'
import { setSections , createSection , fetchSection , removeSection , sortSections , addSection } from './section.actions'
import { newError } from './notifications.actions'
import { generateHash } from '~/utils/crypto'
import { saveLessonCache } from '~/utils/cache'
import { startLoading , finishLoading } from './view.actions'
import { generateLessonHash } from '~/utils/crypto'
import { loadLessonCache, saveLessonCache } from '~/utils/cache'
import { startLoading , finishLoading , setActiveSection } from './view.actions'
import { mapSection } from '~/utils/reducer'
import { isFocused } from '@edtr-io/store'






export const BOOTSTRAP = 'BOOTSTRAP'
export const BOOTSTRAP_FINISHED = 'BOOTSTRAP_FINISHED'
Expand Down Expand Up @@ -73,7 +79,8 @@ export const saveLesson = () => async ({state, dispatch}) => {
}
})

const newHash = generateHash(lesson)
const newHash = generateLessonHash(lesson)
changes.hash = newHash

const message = await editorWS.emit(
'patch',
Expand All @@ -84,13 +91,14 @@ export const saveLesson = () => async ({state, dispatch}) => {

const payload = {
hash: newHash,
// timestamp: message.updatedAt || message.insertedAt
timestamp: message.updatedAt || message.insertedAt
}

dispatch({
type: LESSON_SAVED,
payload
})

saveLessonCache({
...lesson,
...payload,
Expand All @@ -104,6 +112,8 @@ export const saveLesson = () => async ({state, dispatch}) => {

saveLessonCache({
...state.lesson,
changed: Array.from(changed),
savedHash: hash,
savedToBackend: false
})
}
Expand All @@ -117,24 +127,80 @@ export const saveLesson = () => async ({state, dispatch}) => {
* @param {string} courseId - ID of course, lesson belong to
* @param {Object} params - query params for request
*/
export const fetchLesson = (lessonId, courseId, params) => async ({dispatch}) => {
try{
export const fetchLesson = (lessonId, courseId, bootstrap) => async ({dispatch}) => {

dispatch(startLoading())

try {

const cached = loadLessonCache(lessonId) || {};
let sectionIds = [];
let cachedDataExist = false;

if(Object.keys(cached).length !== 0){
cachedDataExist = true;

dispatch({
type: SET_LESSON,
payload: cached
})

sectionIds = cached.sections
dispatch(fetchSection(...sectionIds))
}

const lesson = await editorWS.emit(
'get',
`course/${courseId}/lessons`,
lessonId,
params
lessonId
)
dispatch({
type: SET_LESSON,
lesson
})

return lesson
}catch(error){
dispatch(newError())
if(!lesson.hash){
lesson.hash = generateLessonHash(lesson)
}

if(lesson.hash !== cached.hash){
// lesson.sections = lesson.sections ||

// remove sections that are in cached lesson but not on server array
sectionIds.forEach(s => {
if(!lesson.sections.includes(s)) {
dispatch(removeSection(s))
}
})

dispatch({
type: cachedDataExist ? UPDATE_LESSON : SET_LESSON,
payload: lesson
})

saveLessonCache(lesson);

// load lessons not already loaded
dispatch(
fetchSection(...lesson.sections.filter(s => !sectionIds.includes(s)))
)

try {
// TODO: check if active section was already setted
dispatch(setActiveSection(lesson.sections[0]))
} catch (e) {
console.warn(e)
if(lesson.sections === undefined || lesson.sections.length === 0){
dispatch(createSection())
}
}

dispatch(sortSections(lesson.sections))

}
} catch (err) {
console.error(err)
dispatch(newError("Es konnten keine Daten vom Server oder aus dem Speicher geladen werden"))
}

dispatch(finishLoading())
if(bootstrap === true) dispatch({ type: BOOTSTRAP_FINISHED })
}

/**
Expand Down
10 changes: 7 additions & 3 deletions src/Contexts/lesson.reducer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { ADD_SECTION , REPLACE_ADDED_SECTION_ID } from "./section.actions"
import { invertSplice } from "~/utils/reducer"

export const lessonInitialState = {
title: ''
title: '',
changed: new Set(),
sections: []
}

/**
Expand All @@ -16,8 +18,9 @@ export function lessonReducer(state = lessonInitialState, { type, payload }) {
switch (type) {
case SET_LESSON:
return {
sections: [],
...payload,
changed: new Set()
changed: payload.changed ? new Set(payload.changed) : new Set()
}
case UPDATE_LESSON:
case LESSON_UPDATED:
Expand Down Expand Up @@ -56,9 +59,10 @@ export function lessonReducer(state = lessonInitialState, { type, payload }) {
}

case ADD_SECTION: // TODO: should be saved to local storage but not to the backend
if(state.sections.includes(payload._id)) return state
return {
...state,
sections: invertSplice(state.sections, payload.position, 0, payload.tempId)
sections: invertSplice(state.sections, payload.position, 0, payload._id)
}

case REPLACE_ADDED_SECTION_ID:
Expand Down
Loading