mirror of
https://github.com/sergeyyarkov/educt-client.git
synced 2026-10-11 10:19:22 +03:00
feat: lesson updating
This commit is contained in:
parent
0dc80fd0e8
commit
3971043ad8
@ -88,7 +88,7 @@ const FilesUploader: React.FC<FilesUploaderPropsType> = ({ onChange, files, prel
|
|||||||
<FileContent name={file.name} size={file.size} />
|
<FileContent name={file.name} size={file.size} />
|
||||||
<IconButton
|
<IconButton
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setPreloaded(prev => prev.filter(file => file.size !== file.size));
|
setPreloaded(prev => prev.filter(prevFile => prevFile.size !== file.size));
|
||||||
onChange([]);
|
onChange([]);
|
||||||
}}
|
}}
|
||||||
aria-label='remove'
|
aria-label='remove'
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import * as helpers from '@educt/helpers';
|
|
||||||
import { yupResolver } from '@hookform/resolvers/yup';
|
import { yupResolver } from '@hookform/resolvers/yup';
|
||||||
import { SubmitHandler, useForm } from 'react-hook-form';
|
import { SubmitHandler, useForm } from 'react-hook-form';
|
||||||
import type { InputFields } from './LessonForm';
|
import type { InputFields } from './LessonForm';
|
||||||
@ -13,8 +12,9 @@ import { EditLessonFormSchema } from './LessonForm.validator';
|
|||||||
/**
|
/**
|
||||||
* Hooks
|
* Hooks
|
||||||
*/
|
*/
|
||||||
import { useHistory, useParams } from 'react-router-dom';
|
import { useParams } from 'react-router-dom';
|
||||||
import { ILesson } from '@educt/interfaces';
|
import { ILesson } from '@educt/interfaces';
|
||||||
|
import { useUpdateLesson } from '@educt/hooks/queries';
|
||||||
|
|
||||||
type EditLessonFormPropsType = {
|
type EditLessonFormPropsType = {
|
||||||
lesson: ILesson;
|
lesson: ILesson;
|
||||||
@ -31,17 +31,22 @@ const EditLessonForm: React.FC<EditLessonFormPropsType> = ({ lesson }) => {
|
|||||||
materials: undefined,
|
materials: undefined,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const { id: lesson_id } = useParams<{ id: string }>();
|
const { updateLesson, isLoading } = useUpdateLesson();
|
||||||
const history = useHistory();
|
const { id } = useParams<{ id: string }>();
|
||||||
|
|
||||||
const onSubmit: SubmitHandler<InputFields> = async data => {
|
const onSubmit: SubmitHandler<InputFields> = async data => {
|
||||||
const updatedFields = helpers.getDirtyFields(form.formState.dirtyFields, data);
|
await updateLesson(id, {
|
||||||
console.log(updatedFields);
|
title: data.title,
|
||||||
|
description: data.description,
|
||||||
|
video: data.video,
|
||||||
|
duration: data.duration,
|
||||||
|
materials: data.materials?.length !== 0 ? Array.from(data.materials || []) : null,
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<LessonForm
|
<LessonForm
|
||||||
isLoading={false}
|
isLoading={isLoading}
|
||||||
reactHookForm={{ ...form }}
|
reactHookForm={{ ...form }}
|
||||||
onSubmit={form.handleSubmit(onSubmit)}
|
onSubmit={form.handleSubmit(onSubmit)}
|
||||||
buttonLabel='Save'
|
buttonLabel='Save'
|
||||||
|
|||||||
@ -54,6 +54,11 @@ export function transformToFormData(data: object): FormData {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (value === null) {
|
||||||
|
formData.append(key, '');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
formData.append(key, value);
|
formData.append(key, value);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import { useLogout } from './auth/useLogout';
|
|||||||
*/
|
*/
|
||||||
import { useCreateLesson } from './lesson/useCreateLesson';
|
import { useCreateLesson } from './lesson/useCreateLesson';
|
||||||
import { useDeleteLesson } from './lesson/useDeleteLesson';
|
import { useDeleteLesson } from './lesson/useDeleteLesson';
|
||||||
|
import { useUpdateLesson } from './lesson/useUpdateLesson';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Course
|
* Course
|
||||||
@ -39,6 +40,7 @@ export {
|
|||||||
useDetachStudents,
|
useDetachStudents,
|
||||||
useCreateLesson,
|
useCreateLesson,
|
||||||
useDeleteLesson,
|
useDeleteLesson,
|
||||||
|
useUpdateLesson,
|
||||||
useCreateUser,
|
useCreateUser,
|
||||||
useDeleteUser,
|
useDeleteUser,
|
||||||
useUpdateUser,
|
useUpdateUser,
|
||||||
|
|||||||
42
src/hooks/queries/lesson/useUpdateLesson.ts
Normal file
42
src/hooks/queries/lesson/useUpdateLesson.ts
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import { useToast } from '@chakra-ui/react';
|
||||||
|
import useAsync from '@educt/hooks/useAsync';
|
||||||
|
import { ILesson } from '@educt/interfaces';
|
||||||
|
import { LessonServiceInstance } from '@educt/services';
|
||||||
|
import { UpdateLessonParamsType } from '@educt/types';
|
||||||
|
import axios from 'axios';
|
||||||
|
import { useErrorHandler } from 'react-error-boundary';
|
||||||
|
|
||||||
|
const useUpdateLesson = () => {
|
||||||
|
const toast = useToast();
|
||||||
|
const handleError = useErrorHandler();
|
||||||
|
|
||||||
|
const updateLesson = async (id: string, data: UpdateLessonParamsType) => {
|
||||||
|
try {
|
||||||
|
const result = await LessonServiceInstance.update(id, data);
|
||||||
|
toast({ title: 'Lesson updated', status: 'info' });
|
||||||
|
return result.data;
|
||||||
|
} catch (error) {
|
||||||
|
if (axios.isAxiosError(error)) {
|
||||||
|
if (error.response) {
|
||||||
|
switch (error.response.status) {
|
||||||
|
default:
|
||||||
|
toast({ title: `${error.message}`, status: 'error' });
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
handleError(error);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
handleError(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const { execute, ...state } = useAsync<ILesson, Parameters<typeof updateLesson>>(updateLesson);
|
||||||
|
|
||||||
|
return { updateLesson: execute, ...state };
|
||||||
|
};
|
||||||
|
|
||||||
|
export { useUpdateLesson };
|
||||||
@ -2,7 +2,7 @@ import { AxiosInstance } from 'axios';
|
|||||||
import * as helpers from '@educt/helpers';
|
import * as helpers from '@educt/helpers';
|
||||||
import { IApiRespose, ILesson } from '@educt/interfaces';
|
import { IApiRespose, ILesson } from '@educt/interfaces';
|
||||||
import { ApiServiceInstance } from './ApiService';
|
import { ApiServiceInstance } from './ApiService';
|
||||||
import type { CreateLessonParamsType, LessonProgress } from '@educt/types';
|
import type { CreateLessonParamsType, LessonProgress, UpdateLessonParamsType } from '@educt/types';
|
||||||
|
|
||||||
class LessonService {
|
class LessonService {
|
||||||
public api: AxiosInstance;
|
public api: AxiosInstance;
|
||||||
@ -65,6 +65,18 @@ class LessonService {
|
|||||||
return result.data;
|
return result.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update lesson by id
|
||||||
|
*
|
||||||
|
* @param id Lesson ud
|
||||||
|
* @param data New data
|
||||||
|
* @returns Updated lesson
|
||||||
|
*/
|
||||||
|
public async update(id: string, data: UpdateLessonParamsType): Promise<IApiRespose<ILesson>> {
|
||||||
|
const result = await this.api.patch(`/v1/lessons/${id}`, helpers.transformToFormData(data));
|
||||||
|
return result.data;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save order of lessons
|
* Save order of lessons
|
||||||
*
|
*
|
||||||
|
|||||||
9
src/types/index.d.ts
vendored
9
src/types/index.d.ts
vendored
@ -169,3 +169,12 @@ export type CreateLessonParamsType = {
|
|||||||
video: File;
|
video: File;
|
||||||
materials?: File[] | undefined;
|
materials?: File[] | undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type UpdateLessonParamsType = {
|
||||||
|
title?: string | undefined;
|
||||||
|
description?: string | undefined;
|
||||||
|
duration?: string | undefined;
|
||||||
|
course_id?: string | undefined;
|
||||||
|
video?: File | undefined;
|
||||||
|
materials?: File[] | undefined | null;
|
||||||
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user