Skip to content

Commit

Permalink
add assignToPerson, delete tagModel
Browse files Browse the repository at this point in the history
  • Loading branch information
kaulfield23 committed Sep 26, 2023
1 parent 57de88f commit e6e3c6d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 37 deletions.
13 changes: 11 additions & 2 deletions src/features/tags/hooks/useTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { IFuture } from 'core/caching/futures';
import { loadItemIfNecessary } from 'core/caching/cacheUtils';
import { RootState } from 'core/store';
import { ZetkinTag } from 'utils/types/zetkin';
import { tagLoad, tagLoaded, tagUnassigned } from '../store';
import { tagAssigned, tagLoad, tagLoaded, tagUnassigned } from '../store';
import { useApiClient, useEnv } from 'core/hooks';

interface UseTagReturn {
assignToPerson: (personId: number, value?: string) => void;
getTag: () => IFuture<ZetkinTag>;
removeFromPerson: (personId: number) => Promise<void>;
}
Expand All @@ -17,6 +18,14 @@ export default function useTag(orgId: number, tagId: number): UseTagReturn {
const env = useEnv();
const store = useStore();

const assignToPerson = async (personId: number, value?: string) => {
const data = value ? { value } : undefined;
const tag = await apiClient.put<ZetkinTag>(
`/api/orgs/${orgId}/people/${personId}/tags/${tagId}`,
data
);
store.dispatch(tagAssigned([personId, tag]));
};
const getTag = () => {
const item = tags.tagList.items.find((item) => item.id == tagId);
return loadItemIfNecessary(item, env.store, {
Expand All @@ -33,5 +42,5 @@ export default function useTag(orgId: number, tagId: number): UseTagReturn {
);
store.dispatch(tagUnassigned([personId, tagId]));
};
return { getTag, removeFromPerson };
return { assignToPerson, getTag, removeFromPerson };
}
30 changes: 0 additions & 30 deletions src/features/tags/models/TagModel.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import {

import { IColumnType } from '.';
import TagChip from 'features/tags/components/TagManager/components/TagChip';
import TagModel from 'features/tags/models/TagModel';
import useAccessLevel from 'features/views/hooks/useAccessLevel';
import useModel from 'core/useModel';
import useTag from 'features/tags/hooks/useTag';
import ViewDataModel from 'features/views/models/ViewDataModel';
import { ZetkinObjectAccess } from 'core/api/types';
Expand Down Expand Up @@ -124,8 +122,7 @@ const Cell: FC<{
// TODO: Find a way to share a model between cells in a column
const query = useRouter().query;
const orgId = parseInt(query.orgId as string);
const model = useModel((env) => new TagModel(env, orgId, tagId));
const { getTag, removeFromPerson } = useTag(orgId, tagId);
const { assignToPerson, getTag, removeFromPerson } = useTag(orgId, tagId);
const styles = useStyles();

const [isRestricted] = useAccessLevel();
Expand All @@ -152,7 +149,7 @@ const Cell: FC<{
<Box
className={styles.ghostContainer}
onClick={() => {
model.assignToPerson(personId);
assignToPerson(personId);
}}
>
<Box className={styles.ghost}>
Expand Down

0 comments on commit e6e3c6d

Please sign in to comment.