A React Native library for managing work shifts and work logs in Janis Commerce applications. This package provides comprehensive shift tracking functionality with offline support and seamless integration with the Staff MS (Microservice).
- π Shift Management: Open, close, and manage work shifts
- π Work Log Tracking: Track work activities with start/end times
- π± Offline Support: Continue working even without internet connection
- π Automatic Sync: Sync pending work logs when connection is restored
- π― React Context Integration: Easy state management with React Context
- π‘οΈ Authorization Control: Built-in staff authorization validation
- β‘ Performance Optimized: Uses MMKV for fast local storage
- π§ Error Handling: Comprehensive error reporting with Crashlytics integration
npm install @janiscommerce/app-tracking-shiftMake sure you have the following peer dependencies installed:
npm install @janiscommerce/app-crashlytics@>=2.1.0
npm install @janiscommerce/app-request@>=2.0.0
npm install react@>=17.0.2
npm install react-native@>=0.67.5Wrap your app with the ShiftTrackingProvider:
import React from 'react';
import {ShiftTrackingProvider} from '@janiscommerce/app-tracking-shift';
const App = () => {
return (
<ShiftTrackingProvider onError={(error) => console.error(error)}>
{/* Your app components */}
</ShiftTrackingProvider>
);
};
export default App;Access shift data and methods using the useShiftTracking hook:
import React from 'react';
import {useShiftTracking} from '@janiscommerce/app-tracking-shift';
const MyComponent = () => {
const {
shiftId,
shiftStatus,
shiftData,
workLogTypes,
currentWorkLogData,
hasStaffAuthorization,
isShiftLoading,
error,
} = useShiftTracking();
return (
<div>
<p>Shift Status: {shiftStatus}</p>
<p>Shift ID: {shiftId}</p>
{/* Your component logic */}
</div>
);
};Use the Shift class for shift operations:
import {Shift} from '@janiscommerce/app-tracking-shift';
// Open a shift
const handleOpenShift = async () => {
try {
const shiftId = await Shift.open();
console.log('Shift opened:', shiftId);
} catch (error) {
console.error('Error opening shift:', error);
}
};
// Close a shift
const handleCloseShift = async () => {
try {
const shiftId = await Shift.finish();
console.log('Shift closed:', shiftId);
} catch (error) {
console.error('Error closing shift:', error);
}
};// Open a work log
const handleOpenWorkLog = async () => {
const workLog = {
referenceId: 'task-123',
name: 'Customer Service',
type: 'work',
suggestedTime: 30, // minutes
};
try {
const workLogId = await Shift.openWorkLog(workLog);
console.log('Work log opened:', workLogId);
} catch (error) {
console.error('Error opening work log:', error);
}
};
// Finish a work log
const handleFinishWorkLog = async () => {
const workLog = {
referenceId: 'task-123',
};
try {
const workLogId = await Shift.finishWorkLog(workLog);
console.log('Work log finished:', workLogId);
} catch (error) {
console.error('Error finishing work log:', error);
}
};The main provider component that manages shift state and initialization.
Props:
children(ReactNode): Child componentsonError(function, optional): Error callback function
Returns the current shift tracking state and data.
Returns:
shiftId(string): Current shift IDshiftStatus(string): Current shift status ('opened', 'closed', 'paused')shiftData(object): Complete shift dataworkLogTypes(array): Available work log typescurrentWorkLogData(object): Current active work log datacurrentWorkLogId(string): Current work log IDhasStaffAuthorization(boolean): Staff authorization statusisShiftLoading(boolean): Loading stateerror(object): Current error state
Main class for shift and work log operations.
open()
- Opens a new work shift
- Returns:
Promise<string>- Shift ID
finish(params?)
- Closes the current shift
- Parameters:
{ date?: string }- Optional closing date - Returns:
Promise<string>- Shift ID
openWorkLog(workLog)
- Opens a new work log
- Parameters:
{ referenceId, name, type, suggestedTime? } - Returns:
Promise<string>- Work log ID
finishWorkLog(workLog)
- Finishes the current work log
- Parameters:
{ referenceId } - Returns:
Promise<string>- Work log ID
getWorkLogs(shiftId?)
- Gets work logs for a shift
- Parameters:
shiftId(optional) - Shift ID - Returns:
Promise<Array>- Array of work logs
fetchWorklogTypes()
- Fetches available work log types
- Returns:
Promise<Array>- Array of work log types
sendPendingWorkLogs()
- Sends pending offline work logs
- Returns:
Promise<null>
deleteShiftRegisters()
- Deletes all shift-related data
- Returns:
Promise<boolean>
hasStaffAuthorization (getter)
- Returns:
boolean- Staff authorization status
hasPendingData (getter)
- Returns:
boolean- Pending offline data status
Higher-order component that provides shift tracking data to wrapped components.
import {WithShiftTracking} from '@janiscommerce/app-tracking-shift';
const MyComponent = ({shiftData}) => {
// Component logic
};
export default WithShiftTracking(MyComponent, {
pausedShiftComponent: <PausedShiftNotification />,
});The package provides predefined internal work logs:
import {INTERNAL_WORKLOGS} from '@janiscommerce/app-tracking-shift';
// Available internal work logs:
// INTERNAL_WORKLOGS.PICKING_WORK
// INTERNAL_WORKLOGS.DELIVERY_WORKThe library automatically handles offline scenarios:
- Offline Storage: Work logs are stored locally when offline
- Automatic Sync: Pending work logs are automatically synced when connection is restored
- Data Persistence: Uses MMKV for fast and reliable local storage
- Error Recovery: Graceful error handling for network issues
All errors are standardized and include:
- Descriptive error messages
- Error types for categorization
- Automatic Crashlytics reporting
- Promise rejection for proper error handling
The library uses the following storage keys (managed automatically):
shift.id- Current shift IDshift.status- Current shift statusshift.data- Complete shift dataworklog.id- Current work log IDworklog.data- Current work log dataworklogTypes.data- Work log types cachestaff.authorization- Staff authorization dataoffline.data- Offline work logs
This package is maintained by Janis Commerce. For issues and feature requests, please use the GitHub Issues.