Skip to content

Commit

Permalink
fix: avoid circular dependency by relying on networkPolicy instead of…
Browse files Browse the repository at this point in the history
… refetching query
  • Loading branch information
Jonas-C committed Mar 7, 2025
1 parent 0aebab4 commit 365c5cf
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ interface Props {
export const EditLearningpathStepsPageContent = ({ learningpath }: Props) => {
const { t, i18n } = useTranslation();
const [selectedLearningpathStepId, setSelectedLearningpathStepId] = useState<undefined | number>(undefined);
const [createStep] = useCreateLearningpathStep(learningpath.id.toString() ?? "");
const [createStep] = useCreateLearningpathStep();
const toast = useToast();

const onSaveStep = async (values: FormValues) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const TextWrapper = styled("div", {
},
});

export const previewLearningpathQuery = gql`
const previewLearningpathQuery = gql`
query previewLearningpath($pathId: String!, $transformArgs: TransformedArticleContentInput) {
learningpath(pathId: $pathId) {
id
Expand All @@ -61,7 +61,7 @@ export const PreviewLearningpathPage = () => {
{
variables: { pathId: learningpathId ?? "" },
skip: !learningpathId,
fetchPolicy: "network-only",
fetchPolicy: "no-cache",
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ export const LearningpathStepListItem = ({
const { t, i18n } = useTranslation();
const toast = useToast();

const [updateStep] = useUpdateLearningpathStep(learningpathId.toString());
const [deleteStep] = useDeleteLearningpathStep(learningpathId.toString());
const [updateStep] = useUpdateLearningpathStep();
const [deleteStep] = useDeleteLearningpathStep();

const onSave = async (data: FormValues) => {
const transformedData = formValuesToGQLInput(data);
Expand Down
7 changes: 0 additions & 7 deletions src/containers/MyNdla/Learningpath/learningpathMutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
GQLCopyLearningpathMutation,
} from "../../../graphqlTypes";
import { learningpathFragment, learningpathStepFragment } from "./learningpathFragments";
import { previewLearningpathQuery } from "./PreviewLearningpathPage";

const deleteLearningpathMutation = gql`
mutation deleteLearningpath($id: Int!) {
Expand Down Expand Up @@ -127,15 +126,13 @@ const newLearningpathStepMutation = gql`
`;

export const useCreateLearningpathStep = (
pathId: string,
options?: MutationHookOptions<GQLNewLearningpathStepMutation, GQLNewLearningpathStepMutationVariables>,
) => {
const client = useApolloClient();
return useMutation<GQLNewLearningpathStepMutation, GQLNewLearningpathStepMutationVariables>(
newLearningpathStepMutation,
{
...options,
refetchQueries: [{ query: previewLearningpathQuery, variables: { pathId } }],
awaitRefetchQueries: true,
onCompleted: (data, methodOptions) => {
client.cache.modify({
Expand Down Expand Up @@ -165,15 +162,13 @@ const updateLearningpathStepMutation = gql`
`;

export const useUpdateLearningpathStep = (
pathId: string,
options?: MutationHookOptions<GQLUpdateLearningpathStepMutation, GQLUpdateLearningpathStepMutationVariables>,
) => {
const client = useApolloClient();
return useMutation<GQLUpdateLearningpathStepMutation, GQLUpdateLearningpathStepMutationVariables>(
updateLearningpathStepMutation,
{
...options,
refetchQueries: [{ query: previewLearningpathQuery, variables: { pathId } }],
awaitRefetchQueries: true,
onCompleted: (_data, methodOptions) => {
client.cache.modify({
Expand Down Expand Up @@ -216,7 +211,6 @@ const deleteLearningpathStepMutation = gql`
`;

export const useDeleteLearningpathStep = (
pathId: string,
options?: MutationHookOptions<GQLDeleteLearningpathStepMutation, GQLDeleteLearningpathStepMutationVariables>,
) => {
const client = useApolloClient();
Expand All @@ -231,7 +225,6 @@ export const useDeleteLearningpathStep = (
client.cache.evict({ id: normalizedId, broadcast: true });
client.cache.gc();
},
refetchQueries: [{ query: previewLearningpathQuery, variables: { pathId } }],
awaitRefetchQueries: true,
},
);
Expand Down

0 comments on commit 365c5cf

Please sign in to comment.