Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Feature/reducers #40

Closed
wants to merge 20 commits into from
Closed

Feature/reducers #40

wants to merge 20 commits into from

Conversation

jesusrodrz
Copy link
Contributor

@jesusrodrz jesusrodrz commented Apr 7, 2021

#41

Complex state management with multiples events with MixedEvent hook

import { createMixedEvent, useEvent } from '@cobuildlab/react-simple-state';

const initalState = { showModal: false, selectedItem: '' };

const OnSelectItem = createEvent({
  shared: true,
  reducer: (value: string, state) => ({
    ..state,
    selectedItem: value,
    showModal: true,
  }),
})

const OnCloseModal = createEvent({
  shared: true,
  reducer: (value: string, state) => ({
    ..state,
    selectedItem: value,
    showModal: true,
  }),
})

const actionsEvent = createMixedEvent({ SELECT: OnSelectItem, CLOSE: OnCloseModal, },{ initialValue: initalState, });

export const View = () => {
  const state = useEvent(actionsEvent);

  return (
    <>
      <button onClick={() => actionsEvent.events.SELECT.dispatch('item-id')}>Select item</button>
      <Modal isOpen={state.showModal} onClose={() => actionsEvent.events.CLOSE.dispatch()} />
    </>
  );
};

Other example

import { createEvent, createMixedEvent, useEvent, } from "@cobuildlab/react-simple-state";

const OnCallEnd = createEvent({
  shared: true,
  reducer: (value, prevState) => {
    return { ...prevState, state: "off", showCallWidget: false, number: null };
  },
});

const OnCallStart = createEvent({
  shared: true,
  reducer: (value, prevState) => {
    return {
      ...prevState,
      state: "calling",
      showCallWidget: true,
      number: value,
    };
  },
});

const OnCallUpdate = createEvent({
  shared: true,
  reducer: (value, prevState) => {
    return {
      ...prevState,
      data: value,
    };
  },
});
const initialState = {
  state: "off", // type of state 'off' 'calling' 'on-a-call',
  showCallWidget: false,
  number: null, // phone number to call
  data: {}, // call data
};

const callState = createMixedEvent(
  {
    UPDATE: OnCallUpdate,
    END: OnCallEnd,
    START: OnCallStart,
  },
  initialState
);
export const Calls = () => {
  const state = useEvent(callState);

  return (
    <div>
      <CallWidget
        isOpen={state.showCallWidget}
        number={state.number}
        callState={state.state}
      />
      <button onClick={() => callState.events.START.dispatch("456 677 6789")}>
        Start Call
      </button>
      <button onClick={() => callState.events.END.dispatch()}>End Call</button>
    </div>
  );
};

@jesusrodrz jesusrodrz requested a review from alacret April 7, 2021 15:58
@jesusrodrz jesusrodrz marked this pull request as draft April 8, 2021 00:14
@jesusrodrz jesusrodrz marked this pull request as ready for review April 9, 2021 03:19
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant