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 13be7df
Show file tree
Hide file tree
Showing 27 changed files with 337 additions and 307 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
Binary file not shown.
2 changes: 1 addition & 1 deletion app/subscriber/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"sheetjs": "file:packages/xlsx-0.20.1.tgz",
"styled-components": "6.1.11",
"stylis": "4.3.2",
"tno-core": "./tno-core-0.1.143.tgz"
"tno-core": "./tno-core-0.1.144.tgz"
},
"devDependencies": {
"@testing-library/jest-dom": "6.4.5",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IContentTonePoolModel, ITonePoolModel } from 'tno-core';

import { defaultTonePool } from './defaultTonePool';

export const defaultContentTonePool: IContentTonePoolModel = {
...(defaultTonePool as ITonePoolModel),
value: -99,
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ 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 { 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 +30,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 +44,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 +59,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 +100,23 @@ 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 {
}
},
[addMyTonePool],
);

const handleAddUpdateContent = React.useCallback(
async (values: IReportForm, row: IReportInstanceContentForm) => {
try {
Expand All @@ -143,6 +127,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 +228,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 +313,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 +362,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 13be7df

Please sign in to comment.