Skip to content

Commit

Permalink
[frontend] Fix result on challenge flag (#1226)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomuDeuxfois committed Jul 18, 2024
1 parent 81b933f commit 7c9ca05
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 54 deletions.
6 changes: 3 additions & 3 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ steps:
MINIO_ENDPOINT: minio
MINIO_PORT: 9000
commands:
- mvn clean install -DskipTests
- mvn clean install -q -DskipTests
- cd openbas-api
- mvn test
- mvn test -q
- cd ../openbas-framework
- mvn test
- mvn test -q

- name: frontend-tests
image: node:20-alpine
Expand Down
114 changes: 63 additions & 51 deletions openbas-front/src/public/components/challenges/ChallengesPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,21 @@ const ChallengesPlayer = () => {
},
);
};

// Result
const noResult = () => {
return (currentExpectation?.inject_expectation_results?.length ?? 0) === 0 && currentResult === null;
};
const hasResult = () => {
return (currentExpectation?.inject_expectation_results?.length ?? 0) > 0 || currentResult !== null;
};
const validResult = () => {
return (currentExpectation?.inject_expectation_results?.length ?? 0) > 0;
};
const invalidResult = () => {
return (currentExpectation?.inject_expectation_results?.length ?? 0) === 0 && currentResult !== null;
};

if (exercise) {
const groupChallenges = R.groupBy(
R.path(['challenge_detail', 'challenge_category']),
Expand Down Expand Up @@ -325,7 +340,7 @@ const ChallengesPlayer = () => {
<IconButton
size="large"
color={
expectation?.inject_expectation_result
(expectation?.inject_expectation_results?.length ?? 0) > 0
? 'success'
: 'inherit'
}
Expand Down Expand Up @@ -494,22 +509,20 @@ const ChallengesPlayer = () => {
<Typography variant="h2" style={{ marginTop: 30 }}>
{t('Results')}
</Typography>
{(currentExpectation?.inject_expectation_result !== null
|| currentResult !== null) && (
{hasResult() && (
<div>
{currentExpectation?.inject_expectation_result !== null && (
{validResult() && (
<Alert severity="success">
{t('Flag is correct! It has been successfully submitted.')}
</Alert>
)}
{currentExpectation?.inject_expectation_result === null
&& currentResult !== null && (
<Alert
severity="error"
onClose={() => setCurrentResult(null)}
>
{t('Flag is not correct! Try again...')}
</Alert>
{invalidResult() && (
<Alert
severity="error"
onClose={() => setCurrentResult(null)}
>
{t('Flag is not correct! Try again...')}
</Alert>
)}
<div style={{ float: 'right', marginTop: 20 }}>
<Button onClick={handleClose} style={{ marginRight: 10 }}>
Expand All @@ -518,48 +531,47 @@ const ChallengesPlayer = () => {
</div>
</div>
)}
{currentExpectation?.inject_expectation_result === null
&& currentResult === null && (
<Form
keepDirtyOnReinitialize={true}
onSubmit={(data) => submit(currentChallenge?.challenge_id, data)
{noResult() && (
<Form
keepDirtyOnReinitialize={true}
onSubmit={(data) => submit(currentChallenge?.challenge_id, data)
}
validate={validate}
mutators={{
setValue: ([field, value], state, { changeValue }) => {
changeValue(state, field, () => value);
},
}}
>
{({ handleSubmit, submitting, errors }) => (
<form id="challengeForm" onSubmit={handleSubmit}>
<OldTextField
variant="standard"
name="challenge_value"
fullWidth={true}
label={t('Flag')}
/>
<div style={{ float: 'right', marginTop: 20 }}>
<Button
onClick={handleClose}
style={{ marginRight: 10 }}
disabled={submitting}
>
{t('Cancel')}
</Button>
<Button
color="secondary"
type="submit"
disabled={
validate={validate}
mutators={{
setValue: ([field, value], state, { changeValue }) => {
changeValue(state, field, () => value);
},
}}
>
{({ handleSubmit, submitting, errors }) => (
<form id="challengeForm" onSubmit={handleSubmit}>
<OldTextField
variant="standard"
name="challenge_value"
fullWidth={true}
label={t('Flag')}
/>
<div style={{ float: 'right', marginTop: 20 }}>
<Button
onClick={handleClose}
style={{ marginRight: 10 }}
disabled={submitting}
>
{t('Cancel')}
</Button>
<Button
color="secondary"
type="submit"
disabled={
submitting || Object.keys(errors).length > 0
}
>
{t('Submit')}
</Button>
</div>
</form>
)}
</Form>
>
{t('Submit')}
</Button>
</div>
</form>
)}
</Form>
)}
</DialogContent>
</Dialog>
Expand Down

0 comments on commit 7c9ca05

Please sign in to comment.