Skip to content

Commit

Permalink
Code review changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
areyeslo committed Sep 5, 2024
1 parent 36a00b5 commit 70023a8
Show file tree
Hide file tree
Showing 24 changed files with 330 additions and 302 deletions.
23 changes: 17 additions & 6 deletions api/net/Areas/Subscriber/Controllers/TonePoolController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,32 @@ public TonePoolController(ITonePoolService service)

#region Endpoints
/// <summary>
/// Return an array of TonePool.
/// Find all TonePools.
/// </summary>
/// <returns></returns>
[HttpGet, HttpHead]
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<TonePoolModel>), (int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.NotModified)]
[SwaggerOperation(Tags = new[] { "TonePool" })]
[ETagCacheTableFilter("tone_pools")]
[ResponseCache(Duration = 5 * 60)]
public IActionResult FindAll()
{
return new JsonResult(_service.FindAll());
}

/// <summary>
/// Find a TonePool by 'id'.
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("{id}")]
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(TonePoolModel), (int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(ErrorResponseModel), (int)HttpStatusCode.BadRequest)]
[SwaggerOperation(Tags = new[] { "TonePool" })]
public IActionResult FindById(int id)
{
var result = _service.FindById(id) ?? throw new NoContentException();
var result = _service.FindById(id) ?? throw new NoContentException("TonePool does not exist");
return new JsonResult(new TonePoolModel(result));
}

Expand Down Expand Up @@ -95,8 +108,6 @@ public IActionResult FindByUserId(int userId)
[ProducesResponseType(typeof(TonePoolModel), (int)HttpStatusCode.Created)]
[ProducesResponseType(typeof(ErrorResponseModel), (int)HttpStatusCode.BadRequest)]
[SwaggerOperation(Tags = new[] { "TonePool" })]
[ETagCacheTableFilter("tone_pools")]
[ResponseCache(Duration = 5 * 60)]
public IActionResult Add(TonePoolModel model)
{
var tonePoolEntity = new TonePool(model.Name, model.OwnerId)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IContentTonePoolModel } from 'tno-core';

import { defaultTonePool } from './defaultTonePool';

