Skip to content

Commit

Permalink
Merge pull request #290 from RMoodsTeam/258-invalidate-a-report-form-…
Browse files Browse the repository at this point in the history
…with-no-nlp-analyses-requested

258 invalidate a report form with no nlp analyses requested
  • Loading branch information
m-milek authored Dec 22, 2024
2 parents c081e81 + 67c396a commit 0c49892
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 21 deletions.
15 changes: 15 additions & 0 deletions backend/src/fetcher/feed_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl FetcherFeedRequest {
/// * Post IDs should only contain alphanumeric characters.
/// * All data sources for PostComments should have a `post_id`.
/// * No data sources for UserPosts and SubredditPosts should have a `post_id`.
/// * At least one analysis has to be requested.
#[logfn(err = "ERROR", fmt = "Failed to validate feed request: {0}")]
pub fn validate(&self) -> Result<(), FetcherError> {
// Check if there are any data sources
Expand Down Expand Up @@ -127,6 +128,13 @@ impl FetcherFeedRequest {
}
}

// Check if at least one analysis has been requested
if self.analyses.is_empty() {
return Err(FetcherError::InvalidFeedRequest(
"At least one analysis has to be requested".to_string(),
));
}

Ok(())
}
}
Expand Down Expand Up @@ -305,4 +313,11 @@ mod tests {
];
assert!(feed_request.validate().is_err());
}

#[test]
fn test_validate_feed_request_no_analyses() {
let mut feed_request = testing_request();
feed_request.analyses.clear();
assert!(feed_request.validate().is_err());
}
}
3 changes: 3 additions & 0 deletions frontend/src/routes/report/page/page.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.error {
margin-top: 6px;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ import {
} from '@mantine/core';
import { useForm } from '@mantine/form';
import { useEffect, useState } from 'react';
import { DataSource, ReportFormValidationSchema, ReportFormValues, RowWrapper } from './schema.ts';
import {
DataSource,
ReportFormValidationSchema,
ReportFormValues,
RowWrapper,
} from '../schema.ts';
import { zodResolver } from 'mantine-form-zod-resolver';
import { DataSourceTable } from './DataSourceTable.tsx';
import { transformJson } from './transformJson.ts';
import { RMoodsClient } from '../../rmoods/client/RMoodsClient.ts';
import { DataSourceTable } from '../DataSourceTable.tsx';
import { transformJson } from '../transformJson.ts';
import { RMoodsClient } from '../../../rmoods/client/RMoodsClient.ts';
import { ErrorBoundary } from 'react-error-boundary';
import { PageFallback } from '../PageFallback.tsx';
import { PageFallback } from '../../PageFallback.tsx';
import { IconLock, IconWorld } from '@tabler/icons-react';
import classes from './page.module.scss';

/**
* Report component for creating a new report.
Expand Down Expand Up @@ -202,11 +208,7 @@ const Report = () => {
/>
</Box>
{form.errors.dataSources && (
<Input.Error
style={() => ({
marginTop: '6px',
})}
>
<Input.Error className={classes.error}>
{form.errors.dataSources}
</Input.Error>
)}
Expand Down Expand Up @@ -361,6 +363,11 @@ const Report = () => {
})}
/>
</Stack>
{form.errors.analyses && ( // Display the error message
<Input.Error className={classes.error}>
{form.errors.analyses}
</Input.Error>
)}
</Stack>
</Card>

Expand Down
27 changes: 17 additions & 10 deletions frontend/src/routes/report/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@ export const FeedKindSchema = z.enum([
'postComments',
]);

export const AnalysisTypeSchema = z.object({
language: z.boolean(),
sentiment: z.boolean(),
sarcasm: z.boolean(),
spam: z.boolean(),
politics: z.boolean(),
hateSpeech: z.boolean(),
clickbait: z.boolean(),
trolling: z.boolean(),
});
export const AnalysisTypeSchema = z
.object({
language: z.boolean(),
sentiment: z.boolean(),
sarcasm: z.boolean(),
spam: z.boolean(),
politics: z.boolean(),
hateSpeech: z.boolean(),
clickbait: z.boolean(),
trolling: z.boolean(),
})
.refine(
(value) => {
return Object.values(value).some((val) => val === true);
},
{ message: 'At least 1 analysis type is required' }
);

export const FeedSortingKindSchema = z.enum([
'hot',
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/routes/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Dashboard from './dashboard/page.tsx';
import Login from './login/page.tsx';
import Root from './page.tsx';
import UserPage from './user/page.tsx';
import Report from './report/page.tsx';
import Report from './report/page/page.tsx';
import Layout from '../layouts/standard/layout/Layout.tsx';
import ProtectedRoute from './ProtectedRoute.tsx';
import DashboardLayout from '../layouts/dashboard/layout/DashboardLayout.tsx';
Expand Down

0 comments on commit 0c49892

Please sign in to comment.