Quickstart
This example will quickly connect your react application to a Trixta space using this package in conjunction with trixta-js-core
Setup CRA Template
yarn create react-app --template cra-template-rb my-example-app
cd my-example-appInstall trixta-js packages
yarn add @trixtateam/trixta-js-core @trixtateam/trixta-js-rjsf @trixtateam/phoenix-to-redux @rjsf/core @rjsf/utils @rjsf/validator-ajv6Generate Login Page Example
yarn generateFollow the prompt instructions and create component under Pages directory
Replace file contents with below code. src\app\pages\LoginPage
/**
*
* LoginPage
*
*/
import {
ReservedTrixtaRoles
} from '@trixtateam/trixta-js-core';
import {
TrixtaActionComponent,
TrixtaLoginWidget,
TrixtaReactionComponent,
} from '@trixtateam/trixta-js-rjsf';
import { PageWrapper } from '../../components/PageWrapper';
interface Props {}
export function LoginPage(props: Props) {
return (
<PageWrapper>
<div
style={{
margin: '10px',
padding: '10px',
backgroundColor: '#dce6ea',
}}
>
<TrixtaLoginWidget />
</div>
<div
style={{
margin: '10px',
padding: '10px',
}}
>
<TrixtaActionComponent
actionName="example"
roleName={ReservedTrixtaRoles.EVERYONE_AUTHED}
/>
</div>
<div
style={{
margin: '10px',
padding: '10px',
}}
>
<TrixtaReactionComponent
reactionName="welcome"
requestForEffect
roleName={ReservedTrixtaRoles.EVERYONE_AUTHED}
/>
</div>
</PageWrapper>
);
}Setup Reducer
Replace file contents with below code.
src\store\reducers.ts
/**
* Combine all reducers in this file and export the combined reducers.
*/
import { combineReducers } from '@reduxjs/toolkit';
import { phoenixReducer } from '@trixtateam/phoenix-to-redux';
import { trixtaReducer } from '@trixtateam/trixta-js-core';
import { InjectedReducersType } from 'utils/types/injector-typings';
/**
* Merges the main reducer with the router state and dynamically injected reducers
*/
export function createReducer(injectedReducers: InjectedReducersType = {}) {
// Initially we don't have any injectedReducers, so returning identity function to avoid the error
if (Object.keys(injectedReducers).length === 0) {
return state => state;
} else {
return combineReducers({
trixta: trixtaReducer,
phoenix: phoenixReducer,
...injectedReducers,
});
}
}Setup Trixta Saga
Create and copy saga file src\app\sagas\app.ts
import { setupTrixtaSaga } from '@trixtateam/trixta-js-core';
import { fork } from 'redux-saga/effects';
export default function* rootSaga() {
yield fork(setupTrixtaSaga);
}Configure Store
Replace file contents with below code.
src\store\configureStore.ts
import {
configureStore,
getDefaultMiddleware,
StoreEnhancer,
} from '@reduxjs/toolkit';
import { createInjectorsEnhancer } from 'redux-injectors';
import { createPhoenixChannelMiddleware } from '@trixtateam/phoenix-to-redux';
import createSagaMiddleware from 'redux-saga';
import { createReducer } from './reducers';
export function configureAppStore() {
const reduxSagaMonitorOptions = {};
// Makes redux connected to phoenix channels
const phoenixChannelMiddleWare = createPhoenixChannelMiddleware();
const sagaMiddleware = createSagaMiddleware(reduxSagaMonitorOptions);
const { run: runSaga } = sagaMiddleware;
// Create the store with saga middleware
const middlewares = [sagaMiddleware, phoenixChannelMiddleWare];
const enhancers = [
createInjectorsEnhancer({
createReducer,
runSaga,
}),
] as StoreEnhancer[];
const store = configureStore({
reducer: createReducer(),
middleware: [
...getDefaultMiddleware({
thunk: false,
immutableCheck: {
ignore: ['socket', 'channel', 'trixta', 'phoenix', 'router'],
},
serializableCheck: false,
}),
...middlewares,
],
devTools:
/* istanbul ignore next line */
process.env.NODE_ENV !== 'production' ||
process.env.PUBLIC_URL.length > 0,
enhancers,
});
return store;
}Run Example
Select the light theme
Navigate to http://localhost:3000/login
Follow the prompts and see what happens.
Last updated