All Downloads are FREE. Search and download functionalities are using the official Maven repository.

META-INF.resources.js.context.StoreContext.tsx Maven / Gradle / Ivy

There is a newer version: 2.0.101
Show newest version
/**
 * SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
 * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
 */

import React, {createContext} from 'react';

import {useSafeReducer} from '../utils/useSafeReducer';

interface Action {
	type: 'ADD_WARNING' | null;
}

interface State {
	languageTag?: string;
	publishedToday: boolean;
	warning: boolean;
}

const ADD_WARNING = 'ADD_WARNING';

const INITIAL_STATE: State = {
	publishedToday: false,
	warning: false,
};

export const StoreDispatchContext = React.createContext>(
	() => {}
);

export const StoreStateContext = createContext(INITIAL_STATE);

function reducer(state = INITIAL_STATE, action: Action) {
	let nextState = state;

	switch (action.type) {
		case ADD_WARNING:
			nextState = state.warning ? state : {...state, warning: true};
			break;
		default:
			return state;
	}

	return nextState;
}

interface Props {
	children: React.ReactNode;
	value: object;
}

export function StoreContextProvider({children, value}: Props) {
	const [state, dispatch] = useSafeReducer(reducer, {
		...INITIAL_STATE,
		...value,
	});

	return (
		
			
				{children}
			
		
	);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy