0

i hope everyone is doing fine. im attempting to add the opening and closing hours of a restaurant into the database using nextjs. the best way i could think of was to use useReducer(from react) in order to save state since every day has its own opening and closing time input fields. ( my thought process is in order to send all the information to the database i should store them as an array of objects

restaurantOpeningHours=[{ Monday:{openingHours:'Time'},
Tuesday:{openingHours:'Time'}, Wednesday:{openingHours:'Time'},
Thursday:{openingHours:'Time'}, Friday:{openingHours:'Time'},
Saturday:{openingHours:'Time'}, Sunday:{openingHours:'Time'} }]; restaurantClosingHours=[{ Monday:{closingHours:'Time'},
Tuesday:{closingHours:'Time'}, Wednesday:{closingHours:'Time'},
Thursday:{closingHours:'Time'}, Friday:{closingHours:'Time'},
Saturday:{closingHours:'Time'}, Sunday:{closingHours:'Time'}, }]

the goal is to put them in state using UseReducer and to send them to the database when the save button is clicked. any advice on how i can efficiently write the reducer function with its switch statements? is there any better way i should be researching?

from the tables side in sql

i have "**restaurant_Opening_Hours **"table and "**restaurant_Closing_Hours **"table as separate tables where the columns are the days except for the foreign key of the restaurant. please any advice here will be welcome too.

while trying to create the reducer file for the useReducer hook:

export const initialState = {
openingHours: [],
closingHours: [],
};

const hoursReducer = (state, action) => {
const {type, payload} = action;

switch (type) {
case "ADD_OPENING_HOURS":
return; {
...state,
openingHours:{openingHours.day.payload:{openingHours.day.time.payload }
}
case "ADD_CLOSING_HOURS":
return; {
...state,
closingHours:{closingHours.day:{closingHours.day.time.payload }
}
case "EDIT_OPENIGN_HOUR":
return; {
...state,
openingHours:{openingHours.day:{openingHours.day.time.payload }
}
case "EDIT_CLOSING_HOUR":
return {
...state,
closingHours:{closingHours.day:{closingHours.day.time.payload }
}
}
};

the code above is a failed attempt on getting the reducer to manipulate the initialState object. any help is welcome.

avariant
  • 2,234
  • 5
  • 25
  • 33

0 Answers0