Skip to content

Commit

Permalink
conversion for redux trips too
Browse files Browse the repository at this point in the history
  • Loading branch information
dylmye committed Jul 8, 2023
1 parent abddfd8 commit 515aec6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
43 changes: 43 additions & 0 deletions src/helpers/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,46 @@ export const convertJsonStringToBase64Download = (json: string): string => {
const b64Encoded = Buffer.from(json).toString("base64");
return `data:application/json;base64,${b64Encoded}`;
};

export const convertReduxTripItem = (tripItem: TripItem): TripItem => {
const formattedTripItem: TripItem = { ...tripItem };

Object.keys(tripItem)
.filter<keyof TripItem>((k: string): k is keyof TripItem =>
tripItemTimestampKeys.includes(k as keyof TripItem)
)
.forEach((k) => {
// value is either unset, an ISO date string, or a Firestore Timestamp object
if (
!formattedTripItem[k] ||
!dayjs(formattedTripItem[k] as string).isValid()
) {
return;
}
console.log("converting", formattedTripItem);
// @ts-ignore this works stfu typescript
formattedTripItem[k] = forceDateInUserTimezone(
formattedTripItem[k] as string
).format();
});

return {
...tripItem,
startsAt: forceDateInUserTimezone(tripItem.startsAt).format(),
};
};

/**
* Conversion for trips from redux datastore
* @param trip The raw trip
*/
export const convertReduxTrip = (trip: Trip): Trip => {
return {
...trip,
startsAt: forceDateInUserTimezone(trip.startsAt!).format(),
endsAt: trip.endsAt && forceDateInUserTimezone(trip.endsAt).format(),
createdAtUtc: forceDateInUserTimezone(trip.createdAtUtc).format(),
updatedAtUtc: forceDateInUserTimezone(trip.updatedAtUtc).format(),
items: (trip.items ?? []).map(convertReduxTripItem),
};
};
6 changes: 5 additions & 1 deletion src/store/features/trips/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import TripDetails from "../../../types/TripDetails";
import Trip from "../../../types/Trip";
import {
convertJsonStringToBase64Download,
convertReduxTrip,
convertTripDocument,
convertTripItemDocuments,
} from "../../../helpers/converters";
Expand All @@ -40,6 +41,7 @@ export const useAddTrip: TripHooks["useAddTrip"] = () => {
return useCallback(
async (data) => {
if (activeProvider === "redux") {
// @TODO: data.startsAt is a dayjs non-serializable object which is a no-no for passing
dispatch(
providerRedux.actions.addTrip(data) as PayloadAction<TripDraft>
);
Expand Down Expand Up @@ -279,6 +281,8 @@ export const useGetTripById: TripHooks["useGetTripById"] = (tripId) => {

useEffect(() => {
if (activeProvider === "redux") {
// why is firestore here? because logged out people can view public
// trips :)
// firestore trip IDs are a completely different format to local trips
// so we can safely assume if no trip is returned and a firebase object
// is that the user is a) not logged in and b) trying to view a public trip.
Expand All @@ -288,7 +292,7 @@ export const useGetTripById: TripHooks["useGetTripById"] = (tripId) => {
...firestoreTripValue,
items: firestoreItemValues ?? [],
}
: (trip as Trip),
: convertReduxTrip(trip as Trip),
// @TODO: set this to true if firebase loading ONLY if there's no trip
loading: false,
});
Expand Down
18 changes: 10 additions & 8 deletions src/store/features/trips/redux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ const tripSlice = createSlice({
id: payload.id,
title: payload.title,
location: payload.location,
startsAt: payload.startsAt,
endsAt: payload.endsAt,
startsAt: dayjs(payload.startsAt).utc(true).format(),
endsAt: payload.endsAt && dayjs(payload.endsAt).utc(true).format(),
createdAtUtc: dayjs.utc().format(),
updatedAtUtc: dayjs.utc().format(),
image: payload.image,
Expand All @@ -82,8 +82,8 @@ const tripSlice = createSlice({
// be set otherwise on cloud storage trips
userId: undefined,
public: false,
startsAt: dayjs(payload.startsAt).format("YYYY-MM-DD"),
endsAt: payload.endsAt && dayjs(payload.endsAt).format("YYYY-MM-DD"),
startsAt: dayjs(payload.startsAt).utc(true).format(),
endsAt: payload.endsAt && dayjs(payload.endsAt).utc(true).format(),
updatedAtUtc: dayjs.utc().format(),
};

Expand Down Expand Up @@ -112,8 +112,10 @@ const tripSlice = createSlice({
title: filteredPayload?.title?.length
? filteredPayload.title
: getTripItemTypeLabel(filteredPayload.type),
startsAt: filteredPayload.startsAt,
endsAt: filteredPayload.endsAt,
startsAt: dayjs(filteredPayload.startsAt).utc(true).format(),
endsAt:
filteredPayload.endsAt &&
dayjs(filteredPayload.endsAt).utc(true).format(),
};

const items: TripItem[] = [...(trip?.items || []), newTripItem];
Expand Down Expand Up @@ -149,8 +151,8 @@ const tripSlice = createSlice({
const newDataFormatted: TripItem = {
...newData,
id: newData.id!,
startsAt: newData.startsAt,
endsAt: newData.endsAt,
startsAt: dayjs(newData.startsAt).utc(true).format(),
endsAt: newData.endsAt && dayjs(newData.endsAt).utc(true).format(),
};

const items: TripItem[] = [...allOtherItems, newDataFormatted];
Expand Down

0 comments on commit 515aec6

Please sign in to comment.