Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replaced item id keys with uuid ones #822

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, {useState, useRef} from 'react';
import { CTFragment } from 'layout';
import { v4 as uuidv4 } from 'uuid';
import Typography from '@material-ui/core/Typography';
import Box from '@material-ui/core/Box';
import TextField from '@material-ui/core/TextField';
Expand All @@ -24,7 +25,7 @@ let Tags = ({data, handleDelete}) => {
}

return (
<Box style={boxstyle}>
<Box style={boxstyle} data-testid="tag">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can rename the test id to include the component name like "ChapterContent-test-tag" in the case we ever have a test with lots of different components?

<Typography style={tagstyle}>{data}</Typography>
<Cancel
style={tagstyle}
Expand Down Expand Up @@ -102,9 +103,11 @@ const ChapterContent = ({
placeholder={tags.length < 5 ? "Enter tags" : ""} // tagging specific parts of the book ie. solutions
/>
<CTFragment alignItCenter>
{tags.map((data) => (
<Tags data={data} handleDelete={handleDelete} key={data} />
))}
{tags.map((data) => {
const uuid = uuidv4();
return (
<Tags data={data} handleDelete={handleDelete} key={`tag-${id}-${uuid}`} />
)})}
</CTFragment>


Expand Down
31 changes: 17 additions & 14 deletions src/screens/EPub/views/EditEPubChapter/ChapterEditor/ChapterInfo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { CTFragment } from 'layout';
import { v4 as uuidv4 } from 'uuid';
Copy link
Member

@harsh183 harsh183 Jun 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uuidv4 is totally a better name, nice pick!

import { epub } from '../../../controllers';
import { ChapterTitle } from '../../../components';
import ChapterContent from './ChapterContent';
Expand Down Expand Up @@ -59,20 +60,22 @@ function ChapterInfo({ chapter, currChIndex, dispatch }) {
onSave={onSaveTitle}
/>

{contents.map((content, index) => (
<ChapterContent
id={`ch-content-${id}-${index}`}
key={`ch-content-${id}-${index}`}
index={index}
condition={condition}
dispatch={dispatch}
content={content}
onInsert={onInsert(index)}
onRemove={onRemove(index)}
onTextChange={onTextChange(index)}
onImageChange={onImageChange(index)}
/>
))}
{contents.map((content, index) => {
const uuid = uuidv4();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small readability improvement if you change the key to ch-content-${id}-${uuidv4()} instead of having a variable you don't need the extra set of brackets and return keyword for these components. Since we only call the function once, I think not having a variable is fine in this case.

return (
<ChapterContent
id={`ch-content-${id}-${index}`}
key={`ch-content-${id}-${uuid}`}
index={index}
condition={condition}
dispatch={dispatch}
content={content}
onInsert={onInsert(index)}
onRemove={onRemove(index)}
onTextChange={onTextChange(index)}
onImageChange={onImageChange(index)}
/>
)})}

<ChapterNewContent onInsert={onInsert(contents.length)} />
</CTFragment>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable react/no-array-index-key */
import React from 'react';
import { CTFragment, CTText } from 'layout';
import { v4 as uuidv4 } from 'uuid';
import { epub } from '../../../controllers';
import { ChapterTitle } from '../../../components';
import ChapterContent from './ChapterContent';
Expand Down Expand Up @@ -66,18 +67,20 @@ function SubChapterItem({
onSave={onSaveTitle}
/>

{contents.map((content, index) => (
<ChapterContent
id={`sch-content-${id}-${index}`}
key={`sch-content-${id}-${index}`}
index={index}
content={content}
onInsert={onInsert(index)}
onRemove={onRemove(index)}
onTextChange={onTextChange(index)}
onImageChange={onImageChange(index)}
/>
))}
{contents.map((content, index) => {
const uuid = uuidv4();
return (
<ChapterContent
id={`sch-content-${id}-${index}`}
key={`sch-content-${id}-${uuid}`}
index={index}
content={content}
onInsert={onInsert(index)}
onRemove={onRemove(index)}
onTextChange={onTextChange(index)}
onImageChange={onImageChange(index)}
/>
)})}

<ChapterNewContent index={subChapter.contents.length} onInsert={onInsert(contents.length)} />
</CTFragment>
Expand Down
113 changes: 58 additions & 55 deletions src/screens/EPub/views/EditINote/INoteEditor/INoteChapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { connect } from 'dva'
import { Dialog, DialogTitle, DialogContent, DialogContentText, DialogActions} from '@material-ui/core';
import { EPubImageData } from 'entities/EPubs';
import { timestr } from 'utils';
import { v4 as uuidv4 } from 'uuid';
import { ChapterImage, ChapterText, ChapterTitle, MDEditorModal } from '../../../components';
import {epub as epubTools} from '../../../controllers';

Expand Down Expand Up @@ -246,64 +247,66 @@ function INoteChapter ({
</CTFragment>

) : (// If the chapter has elements, then iterate through all of them
chapter.contents.map((content, itemIdx) => (
<CTFragment key={itemIdx}>
<CTFragment className="item-actions">
{mergeChapterBtnElement(itemIdx)}
{splitBtnElement(itemIdx)}
{addImgElement(itemIdx)}
{addTextElement(itemIdx)}
{watchVideoElement(itemIdx)}
</CTFragment>
chapter.contents.map((content, itemIdx) => {
const uuid = uuidv4();
return (
<CTFragment key={`ch-content-${chapter.id}-${uuid}`}>
<CTFragment className="item-actions">
{mergeChapterBtnElement(itemIdx)}
{splitBtnElement(itemIdx)}
{addImgElement(itemIdx)}
{addTextElement(itemIdx)}
{watchVideoElement(itemIdx)}
</CTFragment>

{typeof content === "object" ? ( // image
<CTFragment className='img-con'>
<ChapterImage
{typeof content === "object" ? ( // image
<CTFragment className='img-con'>
<ChapterImage
id={`ch-content-${chapter.id}-${itemIdx}`}
image={content} // TODO ITEM id and ocr and alttext maybe map between item and content
enableChapterScreenshots
onChooseImage={onImageChange(itemIdx)}
onRemoveImage={onRemove(itemIdx)}
/>
<Dialog
open={dialogOpen}
onClose={handleNo}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
<DialogTitle id="alert-dialog-title">
Delete Image Block
</DialogTitle>
<DialogContent>
<DialogContentText id="alert-dialog-description">
Do you want to delete the Image Block?
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={handleNo} autoFocus>NO</Button>
<Button onClick={handleYes}>YES</Button>
</DialogActions>
</Dialog>
</CTFragment>
) : ( // text
<CTFragment className='item-text'>
<ChapterText
id={`ch-content-${chapter.id}-${itemIdx}`}
image={content} // TODO ITEM id and ocr and alttext maybe map between item and content
enableChapterScreenshots
onChooseImage={onImageChange(itemIdx)}
onRemoveImage={onRemove(itemIdx)}
text={content}
onSaveText={onTextChange(itemIdx)}
/>
<Dialog
open={dialogOpen}
onClose={handleNo}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
<DialogTitle id="alert-dialog-title">
Delete Image Block
</DialogTitle>
<DialogContent>
<DialogContentText id="alert-dialog-description">
Do you want to delete the Image Block?
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={handleNo} autoFocus>NO</Button>
<Button onClick={handleYes}>YES</Button>
</DialogActions>
</Dialog>
</CTFragment>
) : ( // text
<CTFragment className='item-text'>
<ChapterText
id={`ch-content-${chapter.id}-${itemIdx}`}
text={content}
onSaveText={onTextChange(itemIdx)}
/>
</CTFragment>
)}
{itemIdx === chapter.contents.length - 1 && (
<CTFragment className="item-actions">
{mergeChapterBtnElement(chapter.contents.length)}
{splitBtnElement(chapter.contents.length)}
{addImgElement(chapter.contents.length)}
{addTextElement(chapter.contents.length)}
{watchVideoElement(chapter.contents.length)}
</CTFragment>)}
</CTFragment>
)))}
</CTFragment>
)}
{itemIdx === chapter.contents.length - 1 && (
<CTFragment className="item-actions">
{mergeChapterBtnElement(chapter.contents.length)}
{splitBtnElement(chapter.contents.length)}
{addImgElement(chapter.contents.length)}
{addTextElement(chapter.contents.length)}
{watchVideoElement(chapter.contents.length)}
</CTFragment>)}
</CTFragment>
)}))}
</CTFragment>

{insertType !== null && (
Expand Down