Skip to content

Commit

Permalink
Added badges to the code snippet button
Browse files Browse the repository at this point in the history
Signed-off-by: the-pro <[email protected]>
  • Loading branch information
the-pro committed Aug 17, 2021
1 parent 7bfefb3 commit 4d01b9f
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 25 deletions.
3 changes: 1 addition & 2 deletions data/datacreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ async function createChallenges () {
mitigationUrl: showMitigations ? mitigationUrl : null,
disabledEnv: config.get('challenges.safetyOverride') ? null : effectiveDisabledEnv,
tutorialOrder: tutorial ? tutorial.order : null,
findIt: false,
fixIt: false
codingChallengeStatus: 0
})
} catch (err) {
logger.error(`Could not insert Challenge ${name}: ${err.message}`)
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/app/Models/challenge.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ export interface Challenge {
tutorialOrder?: number
hasTutorial?: boolean
hasSnippet?: boolean
findIt?: boolean
fixIt?: boolean
codingChallengeStatus?: number
}
10 changes: 8 additions & 2 deletions frontend/src/app/score-board/score-board.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,15 @@
(click)="showCodeSnippet(challenge.key,challenge.name)"
[matTooltip]="('INFO_VULNERABLE_CODE_SNIPPET' | translate)"
matTooltipPosition="above"
[class] = "generateClass(challenge)"
class='code-snippet-button'
[color]="generateColor(challenge)"
>
<mat-icon
[matBadge]="generateBadge(challenge)"
matBadgeSize="medium"
matBadgePosition="right"
matBadgeColor="warn"
>
<mat-icon>
code
</mat-icon>
</button>
Expand Down
9 changes: 0 additions & 9 deletions frontend/src/app/score-board/score-board.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,3 @@ mat-table {
margin-left: 15px;
}

.dark-green {
background-color: rgb(104, 159, 56) !important;
margin-left: 8px;
}

.green {
background-color: rgb(98, 126, 74) !important;
margin-left: 8px;
}
26 changes: 20 additions & 6 deletions frontend/src/app/score-board/score-board.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,16 +365,30 @@ export class ScoreBoardComponent implements OnInit {
dialogRef.afterClosed().subscribe(result => {
for (const challenge of this.challenges) {
if (challenge.name === name) {
if (!challenge.findIt) { challenge.findIt = result.findIt }
if (!challenge.fixIt) { challenge.fixIt = result.fixIt }
if (challenge.codingChallengeStatus < 1) {
challenge.codingChallengeStatus = result.findIt ? 1 : challenge.codingChallengeStatus
}
if (challenge.codingChallengeStatus < 2) {
challenge.codingChallengeStatus = result.fixIt ? 2 : challenge.codingChallengeStatus
}
}
}
})
}

generateClass (challenge: Challenge) {
if (challenge.fixIt) return 'dark-green'
else if (challenge.findIt) return 'green'
else return 'code-snippet-button'
generateColor (challenge: Challenge) {
switch (challenge.codingChallengeStatus) {
case 2:
return 'accent'
case 1:
return 'accent'
default:
return 'primary'
}
}

generateBadge (challenge: Challenge) {
if (challenge.codingChallengeStatus === 0) return ''
return `${challenge.codingChallengeStatus}/2`
}
}
3 changes: 1 addition & 2 deletions models/challenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ export = (sequelize, { STRING, INTEGER, BOOLEAN, NUMBER }) => {
solved: BOOLEAN,
disabledEnv: STRING,
tutorialOrder: NUMBER,
findIt: BOOLEAN,
fixIt: BOOLEAN
codingChallengeStatus: NUMBER
})
return Challenge
}
2 changes: 1 addition & 1 deletion routes/vulnCodeFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const checkCorrectFix = () => async (req: Request<{}, {}, VerdictRequestB
}

if (selectedFix === fixData.correct) {
await models.Challenge.update({ fixIt: true }, { where: { key: key } })
await models.Challenge.update({ codingChallengeStatus: 2 }, { where: { key: key } })
res.status(200).json({
verdict: true
})
Expand Down
2 changes: 1 addition & 1 deletion routes/vulnCodeSnippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ exports.checkVulnLines = () => async (req: Request<{}, {}, VerdictRequestBody>,
const selectedLines: number[] = req.body.selectedLines
const verdict = getVerdict(vulnLines, selectedLines)
if (verdict) {
await models.Challenge.update({ findIt: true }, { where: { key: req.body.key } })
await models.Challenge.update({ codingChallengeStatus: 1 }, { where: { key: req.body.key } })
res.status(200).json({
verdict: true
})
Expand Down

0 comments on commit 4d01b9f

Please sign in to comment.