Skip to content

Commit

Permalink
WIP101
Browse files Browse the repository at this point in the history
  • Loading branch information
dakotablair committed Sep 13, 2023
1 parent c6d3fab commit 1bdbea7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
15 changes: 14 additions & 1 deletion src/common/api/narrativeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,28 @@ const narrativeService = dynamicService({

export interface NarrativeServiceParams {
getStatus: void;
renameNarrative: { nameNew: string; narrativeRef: string };
restoreNarrative: { objId: number; version: number; wsId: number };
}

interface NarrativeServiceResults {
getStatus: unknown;
renameNarrative: unknown;
restoreNarrative: unknown;
}

export const narrativeServiceApi = baseApi.injectEndpoints({
endpoints: (builder) => ({
renameNarrative: builder.mutation<
NarrativeServiceResults['renameNarrative'],
NarrativeServiceParams['renameNarrative']
>({
query: ({ narrativeRef, nameNew }) =>
narrativeService({
method: 'NarrativeService.rename_narrative',
params: [{ narrative_ref: narrativeRef, new_name: nameNew }],
}),
}),
restoreNarrative: builder.mutation<
NarrativeServiceResults['restoreNarrative'],
NarrativeServiceParams['restoreNarrative']
Expand All @@ -42,4 +54,5 @@ export const narrativeServiceApi = baseApi.injectEndpoints({
}),
});

export const { getStatus, restoreNarrative } = narrativeServiceApi.endpoints;
export const { getStatus, renameNarrative, restoreNarrative } =
narrativeServiceApi.endpoints;
34 changes: 30 additions & 4 deletions src/features/navigator/NarrativeControl/Rename.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/* NarrativeControl/Rename */
import { FC } from 'react';
import { useForm } from 'react-hook-form';
import toast from 'react-hot-toast';
import { isKBaseBaseQueryError } from '../../../common/api/utils/common';
import { parseError } from '../../../common/api/utils/parseError';
import { renameNarrative } from '../../../common/api/narrativeService';
import { Button } from '../../../common/components';
import {
inputRegisterFactory,
Expand All @@ -9,8 +13,8 @@ import {
import { Input } from '../../../common/components/Input';
import { useAppDispatch } from '../../../common/hooks';
import { NarrativeDoc } from '../../../common/types/NarrativeDoc';
import { TODOAddLoadingState } from '../common';
import { renameNarrative } from '../navigatorSlice';
import { renameNarrative as renameAction, setLoading } from '../navigatorSlice';
import { ErrorMessage } from './common';

interface RenameValues {
narrativeRenameName: string;
Expand All @@ -20,22 +24,44 @@ export const Rename: FC<{
narrativeDoc: NarrativeDoc;
modalClose: () => void;
}> = ({ narrativeDoc, modalClose }) => {
/* hooks */
const dispatch = useAppDispatch();
const { formState, getValues, register } = useForm<RenameValues>({
defaultValues: {
narrativeRenameName: narrativeDoc.narrative_title,
},
mode: 'all',
});
const [renameTrigger] = renameNarrative.useMutation();
/* derived values */
const { access_group: wsId, obj_id: objId } = narrativeDoc;
const inputRegister = inputRegisterFactory<RenameValues>({
formState,
register,
});
/* rename narrative callback */
const renameNarrativeHandler = async () => {
const { narrativeRenameName: name } = getValues();
await TODOAddLoadingState();
dispatch(renameNarrative({ wsId: narrativeDoc.access_group, name }));
const message = `Rename ${wsId} to ${name}.`;
modalClose();
dispatch(renameAction({ wsId: narrativeDoc.access_group, name }));
try {
await renameTrigger({
narrativeRef: `${wsId}/${objId}`,
nameNew: name,
}).unwrap();
dispatch(setLoading(false));
} catch (err) {
if (!isKBaseBaseQueryError(err)) {
console.error({ err }); // eslint-disable-line no-console
toast(ErrorMessage({ err }));
return;
}
toast(ErrorMessage({ err: parseError(err) }));
dispatch(setLoading(false));
return;
}
toast(message);
};
return (
<>
Expand Down
2 changes: 2 additions & 0 deletions src/features/navigator/navigatorSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ export const navigatorSlice = createSlice({
const { name, wsId } = action.payload;
const message = `Rename ${wsId} to ${name}.`;
console.log(message); // eslint-disable-line no-console
state.synchronizedLast = Date.now();
state.synchronized = false;
},
restoreNarrative: (
state,
Expand Down

0 comments on commit 1bdbea7

Please sign in to comment.