|
|
@ -1,9 +1,7 @@ |
|
|
|
import {CustomWrapper, View} from "@tarojs/components"; |
|
|
|
import {View} from "@tarojs/components"; |
|
|
|
import {getCurrentInstance} from "@tarojs/runtime"; |
|
|
|
import {getCurrentInstance} from "@tarojs/runtime"; |
|
|
|
import {ManageApi} from "@/api/manage"; |
|
|
|
import {ManageApi} from "@/api/manage"; |
|
|
|
import React, {FC, useEffect, useState} from "react"; |
|
|
|
import React, {FC, useEffect, useState} from "react"; |
|
|
|
import Tabs, {TabList} from "@/components/tabs/tabs"; |
|
|
|
|
|
|
|
import {Category, publicApi} from "@/api/public"; |
|
|
|
|
|
|
|
import Taro from "@tarojs/taro"; |
|
|
|
import Taro from "@tarojs/taro"; |
|
|
|
import VideoCover from "@/components/videoCover/videoCover"; |
|
|
|
import VideoCover from "@/components/videoCover/videoCover"; |
|
|
|
import Empty from "@/components/empty/empty"; |
|
|
|
import Empty from "@/components/empty/empty"; |
|
|
@ -16,50 +14,24 @@ interface AddProps { |
|
|
|
|
|
|
|
|
|
|
|
const AddCur = () => { |
|
|
|
const AddCur = () => { |
|
|
|
const {id} = getCurrentInstance()?.router?.params as { id: string } |
|
|
|
const {id} = getCurrentInstance()?.router?.params as { id: string } |
|
|
|
const [category, setCategory] = useState<TabList[]>([]) |
|
|
|
const [data, setData] = useState<Curriculum[]>([]) |
|
|
|
const [categoryId, setCategoryId] = useState<number>(0) |
|
|
|
|
|
|
|
const [dataMap, setDataMap] = useState<Map<number, Curriculum[]>>(new Map()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function getCategory() { |
|
|
|
async function getData() { |
|
|
|
try { |
|
|
|
try { |
|
|
|
const data: (TabList)[] = [] |
|
|
|
const res = await ManageApi.optionalCur(Number(id)) |
|
|
|
const {categories} = await publicApi.category() |
|
|
|
if (res.length) { |
|
|
|
Object.values(categories).map(d => { |
|
|
|
setData(res) |
|
|
|
data.push(...d.map<TabList<Category>>(c => ({title: c.name, value: c.id}))) |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
setCategory(data) |
|
|
|
|
|
|
|
setCategoryId(data[0].value as number) |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function getData() { |
|
|
|
|
|
|
|
if (categoryId !== null) { |
|
|
|
|
|
|
|
const res = await ManageApi.optionalCur(Number(id), categoryId) |
|
|
|
|
|
|
|
const map = new Map(dataMap) |
|
|
|
|
|
|
|
const data = map.get(categoryId) |
|
|
|
|
|
|
|
if (!data) { |
|
|
|
|
|
|
|
map.delete(categoryId) |
|
|
|
|
|
|
|
map.set(categoryId, res) |
|
|
|
|
|
|
|
setDataMap(map) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
res.forEach(d => { |
|
|
|
|
|
|
|
const index = data.findIndex(c => c.id === d.id) |
|
|
|
|
|
|
|
if (index === -1) { |
|
|
|
|
|
|
|
data.push(d) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
data.splice(index, 1, d) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
map.delete(categoryId) |
|
|
|
|
|
|
|
map.set(categoryId, data) |
|
|
|
|
|
|
|
setDataMap(map) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
|
|
getData() |
|
|
|
|
|
|
|
}, []) |
|
|
|
|
|
|
|
|
|
|
|
const Add: FC<AddProps> = ({cur_id, name, index}) => { |
|
|
|
const Add: FC<AddProps> = ({cur_id, name, index}) => { |
|
|
|
function addCur() { |
|
|
|
function addCur() { |
|
|
|
Taro.showModal({ |
|
|
|
Taro.showModal({ |
|
|
@ -71,14 +43,9 @@ const AddCur = () => { |
|
|
|
const is_required = confirm ? 1 : 0 |
|
|
|
const is_required = confirm ? 1 : 0 |
|
|
|
Taro.showLoading() |
|
|
|
Taro.showLoading() |
|
|
|
await ManageApi.addCur({course_id: [cur_id], dep_id: [Number(id)], is_required}) |
|
|
|
await ManageApi.addCur({course_id: [cur_id], dep_id: [Number(id)], is_required}) |
|
|
|
const map = new Map(dataMap) |
|
|
|
const oldData: Curriculum[] = JSON.parse(JSON.stringify(data)) |
|
|
|
const data = map.get(categoryId!) || [] |
|
|
|
oldData.splice(index, 1) |
|
|
|
if (data) { |
|
|
|
setData(oldData) |
|
|
|
data.splice(index, 1) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
map.delete(categoryId!) |
|
|
|
|
|
|
|
map.set(categoryId!, data) |
|
|
|
|
|
|
|
setDataMap(map) |
|
|
|
|
|
|
|
Taro.showToast({title: "添加成功"}) |
|
|
|
Taro.showToast({title: "添加成功"}) |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
} |
|
|
|
} |
|
|
@ -92,22 +59,12 @@ const AddCur = () => { |
|
|
|
) |
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
|
|
getCategory().then() |
|
|
|
|
|
|
|
}, []) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
|
|
getData().then() |
|
|
|
|
|
|
|
}, [categoryId]) |
|
|
|
|
|
|
|
return ( |
|
|
|
return ( |
|
|
|
<CustomWrapper> |
|
|
|
<> |
|
|
|
<View className='bg-white'> |
|
|
|
|
|
|
|
<Tabs tabList={category} onChange={(data) => setCategoryId(data.tab?.value as number)} current={categoryId}/> |
|
|
|
|
|
|
|
</View> |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
(categoryId && dataMap.get(categoryId)?.length) ? |
|
|
|
data.length ? |
|
|
|
<View className='bg-white mt-2 py-2 flex flex-wrap'> |
|
|
|
<View className='bg-white mt-2 py-2 flex flex-wrap'> |
|
|
|
{dataMap.get(categoryId)?.map((d, index) => ( |
|
|
|
{data?.map((d, index) => ( |
|
|
|
<VideoCover |
|
|
|
<VideoCover |
|
|
|
key={d.id} |
|
|
|
key={d.id} |
|
|
|
thumb={d.thumb} |
|
|
|
thumb={d.thumb} |
|
|
@ -120,7 +77,7 @@ const AddCur = () => { |
|
|
|
</View> |
|
|
|
</View> |
|
|
|
: <Empty name='无更多课程'/> |
|
|
|
: <Empty name='无更多课程'/> |
|
|
|
} |
|
|
|
} |
|
|
|
</CustomWrapper> |
|
|
|
</> |
|
|
|
) |
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|