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

fix: Use data source defined in configurator state #1209

Merged
merged 1 commit into from
Oct 11, 2023
Merged
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
54 changes: 18 additions & 36 deletions app/configurator/configurator-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ import setWith from "lodash/setWith";
import sortBy from "lodash/sortBy";
import unset from "lodash/unset";
import { useRouter } from "next/router";
import {
Dispatch,
ReactNode,
createContext,
useContext,
useEffect,
useMemo,
} from "react";
import { Dispatch, createContext, useContext, useEffect, useMemo } from "react";
import { Client, useClient } from "urql";
import { Reducer, useImmerReducer } from "use-immer";

Expand Down Expand Up @@ -1421,21 +1414,18 @@ export const initChartStateFromLocalStorage = async (
}
};

const ConfiguratorStateProviderInternal = ({
chartId,
children,
initialState = INITIAL_STATE,
allowDefaultRedirect = true,
}: {
key: string;
chartId: string;
children?: ReactNode;
initialState?: ConfiguratorState;
allowDefaultRedirect?: boolean;
}) => {
const ConfiguratorStateProviderInternal = (
props: ConfiguratorStateProviderProps
) => {
const {
chartId,
initialState,
allowDefaultRedirect = true,
children,
} = props;
const { dataSource } = useDataSourceStore();
const initialStateWithDataSource = useMemo(() => {
return { ...initialState, dataSource };
return initialState ? initialState : { ...INITIAL_STATE, dataSource };
}, [initialState, dataSource]);
const locale = useLocale();
const stateAndDispatch = useImmerReducer(reducer, initialStateWithDataSource);
Expand Down Expand Up @@ -1489,13 +1479,6 @@ const ConfiguratorStateProviderInternal = ({
client,
]);

useEffect(() => {
dispatch({
type: "DATASOURCE_CHANGED",
value: dataSource,
});
}, [dispatch, dataSource]);

useEffect(() => {
try {
switch (state.state) {
Expand Down Expand Up @@ -1621,17 +1604,16 @@ const ConfiguratorStateProviderInternal = ({
);
};

export const ConfiguratorStateProvider = ({
chartId,
children,
initialState,
allowDefaultRedirect,
}: {
type ConfiguratorStateProviderProps = React.PropsWithChildren<{
chartId: string;
children?: ReactNode;
initialState?: ConfiguratorState;
allowDefaultRedirect?: boolean;
}) => {
}>;

export const ConfiguratorStateProvider = (
props: ConfiguratorStateProviderProps
) => {
const { chartId, initialState, allowDefaultRedirect, children } = props;
// Ensure that the state is reset by using the `chartId` as `key`
return (
<ConfiguratorStateProviderInternal
Expand Down