Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove tonepools #2273

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ 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 { Col, ContentTypeName, IContentModel, IContentTonePoolModel, ToggleButton } from 'tno-core';
import { Col, ContentTypeName, IContentModel, IContentTonePoolModel } from 'tno-core';

import { defaultContentTonePool } from '../constants/defaultContentTonePool';
import { useReportEditContext } from '../ReportEditContext';
import { ContentActions, ContentForm, UserContentForm } from './stories';
import * as styled from './styled';
Expand All @@ -32,7 +29,7 @@ export const ContentEditForm = React.forwardRef<HTMLDivElement | null, IContentE
const [{ userInfo }] = useApp();
const [, { updateReport }] = useReports();
const [, { addContent, updateContentSilent, getContent }] = useContent();
const [, { addMyTonePool }] = useTonePool();
// const [, { addMyTonePool }] = useTonePool();
const { values, onNavigate, isSubmitting, setSubmitting, setValues, activeRow, setActiveRow } =
useReportEditContext();

Expand All @@ -46,9 +43,9 @@ export const ContentEditForm = React.forwardRef<HTMLDivElement | null, IContentE
const userId = userInfo?.id ?? 0;
const [isUserSentiment, setIsUserSentiment] = React.useState(false);

const toggleSentiment = React.useCallback(() => {
setIsUserSentiment((prev) => !prev);
}, []);
// const toggleSentiment = React.useCallback(() => {
// setIsUserSentiment((prev) => !prev);
// }, []);

React.useEffect(() => {
const updatedUserFormTonePool =
Expand Down Expand Up @@ -100,22 +97,22 @@ 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 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) => {
Expand All @@ -127,24 +124,24 @@ 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);
// 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 (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
Expand Down Expand Up @@ -228,15 +225,13 @@ export const ContentEditForm = React.forwardRef<HTMLDivElement | null, IContentE
},
[
addContent,
createTonePool,
getContent,
isContentUpdated,
setActiveRow,
setSubmitting,
setValues,
updateContentSilent,
updateReport,
userId,
],
);

Expand Down Expand Up @@ -313,7 +308,7 @@ export const ContentEditForm = React.forwardRef<HTMLDivElement | null, IContentE
? ` | ${form.content.page}`
: ''}
</Col>
<ToggleButton
{/* <ToggleButton
on={<FaToggleOn />}
off={<FaToggleOff />}
onClick={toggleSentiment}
Expand All @@ -322,7 +317,7 @@ export const ContentEditForm = React.forwardRef<HTMLDivElement | null, IContentE
color="#6750a4"
label="User Tone"
value={isUserSentiment}
/>
/> */}
<Col className="sentiment">
<Sentiment
value={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ import {
Col,
ContentTypeName,
IContentModel,
IContentTonePoolModel,
Loading,
Show,
Text,
TextArea,
Wysiwyg,
} from 'tno-core';

import { defaultContentTonePool } from '../../constants/defaultContentTonePool';
import { ToneSelector } from './ToneSelector';

export interface IContentFormProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'content'> {
/** The content being edited */
content?: IContentModel;
Expand Down Expand Up @@ -65,11 +61,11 @@ export const ContentForm: React.FC<IContentFormProps> = ({
: content.summary
: content?.body,
};
const userDefaultContentTonePool = {
...defaultContentTonePool,
ownerId: userId,
name: `${userId}`,
};
// const userDefaultContentTonePool = {
// ...defaultContentTonePool,
// ownerId: userId,
// name: `${userId}`,
// };

React.useEffect(() => {
let isMounted = true;
Expand Down Expand Up @@ -97,36 +93,36 @@ export const ContentForm: React.FC<IContentFormProps> = ({

if (!content) return null;

const handleToneSelect = (value: number) => {
const updatedTonePool: IContentTonePoolModel = {
...myTonePool,
ownerId:
myTonePool?.ownerId !== undefined && myTonePool?.ownerId !== 0
? myTonePool.ownerId
: userId,
id: myTonePool?.id ?? 0,
isPublic: myTonePool?.isPublic ?? true,
name:
myTonePool?.name !== undefined && myTonePool?.name !== '' ? myTonePool?.name : `${userId}`,
value,
sortOrder: myTonePool?.sortOrder ?? 0,
isEnabled: myTonePool?.isEnabled ?? true,
description: myTonePool?.description ?? '',
};
// const handleToneSelect = (value: number) => {
// const updatedTonePool: IContentTonePoolModel = {
// ...myTonePool,
// ownerId:
// myTonePool?.ownerId !== undefined && myTonePool?.ownerId !== 0
// ? myTonePool.ownerId
// : userId,
// id: myTonePool?.id ?? 0,
// isPublic: myTonePool?.isPublic ?? true,
// name:
// myTonePool?.name !== undefined && myTonePool?.name !== '' ? myTonePool?.name : `${userId}`,
// value,
// sortOrder: myTonePool?.sortOrder ?? 0,
// isEnabled: myTonePool?.isEnabled ?? true,
// description: myTonePool?.description ?? '',
// };

if (content?.id) {
onContentChange?.({
...content,
tonePools: [...(content.tonePools || []), updatedTonePool],
});
} else {
console.error('Content ID is missing or invalid.');
}
};
// if (content?.id) {
// onContentChange?.({
// ...content,
// tonePools: [...(content.tonePools || []), updatedTonePool],
// });
// } else {
// console.error('Content ID is missing or invalid.');
// }
// };

const userContentTonePool: IContentTonePoolModel =
content?.tonePools?.find((pool: IContentTonePoolModel) => pool.ownerId === userId) ||
userDefaultContentTonePool;
// const userContentTonePool: IContentTonePoolModel =
// content?.tonePools?.find((pool: IContentTonePoolModel) => pool.ownerId === userId) ||
// userDefaultContentTonePool;

return show === 'none' ? null : (
<Col className={`edit-content${className ? ` ${className}` : ''}`} {...rest}>
Expand Down Expand Up @@ -221,9 +217,9 @@ export const ContentForm: React.FC<IContentFormProps> = ({
onContentChange?.(values);
}}
/>
{isUserSentiment && (
{/* {isUserSentiment && (
<ToneSelector onSelect={handleToneSelect} myTonePool={userContentTonePool} />
)}
)} */}
</Show>
</Col>
);
Expand Down
16 changes: 6 additions & 10 deletions libs/net/dal/Extensions/ContentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,21 +163,17 @@ public static TNOContext UpdateContext(this TNOContext context, Content original
}
});

oTonePools.Except(updated.TonePoolsManyToMany).ForEach(a =>
{
context.Entry(a).State = EntityState.Deleted;
});
updated.TonePoolsManyToMany.ForEach(a =>
{
var current = a.TonePoolId != 0 ? oTonePools.FirstOrDefault(o => o.TonePoolId == a.TonePoolId) : null;

// If no matching tone pool exists, add it to the original list
if (current == null)
{
a.TonePool ??= context.TonePools.FirstOrDefault(tp => tp.Id == a.TonePoolId);

if (a.TonePool != null &&
!original.TonePoolsManyToMany.Any(x => x.TonePool != null && x.TonePool.Id == a.TonePool.Id))
{
original.TonePoolsManyToMany.Add(a);
}
}
original.TonePoolsManyToMany.Add(a);
// If a matching tone pool is found but the values have changed, update the values
else if (current.Value != a.Value)
{
Expand Down
Loading