ramda#find TypeScript Examples
The following examples show how to use
ramda#find.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useBookmark.ts From back-home-safe with GNU General Public License v3.0 | 5 votes |
[UseBookmarkLocationProvider, useBookmarkLocation] = constate(
() => {
const { currentTime } = useTime();
const {
value: { savedLocations },
setValue,
} = useData();
const removeBookmarkLocation = useCallback(
(id: string) => {
setValue((prev) => ({
...prev,
savedLocations: reject(
({ id: itemId }) => itemId === id,
prev.savedLocations
),
}));
},
[setValue]
);
const createBookmarkLocation = useCallback(
(record: Omit<SavedLocation, "createdAt">) => {
setValue((prev) => ({
...prev,
savedLocations: [
{
...trimData(record),
createdAt: currentTime.toISOString(),
id: uuid(),
},
...prev.savedLocations,
],
}));
},
[setValue, currentTime]
);
const getBookmarkLocationId = useCallback(
(record: Omit<SavedLocation, "createdAt">) => {
const trimmedData = trimData(record);
const result = find((item) => {
const trimmedItem = trimData(item);
return equals(trimmedData, trimmedItem);
}, savedLocations);
if (!result) return null;
return result.id;
},
[savedLocations]
);
return {
bookmarkLocation: savedLocations,
removeBookmarkLocation,
createBookmarkLocation,
getBookmarkLocationId,
};
}
)
Example #2
Source File: useTravelRecord.ts From back-home-safe with GNU General Public License v3.0 | 4 votes |
[UseTravelRecordProvider, useTravelRecord] = constate(() => {
const { currentTime } = useTime();
const {
value: { travelRecords },
setValue,
} = useData();
const browserHistory = useHistory();
const [autoRemoveRecordDay, setAutoRemoveRecordDay] = useLocalStorage(
"auto_remove_record_after",
30
);
const { pastTravelRecord, currentTravelRecord } = useMemo(() => {
const { pastTravelRecord, currentTravelRecord } = travelRecords.reduce<{
pastTravelRecord: TravelRecord[];
currentTravelRecord: TravelRecord[];
}>(
(acc, item) => {
const isPast =
item.outTime && dayjs(item.outTime).isBefore(currentTime);
return isPast
? {
pastTravelRecord: [item, ...acc.pastTravelRecord],
currentTravelRecord: [...acc.currentTravelRecord],
}
: {
pastTravelRecord: [...acc.pastTravelRecord],
currentTravelRecord: [item, ...acc.currentTravelRecord],
};
},
{
pastTravelRecord: [],
currentTravelRecord: [],
}
);
return {
pastTravelRecord: sortRecord(pastTravelRecord),
currentTravelRecord: sortRecord(currentTravelRecord),
};
}, [travelRecords, currentTime]);
useEffect(() => {
setValue((prev) => ({
...prev,
travelRecords: prev.travelRecords.filter(
({ inTime }) =>
currentTime.diff(inTime, "day") <= (autoRemoveRecordDay || 30)
),
}));
}, [currentTime, setValue, autoRemoveRecordDay]);
const createTravelRecord = useCallback(
(record: TravelRecord) => {
setValue((prev) => ({
...prev,
travelRecords: [record, ...prev.travelRecords],
}));
},
[setValue]
);
const getTravelRecord = useCallback(
(id: string) => find(({ id: itemId }) => itemId === id, travelRecords),
[travelRecords]
);
const updateTravelRecord = useCallback(
(id: string, data: Partial<TravelRecord>) => {
setValue((prev) => {
const index = findIndex(
({ id: itemId }) => itemId === id,
prev.travelRecords
);
if (index < 0) return prev;
return {
...prev,
travelRecords: adjust(
index,
(currentRecord) => ({ ...currentRecord, ...data }),
prev.travelRecords
),
};
});
},
[setValue]
);
const removeTravelRecord = useCallback(
(id: string) => {
setValue((prev) => ({
...prev,
travelRecords: reject(
({ id: itemId }) => itemId === id,
prev.travelRecords
),
}));
},
[setValue]
);
const enterLocation = useCallback(
(location: Location & { inputType: travelRecordInputType }) => {
const id = uuid();
const now = dayjs();
const record = {
...location,
id,
inTime: now.toISOString(),
outTime: now.add(4, "hour").toISOString(),
};
createTravelRecord(record);
browserHistory.push({ pathname: `/confirm/${id}`, state: record });
},
[createTravelRecord, browserHistory]
);
return {
travelRecords,
currentTravelRecord,
pastTravelRecord,
createTravelRecord,
getTravelRecord,
updateTravelRecord,
removeTravelRecord,
setAutoRemoveRecordDay,
autoRemoveRecordDay,
enterLocation,
};
})