I'm trying to create, in a project, a controller when the user logged-in.
Basically I would to save some values, not in the db, but in some variables that I can use in the project.
In my idea, this is the actions that the user should do:
The first time that the user logged in, the user will be redirect to a form should choose some values for two variables ( and i want to save these values, that they will be used in to prepoluate some field in the project ) - POST -
The user can change these values - PUT -
Retrieve the data that are saved - GET -
So I'm trying to create a reducer page, to manage these 3 "actions" that he could do.
It's the first time that created a reducers/actions of this type, so:
import { REQUEST, SUCCESS, FAILURE } from 'app/shared/reducers/action-type.util';
export const ACTION_TYPES = {
FETCH_DATA : 'FETCH_DATA',
CREATE_DATA : 'CREATE_DATA',
UPDATE_DATA : 'UPDATE_DATA'
};
export interface PersonInfo {
info: {
role?: string | number,
environment ?: string | number
}
}
const initialState: PersonInfo = {
info: {
role: '',
environment : ''
}
}
// reducers
export default (state: PersonInfo=initialState, action): PersonInfo => {
switch( action.type ){
case REQUEST(ACTION_TYPES.FETCH_DATA):
console.log("FETCH DATA ", state)
return {
...state
};
case REQUEST(ACTION_TYPES.CREATE_DATA):
case REQUEST(ACTION_TYPES.UPDATE_DATA):
case FAILURE(ACTION_TYPES.FETCH_DATA):
case FAILURE(ACTION_TYPES.CREATE_DATA):
case FAILURE(ACTION_TYPES.UPDATE_DATA):
case SUCCESS(ACTION_TYPES.FETCH_DATA):
return {
...state
};
case SUCCESS(ACTION_TYPES.CREATE_DATA):
case SUCCESS(ACTION_TYPES.UPDATE_DATA):
return {
...state
};
default:
return state;
}
}
// now I need to create the actions, and there I have some problems..
// 3. This should be the action to fetch data and to see if the values are populated
export const getData = info => {
return {
type: ACTION_TYPES.FETCH_DATA,
payload: info
}
}
// 1. This should be the action to post data in the variables: (but I don't think it does anything useful .. I should connect my initialState to this function i believe)
export const createData = entity => {
const userInfo = {
role: entity.role,
environment: entity.environment
}
const result = ({
type: ACTION_TYPES.CREATE_DATA,
payload: userInfo
});
console.log("result ", result)
return result;
};
So my question is, how can i fix this actions/reducers to work?
Thank you
You need to treat these options just like handling state in a single component, but with the mentality that it's for your global state (hence, Redux). If your tree doesn't branch out to where component A has no way of talking to component B AND you need to access this state in either case, AND you are not changing this state constantly, I would recommend using useContext instead of Redux. This is a fundamental concept within React, and state isn't "RESTful". You are either initializing it, setting it, or reading it.