Skip to content

Commit a0dd8fa

Browse files
committed
feat: added dirty state to Context and Provider
1 parent 79cffeb commit a0dd8fa

File tree

7 files changed

+71
-12
lines changed

7 files changed

+71
-12
lines changed

src/components/statementWizard/selectors/PrivacySelector.tsx renamed to src/components/statementWizard/selectors/DELETEPrivacySelector.tsx

File renamed without changes.

src/components/statementWizard/steps/PrivacyStep.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import StepContainer from '../StepContainer';
3-
import { PrivacySelector } from '../selectors/PrivacySelector';
3+
import { PrivacySelector } from '../selectors/DELETEPrivacySelector';
44

55
interface PrivacyStepProps {
66
isPublic: boolean;

src/components/statements/StatementItem.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,19 @@ const StatementItem: React.FC<StatementItemProps> = ({
9292
<Button
9393
variant='ghost'
9494
size='sm'
95-
onClick={() => onTogglePublic(statement.id)}
96-
className={`${
97-
statement.isPublic ? ' text-green-500' : ' text-red-700'
98-
} hover:bg-opacity-75 rounded-md px-3 py-2`}
95+
onClick={() => {
96+
console.log('Privacy toggle clicked for statement:', statement.id);
97+
onTogglePublic(statement.id);
98+
}}
99+
className={`rounded-md px-3 py-2 transition-colors ${
100+
statement.isPublic
101+
? 'bg-green-100 text-green-700 hover:bg-green-200'
102+
: 'bg-red-100 text-red-700 hover:bg-red-200'
103+
}`}
99104
>
100-
{/* {statement.isPublic ? <Eye size={16} /> : <EyeOff size={16} />} */}
101105
{statement.isPublic ? <MailPlus size={16} /> : <MailX size={16} />}
102106
</Button>
107+
103108
<div className='flex flex-1 items-center space-x-2'>
104109
{/* Subject */}
105110
<div

src/components/statements/StatementList.tsx

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,30 @@ const StatementList: React.FC<{ username: string }> = ({ username }) => {
8787
updateEntry(updatedStatement);
8888
};
8989

90+
const handleTogglePublic = (statementId: string) => {
91+
const stmt = entries.find((s) => s.id === statementId);
92+
if (!stmt) return;
93+
const updated = { ...stmt, isPublic: !stmt.isPublic };
94+
setData({ type: 'UPDATE_ENTRY', payload: updated });
95+
updateEntry(updated);
96+
};
97+
98+
const handleSave = (statementId: string) => {
99+
// Find the statement from the entries
100+
const stmt = entries.find((s) => s.id === statementId);
101+
if (!stmt) return;
102+
103+
// Call the API to persist the updated statement
104+
updateEntry(stmt)
105+
.then(() => {
106+
console.log(`Statement ${statementId} saved successfully.`);
107+
// Optionally, show a success message or update local state further.
108+
})
109+
.catch((error) => {
110+
console.error(`Error saving statement ${statementId}:`, error);
111+
});
112+
};
113+
90114
const handlePresetQuestionSelect = (presetQuestion: SetQuestion) => {
91115
setSelectedPresetQuestion(presetQuestion);
92116
setIsWizardOpen(true);
@@ -140,6 +164,20 @@ const StatementList: React.FC<{ username: string }> = ({ username }) => {
140164
}
141165
};
142166

167+
const handlePartUpdate = (
168+
statementId: string,
169+
part: 'subject' | 'verb' | 'object',
170+
value: string
171+
) => {
172+
const updatedEntries = entries.map((entry) =>
173+
entry.id === statementId
174+
? { ...entry, atoms: { ...entry.atoms, [part]: value } }
175+
: entry
176+
);
177+
// Update only context
178+
setData({ type: 'SET_ENTRIES', payload: updatedEntries });
179+
};
180+
143181
const handleResetClick = (statementId: string) => {
144182
const statementToReset = entries.find((s) => s.id === statementId);
145183
if (statementToReset && statementToReset.presetId) {
@@ -233,10 +271,10 @@ const StatementList: React.FC<{ username: string }> = ({ username }) => {
233271
isEditing={statement.id === editingStatementId}
234272
editingPart={null}
235273
onPartClick={handlePartClick}
236-
onPartUpdate={() => {}}
237-
onSave={() => {}}
274+
onPartUpdate={handlePartUpdate}
275+
onSave={handleSave}
238276
onDelete={handleDeleteClick}
239-
onTogglePublic={() => {}}
277+
onTogglePublic={handleTogglePublic}
240278
onEditClick={handleEditClick}
241279
onAddAction={handleAddAction}
242280
onEditAction={handleEditAction}

src/context/EntriesContext.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export type EntriesAction =
1818
| { type: 'SET_ENTRIES'; payload: Entry[] }
1919
| { type: 'ADD_ENTRY'; payload: Entry }
2020
| { type: 'UPDATE_ENTRY'; payload: Entry }
21-
| { type: 'DELETE_ENTRY'; payload: string };
21+
| { type: 'DELETE_ENTRY'; payload: string }
22+
| { type: 'MARK_ENTRY_SAVED'; payload: string };
2223

2324
export const EntriesContext = createContext<EntriesContextType | undefined>(
2425
undefined

src/context/EntriesProvider.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,33 @@ const EntriesReducer = (
1919
case 'SET_ENTRIES':
2020
return { ...data, entries: action.payload };
2121
case 'ADD_ENTRY':
22-
return { ...data, entries: [...data.entries, action.payload] };
22+
return {
23+
...data,
24+
entries: [...data.entries, { ...action.payload, dirty: true }],
25+
};
2326
case 'UPDATE_ENTRY':
2427
return {
2528
...data,
2629
entries: data.entries.map((entry) =>
27-
entry.id === action.payload.id ? action.payload : entry
30+
entry.id === action.payload.id
31+
? { ...action.payload, dirty: true }
32+
: entry
2833
),
2934
};
3035
case 'DELETE_ENTRY':
3136
return {
3237
...data,
3338
entries: data.entries.filter((entry) => entry.id !== action.payload),
3439
};
40+
case 'MARK_ENTRY_SAVED': {
41+
// action.payload is the entry id that has been saved successfully
42+
return {
43+
...data,
44+
entries: data.entries.map((entry) =>
45+
entry.id === action.payload ? { ...entry, dirty: false } : entry
46+
),
47+
};
48+
}
3549
default:
3650
return data;
3751
}

types/entries.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface Entry {
2222
category: string;
2323
presetId?: string;
2424
isResolved?: boolean;
25+
dirty?: boolean; // Indicates if the entry has been edited but not saved
2526
}
2627

2728
export interface Category {

0 commit comments

Comments
 (0)