Skip to content

Commit

Permalink
feat: add timezone support in views (#723)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss committed Apr 8, 2021
1 parent 34c5767 commit 6ae4cf9
Show file tree
Hide file tree
Showing 68 changed files with 313 additions and 269 deletions.
25 changes: 12 additions & 13 deletions app/Helpers/DateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ class DateHelper
* short format like "Oct 29, 1981".
*
* @param Carbon $date
*
* @param string $timezone
* @return string
*/
public static function formatDate(Carbon $date): string
public static function formatDate(Carbon $date, string $timezone = null): string
{
if ($timezone) {
$date->setTimezone($timezone);
}

return $date->format(trans('format.date'));
}

Expand All @@ -25,19 +29,22 @@ public static function formatDate(Carbon $date): string
* short format like "Oct 29, 1981 19:32".
*
* @param Carbon $date
*
* @param string $timezone
* @return string
*/
public static function formatShortDateWithTime(Carbon $date): string
public static function formatShortDateWithTime(Carbon $date, string $timezone = null): string
{
if ($timezone) {
$date->setTimezone($timezone);
}

return $date->format(trans('format.short_date_year_time'));
}

/**
* Return the day and the month in a format like "July 29th".
*
* @param Carbon $date
*
* @return string
*/
public static function formatMonthAndDay(Carbon $date): string
Expand All @@ -49,7 +56,6 @@ public static function formatMonthAndDay(Carbon $date): string
* Return the day and the month in a format like "Jul 29".
*
* @param Carbon $date
*
* @return string
*/
public static function formatShortMonthAndDay(Carbon $date): string
Expand All @@ -61,7 +67,6 @@ public static function formatShortMonthAndDay(Carbon $date): string
* Return the day and the month in a format like "Monday (July 29th)".
*
* @param Carbon $date
*
* @return string
*/
public static function formatDayAndMonthInParenthesis(Carbon $date): string
Expand All @@ -73,7 +78,6 @@ public static function formatDayAndMonthInParenthesis(Carbon $date): string
* Return the complete date like "Monday, July 29th 2020".
*
* @param Carbon $date
*
* @return string
*/
public static function formatFullDate(Carbon $date): string
Expand All @@ -85,7 +89,6 @@ public static function formatFullDate(Carbon $date): string
* Translate the given month to a string using the locale of the app.
*
* @param Carbon $date
*
* @return string
*/
public static function translateMonth(Carbon $date): string
Expand All @@ -97,7 +100,6 @@ public static function translateMonth(Carbon $date): string
* Calculate the next occurence in the future for this date.
*
* @param Carbon $date
*
* @return Carbon
*/
public static function getNextOccurence(Carbon $date): Carbon
Expand All @@ -119,7 +121,6 @@ public static function getNextOccurence(Carbon $date): Carbon
* Get the number of days in a given year.
*
* @param Carbon $date
*
* @return int
*/
public static function getNumberOfDaysInYear(Carbon $date): int
Expand All @@ -131,7 +132,6 @@ public static function getNumberOfDaysInYear(Carbon $date): int
* Determine if the date is in the future, in the present or in the past.
*
* @param Carbon $date
*
* @return string
*/
public static function determineDateStatus(Carbon $date): string
Expand Down Expand Up @@ -179,7 +179,6 @@ public static function hoursOrDaysLeft(Carbon $date): string
*
* @param CompanyPTOPolicy $ptoPolicy
* @param string $locale
*
* @return array
*/
public static function prepareCalendar(CompanyPTOPolicy $ptoPolicy, string $locale = 'en'): array
Expand Down
7 changes: 4 additions & 3 deletions app/Helpers/WorklogHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Carbon\Carbon;
use App\Models\Company\Morale;
use App\Models\Company\Worklog;
use App\Models\Company\Employee;

class WorklogHelper
{
Expand All @@ -15,13 +16,13 @@ class WorklogHelper
* @param Carbon $date
* @param Worklog|null $worklog
* @param Morale|null $morale
*
* @param Employee $loggedEmployee
* @return array
*/
public static function getDailyInformationForEmployee(Carbon $date, Worklog $worklog = null, Morale $morale = null): array
public static function getDailyInformationForEmployee(Carbon $date, Worklog $worklog = null, Morale $morale = null, Employee $loggedEmployee): array
{
return [
'date' => DateHelper::formatShortDateWithTime($date),
'date' => DateHelper::formatShortDateWithTime($date, $loggedEmployee->timezone),
'friendly_date' => DateHelper::formatDayAndMonthInParenthesis($date),
'status' => DateHelper::determineDateStatus($date),
'worklog_parsed_content' => is_null($worklog) ? null : StringHelper::parse($worklog->content),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ class AdminAuditController extends Controller
*/
public function index(Request $request): Response
{
$company = InstanceHelper::getLoggedCompany();
$logs = $company->logs()->with('author')->paginate(15);
$loggedCompany = InstanceHelper::getLoggedCompany();
$loggedEmployee = InstanceHelper::getLoggedEmployee();

$logsCollection = AdminAuditLogViewHelper::index($logs);
$logs = $loggedCompany->logs()->with('author')->paginate(15);

$logsCollection = AdminAuditLogViewHelper::index($logs, $loggedEmployee);

return Inertia::render('Adminland/Audit/Index', [
'logs' => $logsCollection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ class AdminGeneralController extends Controller
*/
public function index(): Response
{
$company = InstanceHelper::getLoggedCompany();
$information = AdminGeneralViewHelper::information($company);
$loggedCompany = InstanceHelper::getLoggedCompany();
$loggedEmployee = InstanceHelper::getLoggedEmployee();

$information = AdminGeneralViewHelper::information($loggedCompany, $loggedEmployee);
$currencies = AdminGeneralViewHelper::currencies();

return Inertia::render('Adminland/General/Index', [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function store(Request $request, int $companyId): JsonResponse
public function show(Request $request, int $companyId, int $hardwareId)
{
$company = InstanceHelper::getLoggedCompany();
$employee = InstanceHelper::getLoggedEmployee();

try {
$hardware = Hardware::where('company_id', $company->id)
Expand All @@ -122,7 +123,7 @@ public function show(Request $request, int $companyId, int $hardwareId)
] : null,
];

$history = AdminHardwareViewHelper::history($hardware);
$history = AdminHardwareViewHelper::history($hardware, $employee);

return Inertia::render('Adminland/Hardware/Show', [
'hardware' => $information,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ class AdminUploadEmployeeController extends Controller
*/
public function index(): Response
{
$company = InstanceHelper::getLoggedCompany();
$importJobs = AdminUploadEmployeeViewHelper::index($company);
$loggedCompany = InstanceHelper::getLoggedCompany();
$loggedEmployee = InstanceHelper::getLoggedEmployee();

$importJobs = AdminUploadEmployeeViewHelper::index($loggedCompany, $loggedEmployee);

return Inertia::render('Adminland/Employee/Archives/Index', [
'notifications' => NotificationHelper::getNotifications(InstanceHelper::getLoggedEmployee()),
'notifications' => NotificationHelper::getNotifications($loggedEmployee),
'importJobs' => $importJobs,
]);
}
Expand Down Expand Up @@ -96,6 +98,7 @@ public function store(Request $request, int $companyId): JsonResponse
public function show(Request $request, int $companyId, int $jobId)
{
$loggedCompany = InstanceHelper::getLoggedCompany();
$loggedEmployee = InstanceHelper::getLoggedEmployee();

try {
$job = ImportJob::where('company_id', $loggedCompany->id)
Expand All @@ -104,7 +107,7 @@ public function show(Request $request, int $companyId, int $jobId)
return redirect('home');
}

$details = AdminUploadEmployeeViewHelper::show($job);
$details = AdminUploadEmployeeViewHelper::show($job, $loggedEmployee);

return Inertia::render('Adminland/Employee/Archives/Show', [
'notifications' => NotificationHelper::getNotifications(InstanceHelper::getLoggedEmployee()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ class CompanyNewsController extends Controller
public function index(): Response
{
$company = InstanceHelper::getLoggedCompany();
$employee = InstanceHelper::getLoggedEmployee();

$newsCollection = CompanyNewsViewHelper::index($company);
$newsCollection = CompanyNewsViewHelper::index($company, $employee);

return Inertia::render('Company/News/Index', [
'news' => $newsCollection,
'notifications' => NotificationHelper::getNotifications(InstanceHelper::getLoggedEmployee()),
'notifications' => NotificationHelper::getNotifications($employee),
]);
}

Expand Down Expand Up @@ -62,7 +63,7 @@ public function show(Request $request, int $companyId, int $questionId)
return Inertia::render('Company/Question/Show', [
'teams' =>$teams,
'question' => $answersCollection,
'notifications' => NotificationHelper::getNotifications(InstanceHelper::getLoggedEmployee()),
'notifications' => NotificationHelper::getNotifications($employee),
'paginator' => PaginatorHelper::getData($answers),
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function show(Request $request, int $companyId, int $projectId): Response

return Inertia::render('Company/Project/Show', [
'project' => ProjectViewHelper::info($project),
'projectDetails' => ProjectViewHelper::summary($project, $company),
'projectDetails' => ProjectViewHelper::summary($project, $company, $employee),
'permissions' => ProjectViewHelper::permissions($project, $employee),
'notifications' => NotificationHelper::getNotifications(InstanceHelper::getLoggedEmployee()),
]);
Expand Down Expand Up @@ -535,7 +535,7 @@ public function createStatus(Request $request, int $companyId, int $projectId)
}

return Inertia::render('Company/Project/CreateStatus', [
'project' => ProjectViewHelper::summary($project, $company),
'project' => ProjectViewHelper::summary($project, $company, $employee),
'notifications' => NotificationHelper::getNotifications(InstanceHelper::getLoggedEmployee()),
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ProjectDecisionsController extends Controller
public function index(Request $request, int $companyId, int $projectId)
{
$company = InstanceHelper::getLoggedCompany();
$employee = InstanceHelper::getLoggedEmployee();

try {
$project = Project::where('company_id', $company->id)
Expand All @@ -45,7 +46,7 @@ public function index(Request $request, int $companyId, int $projectId)
return Inertia::render('Company/Project/Decisions/Index', [
'tab' => 'decisions',
'project' => ProjectViewHelper::info($project),
'decisions' => ProjectDecisionsViewHelper::decisions($project),
'decisions' => ProjectDecisionsViewHelper::decisions($project, $employee),
'notifications' => NotificationHelper::getNotifications(InstanceHelper::getLoggedEmployee()),
]);
}
Expand Down Expand Up @@ -146,7 +147,7 @@ public function store(Request $request, int $companyId, int $projectId): JsonRes
'data' => [
'id' => $projectDecision->id,
'title' => $projectDecision->title,
'decided_at' => DateHelper::formatDate($projectDecision->decided_at),
'decided_at' => DateHelper::formatDate($projectDecision->decided_at, $loggedEmployee->timezone),
'deciders' => $decidersCollection,
],
], 201);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ class ProjectFilesController extends Controller
*/
public function index(Request $request, int $companyId, int $projectId)
{
$company = InstanceHelper::getLoggedCompany();
$loggedCompany = InstanceHelper::getLoggedCompany();
$loggedEmployee = InstanceHelper::getLoggedEmployee();

try {
$project = Project::where('company_id', $company->id)
$project = Project::where('company_id', $loggedCompany->id)
->findOrFail($projectId);
} catch (ModelNotFoundException $e) {
return redirect('home');
Expand All @@ -45,7 +46,7 @@ public function index(Request $request, int $companyId, int $projectId)
return Inertia::render('Company/Project/Files/Index', [
'tab' => 'files',
'project' => ProjectViewHelper::info($project),
'files' => ProjectFilesViewHelper::index($project),
'files' => ProjectFilesViewHelper::index($project, $loggedEmployee),
'uploadcarePublicKey' => config('officelife.uploadcare_public_key'),
'notifications' => NotificationHelper::getNotifications(InstanceHelper::getLoggedEmployee()),
]);
Expand Down Expand Up @@ -105,7 +106,7 @@ public function store(Request $request, int $companyId, int $projectId): ?JsonRe
'employee' => $loggedEmployee,
]),
],
'created_at' => DateHelper::formatDate($file->created_at),
'created_at' => DateHelper::formatDate($file->created_at, $loggedEmployee->timezone),
],
], 200);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function store(Request $request, int $companyId, int $projectId): JsonRes
'name' => $employee->name,
'avatar' => ImageHelper::getAvatar($employee, 64),
'role' => $request->input('role'),
'added_at' => DateHelper::formatDate(Carbon::now()),
'added_at' => DateHelper::formatDate(Carbon::now(), $loggedEmployee->timezone),
'position' => (! $employee->position) ? null : [
'id' => $employee->position->id,
'title' => $employee->position->title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function show(Request $request, int $companyId, int $projectId, int $mess
return Inertia::render('Company/Project/Messages/Show', [
'tab' => 'messages',
'project' => ProjectViewHelper::info($project),
'message' => ProjectMessagesViewHelper::show($message),
'message' => ProjectMessagesViewHelper::show($message, $loggedEmployee),
'notifications' => NotificationHelper::getNotifications(InstanceHelper::getLoggedEmployee()),
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function index(Request $request, int $companyId, int $projectId)
public function show(Request $request, int $companyId, int $projectId, int $taskId)
{
$company = InstanceHelper::getLoggedCompany();
$employee = InstanceHelper::getLoggedEmployee();

try {
$project = Project::where('company_id', $company->id)
Expand All @@ -89,7 +90,7 @@ public function show(Request $request, int $companyId, int $projectId, int $task
return Inertia::render('Company/Project/Tasks/Show', [
'tab' => 'tasks',
'project' => ProjectViewHelper::info($projectTask->project),
'task' => ProjectTasksViewHelper::taskDetails($projectTask, $company),
'task' => ProjectTasksViewHelper::taskDetails($projectTask, $company, $employee),
'members' => ProjectTasksViewHelper::members($project),
'notifications' => NotificationHelper::getNotifications(InstanceHelper::getLoggedEmployee()),
]);
Expand Down Expand Up @@ -286,6 +287,7 @@ public function destroy(Request $request, int $companyId, int $projectId, int $t
public function timeTrackingEntries(Request $request, int $companyId, int $projectId, int $taskId): JsonResponse
{
$company = InstanceHelper::getLoggedCompany();
$employee = InstanceHelper::getLoggedEmployee();

try {
$project = Project::where('company_id', $company->id)
Expand All @@ -302,7 +304,7 @@ public function timeTrackingEntries(Request $request, int $companyId, int $proje
}

return response()->json([
'data' => ProjectTasksViewHelper::timeTrackingEntries($projectTask, $company),
'data' => ProjectTasksViewHelper::timeTrackingEntries($projectTask, $company, $employee),
], 200);
}

Expand Down

0 comments on commit 6ae4cf9

Please sign in to comment.