Skip to content

Commit

Permalink
๐Ÿšš resolved โ†’ relation
Browse files Browse the repository at this point in the history
  • Loading branch information
wappon28dev committed Nov 2, 2024
1 parent a4a0ab8 commit 6f1b135
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 65 deletions.
6 changes: 3 additions & 3 deletions src/components/project/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export function ProjectCard({ project }: { project: Project }): ReactElement {
const swrProjectAbout = useSWRImmutable(key, async () =>
(
await ResultAsync.combine([
project.resolveRelations(),
project.resolveRelation(),
project.resolveReferenced(),
])
.map(([resolved, referenced]) => ({
resolved,
.map(([relation, referenced]) => ({
relation,
referenced,
}))
.mapErr(notifyTableErrorInToast("swrProjectAbout"))
Expand Down
14 changes: 7 additions & 7 deletions src/lib/classes/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ const config = {
} as const satisfies TableConfig;

type Schema = Table2schema<typeof config>;
type SchemaResolvedData = Schema & {
type SchemaRelationData = Schema & {
category: TableSchemaOf<Category>;
territory: TableSchemaOf<Territory>;
};
type SchemaResolved = Override<
SchemaResolvedData,
type SchemaRelation = Override<
SchemaRelationData,
{
category: Category;
territory: Territory;
Expand All @@ -49,7 +49,7 @@ type ProjectStatus = "wakaba" | "tsubomi" | "hana";
export class Project extends Table<
typeof config,
Schema,
SchemaResolved,
SchemaRelation,
SchemaReferenced
> {
constructor(data: Schema) {
Expand All @@ -75,16 +75,16 @@ export class Project extends Table<

public calcStatus = Project.calcStatus;

public override resolveRelations(): TableResult<SchemaResolved> {
public override resolveRelation(): TableResult<SchemaRelation> {
return ResultAsync.fromSafePromise(
supabase
.from("projects")
.select("*, category:categories(*), territory:territories(*)")
.eq(config.primaryKeyName, this.data.project_id)
.returns<SchemaResolvedData>()
.returns<SchemaRelationData>()
.single(),
)
.andThen(this.transform<SchemaResolvedData>)
.andThen(this.transform<SchemaRelationData>)
.map((data) => ({
...data,
category: new Category(data.category),
Expand Down
12 changes: 6 additions & 6 deletions src/lib/classes/project/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@ const config = {
} as const satisfies TableConfig;

type Schema = Table2schema<typeof config>;
type SchemaResolvedData = Schema & {
type SchemaRelationData = Schema & {
project: TableSchemaOf<Project>;
};
type SchemaResolved = Schema & {
type SchemaRelation = Schema & {
project: Project;
};

export class Comment extends Table<typeof config, Schema, SchemaResolved> {
export class Comment extends Table<typeof config, Schema, SchemaRelation> {
constructor(data: Schema) {
super(data, config);
}

static factories = Table.getFactories(Comment, config);

public override resolveRelations(): TableResult<SchemaResolved> {
public override resolveRelation(): TableResult<SchemaRelation> {
return ResultAsync.fromSafePromise(
supabase
.from("comments")
.select("*, project:projects(*)")
.eq(config.primaryKeyName, this.data.comment_id)
.returns<SchemaResolvedData>()
.returns<SchemaRelationData>()
.single(),
)
.andThen(this.transform<SchemaResolvedData>)
.andThen(this.transform<SchemaRelationData>)
.map((data) => ({
...data,
project: new Project(data.project),
Expand Down
12 changes: 6 additions & 6 deletions src/lib/classes/project/fruit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,32 @@ const config = {
} as const satisfies TableConfig;

type Schema = Table2schema<typeof config>;
type SchemaResolvedData = Schema & {
type SchemaRelationData = Schema & {
project: TableSchemaOf<Project>;
sponsor: TableSchemaOf<Sponsor>;
};
type SchemaResolved = Schema & {
type SchemaRelation = Schema & {
project: Project;
sponsor: Sponsor;
};

export class Fruit extends Table<typeof config, Schema, SchemaResolved> {
export class Fruit extends Table<typeof config, Schema, SchemaRelation> {
constructor(data: Schema) {
super(data, config);
}

static factories = Table.getFactories(Fruit, config);

public override resolveRelations(): TableResult<SchemaResolved> {
public override resolveRelation(): TableResult<SchemaRelation> {
return ResultAsync.fromSafePromise(
supabase
.from(config.tableName)
.select("*, project:projects(*), sponsor:sponsors(*)")
.eq(config.primaryKeyName, this.data.fruit_id)
.returns<SchemaResolvedData>()
.returns<SchemaRelationData>()
.single(),
)
.andThen(this.transform<SchemaResolvedData>)
.andThen(this.transform<SchemaRelationData>)
.map((data) => ({
...data,
project: new Project(data.project),
Expand Down
12 changes: 6 additions & 6 deletions src/lib/classes/project/pledge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,32 @@ const config = {
} as const satisfies TableConfig;

type Schema = Table2schema<typeof config>;
type SchemaResolvedData = Schema & {
type SchemaRelationData = Schema & {
project: TableSchemaOf<Project>;
sower: TableSchemaOf<Sower>;
};
type SchemaResolved = Schema & {
type SchemaRelation = Schema & {
project: Project;
sower: Sower;
};

export class Pledge extends Table<typeof config, Schema, SchemaResolved> {
export class Pledge extends Table<typeof config, Schema, SchemaRelation> {
constructor(data: Schema) {
super(data, config);
}

static factories = this.getFactories(Pledge, config);

public override resolveRelations(): TableResult<SchemaResolved> {
public override resolveRelation(): TableResult<SchemaRelation> {
return ResultAsync.fromSafePromise(
supabase
.from(config.tableName)
.select("*, project:projects(*), sower:sowers(*)")
.eq(config.primaryKeyName, this.data.pledges_id)
.returns<SchemaResolvedData>()
.returns<SchemaRelationData>()
.single(),
)
.andThen(this.transform<SchemaResolvedData>)
.andThen(this.transform<SchemaRelationData>)
.map((data) => ({
...data,
project: new Project(data.project),
Expand Down
12 changes: 6 additions & 6 deletions src/lib/classes/project/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,32 @@ const config = {
} as const satisfies TableConfig;

type Schema = Table2schema<typeof config>;
type SchemaResolvedData = Schema & {
type SchemaRelationData = Schema & {
project: TableSchemaOf<Project>;
sponsor: TableSchemaOf<Sponsor>;
};
type SchemaResolved = Schema & {
type SchemaRelation = Schema & {
project: Project;
sponsor: Sponsor;
};

export class Report extends Table<typeof config, Schema, SchemaResolved> {
export class Report extends Table<typeof config, Schema, SchemaRelation> {
constructor(data: Schema) {
super(data, config);
}

static factories = Table.getFactories(Report, config);

public override resolveRelations(): TableResult<SchemaResolved> {
public override resolveRelation(): TableResult<SchemaRelation> {
return ResultAsync.fromSafePromise(
supabase
.from(config.tableName)
.select("*, project:projects(*), sponsor:sponsors(*)")
.eq(config.primaryKeyName, this.data.report_id)
.returns<SchemaResolvedData>()
.returns<SchemaRelationData>()
.single(),
)
.andThen(this.transform<SchemaResolvedData>)
.andThen(this.transform<SchemaRelationData>)
.map((data) => ({
...data,
project: new Project(data.project),
Expand Down
12 changes: 6 additions & 6 deletions src/lib/classes/project/sponsor-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,32 @@ const config = {
} as const satisfies TableConfig;

type Schema = Override<Table2schema<typeof config>, { location: Location }>;
type SchemaResolvedData = Schema & {
type SchemaRelationData = Schema & {
project: TableSchemaOf<Project>;
sponsor: TableSchemaOf<Sponsor>;
};
type SchemaResolved = Schema & {
type SchemaRelation = Schema & {
project: Project;
sponsor: Sponsor;
};

export class SponsorData extends Table<typeof config, Schema, SchemaResolved> {
export class SponsorData extends Table<typeof config, Schema, SchemaRelation> {
constructor(data: Schema) {
super(data, config);
}

static factories = this.getFactories(SponsorData, config);

public override resolveRelations(): TableResult<SchemaResolved> {
public override resolveRelation(): TableResult<SchemaRelation> {
return ResultAsync.fromSafePromise(
supabase
.from(config.tableName)
.select("*, project:projects(*), sponsor:sponsors(*)")
.eq(config.primaryKeyName, this.data.project_id)
.returns<SchemaResolvedData>()
.returns<SchemaRelationData>()
.single(),
)
.andThen(this.transform<SchemaResolvedData>)
.andThen(this.transform<SchemaRelationData>)
.map((data) => ({
...data,
project: new Project(data.project),
Expand Down
14 changes: 7 additions & 7 deletions src/lib/classes/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,35 @@ const config = {
} as const satisfies TableConfig;

type Schema = Override<Table2schema<typeof config>, { location: Location }>;
type SchemaResolvedData = Schema & {
type SchemaRelationData = Schema & {
sower: TableSchemaOf<Sower>;
category: TableSchemaOf<Category>;
};
type SchemaResolved = Override<
SchemaResolvedData,
type SchemaRelation = Override<
SchemaRelationData,
{
sower: Sower;
category: Category;
}
>;

export class Seed extends Table<typeof config, Schema, SchemaResolved> {
export class Seed extends Table<typeof config, Schema, SchemaRelation> {
constructor(data: Schema) {
super(data, config);
}

static factories = Table.getFactories(Seed, config);

public override resolveRelations(): TableResult<SchemaResolved> {
public override resolveRelation(): TableResult<SchemaRelation> {
return ResultAsync.fromSafePromise(
supabase
.from("seeds")
.select("*, category:categories(*), sower:sowers(*)")
.eq(config.primaryKeyName, this.data.seed_id)
.returns<SchemaResolvedData>()
.returns<SchemaRelationData>()
.single(),
)
.andThen(this.transform<SchemaResolvedData>)
.andThen(this.transform<SchemaRelationData>)
.map((data) => ({
...data,
sower: new Sower(data.sower),
Expand Down
4 changes: 2 additions & 2 deletions src/lib/utils/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ export const queryErrorCode = {
export abstract class Table<
Config extends TableConfig,
Schema extends object,
SchemaResolved extends object = Schema,
SchemaRelation extends object = Schema,
SchemaReferenced extends object = Schema,
> {
constructor(
public data: Schema,
protected config: Config,
) {}

public resolveRelations?(): TableResult<SchemaResolved>;
public resolveRelation?(): TableResult<SchemaRelation>;
public resolveReferenced?(): TableResult<SchemaReferenced>;

static transformError(
Expand Down
2 changes: 1 addition & 1 deletion src/routes/_auth/seeds/-components/SownSeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function SownSeed({ seed }: { seed: Seed }): ReactElement {
const swrSeedAbout = useSWRImmutable(`seed-${seed.data.seed_id}`, async () =>
(
await seed
.resolveRelations()
.resolveRelation()
.mapErr(notifyTableErrorInToast("swrSeedAbout"))
)._unsafeUnwrap(),
);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/_auth/seeds/-components/SownSeedInline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function SownSeedInline({ seed }: { seed: Seed }): ReactElement {
const swrSeedAbout = useSWRImmutable(`seed-${seed.data.seed_id}`, async () =>
(
await seed
.resolveRelations()
.resolveRelation()
.mapErr(notifyTableErrorInToast("swrSeedAbout"))
)._unsafeUnwrap(),
);
Expand Down
25 changes: 12 additions & 13 deletions src/routes/projects/$uuid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ import {
type TableBrandedId,
type TableError,
type TableSchemaReferencedOf,
type TableSchemaResolvedOf,
type TableSchemaRelationOf,
} from "@/types/table";
import { type Nullable } from "@/types/utils";

type ProjectData<T = Project> = {
project: T;
resolved: TableSchemaResolvedOf<T>;
relation: TableSchemaRelationOf<T>;
referenced: TableSchemaReferencedOf<T>;
};
type ProjectStatus = ReturnType<Project["calcStatus"]>;
Expand Down Expand Up @@ -129,7 +129,7 @@ function SponsorInfo({
if (sponsorData == null) return undefined;
return (
await sponsorData
.resolveRelations()
.resolveRelation()
.mapErr(notifyTableErrorInToast("swrSponsor"))
)._unsafeUnwrap();
},
Expand Down Expand Up @@ -225,7 +225,7 @@ function GridDetailInfo({
}: {
projectData: ProjectData;
}): ReactElement {
const { project, resolved, referenced } = projectData;
const { project, relation, referenced } = projectData;
const progress = svaProgress();

const swrAddr = useSWRImmutable(
Expand Down Expand Up @@ -392,7 +392,7 @@ function GridDetailInfo({
ไปฅไธ‹ใฎๆ„่ฆ‹ใŒ้›†ใพใฃใฆ็”Ÿๆˆใ•ใ‚Œใพใ—ใŸ
</p.p>
<p.div h="300px" py="5">
<ReferencedSeedsInfo territory={resolved.territory} />
<ReferencedSeedsInfo territory={relation.territory} />
</p.div>
<SponsorInfo
projectStatus={projectStatus}
Expand Down Expand Up @@ -422,14 +422,13 @@ export const Route = createFileRoute("/projects/$uuid")({
const project = Project.factories.from(projectId);
return (
await project.andThen((pj) =>
ResultAsync.combine([
pj.resolveRelations(),
pj.resolveReferenced(),
]).map(([resolved, referenced]) => ({
project: pj,
resolved,
referenced,
})),
ResultAsync.combine([pj.resolveRelation(), pj.resolveReferenced()]).map(
([relation, referenced]) => ({
project: pj,
relation,
referenced,
}),
),
)
).match(
(d) => d,
Expand Down
Loading

0 comments on commit 6f1b135

Please sign in to comment.