Skip to content

Commit

Permalink
feat: render question tags (results, q/db-preview) (#110)
Browse files Browse the repository at this point in the history
* feat: render question tags (results, q/db-preview)

* chore(lint): remove unused import

* feat: add `tag` db column, conditionally render tags section

* fix: tags field may be undefined

* fix: set `tag` default to undefined

* fix: trim tags

---------

Co-authored-by: Federico Grandi <[email protected]>
  • Loading branch information
lorenzocorallo and EndBug authored Feb 13, 2024
1 parent a236da3 commit 100786a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
5 changes: 4 additions & 1 deletion scripts/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ async function getParsedSheets() {
correct: r.rispostaCorretta?.toLowerCase(),
attachments:
DriveClient.matchFileIds(r.immaginiQuesito || '') || undefined,
validated: (r.validato as string | undefined)?.toLowerCase() == 'sì'
validated: (r.validato as string | undefined)?.toLowerCase() == 'sì',
tags: r.tag
? (r.tag as string).split(';').map((s) => s.trim())
: undefined
}))

res['com'] = res['com']?.map((q, _, arr) => {
Expand Down
34 changes: 34 additions & 0 deletions src/components/Util/Question.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Question as IQuestion } from '../../utils/database'
import { StyleSheet } from '../../utils/style'
import QuestionAttachments from './QuestionAttachments'
import RenderedText from './RenderedText'
import { FaAnchor } from 'react-icons/fa'

const styles = StyleSheet.create({
question: {
Expand All @@ -21,6 +22,20 @@ const styles = StyleSheet.create({
icon: {
width: '10px',
height: '10px'
},
tagsContainer: {
borderTop: '1px solid #b1b1b1',
borderBottom: '1px solid #b1b1b1',
borderColor: 'gray',
display: 'flex',
alignItems: 'center',
marginTop: 16,
padding: '12px 0',
gap: 8
},
tags: {
margin: 0,
fontStyle: 'italic'
}
})

Expand Down Expand Up @@ -77,6 +92,25 @@ export default function Question({
</p>
)
})}

{q.tags && q.tags.length > 0 && (
<div style={styles.tagsContainer}>
<FaAnchor />
<p style={styles.tags}>{concatTags(q.tags)}</p>
</div>
)}
</div>
)
}

function concatTags(tagsArr: string[]): string {
return tagsArr
.map((tag, i) => {
const isFirstTag = i === 0
const isLastTag = i === tagsArr.length - 1
if (isFirstTag) return `${tag[0].toUpperCase()}${tag.slice(1)}; `
if (isLastTag) return `${tag}.`
return `${tag}; `
})
.join('')
}
1 change: 1 addition & 0 deletions src/components/pages/QPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default function QPreview(props: Props) {
onChange={(v) => setIsCustom(v === 'custom')}
/>
{isCustom ? <CustomQ /> : <DatabaseQ {...props} />}
<br />
</div>
)
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface Question {
correct: AnswerLetter
attachments: string[]
validated: boolean
tags?: string[]

// These are only present for COM questions
sub?: string
Expand Down

0 comments on commit 100786a

Please sign in to comment.