Skip to content

Commit

Permalink
feat(edit-course): display list of blocks and implement deleting
Browse files Browse the repository at this point in the history
  • Loading branch information
danilych committed Feb 11, 2024
1 parent cc29ae0 commit 3ad571f
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 11 deletions.
110 changes: 110 additions & 0 deletions app/features/hidden-list-component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import clsx from 'clsx'
import { useState } from 'react'
import { Arrow, DeleteIcon, EditIcon } from 'assets/icons'

Check failure on line 3 in app/features/hidden-list-component.tsx

View workflow job for this annotation

GitHub Actions / pipeline (21.x)

'Arrow' is defined but never used

Check failure on line 3 in app/features/hidden-list-component.tsx

View workflow job for this annotation

GitHub Actions / pipeline (21.x)

'Arrow' is defined but never used
import { useDispatch } from 'react-redux'
import { ThunkDispatch } from '@reduxjs/toolkit'
import toast, { Toaster } from 'react-hot-toast'
import { removeBlock } from '~/redux/slices/course'
import { delay } from '~/widgets/helpers/delay'

interface Props {
header: string
index: number
id: string
className?: string
}

export function HiddenListComponent({ header, index, className, id }: Props) {
const [isVisibly, setVisibly] = useState(false)

const dispatch = useDispatch<ThunkDispatch<any, any, any>>()

function changeVisibly() {
setVisibly(!isVisibly)
}

const onClickRemove = async (id: string) => {
try {
let requestData = new FormData();
requestData.append('blockID', id);

const data = await dispatch(
removeBlock(requestData)
)

if (data.payload && data.payload.isError == false) {
console.log(true)

toast.success('Block was successfully deleted!')

delay(2000)

window.location.reload()
} else {
toast.error('Something went wrong!')

return
}
} catch (error) {
toast.error('Something went wrong!')
}
}

return (
<div
className={clsx(
'text-[#0F0F10] cursor-pointer relative w-full pb-3',
isVisibly
? 'border-b-2 border-[#454BE9]'
: 'border-b-2 border-[#B5B7F6]-2',
className
)}
onClick={changeVisibly}
role="presentation"
>
<div className="flex items-center justify-start">
<div className="inline-block">
<p className="inline-block text-[#454be9] text-lg font-semibold">
{index}.
</p>
<p className="inline-block pl-2 text-lg text-[#1a1a1b] font-medium leading-[30px] font-manrope">
{header}
</p>
</div>

<div className="flex flex-row gap-4 absolute right-2">
<img src={EditIcon} alt="arrow" />
<img onClick={() => onClickRemove(id)} src={DeleteIcon} alt="arrow" />
</div>
</div>
<div
className={clsx(
' duration-1400 transition-all ease-in-out',
isVisibly ? 'max-h-72' : 'max-h-0 overflow-hidden'
)}
>
<p className="font-manrope text-[#1A1A1B] leading-[30px] font-normal mt-8 text-base">
ergkmeriojgierjgioerjgiouerghjreuhgou
</p>
</div>

<Toaster
toastOptions={{
duration: 5000,
success: {
style: {
background: 'green',
color: 'white',
},
},
error: {
style: {
background: 'red',
color: 'white',
},
},
}}
/>
</div>
)
}
3 changes: 2 additions & 1 deletion app/features/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './join-us-form'
export * from './language-dropbox'
export * from './faq-question'
export * from './faq-question'
export * from './hidden-list-component'
13 changes: 13 additions & 0 deletions app/redux/slices/course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ export const updateBlock = createAsyncThunk(
}
)

export const removeBlock = createAsyncThunk(
'course/removeBlock',
async (params: any) => {
const { data } = await instance.post('/CourseBlock/Delete', params)

return data
}
)

const initialState = {
course: {
data: null,
Expand Down Expand Up @@ -61,6 +70,10 @@ const courseSlice = createSlice({
state.course.status = 'loaded'
state.course.isError = action.payload.isError
})
builder.addCase(removeBlock.fulfilled, (state, action) => {
state.course.status = 'loaded'
state.course.isError = action.payload.isError
})
},
})

Expand Down
32 changes: 24 additions & 8 deletions app/routes/edit-course.$course.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import { useEffect, useState } from 'react'
import { useForm } from 'react-hook-form'
import toast, { Toaster } from 'react-hot-toast'
import { useDispatch, useSelector } from 'react-redux'
import { HiddenListComponent } from '~/features'
import { fetchCourse, updateBlock } from '~/redux/slices/course'
import {
Header2,
Header3,
Header4,
RoundedInput,
TransparentButton,
} from '~/shared'
import { Header2, Header3, Header4, TransparentButton } from '~/shared'

Check failure on line 10 in app/routes/edit-course.$course.tsx

View workflow job for this annotation

GitHub Actions / pipeline (21.x)

'Header2' is defined but never used

Check failure on line 10 in app/routes/edit-course.$course.tsx

View workflow job for this annotation

GitHub Actions / pipeline (21.x)

'Header2' is defined but never used
import { delay } from '~/widgets/helpers/delay'
// import { delay } from '~/widgets/helpers/delay'

export default function Course() {
const params = useParams()
Expand Down Expand Up @@ -60,6 +57,10 @@ export default function Course() {
console.log(true)

toast.success('Block was successfully created!')

delay(2000)

window.location.reload()
} else {
toast.error('Something went wrong!')

Expand Down Expand Up @@ -103,7 +104,7 @@ export default function Course() {
className="mt-2 flex flex-row gap-[26px]"
>
<input
placeholder='Назва розділу'
placeholder="Назва розділу"
className="pl-9 text-[#4E4E51] w-[656px] h-[50px] bg-[#F6F6F6] rounded-[50px] outline-none text-sm font-manrope font-normal border-0"
{...register('Name', { required: 'Enter name' })}
/>
Expand All @@ -114,6 +115,21 @@ export default function Course() {
</TransparentButton>
</div>
</form>

<div className="mt-6">
{/* ts-ignore */}
{course.data.courseElementsList.map(
(element: any, index: number) => (
<div className="w-[656px] mt-4">

Check warning on line 123 in app/routes/edit-course.$course.tsx

View workflow job for this annotation

GitHub Actions / pipeline (21.x)

Missing "key" prop for element in iterator
<HiddenListComponent
id={element.id}
header={element.name}
index={index + 1}
/>
</div>
)
)}
</div>
</div>
</div>
)}
Expand Down
1 change: 0 additions & 1 deletion app/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ export * from './Typo/header-3'
export * from './Typo/header-4'
export * from './divider'
export * from './tag'

7 changes: 7 additions & 0 deletions assets/icons/delete.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions assets/icons/edit.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion assets/icons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ export { default as ProcterGamble } from './procter-gamble.svg'
export { default as Samsung } from './samsung.svg'
export { default as Volkswagen } from './volkswagen.svg'
export { default as Arrow } from './arrow.svg'
export { default as RatingStar } from './rating-star.svg'
export { default as RatingStar } from './rating-star.svg'
export { default as DeleteIcon } from './delete.svg'
export { default as EditIcon } from './edit.svg'

0 comments on commit 3ad571f

Please sign in to comment.