export const defaultContentTonePool: IContentTonePoolModel = {
...defaultTonePool,
value: undefined,
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import { IContentValidationErrors } from 'features/my-reports/interfaces/IConten
import { toForm } from 'features/my-reports/utils';
import { formatDate } from 'features/utils';
import React from 'react';
import { FaToggleOff, FaToggleOn } from 'react-icons/fa';
import { toast } from 'react-toastify';

Check warning on line 10 in app/subscriber/src/features/my-reports/edit/content/ContentEditForm.tsx

View workflow job for this annotation

GitHub Actions / build-app-subscriber (18.11.0)

'toast' is defined but never used
import { useApp, useContent, useReports } from 'store/hooks';
import { useTonePool } from 'store/hooks/subscriber/useTonePool';
import { useProfileStore } from 'store/slices';
import { Col, ContentTypeName, IContentModel, IContentTonePoolModel } from 'tno-core';
import { Col, ContentTypeName, IContentModel, IContentTonePoolModel, ToggleButton } from 'tno-core';

import { defaultTonePool } from '../constants/defaultTonePool';
import { defaultContentTonePool } from '../constants/defaultContentTonePool';
import { useReportEditContext } from '../ReportEditContext';
import { ContentActions, ContentForm, UserContentForm } from './stories';
import * as styled from './styled';
Expand All @@ -30,10 +31,9 @@ export interface IContentEditFormProps {
export const ContentEditForm = React.forwardRef<HTMLDivElement | null, IContentEditFormProps>(
({ disabled }, ref) => {
const [{ userInfo }] = useApp();
const [{ myTonePool, init }, { storeMyTonePool }] = useProfileStore();
const [, { updateReport }] = useReports();
const [, { addContent, updateContentSilent, getContent }] = useContent();
const [, { addMyTonePool, getMyTonePool }] = useTonePool();
const [, { addMyTonePool }] = useTonePool();
const { values, onNavigate, isSubmitting, setSubmitting, setValues, activeRow, setActiveRow } =
useReportEditContext();

Expand All @@ -45,44 +45,11 @@ export const ContentEditForm = React.forwardRef<HTMLDivElement | null, IContentE

const instance = values.instances.length ? values.instances[0] : undefined;
const userId = userInfo?.id ?? 0;
const [isUserSentiment, setIsUserSentiment] = React.useState(false);

React.useEffect(() => {
const getTonePool = async () => {
try {
if (!init.myTonePool && userId !== 0) {
const response = await getMyTonePool(userId);
if (response?.id) {
storeMyTonePool(response);
} else {
// If no valid tone pool exists, proceed to create one
await createTonePool(userId);
}
}
} catch (error) {
console.error('Error loading tone pool:', error);
} finally {
}
};

const createTonePool = async (userId: number) => {
try {
await addMyTonePool({
...defaultTonePool,
name: `${userId}`,
ownerId: userId,
});
const newTonePool = await getMyTonePool(userId);
storeMyTonePool(newTonePool);
} catch (error) {
console.error('Error creating tone pool:', error);
} finally {
}
};

if (myTonePool.id === 0) {
getTonePool();
}
}, [getMyTonePool, addMyTonePool, myTonePool.id, userId, init.myTonePool, storeMyTonePool]);
const toggleSentiment = React.useCallback(() => {
setIsUserSentiment((prev) => !prev);
}, []);

React.useEffect(() => {
const updatedUserFormTonePool =
Expand All @@ -93,6 +60,7 @@ export const ContentEditForm = React.forwardRef<HTMLDivElement | null, IContentE

React.useEffect(() => {
setForm(activeRow);
setIsUserSentiment(false);
}, [activeRow]);

const validate = (values: IContentModel) => {
Expand Down Expand Up @@ -133,6 +101,20 @@ export const ContentEditForm = React.forwardRef<HTMLDivElement | null, IContentE
[userId],
);

const createTonePool = React.useCallback(async (userId: number) => {
try {
const newTonePool = await addMyTonePool({
...defaultContentTonePool,
name: `${userId}`,
ownerId: userId,
});
return newTonePool;
} catch (error) {
console.error('Error creating tone pool:', error);
} finally {
}
}, []);

Check warning on line 116 in app/subscriber/src/features/my-reports/edit/content/ContentEditForm.tsx

View workflow job for this annotation

GitHub Actions / build-app-subscriber (18.11.0)

React Hook React.useCallback has a missing dependency: 'addMyTonePool'. Either include it or remove the dependency array

const handleAddUpdateContent = React.useCallback(
async (values: IReportForm, row: IReportInstanceContentForm) => {
try {
Expand All @@ -143,6 +125,25 @@ export const ContentEditForm = React.forwardRef<HTMLDivElement | null, IContentE
const err = validate(content);
if (err.hasErrors) return null;
let contentResult: IContentModel | undefined;
let userTonePool: IContentTonePoolModel =
content?.tonePools.find((tp) => tp.ownerId === userId) ?? defaultContentTonePool;
if (userTonePool.id === 0) {
const newTonePool = await createTonePool(userId);

if (newTonePool) {
userTonePool = {
...userTonePool,
id: newTonePool.id,
};
}
if (content?.tonePools) {
content.tonePools = [
...content.tonePools.filter((tp) => tp.ownerId !== userId),
userTonePool,
];
}
}
if (err.hasErrors) return null;
try {
contentResult = !content.id
? await addContent(content)
Expand Down Expand Up @@ -225,13 +226,15 @@ export const ContentEditForm = React.forwardRef<HTMLDivElement | null, IContentE
},
[
addContent,
createTonePool,
getContent,
isContentUpdated,
setActiveRow,
setSubmitting,
setValues,
updateContentSilent,
updateReport,
userId,
],
);

Expand Down Expand Up @@ -308,11 +311,21 @@ export const ContentEditForm = React.forwardRef<HTMLDivElement | null, IContentE
? ` | ${form.content.page}`
: ''}
</Col>
<ToggleButton
on={<FaToggleOn />}
off={<FaToggleOff />}
onClick={toggleSentiment}
width="25px"
height="25px"
color="#6750a4"
label="User Tone"
value={isUserSentiment}
/>
<Col className="sentiment">
<Sentiment
value={
userFormTonePool
? userFormTonePool.value
isUserSentiment
? userFormTonePool?.value
: form?.content?.tonePools && form.content.tonePools.length > 0
? form.content.tonePools[0].value
: undefined
Expand Down Expand Up @@ -347,6 +360,7 @@ export const ContentEditForm = React.forwardRef<HTMLDivElement | null, IContentE
onContentChange={(content) => {
setForm({ ...form, content });
}}
isUserSentiment={isUserSentiment}
loading={isSubmitting}
disabled={disabled}
/>
Expand Down
Loading

0 comments on commit 70023a8

Please sign in to comment.